tizen 2.4 release
[framework/uifw/elementary.git] / src / examples / evas3d_object_on_button_example.c
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
21 typedef 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
36 Evas_Object *win = NULL;
37 static Evas *evas = NULL;
38 static Eo *image = NULL;
39 static Eo *btn = NULL;
40 static float d_angle = 0.5;
41
42 static 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
81 static 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
102 static 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
126 static 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
140 static 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
157 static 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(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
176 static 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(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 }
195 static 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(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
248 static 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(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
269 static void
270 _mirror(Evas_Object *img)
271 {
272    int x, y, w, h;
273    Evas_Map *m_rotate;
274
275    evas_object_geometry_get(img, &x, &y, &w, &h);
276    m_rotate = evas_map_new(4);
277
278    evas_map_util_points_populate_from_object(m_rotate, img);
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(img, m_rotate);
287    evas_object_map_enable_set(img, EINA_TRUE);
288
289    evas_map_free(m_rotate);
290 }
291
292 int
293 elm_main(int argc, char **argv)
294 {
295    Scene_Data data;
296
297    elm_config_accel_preference_set("3d");
298    elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
299
300    win = elm_win_util_standard_add("evas3d-object-button", "A button on the 3d object");
301    elm_win_autodel_set(win, EINA_TRUE);
302
303    evas = evas_object_evas_get(win);
304
305    /* Add an image object for 3D scene rendering. */
306    image = evas_object_image_filled_add(evas);
307    eo_do(image,
308          evas_obj_size_set(WIDTH, HEIGHT),
309          evas_obj_visibility_set(EINA_TRUE));
310
311    btn = elm_button_add(win);
312    evas_object_resize(btn, (WIDTH * 2) / 3, (HEIGHT * 2) / 3);
313    elm_object_text_set(btn, "3D Button");
314    elm_object_scale_set(btn, 3.0);
315    evas_object_show(btn);
316
317    /*Due to inverted image in case proxy object*/
318    _mirror(image);
319
320    /* Set the image object as render target for 3D scene. */
321    _scene_setup(&data);
322    eo_do(image, evas_obj_image_scene_set(data.scene));
323
324    evas_object_event_callback_add(image, EVAS_CALLBACK_MOUSE_DOWN, _stop_scene, &data);
325    evas_object_event_callback_add(image, EVAS_CALLBACK_MOUSE_UP, _play_scene, &data);
326
327    /* Add animation timer callback. */
328    ecore_timer_add(0.016, _animate_scene, &data);
329
330    evas_object_resize(win, WIDTH, HEIGHT);
331    evas_object_show(win);
332
333    /* Enter main loop. */
334    elm_run();
335
336    return 0;
337 }
338 ELM_MAIN()