summaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorBogdan Devichev <b.devichev@samsung.com>2015-03-18 14:46:06 +0100
committerCedric BAIL <cedric@osg.samsung.com>2015-03-18 15:22:20 +0100
commitca111165e9af8651e60d068dad188564717d0ee6 (patch)
treefe86bc4527de0cbf1f3ed22ef8f92ec1abfa954b /src/examples
parent31651a88424744054c41b3544937991488f032ef (diff)
evas: frustum example uses sphere and cube primitives.
Reviewers: cedric, Hermet, raster Subscribers: cedric Differential Revision: https://phab.enlightenment.org/D2167 Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/evas/evas-3d-frustum.c226
1 files changed, 63 insertions, 163 deletions
diff --git a/src/examples/evas/evas-3d-frustum.c b/src/examples/evas/evas-3d-frustum.c
index c9ca845..462159a 100644
--- a/src/examples/evas/evas-3d-frustum.c
+++ b/src/examples/evas/evas-3d-frustum.c
@@ -4,13 +4,13 @@
4 * Use 't'/'g' key to move near frustum plane from/to the camera. 4 * Use 't'/'g' key to move near frustum plane from/to the camera.
5 * Use '1'/'2' key to set camera to the first/second position. 5 * Use '1'/'2' key to set camera to the first/second position.
6 * Use '3'/'4'/'5' key to set bounding sphere/aabb/central point mode. 6 * Use '3'/'4'/'5' key to set bounding sphere/aabb/central point mode.
7 * Use 'i', 'k', 'j', 'l', 'u' and 'o' keys to move mesh node in 3D. 7 * Use 'i', 'k', 'j', 'l', 'u' and 'o' keys to move mesh node (model) in 3D.
8 * Use 'z', 'x', 'c', 'Z', 'X' and 'C' keys to change scaling constants of mesh. 8 * Use 'z', 'x', 'c', 'Z', 'X' and 'C' keys to change scaling constants of mesh (model).
9 * See in terminal output value distance to far plane of frustum and value of visibility of node 9 * See in terminal output value distance to far plane of frustum and value of visibility of node
10 * @see evas_3d_camera_node_visible_get. 10 * @see evas_3d_camera_node_visible_get.
11 * 11 *
12 * @verbatim 12 * @verbatim
13 * gcc -o evas-3d-frustum evas-3d-frustum.c `pkg-config --libs --cflags efl evas ecore ecore-evas eo` -lm 13 * gcc -o evas-3d-frustum evas-3d-frustum.c evas-3d-primitives.c `pkg-config --libs --cflags efl evas ecore ecore-evas eo` -lm
14 * @endverbatim 14 * @endverbatim
15 */ 15 */
16 16
@@ -28,6 +28,7 @@
28#include <Eo.h> 28#include <Eo.h>
29#include <math.h> 29#include <math.h>
30#include "evas-common.h" 30#include "evas-common.h"
31#include "evas-3d-primitives.h"
31 32
32#define WIDTH 800 33#define WIDTH 800
33#define HEIGHT 600 34#define HEIGHT 600
@@ -46,7 +47,8 @@ typedef struct _Scene_Data
46 Eo *camera; 47 Eo *camera;
47 Eo *light; 48 Eo *light;
48 Eo *mesh_model; 49 Eo *mesh_model;
49 Eo *mesh; 50 Eo *mesh_sphere;
51 Eo *mesh_aabb;
50 Eo *material_model; 52 Eo *material_model;
51 Eo *material; 53 Eo *material;
52 Eo *texture_model; 54 Eo *texture_model;
@@ -58,19 +60,7 @@ Evas_Real obj_x = 0.0, obj_y = 0.0, obj_z = 0.0, obj_sc_x = 10.0, obj_sc_y = 10.
58Evas_Real fleft = -5, fright = 5, fbottom = -5, fup = 5, fnear = 20, ffar = 1000; 60Evas_Real fleft = -5, fright = 5, fbottom = -5, fup = 5, fnear = 20, ffar = 1000;
59Evas_Real radius = 0; 61Evas_Real radius = 0;
60Evas_3D_Frustum_Mode key = EVAS_3D_FRUSTUM_MODE_AABB; 62Evas_3D_Frustum_Mode key = EVAS_3D_FRUSTUM_MODE_AABB;
61 63static const vec2 tex_scale = {1, 1};
62typedef struct _vec3
63{
64 float x;
65 float y;
66 float z;
67} vec3;
68
69static void
70_set_ball(Eo *mesh, double r, double x, double y, double z, int p);
71
72static void
73_mesh_aabb(Eo *mesh_b, Eo *mesh_node);
74 64
75static void 65static void
76_show_help() 66_show_help()
@@ -104,16 +94,54 @@ _on_canvas_resize(Ecore_Evas *ee)
104static Eina_Bool 94static Eina_Bool
105_redraw_bounding_object(void *data) 95_redraw_bounding_object(void *data)
106{ 96{
107 Evas_Real x, y, z; 97 Evas_Real px, py, pz, sx, sy, sz;
98 Evas_Real x0, y0, z0, x1, y1, z1, radius;
108 Scene_Data *scene = (Scene_Data *)data; 99 Scene_Data *scene = (Scene_Data *)data;
100 Eo *current_mesh;
101 Eina_List *meshes = NULL, *l;
102
103 eo_do(scene->mesh_node, meshes = (Eina_List *)evas_3d_node_mesh_list_get());
104 EINA_LIST_FOREACH(meshes, l, current_mesh)
105 {
106 eo_do(scene->mesh_node, evas_3d_node_mesh_del(current_mesh));
107 }
109 108
110 eo_do(scene->mesh_node_model, evas_3d_node_bounding_sphere_get(&x, &y, &z, &radius));
111 if (key == EVAS_3D_FRUSTUM_MODE_BSPHERE) 109 if (key == EVAS_3D_FRUSTUM_MODE_BSPHERE)
112 _set_ball(scene->mesh, radius, x, y, z, 100); 110 {
111 eo_do(scene->mesh_node_model,
112 evas_3d_node_bounding_sphere_get(&x0, &y0, &z0, &radius));
113 current_mesh = scene->mesh_sphere;
114 px = x0;
115 py = y0;
116 pz = z0;
117 sx = sy = sz = 2.0 * radius;
118 }
113 else if (key == EVAS_3D_FRUSTUM_MODE_AABB) 119 else if (key == EVAS_3D_FRUSTUM_MODE_AABB)
114 _mesh_aabb(scene->mesh, scene->mesh_node_model); 120 {
121 eo_do(scene->mesh_node_model, evas_3d_node_bounding_box_get(&x0, &y0, &z0,
122 &x1, &y1, &z1));
123 current_mesh = scene->mesh_aabb;
124 px = (x1 + x0) / 2;
125 py = (y1 + y0) / 2;
126 pz = (z1 + z0) / 2;
127 sx = x1 - x0;
128 sy = y1 - y0;
129 sz = z1 - z0;
130 }
115 else if (key == EVAS_3D_FRUSTUM_MODE_CENTRAL_POINT) 131 else if (key == EVAS_3D_FRUSTUM_MODE_CENTRAL_POINT)
116 _set_ball(scene->mesh, 1, x, y, z, 1); 132 {
133 eo_do(scene->mesh_node_model,
134 evas_3d_node_bounding_sphere_get(&x0, &y0, &z0, &radius));
135 current_mesh = scene->mesh_sphere;
136 px = x0;
137 py = y0;
138 pz = z0;
139 sx = sy = sz = 0.1 * radius;
140 }
141
142 eo_do(scene->mesh_node, evas_3d_node_mesh_add(current_mesh),
143 evas_3d_node_position_set(px, py, pz),
144 evas_3d_node_scale_set(sx, sy, sz));
117 145
118 return ECORE_CALLBACK_RENEW; 146 return ECORE_CALLBACK_RENEW;
119} 147}
@@ -291,140 +319,6 @@ _light_setup(Scene_Data *data)
291} 319}
292 320
293static void 321static void
294_mesh_aabb(Eo *mesh_b, Eo *mesh_node)
295{
296 Evas_Real x0, y0, z0, x1, y1, z1;
297
298 eo_do(mesh_node, evas_3d_node_bounding_box_get(&x0, &y0, &z0, &x1, &y1, &z1));
299
300 float vertices[] =
301 {
302 x0, y0, z1,
303 x0, y1, z1,
304 x1, y1, z1,
305 x1, y0, z1,
306
307 x0, y0, z0,
308 x1, y0, z0,
309 x0, y1, z0,
310 x1, y1, z0,
311
312 x0, y0, z0,
313 x0, y1, z0,
314 x0, y0, z1,
315 x0, y1, z1,
316
317 x1, y0, z0,
318 x1, y1, z0,
319 x1, y1, z1,
320 x1, y0, z1,
321
322 x0, y1, z0,
323 x1, y1, z0,
324 x0, y1, z1,
325 x1, y1, z1,
326
327 x0, y0, z0,
328 x1, y0, z0,
329 x1, y0, z1,
330 x0, y0, z1
331 };
332
333 unsigned short indices[] =
334 {
335 0, 1, 2, 3, 1, 2, 0, 3,
336 4, 5, 5, 7, 7, 6, 6, 4,
337 8, 9, 9, 11, 11, 10, 10, 8,
338 12, 13, 13, 14, 14, 15, 15, 12,
339 16, 17, 17, 19, 19, 18, 18, 16,
340 20, 21, 21, 22, 22, 23, 23, 20
341 };
342
343 float *cube_vertices = (float *) malloc(1 * sizeof(vertices));
344 unsigned short *cube_indices = (unsigned short *) malloc(1 * sizeof(indices));
345 memcpy(cube_vertices, vertices, sizeof(vertices));
346 memcpy(cube_indices, indices, sizeof(indices));
347
348 eo_do(mesh_b,
349 evas_3d_mesh_vertex_count_set(24));
350
351 eo_do(mesh_b,
352 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_POSITION,
353 3 * sizeof(float),
354 &cube_vertices[ 0]),
355 evas_3d_mesh_index_data_copy_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
356 48, &cube_indices[0]));
357 free(cube_vertices);
358 free(cube_indices);
359}
360
361static void
362_set_ball(Eo *mesh, double r, double x, double y, double z, int p)
363{
364 int vcount, icount, vccount, i, j;
365 double dtheta, dfi, sinth, costh, fi, theta, sinfi, cosfi;
366 unsigned short *indices, *index;
367
368 icount = p * p * 6;
369 vccount = p + 1;
370 vcount = vccount * vccount;
371
372 dtheta = M_PI / p;
373 dfi = 2 * M_PI / p;
374
375 vec3 *vertices = malloc(sizeof(vec3) * vcount);
376 vec3 *normals = malloc(sizeof(vec3) * vcount);
377
378 for (j = 0; j < vccount; j++)
379 {
380 theta = j * dtheta;
381 sinth = sin(theta);
382 costh = cos(theta);
383 for (i = 0; i < vccount; i++)
384 {
385 fi = i * dfi;
386 sinfi = sin(fi);
387 cosfi = cos(fi);
388 vertices[i + j * vccount].x = r * sinth * cosfi + x;
389 vertices[i + j * vccount].y = r * sinth * sinfi + y;
390 vertices[i + j * vccount].z = r * costh + z;
391
392 normals[i + j * vccount].x = sinth * cosfi;
393 normals[i + j * vccount].y = sinth * sinfi;
394 normals[i + j * vccount].z = costh;
395 }
396 }
397
398 indices = malloc(sizeof(short) * icount);
399 index = &indices[0];
400
401 for(j = 0; j < p; j++)
402 for(i = 0; i < p; i++)
403 {
404 *index++ = (unsigned short)(i + vccount * j);
405 *index++ = i + vccount * (j + 1);
406 *index++ = i + 1 + vccount * (j + 1);
407
408 *index++ = i + vccount * j;
409 *index++ = i + 1 + vccount * j;
410 *index++ = i + vccount * (j + 1) + 1;
411 }
412
413 eo_do(mesh,
414 evas_3d_mesh_vertex_count_set(vcount),
415 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_POSITION,
416 sizeof(vec3), &vertices[0]),
417 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_NORMAL,
418 sizeof(vec3), &normals[0]),
419 evas_3d_mesh_index_data_copy_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
420 icount , &indices[0]));
421
422 free(vertices);
423 free(normals);
424 free(indices);
425}
426
427static void
428_mesh_setup(Scene_Data *data) 322_mesh_setup(Scene_Data *data)
429{ 323{
430 data->material = eo_add(EVAS_3D_MATERIAL_CLASS, evas); 324 data->material = eo_add(EVAS_3D_MATERIAL_CLASS, evas);
@@ -439,18 +333,24 @@ _mesh_setup(Scene_Data *data)
439 evas_3d_material_color_set(EVAS_3D_MATERIAL_SPECULAR, 1.0, 1.0, 1.0, 1.0), 333 evas_3d_material_color_set(EVAS_3D_MATERIAL_SPECULAR, 1.0, 1.0, 1.0, 1.0),
440 evas_3d_material_shininess_set(100.0)); 334 evas_3d_material_shininess_set(100.0));
441 335
442 data->mesh = eo_add(EVAS_3D_MESH_CLASS, evas); 336 data->mesh_aabb = eo_add(EVAS_3D_MESH_CLASS, evas);
443 eo_do(data->mesh, 337 evas_3d_add_cube_frame(data->mesh_aabb, 0);
444 evas_3d_mesh_frame_add(0); 338 eo_do(data->mesh_aabb,
445 evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_LINES); 339 evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_LINES),
446 evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_DIFFUSE), 340 evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_DIFFUSE),
447 evas_3d_mesh_frame_material_set(0, data->material)); 341 evas_3d_mesh_frame_material_set(0, data->material));
448 342
343 data->mesh_sphere = eo_add(EVAS_3D_MESH_CLASS, evas);
344 evas_3d_add_sphere_frame(data->mesh_sphere, 0, 20, tex_scale);
345 eo_do(data->mesh_sphere,
346 evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_LINES),
347 evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_DIFFUSE),
348 evas_3d_mesh_frame_material_set(0, data->material));
449 349
450 data->mesh_node = 350 data->mesh_node =
451 eo_add(EVAS_3D_NODE_CLASS, evas, evas_3d_node_constructor(EVAS_3D_NODE_TYPE_MESH)); 351 eo_add(EVAS_3D_NODE_CLASS, evas, evas_3d_node_constructor(EVAS_3D_NODE_TYPE_MESH));
452 eo_do(data->root_node, evas_3d_node_member_add(data->mesh_node)); 352 eo_do(data->root_node, evas_3d_node_member_add(data->mesh_node));
453 eo_do(data->mesh_node, evas_3d_node_mesh_add(data->mesh)); 353 eo_do(data->mesh_node, evas_3d_node_mesh_add(data->mesh_aabb));
454} 354}
455 355
456static void 356static void