tizen 2.4 release
[framework/uifw/elementary.git] / src / examples / evas3d_scene_on_button_example.c
1 /*
2 * This example shows the mechanism of scene object adding (3D cube) to the widget button and
3 * illustrates the work of callback of event from mouse.
4 *
5 * Compile with:
6 * gcc -o 3d_scene_on_button_example 3d_scene_on_button example.c -g `pkg-config --libs --cflags evas ecore eo elementary`
7 */
8
9 #define EFL_EO_API_SUPPORT
10 #define EFL_BETA_API_SUPPORT
11
12 #define WIDTH 500
13 #define HEIGHT 500
14 #define d_w 100
15 #define d_h 100
16
17
18 #include <Eo.h>
19 #include <Evas.h>
20 #include <Ecore.h>
21 #include <Elementary.h>
22 #include <stdio.h>
23
24 typedef struct _Scene_Data
25 {
26    Eo *scene;
27    Eo *root_node;
28    Eo *camera_node;
29    Eo *light_node;
30    Eo *mesh_node;
31
32    Eo *camera;
33    Eo *light;
34    Eo *mesh;
35    Eo *material;
36 } Scene_Data;
37
38 Evas_Object *win = NULL;
39 Evas_Object *btn = NULL;
40 float d_angle = 0.5;
41 static Evas *evas = NULL;
42 static Eo *image = NULL;
43
44 static const float cube_vertices[] =
45 {
46    /* Front */
47    -1.0,  1.0,  1.0,     0.0,  0.0,  1.0,     1.0, 0.0, 0.0, 1.0,     0.0,  1.0,
48     1.0,  1.0,  1.0,     0.0,  0.0,  1.0,     1.0, 0.0, 0.0, 1.0,     1.0,  1.0,
49    -1.0, -1.0,  1.0,     0.0,  0.0,  1.0,     1.0, 0.0, 0.0, 1.0,     0.0,  0.0,
50     1.0, -1.0,  1.0,     0.0,  0.0,  1.0,     1.0, 0.0, 0.0, 1.0,     1.0,  0.0,
51
52    /* Back */
53     1.0,  1.0, -1.0,     0.0,  0.0, -1.0,     0.0, 0.0, 1.0, 1.0,     0.0,  1.0,
54    -1.0,  1.0, -1.0,     0.0,  0.0, -1.0,     0.0, 0.0, 1.0, 1.0,     1.0,  1.0,
55     1.0, -1.0, -1.0,     0.0,  0.0, -1.0,     0.0, 0.0, 1.0, 1.0,     0.0,  0.0,
56    -1.0, -1.0, -1.0,     0.0,  0.0, -1.0,     0.0, 0.0, 1.0, 1.0,     1.0,  0.0,
57
58    /* Left */
59    -1.0,  1.0, -1.0,    -1.0,  0.0,  0.0,     0.0, 1.0, 0.0, 1.0,     0.0,  1.0,
60    -1.0,  1.0,  1.0,    -1.0,  0.0,  0.0,     0.0, 1.0, 0.0, 1.0,     1.0,  1.0,
61    -1.0, -1.0, -1.0,    -1.0,  0.0,  0.0,     0.0, 1.0, 0.0, 1.0,     0.0,  0.0,
62    -1.0, -1.0,  1.0,    -1.0,  0.0,  0.0,     0.0, 1.0, 0.0, 1.0,     1.0,  0.0,
63
64    /* Right */
65     1.0,  1.0,  1.0,     1.0,  0.0,  0.0,     1.0, 1.0, 0.0, 1.0,     0.0,  1.0,
66     1.0,  1.0, -1.0,     1.0,  0.0,  0.0,     1.0, 1.0, 0.0, 1.0,     1.0,  1.0,
67     1.0, -1.0,  1.0,     1.0,  0.0,  0.0,     1.0, 1.0, 0.0, 1.0,     0.0,  0.0,
68     1.0, -1.0, -1.0,     1.0,  0.0,  0.0,     1.0, 1.0, 0.0, 1.0,     1.0,  0.0,
69
70    /* Top */
71    -1.0,  1.0, -1.0,     0.0,  1.0,  0.0,     1.0, 0.0, 1.0, 1.0,     0.0,  1.0,
72     1.0,  1.0, -1.0,     0.0,  1.0,  0.0,     1.0, 0.0, 1.0, 1.0,     1.0,  1.0,
73    -1.0,  1.0,  1.0,     0.0,  1.0,  0.0,     1.0, 0.0, 1.0, 1.0,     0.0,  0.0,
74     1.0,  1.0,  1.0,     0.0,  1.0,  0.0,     1.0, 0.0, 1.0, 1.0,     1.0,  0.0,
75
76    /* Bottom */
77     1.0, -1.0, -1.0,     0.0, -1.0,  0.0,     0.0, 1.0, 1.0, 1.0,     0.0,  1.0,
78    -1.0, -1.0, -1.0,     0.0, -1.0,  0.0,     0.0, 1.0, 1.0, 1.0,     1.0,  1.0,
79     1.0, -1.0,  1.0,     0.0, -1.0,  0.0,     0.0, 1.0, 1.0, 1.0,     0.0,  0.0,
80    -1.0, -1.0,  1.0,     0.0, -1.0,  0.0,     0.0, 1.0, 1.0, 1.0,     1.0,  0.0,
81 };
82
83 static const unsigned short cube_indices[] =
84 {
85    /* Front */
86    0,   1,  2,  2,  1,  3,
87
88    /* Back */
89    4,   5,  6,  6,  5,  7,
90
91    /* Left */
92    8,   9, 10, 10,  9, 11,
93
94    /* Right */
95    12, 13, 14, 14, 13, 15,
96
97    /* Top */
98    16, 17, 18, 18, 17, 19,
99
100    /* Bottom */
101    20, 21, 22, 22, 21, 23
102 };
103
104 static Eina_Bool
105 _animate_scene(void *data)
106 {
107    static float angle = 0.0f;
108    Scene_Data *scene = (Scene_Data *)data;
109
110    angle += d_angle;
111
112    eo_do(scene->mesh_node,
113          evas_3d_node_orientation_angle_axis_set(angle, 1.0, 1.0, 1.0));
114
115    /* Rotate */
116    if (angle > 360.0) angle -= 360.0f;
117
118    return EINA_TRUE;
119 }
120
121 static void
122 _camera_setup(Scene_Data *data)
123 {
124    data->camera = eo_add(EVAS_3D_CAMERA_CLASS, evas);
125
126    eo_do(data->camera,
127          evas_3d_camera_projection_perspective_set(60.0, 1.0, 2.0, 50.0));
128
129    data->camera_node =
130       eo_add(EVAS_3D_NODE_CLASS, evas,
131                     evas_3d_node_constructor(EVAS_3D_NODE_TYPE_CAMERA));
132    eo_do(data->camera_node,
133          evas_3d_node_camera_set(data->camera),
134          evas_3d_node_position_set(0.0, 0.0, 10.0),
135          evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0,
136                                   EVAS_3D_SPACE_PARENT, 0.0, 1.0, 0.0));
137    eo_do(data->root_node, evas_3d_node_member_add(data->camera_node));
138 }
139
140 static void
141 _light_setup(Scene_Data *data)
142 {
143    data->light = eo_add(EVAS_3D_LIGHT_CLASS, evas);
144    eo_do(data->light,
145          evas_3d_light_ambient_set(0.2, 0.2, 0.2, 1.0),
146          evas_3d_light_diffuse_set(1.0, 1.0, 1.0, 1.0),
147          evas_3d_light_specular_set(1.0, 1.0, 1.0, 1.0));
148
149    data->light_node =
150       eo_add(EVAS_3D_NODE_CLASS, evas,
151                     evas_3d_node_constructor(EVAS_3D_NODE_TYPE_LIGHT));
152    eo_do(data->light_node,
153          evas_3d_node_light_set(data->light),
154          evas_3d_node_position_set(0.0, 0.0, 10.0),
155          evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0,
156                                   EVAS_3D_SPACE_PARENT, 0.0, 1.0, 0.0));
157    eo_do(data->root_node, evas_3d_node_member_add(data->light_node));
158 }
159 static void
160 _mesh_setup(Scene_Data *data)
161 {
162    /* Setup material. */
163    data->material = eo_add(EVAS_3D_MATERIAL_CLASS, evas);
164
165    eo_do(data->material,
166          evas_3d_material_enable_set(EVAS_3D_MATERIAL_AMBIENT, EINA_TRUE),
167          evas_3d_material_enable_set(EVAS_3D_MATERIAL_DIFFUSE, EINA_TRUE),
168          evas_3d_material_enable_set(EVAS_3D_MATERIAL_SPECULAR, EINA_TRUE),
169
170          evas_3d_material_color_set(EVAS_3D_MATERIAL_AMBIENT,
171                                     0.2, 0.2, 0.2, 1.0),
172          evas_3d_material_color_set(EVAS_3D_MATERIAL_DIFFUSE,
173                                     0.8, 0.8, 0.8, 1.0),
174          evas_3d_material_color_set(EVAS_3D_MATERIAL_SPECULAR,
175                                     1.0, 1.0, 1.0, 1.0),
176          evas_3d_material_shininess_set(100.0));
177
178    /* Setup mesh. */
179    data->mesh = eo_add(EVAS_3D_MESH_CLASS, evas);
180    eo_do(data->mesh,
181          evas_3d_mesh_vertex_count_set(24),
182          evas_3d_mesh_frame_add(0),
183
184          evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_POSITION,
185                                             12 * sizeof(float),
186                                             &cube_vertices[ 0]),
187          evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_NORMAL,
188                                             12 * sizeof(float),
189                                             &cube_vertices[ 3]),
190          evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_COLOR,
191                                             12 * sizeof(float),
192                                             &cube_vertices[ 6]),
193          evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_TEXCOORD,
194                                             12 * sizeof(float),
195                                             &cube_vertices[10]),
196
197          evas_3d_mesh_index_data_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
198                                      36, &cube_indices[0]),
199          evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES),
200          evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_PHONG),
201          evas_3d_mesh_frame_material_set(0, data->material));
202
203    data->mesh_node =
204       eo_add(EVAS_3D_NODE_CLASS, evas,
205                     evas_3d_node_constructor(EVAS_3D_NODE_TYPE_MESH));
206    eo_do(data->root_node, evas_3d_node_member_add(data->mesh_node));
207    eo_do(data->mesh_node, evas_3d_node_mesh_add(data->mesh));
208 }
209
210 static void
211 _scene_setup(Scene_Data *data)
212 {
213    data->scene = eo_add(EVAS_3D_SCENE_CLASS, evas);
214    eo_do(data->scene,
215          evas_3d_scene_size_set(WIDTH - d_w, HEIGHT - d_h);
216          evas_3d_scene_background_color_set(0.0, 0.0, 0.0, 0.0));
217
218    data->root_node =
219       eo_add(EVAS_3D_NODE_CLASS, evas,
220                     evas_3d_node_constructor(EVAS_3D_NODE_TYPE_NODE));
221
222    _camera_setup(data);
223    _light_setup(data);
224    _mesh_setup(data);
225
226    eo_do(data->scene,
227          evas_3d_scene_root_node_set(data->root_node),
228          evas_3d_scene_camera_node_set(data->camera_node));
229 }
230
231 static void
232 _stop_scene(void *data,
233             Evas *e EINA_UNUSED,
234             Evas_Object *eo EINA_UNUSED,
235             void *event_info)
236 {
237    Evas_Event_Mouse_Down *ev = event_info;
238    Scene_Data *d = (Scene_Data *)data;
239
240    eo_do(d->mesh_node,
241          evas_3d_node_scale_set(0.97, 0.97, 0.97));
242
243    if (ev->button == 1)
244      {
245         if (eo_do(d->scene,
246                   evas_3d_scene_exist((ev->canvas.x - (d_w / 2)),
247                                       (ev->canvas.y - (d_h / 2)),
248                                       d->mesh_node)))
249           {
250              d_angle = 0;
251           }
252      }
253 }
254
255 static void
256 _play_scene(void *data,
257             Evas *e EINA_UNUSED,
258             Evas_Object *eo EINA_UNUSED,
259             void *event_info EINA_UNUSED)
260 {
261    Scene_Data *d = (Scene_Data *)data;
262
263    d_angle = 0.5;
264
265    eo_do(d->mesh_node,
266          evas_3d_node_scale_set(1.0, 1.0, 1.0));
267 }
268
269 int
270 elm_main(int argc, char **argv)
271 {
272    Scene_Data data;
273
274    elm_config_accel_preference_set("3d");
275    elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
276
277    win = elm_win_util_standard_add("evas3d-scene-button", "3d object on the button");
278    if (!win) return 0;
279    elm_win_autodel_set(win, EINA_TRUE);
280
281    evas = evas_object_evas_get(win);
282    if (!evas) return 0;
283
284    _scene_setup(&data);
285
286    image = evas_object_image_filled_add(evas);
287    eo_do(image,
288          evas_obj_visibility_set(EINA_TRUE));
289
290    /* Set the image object as render target for 3D scene. */
291    eo_do(image, evas_obj_image_scene_set(data.scene));
292
293    /* Setup scene to the widget button. */
294    btn = elm_button_add(win);
295    elm_object_content_set(btn, image);
296    evas_object_resize(btn, (WIDTH - d_w), (HEIGHT - d_h));
297    evas_object_move(btn, (d_w / 2), (d_h / 2));
298    evas_object_show(btn);
299
300    evas_object_event_callback_add(btn, EVAS_CALLBACK_MOUSE_DOWN, _stop_scene,
301                                   &data);
302    evas_object_event_callback_add(btn, EVAS_CALLBACK_MOUSE_UP, _play_scene,
303                                   &data);
304
305    /* Add animation timer callback. */
306    ecore_timer_add(0.016, _animate_scene, &data);
307
308    evas_object_resize(win, WIDTH, HEIGHT);
309    evas_object_show(win);
310
311    /* Enter main loop. */
312    elm_run();
313
314    return 0;
315 }
316 ELM_MAIN()