summaryrefslogtreecommitdiff
path: root/src/examples/evas/evas-3d-aabb.c
diff options
context:
space:
mode:
authorOleksandr Shcherbina <o.shcherbina@samsung.com>2014-05-27 22:07:28 +0900
committerChunEon Park <hermet@hermet.pe.kr>2014-05-27 22:07:29 +0900
commite47373aee1fd516d3436220ed4f1489baa04f502 (patch)
tree1c16f659c160508e5b25325ece9d5f15cbc8f6ab /src/examples/evas/evas-3d-aabb.c
parentb2841c0db6aa4bbffafe7848b9f356be0f48eaee (diff)
[evas/evas-3d] Add OBB and AABB.
Summary: Add axis-aligned bounding box (AABB) to frame. Set and update AABB and oriented bounding box (OBB) in node. Reviewers: Hermet, cedric, raster CC: cedric Differential Revision: https://phab.enlightenment.org/D881
Diffstat (limited to 'src/examples/evas/evas-3d-aabb.c')
-rw-r--r--src/examples/evas/evas-3d-aabb.c308
1 files changed, 308 insertions, 0 deletions
diff --git a/src/examples/evas/evas-3d-aabb.c b/src/examples/evas/evas-3d-aabb.c
new file mode 100644
index 0000000..c1ced0d
--- /dev/null
+++ b/src/examples/evas/evas-3d-aabb.c
@@ -0,0 +1,308 @@
1/*
2 * This example shows how to get and draw axis-aligned bounding box.
3
4 * @see _mesh_aabb(Evas_3D_Mesh **mesh_b, const Evas_3D_Node *node);
5 * Rotate axises (keys 1-4) for model and bounding box view from another angle.
6 * Compile with "gcc -o evas-3d-aabb evas-3d-aabb.c `pkg-config --libs --cflags evas ecore ecore-evas eo`"
7 */
8
9#define EFL_EO_API_SUPPORT
10#define EFL_BETA_API_SUPPORT
11
12#include <Eo.h>
13#include <Evas.h>
14#include <Ecore.h>
15#include <Ecore_Evas.h>
16
17#define WIDTH 400
18#define HEIGHT 400
19
20Ecore_Evas *ecore_evas = NULL;
21Evas *evas = NULL;
22Eo *background = NULL;
23Eo *image = NULL;
24
25Eo *scene = NULL;
26Eo *root_node = NULL;
27Eo *camera_node = NULL;
28Eo *light_node = NULL;
29Eo *camera = NULL;
30Eo *mesh_node = NULL;
31Eo *mesh_box_node = NULL;
32Eo *mesh = NULL;
33Eo *mesh_box = NULL;
34Eo *material_box = NULL;
35Eo *material = NULL;
36Eo *texture = NULL;
37Eo *light = NULL;
38
39
40static Eina_Bool
41_mesh_aabb(Evas_3D_Mesh **mesh_b, const Evas_3D_Node *node);
42
43static Eina_Bool
44_animate_scene(void *data)
45{
46 static int frame = 0;
47
48 eo_do((Evas_3D_Node *)data, evas_3d_node_mesh_frame_set(mesh, frame));
49
50 _mesh_aabb(&mesh_box, mesh_box_node);
51
52 frame += 32;
53
54 if (frame > 256 * 50) frame = 0;
55
56 return EINA_TRUE;
57}
58
59static void
60_on_delete(Ecore_Evas *ee EINA_UNUSED)
61{
62 ecore_main_loop_quit();
63}
64
65static void
66_on_key_down(void *data, Evas *e EINA_UNUSED, Evas_Object *eo EINA_UNUSED, void *event_info)
67{
68 Evas_Event_Key_Down *ev = event_info;
69 Evas_3D_Node *node = (Evas_3D_Node *)data;
70
71 switch(atoi(ev->key))
72 {
73 case 1:
74 {
75 eo_do(node, evas_3d_node_orientation_angle_axis_set(90, 1.0, 0.0, 0.0));
76 break;
77 }
78 case 2:
79 {
80 eo_do(node, evas_3d_node_orientation_angle_axis_set(90, 0.0, 1.0, 0.0));
81 break;
82 }
83 case 3:
84 {
85 eo_do(node, evas_3d_node_orientation_angle_axis_set(90, 0.0, 0.0, 1.0));
86 break;
87 }
88 case 4:
89 {
90 eo_do(node, evas_3d_node_orientation_angle_axis_set(90, 1.0, 1.0, 0.0));
91 break;
92 }
93 }
94}
95
96static void
97_on_canvas_resize(Ecore_Evas *ee)
98{
99 int w, h;
100
101 ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
102 eo_do(background, evas_obj_size_set(w, h));
103 eo_do(image, evas_obj_size_set(w, h));
104}
105
106static Eina_Bool
107_mesh_aabb(Evas_3D_Mesh **mesh_b, const Evas_3D_Node *node)
108{
109 Evas_Real x0, y0, z0, x1, y1, z1;
110
111 eo_do(mesh_node, evas_3d_node_bounding_box_get(&x0, &y0, &z0, &x1, &y1, &z1));
112
113 float vertices[] =
114 {
115 x0, y0, z1,
116 x0, y1, z1,
117 x1, y1, z1,
118 x1, y0, z1,
119
120 x0, y0, z0,
121 x1, y0, z0,
122 x0, y1, z0,
123 x1, y1, z0,
124
125 x0, y0, z0,
126 x0, y1, z0,
127 x0, y0, z1,
128 x0, y1, z1,
129
130 x1, y0, z0,
131 x1, y1, z0,
132 x1, y1, z1,
133 x1, y0, z1,
134
135 x0, y1, z0,
136 x1, y1, z0,
137 x0, y1, z1,
138 x1, y1, z1,
139
140 x0, y0, z0,
141 x1, y0, z0,
142 x1, y0, z1,
143 x0, y0, z1
144 };
145
146 unsigned short indices[] =
147 {
148 0, 1, 2, 3, 1, 2, 0, 3,
149 4, 5, 5, 7, 7, 6, 6, 4,
150 8, 9, 9, 11, 11, 10, 10, 8,
151 12, 13, 13, 14, 14, 15, 15, 12,
152 16, 17, 17, 19, 19, 18, 18, 16,
153 20, 21, 21, 22, 22, 23, 23, 20
154 };
155
156 float *cube_vertices = (float *) malloc(1 * sizeof(vertices));
157 unsigned short *cube_indices = (unsigned short *) malloc(1 * sizeof(indices));
158 memcpy(cube_vertices, vertices, sizeof(vertices));
159 memcpy(cube_indices, indices, sizeof(indices));
160
161 eo_do(*mesh_b,
162 evas_3d_mesh_vertex_count_set(24),
163 evas_3d_mesh_frame_add( 0),
164 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_POSITION, 3 * sizeof(float), &cube_vertices[ 0]),
165 evas_3d_mesh_index_data_copy_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT, 48, &cube_indices[0]),
166 evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_LINES),
167 evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_DIFFUSE),
168 evas_3d_mesh_frame_material_set(0, material_box));
169
170 free(cube_vertices);
171 free(cube_indices);
172
173 return EINA_TRUE;
174}
175
176int
177main(void)
178{
179 if (!ecore_evas_init()) return 0;
180
181 ecore_evas = ecore_evas_new("opengl_x11", 10, 10, WIDTH, HEIGHT, NULL);
182
183 if (!ecore_evas) return 0;
184
185 ecore_evas_callback_delete_request_set(ecore_evas, _on_delete);
186 ecore_evas_callback_resize_set(ecore_evas, _on_canvas_resize);
187 ecore_evas_show(ecore_evas);
188
189 evas = ecore_evas_get(ecore_evas);
190
191 scene = eo_add(EVAS_3D_SCENE_CLASS, evas);
192
193 root_node = eo_add_custom(EVAS_3D_NODE_CLASS, evas,
194 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_NODE));
195
196 camera = eo_add(EVAS_3D_CAMERA_CLASS, evas);
197 eo_do(camera,
198 evas_3d_camera_projection_perspective_set(60.0, 1.0, 1.0, 500.0));
199
200 camera_node =
201 eo_add_custom(EVAS_3D_NODE_CLASS, evas,
202 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_CAMERA));
203 eo_do(camera_node,
204 evas_3d_node_camera_set(camera));
205 eo_do(root_node,
206 evas_3d_node_member_add(camera_node));
207 eo_do(camera_node,
208 evas_3d_node_position_set(100.0, 50.0, 20.0),
209 evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 20.0,
210 EVAS_3D_SPACE_PARENT, 0.0, 0.0, 1.0));
211 light = eo_add(EVAS_3D_LIGHT_CLASS, evas);
212 eo_do(light,
213 evas_3d_light_ambient_set(1.0, 1.0, 1.0, 1.0),
214 evas_3d_light_diffuse_set(1.0, 1.0, 1.0, 1.0),
215 evas_3d_light_specular_set(1.0, 1.0, 1.0, 1.0),
216 evas_3d_light_directional_set(EINA_TRUE));
217
218 light_node =
219 eo_add_custom(EVAS_3D_NODE_CLASS, evas,
220 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_LIGHT));
221 eo_do(light_node,
222 evas_3d_node_light_set(light),
223 evas_3d_node_position_set(1000.0, 0.0, 1000.0),
224 evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0,
225 EVAS_3D_SPACE_PARENT, 0.0, 1.0, 0.0));
226 eo_do(root_node,
227 evas_3d_node_member_add(light_node));
228
229 mesh = eo_add(EVAS_3D_MESH_CLASS, evas);
230 material = eo_add(EVAS_3D_MATERIAL_CLASS, evas);
231
232 eo_do(mesh,
233 evas_3d_mesh_file_set(EVAS_3D_MESH_FILE_TYPE_MD2, "sonic.md2", NULL),
234 evas_3d_mesh_frame_material_set(0, material),
235 evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_PHONG));
236
237 texture = eo_add(EVAS_3D_TEXTURE_CLASS, evas);
238 eo_do(texture,
239 evas_3d_texture_file_set("sonic.png", NULL),
240 evas_3d_texture_filter_set(EVAS_3D_TEXTURE_FILTER_NEAREST,
241 EVAS_3D_TEXTURE_FILTER_NEAREST),
242 evas_3d_texture_wrap_set(EVAS_3D_WRAP_MODE_REPEAT,
243 EVAS_3D_WRAP_MODE_REPEAT));
244 eo_do(material,
245 evas_3d_material_texture_set(EVAS_3D_MATERIAL_DIFFUSE, texture),
246 evas_3d_material_enable_set(EVAS_3D_MATERIAL_AMBIENT, EINA_TRUE),
247 evas_3d_material_enable_set(EVAS_3D_MATERIAL_DIFFUSE, EINA_TRUE),
248 evas_3d_material_enable_set(EVAS_3D_MATERIAL_SPECULAR, EINA_TRUE),
249 evas_3d_material_enable_set(EVAS_3D_MATERIAL_NORMAL, EINA_TRUE),
250 evas_3d_material_color_set(EVAS_3D_MATERIAL_AMBIENT,
251 0.01, 0.01, 0.01, 1.0),
252 evas_3d_material_color_set(EVAS_3D_MATERIAL_DIFFUSE,
253 1.0, 1.0, 1.0, 1.0),
254 evas_3d_material_color_set(EVAS_3D_MATERIAL_SPECULAR,
255 1.0, 1.0, 1.0, 1.0),
256 evas_3d_material_shininess_set(50.0));
257
258 mesh_node = eo_add_custom(EVAS_3D_NODE_CLASS, evas,
259 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_MESH));
260 mesh_box_node = eo_add_custom(EVAS_3D_NODE_CLASS, evas,
261 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_MESH));
262
263 material_box = eo_add(EVAS_3D_MATERIAL_CLASS, evas);
264 eo_do(material_box, evas_3d_material_enable_set(EVAS_3D_MATERIAL_DIFFUSE, EINA_TRUE));
265
266 mesh_box = eo_add(EVAS_3D_MESH_CLASS, evas);
267 _mesh_aabb(&mesh_box, mesh_box_node);
268
269 eo_do(root_node,
270 evas_3d_node_member_add(mesh_box_node));
271 eo_do(mesh_box_node,
272 evas_3d_node_mesh_add(mesh_box));
273
274 eo_do(root_node,
275 evas_3d_node_member_add(mesh_node));
276 eo_do(mesh_node,
277 evas_3d_node_mesh_add(mesh));
278
279 eo_do(scene,
280 evas_3d_scene_root_node_set(root_node),
281 evas_3d_scene_camera_node_set(camera_node),
282 evas_3d_scene_size_set(WIDTH, HEIGHT));
283
284 background = eo_add(EVAS_OBJ_RECTANGLE_CLASS, evas);
285 eo_unref(background);
286 eo_do(background,
287 evas_obj_color_set(0, 0, 0, 255),
288 evas_obj_size_set(WIDTH, HEIGHT),
289 evas_obj_visibility_set(EINA_TRUE));
290
291 image = evas_object_image_filled_add(evas);
292 eo_do(image,
293 evas_obj_size_set(WIDTH, HEIGHT),
294 evas_obj_visibility_set(EINA_TRUE));
295 evas_object_focus_set(image, EINA_TRUE);
296 eo_do(image, evas_obj_image_scene_set(scene));
297
298 evas_object_event_callback_add(image, EVAS_CALLBACK_KEY_DOWN, _on_key_down, root_node);
299
300 ecore_timer_add(0.01, _animate_scene, mesh_node);
301
302 ecore_main_loop_begin();
303
304 ecore_evas_free(ecore_evas);
305 ecore_evas_shutdown();
306
307 return 0;
308}