diff options
Diffstat (limited to '')
-rw-r--r-- | src/extantz/Evas_3D_demo.c | 243 |
1 files changed, 5 insertions, 238 deletions
diff --git a/src/extantz/Evas_3D_demo.c b/src/extantz/Evas_3D_demo.c index 7e3e8cd..dbc589d 100644 --- a/src/extantz/Evas_3D_demo.c +++ b/src/extantz/Evas_3D_demo.c | |||
@@ -1,7 +1,5 @@ | |||
1 | #include "extantz.h" | 1 | #include "extantz.h" |
2 | 2 | #include "scenri.h" | |
3 | |||
4 | static Scene_Data ourScene; | ||
5 | 3 | ||
6 | 4 | ||
7 | static const float cube_vertices[] = | 5 | static const float cube_vertices[] = |
@@ -81,36 +79,6 @@ static const unsigned int pixels1[] = | |||
81 | }; | 79 | }; |
82 | 80 | ||
83 | 81 | ||
84 | typedef struct _vec4 | ||
85 | { | ||
86 | float x; | ||
87 | float y; | ||
88 | float z; | ||
89 | float w; | ||
90 | } vec4; | ||
91 | |||
92 | typedef struct _vec3 | ||
93 | { | ||
94 | float x; | ||
95 | float y; | ||
96 | float z; | ||
97 | } vec3; | ||
98 | |||
99 | typedef struct _vec2 | ||
100 | { | ||
101 | float x; | ||
102 | float y; | ||
103 | } vec2; | ||
104 | |||
105 | typedef struct _vertex | ||
106 | { | ||
107 | vec3 position; | ||
108 | vec3 normal; | ||
109 | vec3 tangent; | ||
110 | vec4 color; | ||
111 | vec3 texcoord; | ||
112 | } vertex; | ||
113 | |||
114 | static int vertex_count = 0; | 82 | static int vertex_count = 0; |
115 | static vertex *sphere_vertices = NULL; | 83 | static vertex *sphere_vertices = NULL; |
116 | 84 | ||
@@ -265,7 +233,6 @@ _sphere_init(int precision) | |||
265 | } | 233 | } |
266 | } | 234 | } |
267 | 235 | ||
268 | |||
269 | Eina_Bool _animate_scene(globals *ourGlobals) | 236 | Eina_Bool _animate_scene(globals *ourGlobals) |
270 | { | 237 | { |
271 | static float angle = 0.0f; | 238 | static float angle = 0.0f; |
@@ -273,10 +240,6 @@ Eina_Bool _animate_scene(globals *ourGlobals) | |||
273 | static int frame = 0; | 240 | static int frame = 0; |
274 | static int inc = 1; | 241 | static int inc = 1; |
275 | static int sonicFrame = 0; | 242 | static int sonicFrame = 0; |
276 | Evas_Real x, y, z, w; | ||
277 | EPhysics_Quaternion *quat = ephysics_quaternion_new(); | ||
278 | EPhysics_Quaternion *quat1 = ephysics_quaternion_new(); | ||
279 | EPhysics_Quaternion *result = ephysics_quaternion_new(); | ||
280 | 243 | ||
281 | Scene_Data *scene = ourGlobals->scene; | 244 | Scene_Data *scene = ourGlobals->scene; |
282 | 245 | ||
@@ -307,67 +270,9 @@ Eina_Bool _animate_scene(globals *ourGlobals) | |||
307 | evas_3d_node_orientation_angle_axis_set(angle, 0.0, 1.0, 0.0) | 270 | evas_3d_node_orientation_angle_axis_set(angle, 0.0, 1.0, 0.0) |
308 | ); | 271 | ); |
309 | 272 | ||
310 | // Camera movement. | ||
311 | ephysics_quaternion_euler_set(quat1, ourGlobals->gld.move->r, ourGlobals->gld.move->s, ourGlobals->gld.move->t); | ||
312 | eo_do(scene->camera_node, evas_3d_node_orientation_get(EVAS_3D_SPACE_PARENT, &x, &y, &z, &w)); | ||
313 | ephysics_quaternion_set(quat, x, y, z, w); | ||
314 | ephysics_quaternion_multiply(quat, quat1, result); | ||
315 | ephysics_quaternion_normalize(result); | ||
316 | ephysics_quaternion_get(result, &x, &y, &z, &w); | ||
317 | eo_do(scene->camera_node, evas_3d_node_orientation_set(x, y, z, w)); | ||
318 | |||
319 | eo_do(scene->camera_node, evas_3d_node_position_get(EVAS_3D_SPACE_PARENT, &x, &y, &z)); | ||
320 | x -= ourGlobals->gld.move->x; | ||
321 | y -= ourGlobals->gld.move->y; | ||
322 | z -= ourGlobals->gld.move->z; | ||
323 | eo_do(scene->camera_node, evas_3d_node_position_set(x, y, z)); | ||
324 | |||
325 | free(result); | ||
326 | free(quat1); | ||
327 | free(quat); | ||
328 | |||
329 | return EINA_TRUE; | 273 | return EINA_TRUE; |
330 | } | 274 | } |
331 | 275 | ||
332 | |||
333 | static void | ||
334 | _camera_setup(globals *ourGlobals, Scene_Data *scene) | ||
335 | { | ||
336 | scene->camera = eo_add(EVAS_3D_CAMERA_CLASS, ourGlobals->evas, | ||
337 | evas_3d_camera_projection_perspective_set(60.0, 1.0, 1.0, 500.0) | ||
338 | ); | ||
339 | |||
340 | scene->camera_node = evas_3d_node_add(ourGlobals->evas, EVAS_3D_NODE_TYPE_CAMERA); | ||
341 | eo_do(scene->camera_node, | ||
342 | evas_3d_node_camera_set(scene->camera), | ||
343 | evas_3d_node_position_set(50.0, 0.0, 20.0), | ||
344 | evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 20.0, EVAS_3D_SPACE_PARENT, 0.0, 0.0, 1.0) | ||
345 | ); | ||
346 | |||
347 | eo_do(scene->root_node, evas_3d_node_member_add(scene->camera_node)); | ||
348 | } | ||
349 | |||
350 | static void | ||
351 | _light_setup(globals *ourGlobals, Scene_Data *scene) | ||
352 | { | ||
353 | scene->light = eo_add(EVAS_3D_LIGHT_CLASS, ourGlobals->evas, | ||
354 | evas_3d_light_ambient_set(1.0, 1.0, 1.0, 1.0), | ||
355 | evas_3d_light_diffuse_set(1.0, 1.0, 1.0, 1.0), | ||
356 | evas_3d_light_specular_set(1.0, 1.0, 1.0, 1.0), | ||
357 | evas_3d_light_directional_set(EINA_TRUE) | ||
358 | ); | ||
359 | |||
360 | scene->light_node = evas_3d_node_add(ourGlobals->evas, EVAS_3D_NODE_TYPE_LIGHT); | ||
361 | eo_do(scene->light_node, | ||
362 | evas_3d_node_light_set(scene->light), | ||
363 | evas_3d_node_position_set(1000.0, 0.0, 1000.0), | ||
364 | evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0, EVAS_3D_SPACE_PARENT, 0.0, 1.0, 0.0) | ||
365 | ); | ||
366 | |||
367 | eo_do(scene->root_node, evas_3d_node_member_add(scene->light_node)); | ||
368 | |||
369 | } | ||
370 | |||
371 | static void _cube_setup(globals *ourGlobals, Scene_Data *scene) | 276 | static void _cube_setup(globals *ourGlobals, Scene_Data *scene) |
372 | { | 277 | { |
373 | char buf[PATH_MAX]; | 278 | char buf[PATH_MAX]; |
@@ -541,151 +446,13 @@ static void _earth_setup(globals *ourGlobals, Scene_Data *scene) | |||
541 | } | 446 | } |
542 | 447 | ||
543 | 448 | ||
544 | static void | ||
545 | _scene_setup(globals *ourGlobals, Scene_Data *scene) | ||
546 | { | ||
547 | // TODO - I have no idea how this should work. | ||
548 | // It seems the people that wrote the examples don't know either. lol | ||
549 | // scene->root_node = eo_add(EVAS_3D_NODE_CLASS, ourGlobals->evas, EVAS_3D_NODE_TYPE_NODE); | ||
550 | scene->root_node = evas_3d_node_add(ourGlobals->evas, EVAS_3D_NODE_TYPE_NODE); | ||
551 | |||
552 | scene->scene = eo_add(EVAS_3D_SCENE_CLASS, ourGlobals->evas, | ||
553 | evas_3d_scene_root_node_set(scene->root_node), | ||
554 | evas_3d_scene_size_set(512, 512), | ||
555 | evas_3d_scene_background_color_set(0.0, 0.0, 0.0, 0.0) | ||
556 | ); | ||
557 | |||
558 | _camera_setup(ourGlobals, scene); | ||
559 | _light_setup(ourGlobals, scene); | ||
560 | _cube_setup(ourGlobals, scene); | ||
561 | _sonic_setup(ourGlobals, scene); | ||
562 | _earth_setup(ourGlobals, scene); | ||
563 | |||
564 | eo_do(scene->scene, | ||
565 | evas_3d_scene_camera_node_set(scene->camera_node) | ||
566 | ); | ||
567 | } | ||
568 | |||
569 | |||
570 | static void _on_mouse_move(void *data, Evas *e EINA_UNUSED, Evas_Object *o, void *einfo) | ||
571 | { | ||
572 | Scene_Data *scene = data; | ||
573 | Evas_Event_Mouse_Move *ev = einfo; | ||
574 | Evas_Coord x, y, w, h; | ||
575 | Evas_Coord obj_x, obj_y; | ||
576 | int scene_w, scene_h; | ||
577 | Evas_Real scene_x, scene_y; | ||
578 | Evas_Real s, t; | ||
579 | Evas_3D_Node *n; | ||
580 | Evas_3D_Mesh *m; | ||
581 | Eina_Bool pick; | ||
582 | char *name = NULL; | ||
583 | |||
584 | evas_object_geometry_get(o, &x, &y, &w, &h); | ||
585 | |||
586 | obj_x = ev->cur.canvas.x - x; | ||
587 | obj_y = ev->cur.canvas.y - y; | ||
588 | |||
589 | eo_do(scene->scene, evas_3d_scene_size_get(&scene_w, &scene_h)); | ||
590 | |||
591 | scene_x = obj_x * scene_w / (Evas_Real)w; | ||
592 | scene_y = obj_y * scene_h / (Evas_Real)h; | ||
593 | |||
594 | eo_do(scene->scene, pick = evas_3d_scene_pick(scene_x, scene_y, &n, &m, &s, &t)); | ||
595 | if (pick) | ||
596 | name = evas_object_data_get(n, "Name"); | ||
597 | // This is a raw Evas callback, on the Elm image internal Evas_Object. | ||
598 | // So we need to get the Elm Image back from the raw Evas_Object. | ||
599 | // Which is why we stuffed it in the scene structure. | ||
600 | if (name) | ||
601 | { | ||
602 | elm_object_tooltip_text_set(scene->image, name); | ||
603 | elm_object_tooltip_show(scene->image); | ||
604 | } | ||
605 | else | ||
606 | { | ||
607 | elm_object_tooltip_text_set(scene->image, ""); | ||
608 | elm_object_tooltip_hide(scene->image); | ||
609 | } | ||
610 | } | ||
611 | |||
612 | static void _on_mouse_down(void *data, Evas *e EINA_UNUSED, Evas_Object *o, void *einfo) | ||
613 | { | ||
614 | Scene_Data *scene = data; | ||
615 | Evas_Event_Mouse_Down *ev = einfo; | ||
616 | Evas_Coord x, y, w, h; | ||
617 | Evas_Coord obj_x, obj_y; | ||
618 | int scene_w, scene_h; | ||
619 | Evas_Real scene_x, scene_y; | ||
620 | Evas_Real s, t; | ||
621 | Evas_3D_Node *n; | ||
622 | Evas_3D_Mesh *m; | ||
623 | Eina_Bool pick; | ||
624 | char *name = NULL; | ||
625 | |||
626 | // Set the focus onto us. | ||
627 | elm_object_focus_set(o, EINA_TRUE); | ||
628 | |||
629 | evas_object_geometry_get(o, &x, &y, &w, &h); | ||
630 | |||
631 | obj_x = ev->canvas.x - x; | ||
632 | obj_y = ev->canvas.y - y; | ||
633 | |||
634 | eo_do(scene->scene, evas_3d_scene_size_get(&scene_w, &scene_h)); | ||
635 | |||
636 | scene_x = obj_x * scene_w / (Evas_Real)w; | ||
637 | scene_y = obj_y * scene_h / (Evas_Real)h; | ||
638 | |||
639 | eo_do(scene->scene, pick = evas_3d_scene_pick(scene_x, scene_y, &n, &m, &s, &t)); | ||
640 | if (pick) | ||
641 | { | ||
642 | name = evas_object_data_get(n, "Name"); | ||
643 | printf("Picked : "); | ||
644 | } | ||
645 | else | ||
646 | printf("Not picked : "); | ||
647 | if (NULL == name) | ||
648 | name = ""; | ||
649 | |||
650 | printf("output(%d, %d) canvas(%d, %d) object(%d, %d) scene(%f, %f) texcoord(%f, %f) " | ||
651 | "node(%p) %s mesh(%p)\n", | ||
652 | ev->output.x, ev->output.y, | ||
653 | ev->canvas.x, ev->canvas.y, | ||
654 | obj_x, obj_y, | ||
655 | scene_x, scene_y, | ||
656 | s, t, n, name, m); | ||
657 | } | ||
658 | |||
659 | void Evas_3D_Demo_add(globals *ourGlobals) | 449 | void Evas_3D_Demo_add(globals *ourGlobals) |
660 | { | 450 | { |
661 | Evas_Object *obj, *temp; | 451 | ourGlobals->scene = scenriAdd(ourGlobals); |
662 | |||
663 | ourGlobals->scene = &ourScene; | ||
664 | _scene_setup(ourGlobals, &ourScene); | ||
665 | |||
666 | // Add an image object for 3D scene rendering. | ||
667 | obj = eo_add(ELM_OBJ_IMAGE_CLASS, ourGlobals->win, | ||
668 | evas_obj_size_hint_weight_set(EVAS_HINT_EXPAND, EVAS_HINT_EXPAND), | ||
669 | elm_obj_image_fill_outside_set(EINA_TRUE), | ||
670 | evas_obj_visibility_set(EINA_TRUE), | ||
671 | temp = elm_obj_image_object_get() | ||
672 | ); | ||
673 | elm_object_tooltip_text_set(obj, ""); | ||
674 | elm_object_tooltip_hide(obj); | ||
675 | ourScene.image = obj; | ||
676 | |||
677 | eo_do(temp, | ||
678 | evas_obj_image_scene_set(ourScene.scene) | ||
679 | ); | ||
680 | // Elm can't seem to be able to tell us WHERE an image was clicked, so use raw Evas calbacks instead. | ||
681 | evas_object_event_callback_add(temp, EVAS_CALLBACK_MOUSE_MOVE, _on_mouse_move, &ourScene); | ||
682 | evas_object_event_callback_add(temp, EVAS_CALLBACK_MOUSE_DOWN, _on_mouse_down, &ourScene); | ||
683 | |||
684 | cameraAdd(ourGlobals, obj); | ||
685 | elm_win_resize_object_add(ourGlobals->win, obj); | ||
686 | // elm_box_pack_end(ourGlobals->gld.bx, obj); | ||
687 | 452 | ||
688 | ourGlobals->gld.move = calloc(1, sizeof(cameraMove)); | 453 | _cube_setup(ourGlobals, ourGlobals->scene); |
454 | _sonic_setup(ourGlobals, ourGlobals->scene); | ||
455 | _earth_setup(ourGlobals, ourGlobals->scene); | ||
689 | } | 456 | } |
690 | 457 | ||
691 | void Evas_3D_Demo_fini(globals *ourGlobals) | 458 | void Evas_3D_Demo_fini(globals *ourGlobals) |