summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOleksandr Shcherbina <o.shcherbina@samsung.com>2014-10-09 11:41:23 +0300
committerCedric BAIL <cedric@osg.samsung.com>2014-12-01 06:27:40 +0100
commit30a44d91006ca7dbcce3f00b5ebd4192ebfa40ef (patch)
treed7f43ac6a19c304d667e9746472bfc8e01ca5b23
parent54c1667d4e4dfc160fa8a7ce6118d6032645314a (diff)
evas: Evas_3D - example which show frustum culing feature
Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
-rw-r--r--src/examples/evas/evas-3d-frustum.c471
1 files changed, 413 insertions, 58 deletions
diff --git a/src/examples/evas/evas-3d-frustum.c b/src/examples/evas/evas-3d-frustum.c
index be81add..3e88bd8 100644
--- a/src/examples/evas/evas-3d-frustum.c
+++ b/src/examples/evas/evas-3d-frustum.c
@@ -1,11 +1,14 @@
1/* 1/*
2 * This example shows how to work frustum culling. 2 * This example shows how to work frustum culling.
3 * Use keys Up/Down for moving far plane of frustum. 3 * Use 'w'/'s' key to move far frustum plane from/to the camera.
4 * Use keys Left/Right for changing camera view. 4 * Use 't'/'g' key to move near frustum plane from/to the camera.
5 * See in terminal output value of z coordinate of far plane of frustum 5 * Use '1'/'2' key to set camera to the first/second position.
6 * and check OBB's points inside frustum. 6 * Use '3'/'4'/'5' key to set bounding sphere/aabb/central point mode.
7 * @see evas_3d_node_obb_frustum_check. 7 * Use 'i', 'k', 'j', 'l', 'u' and 'o' keys to move mesh node in 3D.
8 * Compile with "gcc -o evas-3d-frustum evas-3d-frustum.c `pkg-config --libs --cflags evas ecore ecore-evas eo`" 8 * Use 'z', 'x', 'c', 'Z', 'X' and 'C' keys to change scaling constants of mesh.
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.
11 * Compile with "gcc -o evas-3d-frustum evas-3d-frustum.c `pkg-config --libs --cflags evas ecore ecore-evas eo eina` -lm"
9 */ 12 */
10 13
11#define EFL_EO_API_SUPPORT 14#define EFL_EO_API_SUPPORT
@@ -15,6 +18,7 @@
15#include <Ecore_Evas.h> 18#include <Ecore_Evas.h>
16#include <Evas.h> 19#include <Evas.h>
17#include <Eo.h> 20#include <Eo.h>
21#include <math.h>
18 22
19#define WIDTH 800 23#define WIDTH 800
20#define HEIGHT 600 24#define HEIGHT 600
@@ -25,17 +29,47 @@ typedef struct _Scene_Data
25 Eo *camera_node; 29 Eo *camera_node;
26 Eo *light_node; 30 Eo *light_node;
27 Eo *mesh_node_model; 31 Eo *mesh_node_model;
32 Eo *mesh_node;
28 Eo *scene; 33 Eo *scene;
29 Eo *camera; 34 Eo *camera;
30 Eo *light; 35 Eo *light;
31 Eo *mesh_model; 36 Eo *mesh_model;
37 Eo *mesh;
32 Eo *material_model; 38 Eo *material_model;
39 Eo *material;
33 Eo *texture_model; 40 Eo *texture_model;
34} Scene_Data; 41} Scene_Data;
35 42
36Evas *evas; 43Evas *evas;
37Evas_Object *background,*image; 44Evas_Object *background,*image;
38Evas_Real fleft = -5, fright = 5, fbottom = -5, fup = 5, fnear = 20, ffar = 40; 45Evas_Real obj_x = 0.0, obj_y = 0.0, obj_z = 0.0, obj_sc_x = 1.0, obj_sc_y = 1.0, obj_sc_z = 1.0;
46Evas_Real fleft = -5, fright = 5, fbottom = -5, fup = 5, fnear = 20, ffar = 1000;
47Evas_Real radius = 0;
48Evas_3D_Frustum_Mode key = EVAS_3D_FRUSTUM_MODE_BSPHERE;
49
50typedef struct _vec3
51{
52 float x;
53 float y;
54 float z;
55} vec3;
56
57static void
58_set_ball(Eo *mesh, double r, double x, double y, double z, int p, Evas_3D_Material *material);
59
60static void
61_mesh_aabb(Eo *mesh_b, Eo *mesh_node);
62
63static void
64_show_help()
65{
66 fprintf(stdout, "Press 'w'/'s' key to move far frustum plane from/to the camera\n");
67 fprintf(stdout, "Press 't'/'g' key to move near frustum plane from/to the camera\n");
68 fprintf(stdout, "Press '1'/'2' key to set camera to the first/second position\n");
69 fprintf(stdout, "Press '3'/'4'/'5' key to set bounding sphere/aabb/central point mode\n");
70 fprintf(stdout, "Press 'i', 'k', 'j', 'l', 'u' and 'o' keys to move mesh node in 3D\n");
71 fprintf(stdout, "Press 'z', 'x', 'c', 'Z', 'X' and 'C' keys to change scaling constants of mesh\n");
72}
39 73
40static void 74static void
41_on_delete(Ecore_Evas *ee EINA_UNUSED) 75_on_delete(Ecore_Evas *ee EINA_UNUSED)
@@ -55,66 +89,216 @@ _on_canvas_resize(Ecore_Evas *ee)
55 evas_object_move(image, 0, 0); 89 evas_object_move(image, 0, 0);
56} 90}
57 91
92static Eina_Bool
93_redraw_bounding_object(void *data)
94{
95 Evas_Real x, y, z;
96 vec3 center, ref, temp;
97 Scene_Data *scene = (Scene_Data *)data;
98
99 eo_do(scene->mesh_node_model, evas_3d_node_bounding_sphere_get(&x, &y, &z, &radius));
100 if (key == EVAS_3D_FRUSTUM_MODE_BSPHERE)
101 _set_ball(scene->mesh, radius, x, y, z, 100, scene->material);
102 else if (key == EVAS_3D_FRUSTUM_MODE_AABB)
103 _mesh_aabb(scene->mesh, scene->mesh_node_model);
104 else if (key == EVAS_3D_FRUSTUM_MODE_CENTRAL_POINT)
105 _set_ball(scene->mesh, 1, x, y, z, 1, scene->material);
106
107 return ECORE_CALLBACK_RENEW;
108}
109
58static void 110static void
59_on_key_down(void *data, Evas *e EINA_UNUSED, Evas_Object *eo EINA_UNUSED, void *event_info) 111_on_key_down(void *data, Evas *e EINA_UNUSED, Evas_Object *eo EINA_UNUSED, void *event_info)
60{ 112{
61
62 Scene_Data *scene = (Scene_Data *)data; 113 Scene_Data *scene = (Scene_Data *)data;
63 Evas_Event_Key_Down *ev = event_info; 114 Evas_Event_Key_Down *ev = event_info;
64 int frustum; 115 Eina_Bool frustum;
116 Evas_Real x, y, z;
65 117
66 if (!strcmp("Up", ev->key)) 118 if (!strcmp("w", ev->key))
119 {
120 ffar += 20;
121 eo_do(scene->camera, evas_3d_camera_projection_frustum_set(fleft, fright, fbottom, fup, fnear, ffar),
122 frustum = evas_3d_camera_node_visible_get(scene->camera_node, scene->mesh_node_model, key));
123 fprintf(stdout, "far - %f frustum - %d radius - %f\n", ffar, frustum, radius);
124 }
125 else if(!strcmp("s", ev->key))
67 { 126 {
68 ffar += 5; 127 ffar -= 20;
69 eo_do(scene->camera, evas_3d_camera_projection_frustum_set(fleft, fright, fbottom, fup, fnear, ffar)); 128 eo_do(scene->camera, evas_3d_camera_projection_frustum_set(fleft, fright, fbottom, fup, fnear, ffar),
129 frustum = evas_3d_camera_node_visible_get(scene->camera_node, scene->mesh_node_model, key));
130 fprintf(stdout, "far - %f frustum - %d radius - %f\n", ffar, frustum, radius);
131 }
132 else if(!strcmp("t", ev->key))
133 {
134 fnear += 2;
135 eo_do(scene->camera, evas_3d_camera_projection_frustum_set(fleft, fright, fbottom, fup, fnear, ffar),
136 frustum = evas_3d_camera_node_visible_get(scene->camera_node, scene->mesh_node_model, key));
137 fprintf(stdout, "far - %f frustum - %d radius - %f\n", ffar, frustum, radius);
138 }
139 else if(!strcmp("g", ev->key))
140 {
141 fnear -= 2;
142 eo_do(scene->camera, evas_3d_camera_projection_frustum_set(fleft, fright, fbottom, fup, fnear, ffar),
143 frustum = evas_3d_camera_node_visible_get(scene->camera_node, scene->mesh_node_model, key));
144 fprintf(stdout, "far - %f frustum - %d radius - %f\n", ffar, frustum, radius);
145 }
146 else if(!strcmp("i", ev->key))
147 {
148 obj_y++;
70 eo_do(scene->mesh_node_model, 149 eo_do(scene->mesh_node_model,
71 frustum = evas_3d_node_obb_frustum_check(scene->camera_node)); 150 evas_3d_node_position_set(obj_x, obj_y, obj_z),
72 fprintf(stdout, "far - %f frustum - %d \n", ffar, frustum); 151 evas_3d_node_scale_set(obj_sc_x, obj_sc_y, obj_sc_z),
152 evas_3d_object_update());
153 eo_do(scene->camera, frustum = evas_3d_camera_node_visible_get(scene->camera_node, scene->mesh_node_model, key));
154 fprintf(stdout, "far - %f frustum - %d radius - %f\n", ffar, frustum, radius);
73 } 155 }
74 else if(!strcmp("Down", ev->key)) 156 else if(!strcmp("k", ev->key))
75 { 157 {
76 ffar -= 5; 158 obj_y--;
77 eo_do(scene->camera, evas_3d_camera_projection_frustum_set(fleft, fright, fbottom, fup, fnear, ffar));
78 eo_do(scene->mesh_node_model, 159 eo_do(scene->mesh_node_model,
79 frustum = evas_3d_node_obb_frustum_check(scene->camera_node)); 160 evas_3d_node_position_set(obj_x, obj_y, obj_z),
80 fprintf(stdout, "far - %f frustum - %d \n", ffar, frustum); 161 evas_3d_node_scale_set(obj_sc_x, obj_sc_y, obj_sc_z),
162 evas_3d_object_update());
163 eo_do(scene->camera, frustum = evas_3d_camera_node_visible_get(scene->camera_node, scene->mesh_node_model, key));
164 fprintf(stdout, "far - %f frustum - %d radius - %f\n", ffar, frustum, radius);
81 } 165 }
82 else if(!strcmp("Return", ev->key)) 166 else if(!strcmp("o", ev->key))
167 {
168 obj_z++;
169 eo_do(scene->mesh_node_model,
170 evas_3d_node_position_set(obj_x, obj_y, obj_z),
171 evas_3d_node_scale_set(obj_sc_x, obj_sc_y, obj_sc_z),
172 evas_3d_object_update());
173 eo_do(scene->camera, frustum = evas_3d_camera_node_visible_get(scene->camera_node, scene->mesh_node_model, key));
174 fprintf(stdout, "far - %f frustum - %d radius - %f\n", ffar, frustum, radius);
175 }
176 else if(!strcmp("u", ev->key))
177 {
178 obj_z--;
179 eo_do(scene->mesh_node_model,
180 evas_3d_node_position_set(obj_x, obj_y, obj_z),
181 evas_3d_node_scale_set(obj_sc_x, obj_sc_y, obj_sc_z),
182 evas_3d_object_update());
183 eo_do(scene->camera, frustum = evas_3d_camera_node_visible_get(scene->camera_node, scene->mesh_node_model, key));
184 fprintf(stdout, "far - %f frustum - %d radius - %f\n", ffar, frustum, radius);
185 }
186 else if(!strcmp("l", ev->key))
187 {
188 obj_x++;
189 eo_do(scene->mesh_node_model,
190 evas_3d_node_position_set(obj_x, obj_y, obj_z),
191 evas_3d_node_scale_set(obj_sc_x, obj_sc_y, obj_sc_z),
192 evas_3d_object_update());
193 eo_do(scene->camera, frustum = evas_3d_camera_node_visible_get(scene->camera_node, scene->mesh_node_model, key));
194 fprintf(stdout, "far - %f frustum - %d radius - %f\n", ffar, frustum, radius);
195 }
196 else if(!strcmp("j", ev->key))
197 {
198 obj_x--;
199 eo_do(scene->mesh_node_model,
200 evas_3d_node_position_set(obj_x, obj_y, obj_z),
201 evas_3d_node_scale_set(obj_sc_x, obj_sc_y, obj_sc_z),
202 evas_3d_object_update());
203 eo_do(scene->camera, frustum = evas_3d_camera_node_visible_get(scene->camera_node, scene->mesh_node_model, key));
204 fprintf(stdout, "far - %f frustum - %d radius - %f\n", ffar, frustum, radius);
205 }
206 else if(!strcmp("z", ev->key))
207 {
208 obj_sc_y+=0.02;
209 eo_do(scene->mesh_node_model,
210 evas_3d_node_position_set(obj_x, obj_y, obj_z),
211 evas_3d_node_scale_set(obj_sc_x, obj_sc_y, obj_sc_z),
212 evas_3d_object_update());
213 eo_do(scene->camera, frustum = evas_3d_camera_node_visible_get(scene->camera_node, scene->mesh_node_model, key));
214 fprintf(stdout, "far - %f frustum - %d radius - %f\n", ffar, frustum, radius);
215 }
216 else if(!strcmp("Z", ev->key))
217 {
218 obj_sc_y-=0.02;
219 eo_do(scene->mesh_node_model,
220 evas_3d_node_position_set(obj_x, obj_y, obj_z),
221 evas_3d_node_scale_set(obj_sc_x, obj_sc_y, obj_sc_z),
222 evas_3d_object_update());
223 eo_do(scene->camera, frustum = evas_3d_camera_node_visible_get(scene->camera_node, scene->mesh_node_model, key));
224 fprintf(stdout, "far - %f frustum - %d radius - %f\n", ffar, frustum, radius);
225 }
226 else if(!strcmp("x", ev->key))
227 {
228 obj_sc_z+=0.02;
229 eo_do(scene->mesh_node_model,
230 evas_3d_node_position_set(obj_x, obj_y, obj_z),
231 evas_3d_node_scale_set(obj_sc_x, obj_sc_y, obj_sc_z),
232 evas_3d_object_update());
233 eo_do(scene->camera, frustum = evas_3d_camera_node_visible_get(scene->camera_node, scene->mesh_node_model, key));
234 fprintf(stdout, "far - %f frustum - %d radius - %f\n", ffar, frustum, radius);
235 }
236 else if(!strcmp("X", ev->key))
237 {
238 obj_sc_z-=0.02;
239 eo_do(scene->mesh_node_model,
240 evas_3d_node_position_set(obj_x, obj_y, obj_z),
241 evas_3d_node_scale_set(obj_sc_x, obj_sc_y, obj_sc_z),
242 evas_3d_object_update());
243 eo_do(scene->camera, frustum = evas_3d_camera_node_visible_get(scene->camera_node, scene->mesh_node_model, key));
244 fprintf(stdout, "far - %f frustum - %d radius - %f\n", ffar, frustum, radius);
245 }
246 else if(!strcmp("c", ev->key))
247 {
248 obj_sc_x+=0.02;
249 eo_do(scene->mesh_node_model,
250 evas_3d_node_position_set(obj_x, obj_y, obj_z),
251 evas_3d_node_scale_set(obj_sc_x, obj_sc_y, obj_sc_z),
252 evas_3d_object_update());
253 eo_do(scene->camera, frustum = evas_3d_camera_node_visible_get(scene->camera_node, scene->mesh_node_model, key));
254 fprintf(stdout, "far - %f frustum - %d radius - %f\n", ffar, frustum, radius);
255 }
256 else if(!strcmp("C", ev->key))
83 { 257 {
258 obj_sc_x-=0.02;
84 eo_do(scene->mesh_node_model, 259 eo_do(scene->mesh_node_model,
85 frustum = evas_3d_node_obb_frustum_check(scene->camera_node)); 260 evas_3d_node_position_set(obj_x, obj_y, obj_z),
86 fprintf(stdout, "far - %f frustum - %d \n", ffar, frustum); 261 evas_3d_node_scale_set(obj_sc_x, obj_sc_y, obj_sc_z),
262 evas_3d_object_update());
263 eo_do(scene->camera, frustum = evas_3d_camera_node_visible_get(scene->camera_node, scene->mesh_node_model, key));
264 fprintf(stdout, "far - %f frustum - %d radius - %f\n", ffar, frustum, radius);
87 } 265 }
88 else if (!strcmp("Left", ev->key)) 266 else if(!strcmp("Return", ev->key))
267 {
268 eo_do(scene->camera, evas_3d_camera_projection_frustum_set(fleft, fright, fbottom, fup, fnear, ffar),
269 frustum = evas_3d_camera_node_visible_get(scene->camera_node, scene->mesh_node_model, key));
270 fprintf(stdout, "far - %f frustum - %d radius - %f\n", ffar, frustum, radius);
271 }
272 else if (!strcmp("1", ev->key))
89 { 273 {
90 eo_do(scene->camera_node, evas_3d_node_position_set(50.0, 0.0, 0.0), 274 eo_do(scene->camera_node, evas_3d_node_position_set(0.0, 0.0, 300.0),
91 evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0, EVAS_3D_SPACE_PARENT, 0.0, 0.0, 1.0)); 275 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));
92 fprintf(stdout, "Changed position and view of camera\n"); 276 fprintf(stdout, "Changed position and view of camera\n");
93 } 277 }
94 else if (!strcmp("Right", ev->key)) 278 else if (!strcmp("2", ev->key))
95 { 279 {
96 eo_do(scene->camera_node, evas_3d_node_position_set(0.0, 0.0, 50.0); 280 eo_do(scene->camera_node, evas_3d_node_position_set(300.0, 300.0, 300.0);
97 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)); 281 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));
98 fprintf(stdout, "Changed position and view of camera\n"); 282 fprintf(stdout, "Changed position and view of camera\n");
99 } 283 }
284 else if (!strcmp("3", ev->key))
285 {
286 key = EVAS_3D_FRUSTUM_MODE_BSPHERE;
287 }
288 else if (!strcmp("4", ev->key))
289 {
290 key = EVAS_3D_FRUSTUM_MODE_AABB;
291 }
292 else if (!strcmp("5", ev->key))
293 {
294 key = EVAS_3D_FRUSTUM_MODE_CENTRAL_POINT;
295 }
100 else 296 else
101 fprintf(stdout, "Press Right/Left/Up/Bottom keys\n"); 297 {
102} 298 _show_help();
103 299 }
104static Eina_Bool
105_animate_scene_model(void *data)
106{
107 static int frame = 0;
108 Scene_Data *scene = (Scene_Data *)data;
109
110 eo_do(scene->mesh_node_model,
111 evas_3d_node_mesh_frame_set(scene->mesh_model, frame),
112 evas_3d_node_orientation_angle_axis_set(90, 1.0, 0.0, 0.0));
113 frame += 20;
114
115 if (frame > 256 * 18) frame = 0;
116 300
117 return ECORE_CALLBACK_RENEW; 301 _redraw_bounding_object(data);
118} 302}
119 303
120static void 304static void
@@ -125,8 +309,10 @@ _camera_setup(Scene_Data *data)
125 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_CAMERA)); 309 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_CAMERA));
126 eo_do(data->camera_node, 310 eo_do(data->camera_node,
127 evas_3d_node_camera_set(data->camera), 311 evas_3d_node_camera_set(data->camera),
128 evas_3d_node_position_set(0.0, 0.0, 50.0); 312 evas_3d_node_position_set(0.0, 0.0, 300.0);
129 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)); 313 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));
314
315 eo_do(data->camera, evas_3d_camera_projection_frustum_set(fleft, fright, fbottom, fup, fnear, ffar));
130 316
131 eo_do(data->root_node, evas_3d_node_member_add(data->camera_node)); 317 eo_do(data->root_node, evas_3d_node_member_add(data->camera_node));
132} 318}
@@ -145,12 +331,178 @@ _light_setup(Scene_Data *data)
145 eo_do(data->light_node, 331 eo_do(data->light_node,
146 evas_3d_node_light_set(data->light), 332 evas_3d_node_light_set(data->light),
147 evas_3d_node_position_set(0.0, 0.0, 0.0), 333 evas_3d_node_position_set(0.0, 0.0, 0.0),
148 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)); 334 evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0, EVAS_3D_SPACE_PARENT, 0.0, 0.0, 1.0));
149 335
150 eo_do(data->root_node, evas_3d_node_member_add(data->light_node)); 336 eo_do(data->root_node, evas_3d_node_member_add(data->light_node));
151} 337}
152 338
153static void 339static void
340_mesh_aabb(Eo *mesh_b, Eo *mesh_node)
341{
342 Evas_Real x0, y0, z0, x1, y1, z1;
343
344 eo_do(mesh_node, evas_3d_node_bounding_box_get(&x0, &y0, &z0, &x1, &y1, &z1));
345
346 float vertices[] =
347 {
348 x0, y0, z1,
349 x0, y1, z1,
350 x1, y1, z1,
351 x1, y0, z1,
352
353 x0, y0, z0,
354 x1, y0, z0,
355 x0, y1, z0,
356 x1, y1, z0,
357
358 x0, y0, z0,
359 x0, y1, z0,
360 x0, y0, z1,
361 x0, y1, z1,
362
363 x1, y0, z0,
364 x1, y1, z0,
365 x1, y1, z1,
366 x1, y0, z1,
367
368 x0, y1, z0,
369 x1, y1, z0,
370 x0, y1, z1,
371 x1, y1, z1,
372
373 x0, y0, z0,
374 x1, y0, z0,
375 x1, y0, z1,
376 x0, y0, z1
377 };
378
379 unsigned short indices[] =
380 {
381 0, 1, 2, 3, 1, 2, 0, 3,
382 4, 5, 5, 7, 7, 6, 6, 4,
383 8, 9, 9, 11, 11, 10, 10, 8,
384 12, 13, 13, 14, 14, 15, 15, 12,
385 16, 17, 17, 19, 19, 18, 18, 16,
386 20, 21, 21, 22, 22, 23, 23, 20
387 };
388
389 float *cube_vertices = (float *) malloc(1 * sizeof(vertices));
390 unsigned short *cube_indices = (unsigned short *) malloc(1 * sizeof(indices));
391 memcpy(cube_vertices, vertices, sizeof(vertices));
392 memcpy(cube_indices, indices, sizeof(indices));
393
394 eo_do(mesh_b,
395 evas_3d_mesh_vertex_count_set(24));
396
397 eo_do(mesh_b,
398 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_POSITION,
399 3 * sizeof(float),
400 &cube_vertices[ 0]),
401 evas_3d_mesh_index_data_copy_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
402 48, &cube_indices[0]));
403 free(cube_vertices);
404 free(cube_indices);
405}
406
407static void
408_set_ball(Eo *mesh, double r, double x, double y, double z, int p, Evas_3D_Material *material)
409{
410 int vcount, icount, vccount, i, j;
411 double dtheta, dfi, sinth, costh, fi, theta, sinfi, cosfi;
412 unsigned short *indices, *index;
413
414 icount = p * p * 6;
415 vccount = p + 1;
416 vcount = vccount * vccount;
417
418 dtheta = M_PI / p;
419 dfi = 2 * M_PI / p;
420
421 vec3 *vertices = malloc(sizeof(vec3) * vcount);
422 vec3 *normals = malloc(sizeof(vec3) * vcount);
423
424 for (j = 0; j < vccount; j++)
425 {
426 theta = j * dtheta;
427 sinth = sin(theta);
428 costh = cos(theta);
429 for (i = 0; i < vccount; i++)
430 {
431 fi = i * dfi;
432 sinfi = sin(fi);
433 cosfi = cos(fi);
434 vertices[i + j * vccount].x = r * sinth * cosfi + x;
435 vertices[i + j * vccount].y = r * sinth * sinfi + y;
436 vertices[i + j * vccount].z = r * costh + z;
437
438 normals[i + j * vccount].x = sinth * cosfi;
439 normals[i + j * vccount].y = sinth * sinfi;
440 normals[i + j * vccount].z = costh;
441 }
442 }
443
444 indices = malloc(sizeof(short) * icount);
445 index = &indices[0];
446
447 for(j = 0; j < p; j++)
448 for(i = 0; i < p; i++)
449 {
450 *index++ = (unsigned short)(i + vccount * j);
451 *index++ = i + vccount * (j + 1);
452 *index++ = i + 1 + vccount * (j + 1);
453
454 *index++ = i + vccount * j;
455 *index++ = i + 1 + vccount * j;
456 *index++ = i + vccount * (j + 1) + 1;
457 }
458
459 eo_do(mesh,
460 evas_3d_mesh_vertex_count_set(vcount),
461 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_POSITION,
462 sizeof(vec3), &vertices[0]),
463 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_NORMAL,
464 sizeof(vec3), &normals[0]),
465 evas_3d_mesh_index_data_copy_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
466 icount , &indices[0]));
467
468 free(vertices);
469 free(normals);
470 free(indices);
471}
472
473static void
474_mesh_setup(Scene_Data *data)
475{
476 data->material = eo_add(EVAS_3D_MATERIAL_CLASS, evas);
477
478 eo_do(data->material,
479 evas_3d_material_enable_set(EVAS_3D_MATERIAL_AMBIENT, EINA_TRUE),
480 evas_3d_material_enable_set(EVAS_3D_MATERIAL_DIFFUSE, EINA_TRUE),
481 evas_3d_material_enable_set(EVAS_3D_MATERIAL_SPECULAR, EINA_TRUE),
482
483 evas_3d_material_color_set(EVAS_3D_MATERIAL_AMBIENT, 0.2, 0.2, 0.2, 1.0),
484 evas_3d_material_color_set(EVAS_3D_MATERIAL_DIFFUSE, 0.8, 0.8, 0.8, 1.0),
485 evas_3d_material_color_set(EVAS_3D_MATERIAL_SPECULAR, 1.0, 1.0, 1.0, 1.0),
486 evas_3d_material_shininess_set(100.0));
487
488 data->mesh = eo_add(EVAS_3D_MESH_CLASS, evas);
489 eo_do(data->mesh,
490 evas_3d_mesh_frame_add(0);
491 evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_LINES);
492 evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_DIFFUSE),
493 evas_3d_mesh_frame_material_set(0, data->material));
494
495
496 data->mesh_node =
497 eo_add(EVAS_3D_NODE_CLASS, evas,
498 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_MESH),
499 evas_3d_node_position_set(obj_x, obj_y, obj_z),
500 evas_3d_node_scale_set(obj_sc_x, obj_sc_y, obj_sc_z));
501 eo_do(data->root_node, evas_3d_node_member_add(data->mesh_node));
502 eo_do(data->mesh_node, evas_3d_node_mesh_add(data->mesh));
503}
504
505static void
154_mesh_setup_model(Scene_Data *data) 506_mesh_setup_model(Scene_Data *data)
155{ 507{
156 data->mesh_model = eo_add(EVAS_3D_MESH_CLASS, evas); 508 data->mesh_model = eo_add(EVAS_3D_MESH_CLASS, evas);
@@ -187,20 +539,22 @@ _scene_setup(Scene_Data *data)
187 539
188 data->root_node = eo_add(EVAS_3D_NODE_CLASS, evas, 540 data->root_node = eo_add(EVAS_3D_NODE_CLASS, evas,
189 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_NODE)); 541 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_NODE));
190 _camera_setup(data); 542
191 _light_setup(data); 543 _light_setup(data);
192 _mesh_setup_model(data); 544 _mesh_setup_model(data);
545 _camera_setup(data);
193 546
194 data->mesh_node_model = eo_add(EVAS_3D_NODE_CLASS, evas, 547 data->mesh_node_model = eo_add(EVAS_3D_NODE_CLASS, evas,
195 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_MESH)); 548 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_MESH));
196 eo_do(data->mesh_node_model, 549 eo_do(data->mesh_node_model,
197 evas_3d_node_position_set(0, 0, 0); 550 evas_3d_node_position_set(obj_x, obj_y, obj_z),
198 evas_3d_node_scale_set(0.3, 0.3, 0.3), 551 evas_3d_node_scale_set(obj_sc_x, obj_sc_y, obj_sc_z));
199 evas_3d_node_orientation_angle_axis_set(90, 1.0, 1.0, 0.0));
200 eo_do(data->root_node, evas_3d_node_member_add(data->mesh_node_model)); 552 eo_do(data->root_node, evas_3d_node_member_add(data->mesh_node_model));
201 eo_do(data->mesh_node_model, evas_3d_node_mesh_add(data->mesh_model)); 553 eo_do(data->mesh_node_model, evas_3d_node_mesh_add(data->mesh_model));
202 554
203 eo_do(data->scene, 555 _mesh_setup(data);
556
557 eo_do(data->scene,
204 evas_3d_scene_size_set( WIDTH, HEIGHT), 558 evas_3d_scene_size_set( WIDTH, HEIGHT),
205 evas_3d_scene_background_color_set(0.5, 0.5, 0.5, 0.0), 559 evas_3d_scene_background_color_set(0.5, 0.5, 0.5, 0.0),
206 evas_3d_scene_root_node_set(data->root_node); 560 evas_3d_scene_root_node_set(data->root_node);
@@ -211,14 +565,12 @@ int
211main(void) 565main(void)
212{ 566{
213 Scene_Data data; 567 Scene_Data data;
214 568 Ecore_Evas *ecore_evas = NULL;
215 Ecore_Evas *ecore_evas;
216 569
217 if (!ecore_evas_init()) return 0; 570 if (!ecore_evas_init()) return 0;
218 571
219 setenv("ECORE_EVAS_ENGINE", "opengl_x11", 1); 572 setenv("ECORE_EVAS_ENGINE", "opengl_x11", 1);
220 573 ecore_evas = ecore_evas_new("opengl_x11", 0, 0, WIDTH, HEIGHT, NULL);
221 ecore_evas = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL);
222 574
223 if (!ecore_evas) return 0; 575 if (!ecore_evas) return 0;
224 576
@@ -231,6 +583,7 @@ main(void)
231 _scene_setup(&data); 583 _scene_setup(&data);
232 584
233 background = evas_object_rectangle_add(evas); 585 background = evas_object_rectangle_add(evas);
586
234 evas_object_color_set(background, 0, 0, 0, 255); 587 evas_object_color_set(background, 0, 0, 0, 255);
235 evas_object_move(background, 0, 0); 588 evas_object_move(background, 0, 0);
236 evas_object_resize(background, WIDTH, HEIGHT); 589 evas_object_resize(background, WIDTH, HEIGHT);
@@ -244,7 +597,9 @@ main(void)
244 evas_object_focus_set(image, EINA_TRUE); 597 evas_object_focus_set(image, EINA_TRUE);
245 eo_do(image, evas_obj_image_scene_set(data.scene)); 598 eo_do(image, evas_obj_image_scene_set(data.scene));
246 599
247 ecore_timer_add(0.1, _animate_scene_model, &data); 600 eo_do(data.mesh_node_model, evas_3d_object_update());
601 _redraw_bounding_object(&data);
602 _show_help();
248 603
249 evas_object_event_callback_add(image, EVAS_CALLBACK_KEY_DOWN, _on_key_down, &data); 604 evas_object_event_callback_add(image, EVAS_CALLBACK_KEY_DOWN, _on_key_down, &data);
250 ecore_main_loop_begin(); 605 ecore_main_loop_begin();