summaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorOleksandr Shcherbina <o.shcherbina@samsung.com>2015-02-18 21:43:23 +0100
committerCedric BAIL <cedric@osg.samsung.com>2015-02-18 22:42:07 +0100
commitd28b82be45451c08cb993d2f835675d8b5dcbe24 (patch)
treef45b8c07bb8f370f4e845b8f3d924ef33d8bd57e /src/examples
parentb9b5ced501f4642f03d8bcc8441fd3f7b55a7f52 (diff)
evas: Evas_3D - callbacks for Evas 3D.
Summary: Add class and type Evas_3D_Callback like wrapper under smart object Incapsulate Evas_3D_Callback in Evas_3D_Object Add virtual function register and unregister in Evas_3D_Object Add function evas_3d_callback_call Add callbacks clicked and collision for Evas_3D_Node @feature Reviewers: raster, Hermet, cedric Reviewed By: cedric Subscribers: artem.popov, cedric Differential Revision: https://phab.enlightenment.org/D1914 Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/evas/evas-3d-shadows.c179
1 files changed, 170 insertions, 9 deletions
diff --git a/src/examples/evas/evas-3d-shadows.c b/src/examples/evas/evas-3d-shadows.c
index b66da8f..960186d 100644
--- a/src/examples/evas/evas-3d-shadows.c
+++ b/src/examples/evas/evas-3d-shadows.c
@@ -1,9 +1,11 @@
1/* 1/*
2 * This example illustrating use of shadows in the scene. 2 * This example illustrating use of shadows in the scene and callbacks(clicked, collision).
3 * 3 * Model and cube are clickable. Model detects collision with sphere.
4 * Cube detects collision with sphere, model and cone.
4 * @see evas_3d_scene_shadows_enable_set(Eina_Bool _shadows_enabled) 5 * @see evas_3d_scene_shadows_enable_set(Eina_Bool _shadows_enabled)
6 * @see evas_3d_object_callback_register
5 * 7 *
6 * Compile with "gcc -o evas-3d-shadows evas-3d-shadows.c `pkg-config --libs --cflags efl evas ecore ecore-evas eo` -lm" 8 * Compile with gcc -o evas-3d-shadows evas-3d-shadows.c `pkg-config --libs --cflags efl evas ecore ecore-evas eo eina` -lm
7 */ 9 */
8 10
9#define EFL_EO_API_SUPPORT 11#define EFL_EO_API_SUPPORT
@@ -13,12 +15,14 @@
13#include <Evas.h> 15#include <Evas.h>
14#include <Ecore.h> 16#include <Ecore.h>
15#include <Ecore_Evas.h> 17#include <Ecore_Evas.h>
18#include <Eina.h>
16#include <math.h> 19#include <math.h>
17#include "evas-3d-primitives.c" 20#include "evas-3d-primitives.c"
18 21
19#define WIDTH 1024 22#define WIDTH 1024
20#define HEIGHT 1024 23#define HEIGHT 1024
21 24
25#define STEP 0.1
22#define BG_COLOR 0.2, 0.2, 0.2 26#define BG_COLOR 0.2, 0.2, 0.2
23#define AMBIENT_LIGHT 0.2, 0.2, 0.2 27#define AMBIENT_LIGHT 0.2, 0.2, 0.2
24#define DIFFUSE_LIGHT 1.0, 1.0, 1.0 28#define DIFFUSE_LIGHT 1.0, 1.0, 1.0
@@ -28,6 +32,7 @@ Ecore_Evas *ecore_evas = NULL;
28Evas *evas = NULL; 32Evas *evas = NULL;
29Eo *background = NULL; 33Eo *background = NULL;
30Eo *image = NULL; 34Eo *image = NULL;
35Evas_3D_Node *choosed_node = NULL;
31 36
32typedef struct _Body_3D 37typedef struct _Body_3D
33{ 38{
@@ -53,6 +58,51 @@ typedef struct _Scene_Data
53 Body_3D cone; 58 Body_3D cone;
54} Scene_Data; 59} Scene_Data;
55 60
61Eina_Bool
62_cb_clicked(void *data, Eo *obj, const Eo_Event_Description *desc, void *event_info)
63{
64 Eina_List *meshes = NULL, *l;
65 Evas_3D_Mesh *m;
66 eo_do((Evas_3D_Node *)event_info, meshes = (Eina_List *)evas_3d_node_mesh_list_get());
67 EINA_LIST_FOREACH(meshes, l, m)
68 {
69 eo_do(m, evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_DIFFUSE));
70 }
71 if (choosed_node != (Evas_3D_Node *)event_info)
72 {
73 eo_do(choosed_node, meshes = (Eina_List *)evas_3d_node_mesh_list_get());
74 EINA_LIST_FOREACH(meshes, l, m)
75 {
76 eo_do(m, evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_PHONG));
77 }
78 choosed_node = (Evas_3D_Node *)event_info;
79 }
80
81}
82
83Eina_Bool
84_cb_collision(void *data, Eo *obj, const Eo_Event_Description *desc, void *event_info)
85{
86 Eina_List *meshes = NULL, *l;
87 Evas_3D_Mesh *m;
88 eo_do((Evas_3D_Node *)event_info, meshes = (Eina_List *)evas_3d_node_mesh_list_get());
89 EINA_LIST_FOREACH(meshes, l, m)
90 {
91 eo_do(m, evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_DIFFUSE));
92 }
93}
94
95static void
96_show_help()
97{
98 fprintf(stdout, "Press 'w'/'s' key to move up/down object\n");
99 fprintf(stdout, "Press 'a'/'d' key to move left/right object\n");
100 fprintf(stdout, "Press 'q'/'e' key to to move near/far object\n");
101 fprintf(stdout, "Cude and model can be moved.\n");
102 fprintf(stdout, "Cube detects intersection with model, sphere, cone\n");
103 fprintf(stdout, "Model detects intersection with sphere\n");
104}
105
56static Eina_Bool 106static Eina_Bool
57_animate_scene(void *data) 107_animate_scene(void *data)
58{ 108{
@@ -61,9 +111,9 @@ _animate_scene(void *data)
61 111
62 eo_do(body->node, evas_3d_node_mesh_frame_set(body->mesh, frame)); 112 eo_do(body->node, evas_3d_node_mesh_frame_set(body->mesh, frame));
63 113
64 frame += 32; 114 /*frame += 32;*/
65 115
66 if (frame > 256 * 50) frame = 0; 116 if (frame > 256 * 20) frame = 0;
67 117
68 return EINA_TRUE; 118 return EINA_TRUE;
69} 119}
@@ -114,7 +164,7 @@ _sphere_setup(Body_3D *sphere)
114 sphere->node = 164 sphere->node =
115 eo_add(EVAS_3D_NODE_CLASS, evas, 165 eo_add(EVAS_3D_NODE_CLASS, evas,
116 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_MESH), 166 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_MESH),
117 evas_3d_node_position_set(2.0, 3.0, 1.0)); 167 evas_3d_node_position_set(3.0, 3.0, 0.0));
118 eo_do(sphere->node, evas_3d_node_mesh_add(sphere->mesh)); 168 eo_do(sphere->node, evas_3d_node_mesh_add(sphere->mesh));
119} 169}
120 170
@@ -293,12 +343,106 @@ _scene_setup(Scene_Data *data)
293 evas_3d_scene_shadows_enable_set(EINA_TRUE)); 343 evas_3d_scene_shadows_enable_set(EINA_TRUE));
294} 344}
295 345
346static void
347_on_key_down(void *data, Evas *e EINA_UNUSED, Evas_Object *eo EINA_UNUSED, void *event_info)
348{
349 Scene_Data *scene = (Scene_Data *)data;
350 Evas_Event_Key_Down *ev = event_info;
351 if (!strcmp("w", ev->key))
352 {
353 Evas_Real x, y, z;
354 eo_do(choosed_node, evas_3d_node_position_get(EVAS_3D_SPACE_PARENT, &x, &y, &z));
355 eo_do(choosed_node, evas_3d_node_position_set(x, y + STEP, z));
356 }
357 else if(!strcmp("s", ev->key))
358 {
359 Evas_Real x, y, z;
360 eo_do(choosed_node, evas_3d_node_position_get(EVAS_3D_SPACE_PARENT, &x, &y, &z));
361 eo_do(choosed_node, evas_3d_node_position_set(x, y - STEP, z));
362 }
363 else if(!strcmp("a", ev->key))
364 {
365 Evas_Real x, y, z;
366 eo_do(choosed_node, evas_3d_node_position_get(EVAS_3D_SPACE_PARENT, &x, &y, &z));
367 eo_do(choosed_node, evas_3d_node_position_set(x - STEP, y, z));
368 }
369 else if(!strcmp("d", ev->key))
370 {
371 Evas_Real x, y, z;
372 eo_do(choosed_node, evas_3d_node_position_get(EVAS_3D_SPACE_PARENT, &x, &y, &z));
373 eo_do(choosed_node, evas_3d_node_position_set(x + STEP, y, z));
374 }
375 else if(!strcmp("q", ev->key))
376 {
377 Evas_Real x, y, z;
378 eo_do(choosed_node, evas_3d_node_position_get(EVAS_3D_SPACE_PARENT, &x, &y, &z));
379 eo_do(choosed_node, evas_3d_node_position_set(x, y, z - STEP));
380 }
381 else if(!strcmp("e", ev->key))
382 {
383 Evas_Real x, y, z;
384 eo_do(choosed_node, evas_3d_node_position_get(EVAS_3D_SPACE_PARENT, &x, &y, &z));
385 eo_do(choosed_node, evas_3d_node_position_set(x, y, z + STEP));
386 }
387 else
388 {
389 _show_help();
390 }
391}
392
393static void _init(Scene_Data *data)
394{
395 Eina_List *meshes = NULL, *l;
396 Evas_3D_Mesh *m;
397 eo_do(data->sphere.node, meshes = (Eina_List *)evas_3d_node_mesh_list_get());
398 EINA_LIST_FOREACH(meshes, l, m)
399 {
400 eo_do(m, evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_PHONG));
401 }
402 eo_do(data->cube.node, meshes = (Eina_List *)evas_3d_node_mesh_list_get());
403 EINA_LIST_FOREACH(meshes, l, m)
404 {
405 eo_do(m, evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_PHONG));
406 }
407 eo_do(data->cylinder.node, meshes = (Eina_List *)evas_3d_node_mesh_list_get());
408 EINA_LIST_FOREACH(meshes, l, m)
409 {
410 eo_do(m, evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_PHONG));
411 }
412 eo_do(data->model.node, meshes = (Eina_List *)evas_3d_node_mesh_list_get());
413 EINA_LIST_FOREACH(meshes, l, m)
414 {
415 eo_do(m, evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_PHONG));
416 }
417 eo_do(data->cone.node, meshes = (Eina_List *)evas_3d_node_mesh_list_get());
418 EINA_LIST_FOREACH(meshes, l, m)
419 {
420 eo_do(m, evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_PHONG));
421 }
422}
423
424static void
425_on_mouse_down(void *data, Evas *e EINA_UNUSED, Evas_Object *eo EINA_UNUSED, void *event_info)
426{
427 Scene_Data *d = (Scene_Data *)data;
428 Evas_Event_Mouse_Down *ev = event_info;
429 Evas_3D_Node *n = NULL;
430 Evas_3D_Mesh *m = NULL;
431 Evas_Real s, t;
432 if (ev->button == 3)
433 {
434 _init(d);
435 return;
436 }
437 eo_do(d->scene, evas_3d_scene_pick(ev->canvas.x, ev->canvas.y, &n, &m, &s, &t));
438
439}
296int 440int
297main(void) 441main(void)
298{ 442{
299 Scene_Data data; 443 Scene_Data data;
300 Ecore_Animator *anim; 444 Ecore_Animator *anim;
301 445 Eina_List *nodes1 = NULL, *nodes2 = NULL;
302 //Unless Evas 3D supports Software renderer, we set gl backened forcely. 446 //Unless Evas 3D supports Software renderer, we set gl backened forcely.
303 setenv("ECORE_EVAS_ENGINE", "opengl_x11", 1); 447 setenv("ECORE_EVAS_ENGINE", "opengl_x11", 1);
304 if (!ecore_evas_init()) return 0; 448 if (!ecore_evas_init()) return 0;
@@ -327,9 +471,25 @@ main(void)
327 evas_obj_size_set(WIDTH, HEIGHT), 471 evas_obj_size_set(WIDTH, HEIGHT),
328 evas_obj_visibility_set(EINA_TRUE)); 472 evas_obj_visibility_set(EINA_TRUE));
329 473
474 evas_object_focus_set(image, EINA_TRUE);
330 /* Set the image object as render target for 3D scene. */ 475 /* Set the image object as render target for 3D scene. */
331 eo_do(image, evas_obj_image_scene_set(data.scene)); 476 eo_do(image, evas_obj_image_scene_set(data.scene));
332 477
478 nodes1 = eina_list_append(nodes1, data.sphere.node);
479 nodes2 = eina_list_append(nodes2, data.sphere.node);
480 nodes2 = eina_list_append(nodes2, data.model.node);
481 nodes2 = eina_list_append(nodes2, data.cone.node);
482
483 /*Set callbacks*/
484 eo_do(data.cube.node, eo_event_callback_add(EVAS_3D_OBJECT_EVENT_CLICKED, _cb_clicked, NULL));
485 eo_do(data.cube.node, eo_event_callback_add(EVAS_3D_OBJECT_EVENT_COLLISION, _cb_collision, nodes2));
486
487 eo_do(data.model.node, eo_event_callback_add(EVAS_3D_OBJECT_EVENT_CLICKED, _cb_clicked, NULL));
488 eo_do(data.model.node, eo_event_callback_add(EVAS_3D_OBJECT_EVENT_COLLISION, _cb_collision, nodes1));
489
490 evas_object_event_callback_add(image, EVAS_CALLBACK_MOUSE_DOWN, _on_mouse_down, &data);
491 evas_object_event_callback_add(image, EVAS_CALLBACK_KEY_DOWN, _on_key_down, &data);
492
333 /* Add animator. */ 493 /* Add animator. */
334 ecore_animator_frametime_set(0.008); 494 ecore_animator_frametime_set(0.008);
335 anim = ecore_animator_add(_animate_scene, &data.model); 495 anim = ecore_animator_add(_animate_scene, &data.model);
@@ -337,7 +497,8 @@ main(void)
337 /* Enter main loop. */ 497 /* Enter main loop. */
338 ecore_main_loop_begin(); 498 ecore_main_loop_begin();
339 ecore_animator_del(anim); 499 ecore_animator_del(anim);
340 500 eina_list_free(nodes1);
501 eina_list_free(nodes2);
341 ecore_evas_free(ecore_evas); 502 ecore_evas_free(ecore_evas);
342 ecore_evas_shutdown(); 503 ecore_evas_shutdown();
343 504