summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorse.osadchy <se.osadchy@samsung.com>2014-06-04 19:19:25 +0200
committerCedric BAIL <c.bail@partner.samsung.com>2014-06-04 19:37:23 +0200
commit8ef70b665ed8b252a148321ed22de36e77cab985 (patch)
tree05c7c0279ad652696b7b2db00da3b37d51afff2f
parente03cb09b678eec288c5719c8f39d1c127cec9b4f (diff)
example: add new example for button and Evas_3D object.
Summary: This example shows the mechanism of elementary widget (button) adding to the 3d scene object. Reviewers: Hermet, cedric, raster Differential Revision: https://phab.enlightenment.org/D937 Signed-off-by: Cedric BAIL <c.bail@partner.samsung.com>
-rw-r--r--src/examples/Makefile.am2
-rw-r--r--src/examples/button_on_3d_object_example.c339
2 files changed, 341 insertions, 0 deletions
diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am
index 8998cf0f9..512fa120a 100644
--- a/src/examples/Makefile.am
+++ b/src/examples/Makefile.am
@@ -140,6 +140,7 @@ web_example_01.c \
140web_example_02.c \ 140web_example_02.c \
141win_example.c \ 141win_example.c \
1423d_scene_on_button_example.c \ 1423d_scene_on_button_example.c \
143button_on_3d_object_example.c \
143track_example_01.c 144track_example_01.c
144 145
145.edc.edj: 146.edc.edj:
@@ -280,6 +281,7 @@ web_example_01 \
280web_example_02 \ 281web_example_02 \
281win_example \ 282win_example \
2823d_scene_on_button_example \ 2833d_scene_on_button_example \
284button_on_3d_object_example \
283track_example_01 285track_example_01
284 286
285if ELEMENTARY_WINDOWS_BUILD 287if ELEMENTARY_WINDOWS_BUILD
diff --git a/src/examples/button_on_3d_object_example.c b/src/examples/button_on_3d_object_example.c
new file mode 100644
index 000000000..53a33f265
--- /dev/null
+++ b/src/examples/button_on_3d_object_example.c
@@ -0,0 +1,339 @@
1/*
2 * This example shows the mechanism of elementary widget adding (button) to the 3d scene object (cube) and
3 * illustrates the work of callback of event from mouse.
4 *
5 * Compile with:
6 * gcc -o button_on_3d_object_example button_on_3d_object_example.c -g `pkg-config --libs --cflags evas ecore eo elementary`
7 */
8
9
10#define EFL_EO_API_SUPPORT
11#define EFL_BETA_API_SUPPORT
12
13#include <Eo.h>
14#include <Evas.h>
15#include <Ecore.h>
16#include <Elementary.h>
17
18#define WIDTH 400
19#define HEIGHT 400
20
21typedef struct _Scene_Data
22{
23 Eo *scene;
24 Eo *root_node;
25 Eo *camera_node;
26 Eo *light_node;
27 Eo *mesh_node;
28
29 Eo *camera;
30 Eo *light;
31 Eo *mesh;
32 Eo *material;
33 Eo *texture;
34} Scene_Data;
35
36Evas_Object *win = NULL;
37static Evas *evas = NULL;
38static Eo *image = NULL;
39static Eo *btn = NULL;
40static float d_angle = 0.5;
41
42static const float cube_vertices[] =
43{
44 /* Front */
45 -1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0,
46 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0,
47 -1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
48 1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0,
49
50 /* Back */
51 1.0, 1.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0,
52 -1.0, 1.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0,
53 1.0, -1.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0,
54 -1.0, -1.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0,
55
56 /* Left */
57 -1.0, 1.0, -1.0, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0,
58 -1.0, 1.0, 1.0, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0,
59 -1.0, -1.0, -1.0, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0,
60 -1.0, -1.0, 1.0, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0,
61
62 /* Right */
63 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0,
64 1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0,
65 1.0, -1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0,
66 1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0,
67
68 /* Top */
69 -1.0, 1.0, -1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
70 1.0, 1.0, -1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0,
71 -1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0,
72 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0,
73
74 /* Bottom */
75 1.0, -1.0, -1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0,
76 -1.0, -1.0, -1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0,
77 1.0, -1.0, 1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0,
78 -1.0, -1.0, 1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0,
79};
80
81static const unsigned short cube_indices[] =
82{
83 /* Front */
84 0, 1, 2, 2, 1, 3,
85
86 /* Back */
87 4, 5, 6, 6, 5, 7,
88
89 /* Left */
90 8, 9, 10, 10, 9, 11,
91
92 /* Right */
93 12, 13, 14, 14, 13, 15,
94
95 /* Top */
96 16, 17, 18, 18, 17, 19,
97
98 /* Bottom */
99 20, 21, 22, 22, 21, 23
100};
101
102static void
103_stop_scene(void *data,
104 Evas *e EINA_UNUSED,
105 Evas_Object *eo EINA_UNUSED,
106 void *event_info)
107{
108 Evas_Event_Mouse_Down *ev = event_info;
109 Evas_3D_Node *n;
110 Evas_3D_Mesh *m;
111 Evas_Real s, t;
112 Scene_Data *d = (Scene_Data *)data;
113
114 if (ev->button == 1)
115 {
116 if (eo_do(d->scene,
117 evas_3d_scene_pick(ev->canvas.x, ev->canvas.y, &n, &m, &s, &t)))
118 {
119 d_angle = 0.0;
120 elm_object_signal_emit(btn, "mouse,down,1", "event");
121 eo_do(d->mesh_node, evas_3d_node_position_set(0.0, 0.0, -0.2));
122 }
123 }
124}
125
126static void
127_play_scene(void *data,
128 Evas *e EINA_UNUSED,
129 Evas_Object *eo EINA_UNUSED,
130 void *event_info EINA_UNUSED)
131{
132 Scene_Data *d = (Scene_Data *)data;
133
134 d_angle = 0.5;
135 elm_object_signal_emit(btn, "mouse,up,1", "event");
136 eo_do(d->mesh_node, evas_3d_node_position_set(0.0, 0.0, 0.0));
137}
138
139
140static Eina_Bool
141_animate_scene(void *data)
142{
143 static float angle = 0.0f;
144 Scene_Data *scene = (Scene_Data *)data;
145
146 angle += d_angle;
147
148 eo_do(scene->mesh_node,
149 evas_3d_node_orientation_angle_axis_set(angle, 1.0, 1.0, 1.0));
150
151 /* Rotate */
152 if (angle > 360.0) angle -= 360.0f;
153
154 return EINA_TRUE;
155}
156
157static void
158_camera_setup(Scene_Data *data)
159{
160 data->camera = eo_add(EVAS_3D_CAMERA_CLASS, evas);
161
162 eo_do(data->camera,
163 evas_3d_camera_projection_perspective_set(20.0, 1.0, 2.0, 50.0));
164
165 data->camera_node =
166 eo_add_custom(EVAS_3D_NODE_CLASS, evas,
167 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_CAMERA));
168 eo_do(data->camera_node,
169 evas_3d_node_camera_set(data->camera),
170 evas_3d_node_position_set(0.0, 0.0, 15.0),
171 evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0,
172 EVAS_3D_SPACE_PARENT, 0.0, -1.0, 0.0));
173 eo_do(data->root_node, evas_3d_node_member_add(data->camera_node));
174}
175
176static void
177_light_setup(Scene_Data *data)
178{
179 data->light = eo_add(EVAS_3D_LIGHT_CLASS, evas);
180 eo_do(data->light,
181 evas_3d_light_ambient_set(0.2, 0.2, 0.2, 1.0),
182 evas_3d_light_diffuse_set(1.0, 1.0, 1.0, 1.0),
183 evas_3d_light_specular_set(1.0, 1.0, 1.0, 1.0));
184
185 data->light_node =
186 eo_add_custom(EVAS_3D_NODE_CLASS, evas,
187 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_LIGHT));
188 eo_do(data->light_node,
189 evas_3d_node_light_set(data->light),
190 evas_3d_node_position_set(0.0, 0.0, 10.0),
191 evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0,
192 EVAS_3D_SPACE_PARENT, 0.0, 1.0, 0.0));
193 eo_do(data->root_node, evas_3d_node_member_add(data->light_node));
194}
195static void
196_mesh_setup(Scene_Data *data)
197{
198 /* Setup material and texture as widget button. */
199 data->material = eo_add(EVAS_3D_MATERIAL_CLASS, evas);
200 data->texture = eo_add(EVAS_3D_TEXTURE_CLASS, evas);
201
202 eo_do(data->texture,
203 evas_3d_texture_source_set(btn),
204 evas_3d_texture_source_visible_set(EINA_FALSE));
205
206 eo_do(data->material,
207 evas_3d_material_texture_set(EVAS_3D_MATERIAL_DIFFUSE, data->texture),
208
209 evas_3d_material_enable_set(EVAS_3D_MATERIAL_AMBIENT, EINA_TRUE),
210 evas_3d_material_enable_set(EVAS_3D_MATERIAL_DIFFUSE, EINA_TRUE),
211 evas_3d_material_enable_set(EVAS_3D_MATERIAL_SPECULAR, EINA_TRUE),
212
213 evas_3d_material_color_set(EVAS_3D_MATERIAL_AMBIENT, 0.2, 0.2, 0.2, 1.0),
214 evas_3d_material_color_set(EVAS_3D_MATERIAL_DIFFUSE, 0.8, 0.8, 0.8, 1.0),
215 evas_3d_material_color_set(EVAS_3D_MATERIAL_SPECULAR, 1.0, 1.0, 1.0, 1.0),
216 evas_3d_material_shininess_set(100.0));
217
218 /* Setup mesh. */
219 data->mesh = eo_add(EVAS_3D_MESH_CLASS, evas);
220 eo_do(data->mesh,
221 evas_3d_mesh_vertex_count_set(24),
222 evas_3d_mesh_frame_add(0),
223
224 evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_POSITION,
225 12 * sizeof(float), &cube_vertices[ 0]),
226 evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_NORMAL,
227 12 * sizeof(float), &cube_vertices[ 3]),
228 evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_COLOR,
229 12 * sizeof(float), &cube_vertices[ 6]),
230 evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_TEXCOORD,
231 12 * sizeof(float), &cube_vertices[10]),
232
233 evas_3d_mesh_index_data_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
234 36, &cube_indices[0]),
235 evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES),
236
237 evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_PHONG),
238
239 evas_3d_mesh_frame_material_set(0, data->material));
240
241 data->mesh_node =
242 eo_add_custom(EVAS_3D_NODE_CLASS, evas,
243 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_MESH));
244 eo_do(data->root_node, evas_3d_node_member_add(data->mesh_node));
245 eo_do(data->mesh_node, evas_3d_node_mesh_add(data->mesh));
246}
247
248static void
249_scene_setup(Scene_Data *data)
250{
251 data->scene = eo_add(EVAS_3D_SCENE_CLASS, evas);
252 eo_do(data->scene,
253 evas_3d_scene_size_set(WIDTH, HEIGHT);
254 evas_3d_scene_background_color_set(0.0, 0.0, 0.0, 0.0));
255
256 data->root_node =
257 eo_add_custom(EVAS_3D_NODE_CLASS, evas,
258 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_NODE));
259
260 _camera_setup(data);
261 _light_setup(data);
262 _mesh_setup(data);
263
264 eo_do(data->scene,
265 evas_3d_scene_root_node_set(data->root_node),
266 evas_3d_scene_camera_node_set(data->camera_node));
267}
268
269static void
270_mirror(Evas_Object *image)
271{
272 int x, y, w, h;
273 Evas_Map *m_rotate;
274
275 evas_object_geometry_get(image, &x, &y, &w, &h);
276 m_rotate = evas_map_new(4);
277
278 evas_map_util_points_populate_from_object(m_rotate, image);
279 evas_map_util_rotate(m_rotate, 180, x + (w / 2), y + (h / 2));
280
281 evas_map_point_image_uv_set(m_rotate, 0, 0, h);
282 evas_map_point_image_uv_set(m_rotate, 1, w, h);
283 evas_map_point_image_uv_set(m_rotate, 2, w, h / 10);
284 evas_map_point_image_uv_set(m_rotate, 3, 0, h / 10);
285
286 evas_object_map_set(image, m_rotate);
287 evas_object_map_enable_set(image, EINA_TRUE);
288
289 evas_map_free(m_rotate);
290}
291
292int
293main(void)
294{
295 Scene_Data data;
296
297 if (!elm_init(0, 0)) return 0;
298
299 elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
300
301 win = elm_win_util_standard_add("__WIN__", " A button on the 3d object");
302 elm_win_autodel_set(win, EINA_TRUE);
303 evas_object_resize(win, WIDTH, HEIGHT);
304 evas_object_show(win);
305
306 evas = evas_object_evas_get(win);
307
308 /* Add an image object for 3D scene rendering. */
309 image = evas_object_image_filled_add(evas);
310 eo_do(image,
311 evas_obj_size_set(WIDTH, HEIGHT),
312 evas_obj_visibility_set(EINA_TRUE));
313
314 btn = elm_button_add(win);
315 evas_object_resize(btn, (WIDTH * 2) / 3, (HEIGHT * 2) / 3);
316 elm_object_text_set(btn, "3D Button");
317 elm_object_scale_set(btn, 3.0);
318 evas_object_show(btn);
319
320 /*Due to inverted image in case proxy object*/
321 _mirror(image);
322
323 /* Set the image object as render target for 3D scene. */
324 _scene_setup(&data);
325 eo_do(image, evas_obj_image_scene_set(data.scene));
326
327 evas_object_event_callback_add(image, EVAS_CALLBACK_MOUSE_DOWN, _stop_scene, &data);
328 evas_object_event_callback_add(image, EVAS_CALLBACK_MOUSE_UP, _play_scene, &data);
329
330 /* Add animation timer callback. */
331 ecore_timer_add(0.016, _animate_scene, &data);
332
333 /* Enter main loop. */
334 elm_run();
335
336 elm_shutdown();
337
338 return 0;
339}