summaryrefslogtreecommitdiff
path: root/src/examples/evas/evas-3d-cube2.c
diff options
context:
space:
mode:
authorTaekyun Kim <tkq.kim@samsung.com>2013-12-27 18:45:41 +0900
committerChunEon Park <hermet@hermet.pe.kr>2014-04-25 16:18:50 +0900
commit6daa0537df6d98a67a4a7185ca6098417612e038 (patch)
tree4621c7a859af7a245b78f84ef5581ed4bd4c5fde /src/examples/evas/evas-3d-cube2.c
parentc15c1620167ef7d14e4bee8f19681e4b9a453fee (diff)
Evas: 3D: Example: Rotating Cube 2
Demonstrating key frame material animation and normal mapping.
Diffstat (limited to 'src/examples/evas/evas-3d-cube2.c')
-rw-r--r--src/examples/evas/evas-3d-cube2.c321
1 files changed, 321 insertions, 0 deletions
diff --git a/src/examples/evas/evas-3d-cube2.c b/src/examples/evas/evas-3d-cube2.c
new file mode 100644
index 0000000..fe4d6a8
--- /dev/null
+++ b/src/examples/evas/evas-3d-cube2.c
@@ -0,0 +1,321 @@
1#include <Ecore.h>
2#include <Ecore_Evas.h>
3#include <stdio.h>
4#include <math.h>
5#include <Evas_3D.h>
6
7#define WIDTH 1024
8#define HEIGHT 1024
9
10typedef struct _Scene_Data
11{
12 Evas_3D_Scene *scene;
13 Evas_3D_Node *root_node;
14 Evas_3D_Node *camera_node;
15 Evas_3D_Node *light_node;
16 Evas_3D_Node *mesh_node;
17
18 Evas_3D_Camera *camera;
19 Evas_3D_Light *light;
20 Evas_3D_Mesh *mesh;
21 Evas_3D_Material *material0;
22 Evas_3D_Material *material1;
23
24 Evas_3D_Texture *texture0;
25 Evas_3D_Texture *texture1;
26 Evas_3D_Texture *texture_normal;
27} Scene_Data;
28
29Ecore_Evas *ecore_evas = NULL;
30Evas *evas = NULL;
31Evas_Object *background = NULL;
32Evas_Object *image = NULL;
33
34static const float cube_vertices[] =
35{
36 /* Front */
37 -1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0,
38 1.0, 1.0, 1.0, 0.0, 0.0,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, -1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0,
63 -1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0,
64 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0,
65
66 /* Bottom */
67 1.0, -1.0, -1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0,
68 -1.0, -1.0, -1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0,
69 1.0, -1.0, 1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0,
70 -1.0, -1.0, 1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0,
71};
72
73static const unsigned short cube_indices[] =
74{
75 /* Front */
76 0, 1, 2, 2, 1, 3,
77
78 /* Back */
79 4, 5, 6, 6, 5, 7,
80
81 /* Left */
82 8, 9, 10, 10, 9, 11,
83
84 /* Right */
85 12, 13, 14, 14, 13, 15,
86
87 /* Top */
88 16, 17, 18, 18, 17, 19,
89
90 /* Bottom */
91 20, 21, 22, 22, 21, 23
92};
93
94static const unsigned int pixels0[] =
95{
96 0xff0000ff, 0xff0000ff, 0xffff0000, 0xffff0000,
97 0xff0000ff, 0xff0000ff, 0xffff0000, 0xffff0000,
98 0xff00ff00, 0xff00ff00, 0xff000000, 0xff000000,
99 0xff00ff00, 0xff00ff00, 0xff000000, 0xff000000,
100};
101
102static const unsigned int pixels1[] =
103{
104 0xffff0000, 0xffff0000, 0xff00ff00, 0xff00ff00,
105 0xffff0000, 0xffff0000, 0xff00ff00, 0xff00ff00,
106 0xff0000ff, 0xff0000ff, 0xffffffff, 0xffffffff,
107 0xff0000ff, 0xff0000ff, 0xffffffff, 0xffffffff,
108};
109
110static void
111_on_delete(Ecore_Evas *ee EINA_UNUSED)
112{
113 ecore_main_loop_quit();
114}
115
116static void
117_on_canvas_resize(Ecore_Evas *ee)
118{
119 int w, h;
120
121 ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
122
123 evas_object_resize(background, w, h);
124 evas_object_resize(image, w, h);
125 evas_object_move(image, 0, 0);
126}
127
128static Eina_Bool
129_animate_scene(void *data)
130{
131 static float angle = 0.0f;
132 static int frame = 0;
133 static int inc = 1;
134 Scene_Data *scene = (Scene_Data *)data;
135
136 angle += 0.2;
137
138 evas_3d_node_orientation_angle_axis_set(scene->mesh_node, angle, 1.0, 1.0, 1.0);
139
140 /* Rotate */
141 if (angle > 360.0)
142 angle -= 360.0f;
143
144 frame += inc;
145
146 if (frame >= 20)
147 inc = -1;
148 else if (frame <= 0)
149 inc = 1;
150
151 evas_3d_node_mesh_frame_set(scene->mesh_node, scene->mesh, frame);
152
153 return EINA_TRUE;
154}
155
156static void
157_camera_setup(Scene_Data *data)
158{
159 data->camera = evas_3d_camera_add(evas);
160 evas_3d_camera_projection_perspective_set(data->camera, 60.0, 1.0, 2.0, 50.0);
161
162 data->camera_node = evas_3d_node_add(evas, EVAS_3D_NODE_TYPE_CAMERA);
163 evas_3d_node_camera_set(data->camera_node, data->camera);
164 evas_3d_node_member_add(data->root_node, data->camera_node);
165 evas_3d_node_position_set(data->camera_node, 0.0, 0.0, 10.0);
166 evas_3d_node_look_at_set(data->camera_node, EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0,
167 EVAS_3D_SPACE_PARENT, 0.0, 1.0, 0.0);
168}
169
170static void
171_light_setup(Scene_Data *data)
172{
173 data->light = evas_3d_light_add(evas);
174 evas_3d_light_ambient_set(data->light, 0.2, 0.2, 0.2, 1.0);
175 evas_3d_light_diffuse_set(data->light, 1.0, 1.0, 1.0, 1.0);
176 evas_3d_light_specular_set(data->light, 1.0, 1.0, 1.0, 1.0);
177
178 data->light_node = evas_3d_node_add(evas, EVAS_3D_NODE_TYPE_LIGHT);
179 evas_3d_node_light_set(data->light_node, data->light);
180 evas_3d_node_member_add(data->root_node, data->light_node);
181 evas_3d_node_position_set(data->light_node, 0.0, 0.0, 10.0);
182 evas_3d_node_look_at_set(data->light_node, EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0,
183 EVAS_3D_SPACE_PARENT, 0.0, 1.0, 0.0);
184}
185
186static void
187_mesh_setup(Scene_Data *data)
188{
189 /* Setup material. */
190 data->material0 = evas_3d_material_add(evas);
191 data->material1 = evas_3d_material_add(evas);
192
193 evas_3d_material_enable_set(data->material0, EVAS_3D_MATERIAL_AMBIENT, EINA_TRUE);
194 evas_3d_material_enable_set(data->material0, EVAS_3D_MATERIAL_DIFFUSE, EINA_TRUE);
195 evas_3d_material_enable_set(data->material0, EVAS_3D_MATERIAL_SPECULAR, EINA_TRUE);
196 evas_3d_material_enable_set(data->material0, EVAS_3D_MATERIAL_NORMAL, EINA_TRUE);
197
198 evas_3d_material_color_set(data->material0, EVAS_3D_MATERIAL_AMBIENT, 0.2, 0.2, 0.2, 1.0);
199 evas_3d_material_color_set(data->material0, EVAS_3D_MATERIAL_DIFFUSE, 0.8, 0.8, 0.8, 1.0);
200 evas_3d_material_color_set(data->material0, EVAS_3D_MATERIAL_SPECULAR, 1.0, 1.0, 1.0, 1.0);
201 evas_3d_material_shininess_set(data->material0, 100.0);
202
203 evas_3d_material_enable_set(data->material1, EVAS_3D_MATERIAL_AMBIENT, EINA_TRUE);
204 evas_3d_material_enable_set(data->material1, EVAS_3D_MATERIAL_DIFFUSE, EINA_TRUE);
205 evas_3d_material_enable_set(data->material1, EVAS_3D_MATERIAL_SPECULAR, EINA_TRUE);
206 evas_3d_material_enable_set(data->material1, EVAS_3D_MATERIAL_NORMAL, EINA_TRUE);
207
208 evas_3d_material_color_set(data->material1, EVAS_3D_MATERIAL_AMBIENT, 0.2, 0.2, 0.2, 1.0);
209 evas_3d_material_color_set(data->material1, EVAS_3D_MATERIAL_DIFFUSE, 0.8, 0.8, 0.8, 1.0);
210 evas_3d_material_color_set(data->material1, EVAS_3D_MATERIAL_SPECULAR, 1.0, 1.0, 1.0, 1.0);
211 evas_3d_material_shininess_set(data->material1, 100.0);
212
213 data->texture0 = evas_3d_texture_add(evas);
214 data->texture1 = evas_3d_texture_add(evas);
215 data->texture_normal = evas_3d_texture_add(evas);
216
217 evas_3d_texture_data_set(data->texture0, EVAS_3D_COLOR_FORMAT_RGBA,
218 EVAS_3D_PIXEL_FORMAT_8888, 4, 4, &pixels0[0]);
219 evas_3d_texture_data_set(data->texture1, EVAS_3D_COLOR_FORMAT_RGBA,
220 EVAS_3D_PIXEL_FORMAT_8888, 4, 4, &pixels1[0]);
221 evas_3d_texture_file_set(data->texture_normal, "normal_lego.png", NULL);
222
223 evas_3d_material_texture_set(data->material0, EVAS_3D_MATERIAL_DIFFUSE, data->texture0);
224 evas_3d_material_texture_set(data->material1, EVAS_3D_MATERIAL_DIFFUSE, data->texture1);
225 evas_3d_material_texture_set(data->material1, EVAS_3D_MATERIAL_NORMAL, data->texture_normal);
226
227 /* Setup mesh. */
228 data->mesh = evas_3d_mesh_add(evas);
229 evas_3d_mesh_vertex_count_set(data->mesh, 24);
230 evas_3d_mesh_frame_add(data->mesh, 0);
231
232 evas_3d_mesh_frame_vertex_data_set(data->mesh, 0, EVAS_3D_VERTEX_POSITION,
233 12 * sizeof(float), &cube_vertices[ 0]);
234 evas_3d_mesh_frame_vertex_data_set(data->mesh, 0, EVAS_3D_VERTEX_NORMAL,
235 12 * sizeof(float), &cube_vertices[ 3]);
236 evas_3d_mesh_frame_vertex_data_set(data->mesh, 0, EVAS_3D_VERTEX_COLOR,
237 12 * sizeof(float), &cube_vertices[ 6]);
238 evas_3d_mesh_frame_vertex_data_set(data->mesh, 0, EVAS_3D_VERTEX_TEXCOORD,
239 12 * sizeof(float), &cube_vertices[10]);
240
241 evas_3d_mesh_index_data_set(data->mesh, EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
242 36, &cube_indices[0]);
243 evas_3d_mesh_vertex_assembly_set(data->mesh, EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES);
244
245 evas_3d_mesh_shade_mode_set(data->mesh, EVAS_3D_SHADE_MODE_NORMAL_MAP);
246
247 evas_3d_mesh_frame_material_set(data->mesh, 0, data->material0);
248
249 evas_3d_mesh_frame_add(data->mesh, 20);
250 evas_3d_mesh_frame_material_set(data->mesh, 20, data->material1);
251
252 data->mesh_node = evas_3d_node_add(evas, EVAS_3D_NODE_TYPE_MESH);
253 evas_3d_node_member_add(data->root_node, data->mesh_node);
254 evas_3d_node_mesh_add(data->mesh_node, data->mesh);
255}
256
257static void
258_scene_setup(Scene_Data *data)
259{
260 data->scene = evas_3d_scene_add(evas);
261 evas_3d_scene_size_set(data->scene, WIDTH, HEIGHT);
262 evas_3d_scene_background_color_set(data->scene, 0.0, 0.0, 0.0, 0.0);
263
264 data->root_node = evas_3d_node_add(evas, EVAS_3D_NODE_TYPE_NODE);
265
266 _camera_setup(data);
267 _light_setup(data);
268 _mesh_setup(data);
269
270 evas_3d_scene_root_node_set(data->scene, data->root_node);
271 evas_3d_scene_camera_node_set(data->scene, data->camera_node);
272}
273
274int
275main(void)
276{
277 Scene_Data data;
278
279 if (!ecore_evas_init())
280 return 0;
281
282 ecore_evas = ecore_evas_new(NULL, 10, 10, WIDTH, HEIGHT, NULL);
283
284 if (!ecore_evas)
285 return 0;
286
287 ecore_evas_callback_delete_request_set(ecore_evas, _on_delete);
288 ecore_evas_callback_resize_set(ecore_evas, _on_canvas_resize);
289 ecore_evas_show(ecore_evas);
290
291 evas = ecore_evas_get(ecore_evas);
292
293 _scene_setup(&data);
294
295 /* Add a background rectangle objects. */
296 background = evas_object_rectangle_add(evas);
297 evas_object_color_set(background, 0, 0, 0, 255);
298 evas_object_move(background, 0, 0);
299 evas_object_resize(background, WIDTH, HEIGHT);
300 evas_object_show(background);
301
302 /* Add an image object for 3D scene rendering. */
303 image = evas_object_image_filled_add(evas);
304 evas_object_move(image, 0, 0);
305 evas_object_resize(image, WIDTH, HEIGHT);
306 evas_object_show(image);
307
308 /* Set the image object as render target for 3D scene. */
309 evas_object_image_3d_scene_set(image, data.scene);
310
311 /* Add animation timer callback. */
312 ecore_timer_add(0.01, _animate_scene, &data);
313
314 /* Enter main loop. */
315 ecore_main_loop_begin();
316
317 ecore_evas_free(ecore_evas);
318 ecore_evas_shutdown();
319
320 return 0;
321}