summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorse.osadchy <se.osadchy@samsung.com>2014-06-02 22:34:25 +0900
committerChunEon Park <hermet@hermet.pe.kr>2014-06-02 22:34:25 +0900
commit1e2f983098b1599ff60ddd82ff91b6b64d5295dc (patch)
tree34e3121e112d5f04e06da42cac600f0f86ed1c7f
parent132a4d38735441c983be92d8ad4b0fe339557e72 (diff)
[Examples] Add new example for evas-3d and widget.
Summary: This example shows the mechanism of scene object adding to the widget button. Reviewers: Hermet, cedric, raster Differential Revision: https://phab.enlightenment.org/D914
-rw-r--r--src/examples/3d_scene_on_button_example.c308
-rw-r--r--src/examples/Makefile.am2
2 files changed, 310 insertions, 0 deletions
diff --git a/src/examples/3d_scene_on_button_example.c b/src/examples/3d_scene_on_button_example.c
new file mode 100644
index 000000000..5c69638e2
--- /dev/null
+++ b/src/examples/3d_scene_on_button_example.c
@@ -0,0 +1,308 @@
1/*
2* This example shows the mechanism of scene object adding (3D cube) to the widget button and
3* illustrates the work of callback of event from mouse.
4*
5* Compile with:
6* gcc -o 3d_scene_on_button_example 3d_scene_on_button example.c -g `pkg-config --libs --cflags evas ecore eo elementary`
7*/
8
9#define EFL_EO_API_SUPPORT
10#define EFL_BETA_API_SUPPORT
11
12#define WIDTH 500
13#define HEIGHT 500
14#define d_w 100
15#define d_h 100
16
17
18#include <Eo.h>
19#include <Evas.h>
20#include <Ecore.h>
21#include <Elementary.h>
22#include <stdio.h>
23
24typedef struct _Scene_Data
25{
26 Eo *scene;
27 Eo *root_node;
28 Eo *camera_node;
29 Eo *light_node;
30 Eo *mesh_node;
31
32 Eo *camera;
33 Eo *light;
34 Eo *mesh;
35 Eo *material;
36} Scene_Data;
37
38Evas_Object *win = NULL;
39Evas_Object *btn = NULL;
40float d_angle = 0.5;
41static Evas *evas = NULL;
42static Eo *image = NULL;
43
44static const float cube_vertices[] =
45{
46 /* Front */
47 -1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0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0, 0.0, 1.0, 1.0, 0.0, 1.0,
72 1.0, 1.0, -1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0,
73 -1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0,
74 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0,
75
76 /* Bottom */
77 1.0, -1.0, -1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0,
78 -1.0, -1.0, -1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0,
79 1.0, -1.0, 1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0,
80 -1.0, -1.0, 1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0,
81};
82
83static const unsigned short cube_indices[] =
84{
85 /* Front */
86 0, 1, 2, 2, 1, 3,
87
88 /* Back */
89 4, 5, 6, 6, 5, 7,
90
91 /* Left */
92 8, 9, 10, 10, 9, 11,
93
94 /* Right */
95 12, 13, 14, 14, 13, 15,
96
97 /* Top */
98 16, 17, 18, 18, 17, 19,
99
100 /* Bottom */
101 20, 21, 22, 22, 21, 23
102};
103
104static Eina_Bool
105_animate_scene(void *data)
106{
107 static float angle = 0.0f;
108 Scene_Data *scene = (Scene_Data *)data;
109
110 angle += d_angle;
111
112 eo_do(scene->mesh_node,
113 evas_3d_node_orientation_angle_axis_set(angle, 1.0, 1.0, 1.0));
114
115 /* Rotate */
116 if (angle > 360.0) angle -= 360.0f;
117
118 return EINA_TRUE;
119}
120
121static void
122_camera_setup(Scene_Data *data)
123{
124 data->camera = eo_add(EVAS_3D_CAMERA_CLASS, evas);
125
126 eo_do(data->camera,
127 evas_3d_camera_projection_perspective_set(60.0, 1.0, 2.0, 50.0));
128
129 data->camera_node =
130 eo_add_custom(EVAS_3D_NODE_CLASS, evas,
131 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_CAMERA));
132 eo_do(data->camera_node,
133 evas_3d_node_camera_set(data->camera),
134 evas_3d_node_position_set(0.0, 0.0, 10.0),
135 evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0,
136 EVAS_3D_SPACE_PARENT, 0.0, 1.0, 0.0));
137 eo_do(data->root_node, evas_3d_node_member_add(data->camera_node));
138}
139
140static void
141_light_setup(Scene_Data *data)
142{
143 data->light = eo_add(EVAS_3D_LIGHT_CLASS, evas);
144 eo_do(data->light,
145 evas_3d_light_ambient_set(0.2, 0.2, 0.2, 1.0),
146 evas_3d_light_diffuse_set(1.0, 1.0, 1.0, 1.0),
147 evas_3d_light_specular_set(1.0, 1.0, 1.0, 1.0));
148
149 data->light_node =
150 eo_add_custom(EVAS_3D_NODE_CLASS, evas,
151 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_LIGHT));
152 eo_do(data->light_node,
153 evas_3d_node_light_set(data->light),
154 evas_3d_node_position_set(0.0, 0.0, 10.0),
155 evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0,
156 EVAS_3D_SPACE_PARENT, 0.0, 1.0, 0.0));
157 eo_do(data->root_node, evas_3d_node_member_add(data->light_node));
158}
159static void
160_mesh_setup(Scene_Data *data)
161{
162 /* Setup material. */
163 data->material = eo_add(EVAS_3D_MATERIAL_CLASS, evas);
164
165 eo_do(data->material,
166 evas_3d_material_enable_set(EVAS_3D_MATERIAL_AMBIENT, EINA_TRUE),
167 evas_3d_material_enable_set(EVAS_3D_MATERIAL_DIFFUSE, EINA_TRUE),
168 evas_3d_material_enable_set(EVAS_3D_MATERIAL_SPECULAR, EINA_TRUE),
169
170 evas_3d_material_color_set(EVAS_3D_MATERIAL_AMBIENT, 0.2, 0.2, 0.2, 1.0),
171 evas_3d_material_color_set(EVAS_3D_MATERIAL_DIFFUSE, 0.8, 0.8, 0.8, 1.0),
172 evas_3d_material_color_set(EVAS_3D_MATERIAL_SPECULAR, 1.0, 1.0, 1.0, 1.0),
173 evas_3d_material_shininess_set(100.0));
174
175 /* Setup mesh. */
176 data->mesh = eo_add(EVAS_3D_MESH_CLASS, evas);
177 eo_do(data->mesh,
178 evas_3d_mesh_vertex_count_set(24),
179 evas_3d_mesh_frame_add(0),
180
181 evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_POSITION,
182 12 * sizeof(float), &cube_vertices[ 0]),
183 evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_NORMAL,
184 12 * sizeof(float), &cube_vertices[ 3]),
185 evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_COLOR,
186 12 * sizeof(float), &cube_vertices[ 6]),
187 evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_TEXCOORD,
188 12 * sizeof(float), &cube_vertices[10]),
189
190 evas_3d_mesh_index_data_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
191 36, &cube_indices[0]),
192 evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES),
193
194 evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_PHONG),
195
196 evas_3d_mesh_frame_material_set(0, data->material));
197
198 data->mesh_node =
199 eo_add_custom(EVAS_3D_NODE_CLASS, evas,
200 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_MESH));
201 eo_do(data->root_node, evas_3d_node_member_add(data->mesh_node));
202 eo_do(data->mesh_node, evas_3d_node_mesh_add(data->mesh));
203}
204
205static void
206_scene_setup(Scene_Data *data)
207{
208 data->scene = eo_add(EVAS_3D_SCENE_CLASS, evas);
209 eo_do(data->scene,
210 evas_3d_scene_size_set(WIDTH - d_w, HEIGHT - d_h);
211 evas_3d_scene_background_color_set(0.0, 0.0, 0.0, 0.0));
212
213 data->root_node =
214 eo_add_custom(EVAS_3D_NODE_CLASS, evas,
215 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_NODE));
216
217 _camera_setup(data);
218 _light_setup(data);
219 _mesh_setup(data);
220
221 eo_do(data->scene,
222 evas_3d_scene_root_node_set(data->root_node),
223 evas_3d_scene_camera_node_set(data->camera_node));
224}
225
226static void
227_stop_scene(void *data,
228 Evas *e EINA_UNUSED,
229 Evas_Object *eo EINA_UNUSED,
230 void *event_info)
231{
232 Evas_Event_Mouse_Down *ev = event_info;
233 Scene_Data *d = (Scene_Data *)data;
234
235 eo_do(d->mesh_node,
236 evas_3d_node_scale_set(0.97, 0.97, 0.97));
237
238 if (ev->button == 1)
239 {
240 if (eo_do(d->scene,
241 evas_3d_scene_exist((ev->canvas.x - d_w/2), (ev->canvas.y - d_h/2), d->mesh_node)))
242 d_angle = 0;
243 }
244}
245
246static void
247_play_scene(void *data,
248 Evas *e EINA_UNUSED,
249 Evas_Object *eo EINA_UNUSED,
250 void *event_info EINA_UNUSED)
251{
252 Scene_Data *d = (Scene_Data *)data;
253
254 d_angle = 0.5;
255
256 eo_do(d->mesh_node,
257 evas_3d_node_scale_set(1.0, 1.0, 1.0));
258}
259
260int
261main(void)
262{
263 setenv("ELM_ENGINE", "opengl_x11", 1);
264
265 Scene_Data data;
266
267 if (!elm_init(0,0)) return 0;
268
269 elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
270
271 win = elm_win_util_standard_add("__WIN__", "3d object on the button");
272 elm_win_autodel_set(win, EINA_TRUE);
273 evas_object_resize(win, WIDTH, HEIGHT);
274 evas_object_show(win);
275
276 evas = evas_object_evas_get(win);
277
278 if ((!win) || (!evas)) return 0;
279
280 _scene_setup(&data);
281
282 image = evas_object_image_filled_add(evas);
283 eo_do(image,
284 evas_obj_visibility_set(EINA_TRUE));
285
286 /* Set the image object as render target for 3D scene. */
287 eo_do(image, evas_obj_image_scene_set(data.scene));
288
289 /* Setup scene to the widget button. */
290 btn = elm_button_add(win);
291 elm_object_part_content_set(btn, "elm.swallow.content", image);
292 evas_object_resize(btn, WIDTH - d_w, HEIGHT - d_h);
293 evas_object_move(btn, d_w/2, d_h/2);
294 evas_object_show(btn);
295
296 evas_object_event_callback_add(btn, EVAS_CALLBACK_MOUSE_DOWN, _stop_scene, &data);
297 evas_object_event_callback_add(btn, EVAS_CALLBACK_MOUSE_UP, _play_scene, &data);
298
299 /* Add animation timer callback. */
300 ecore_timer_add(0.016, _animate_scene, &data);
301
302 /* Enter main loop. */
303 elm_run();
304
305 elm_shutdown();
306
307 return 0;
308}
diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am
index fc35c7515..8998cf0f9 100644
--- a/src/examples/Makefile.am
+++ b/src/examples/Makefile.am
@@ -139,6 +139,7 @@ transit_example_04.c \
139web_example_01.c \ 139web_example_01.c \
140web_example_02.c \ 140web_example_02.c \
141win_example.c \ 141win_example.c \
1423d_scene_on_button_example.c \
142track_example_01.c 143track_example_01.c
143 144
144.edc.edj: 145.edc.edj:
@@ -278,6 +279,7 @@ transit_example_04 \
278web_example_01 \ 279web_example_01 \
279web_example_02 \ 280web_example_02 \
280win_example \ 281win_example \
2823d_scene_on_button_example \
281track_example_01 283track_example_01
282 284
283if ELEMENTARY_WINDOWS_BUILD 285if ELEMENTARY_WINDOWS_BUILD