summaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorDmytro Dadyka <d.dadyka@samsung.com>2015-02-23 14:25:49 +0100
committerCedric BAIL <cedric@osg.samsung.com>2015-02-23 15:46:25 +0100
commit1b142d09d951a53e44ce2f1a98b5cc85616756f9 (patch)
tree1e27cfe23ed855a36d026a9c7ddd705127001c80 /src/examples
parent49f350d046535e0d0e65d7955c1ce670d17bdaaf (diff)
evas: Evas_3D - add parallax occlusion shading.
Reviewers: Hermet, cedric Reviewed By: cedric Subscribers: cedric Differential Revision: https://phab.enlightenment.org/D2018
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/evas/evas-3d-parallax-occlusion.c276
-rw-r--r--src/examples/evas/four_NM_height.tgabin0 -> 262188 bytes
-rw-r--r--src/examples/evas/rocks.jpgbin0 -> 128423 bytes
-rw-r--r--src/examples/evas/rocks_NM_height.tgabin0 -> 262188 bytes
-rw-r--r--src/examples/evas/wood.jpgbin0 -> 94417 bytes
5 files changed, 276 insertions, 0 deletions
diff --git a/src/examples/evas/evas-3d-parallax-occlusion.c b/src/examples/evas/evas-3d-parallax-occlusion.c
new file mode 100644
index 0000000..ca25b1f
--- /dev/null
+++ b/src/examples/evas/evas-3d-parallax-occlusion.c
@@ -0,0 +1,276 @@
1/**
2 * Example illustrating usage of parallax occlusion shading and texture animation.
3 *
4 * Press "n" for use normal mapping shading. Pres "p" for use parallax occlusion mapping shading.
5 *
6 * @verbatim
7 * gcc -o evas-3d-parallax-occlusion evas-3d-parallax-occlusion.c evas-3d-primitives.c `pkg-config --libs --cflags evas ecore ecore-evas eo`-lm
8 * @endverbatim
9 */
10
11#define EFL_EO_API_SUPPORT
12#define EFL_BETA_API_SUPPORT
13
14#include <Eo.h>
15#include <Evas.h>
16#include <Ecore.h>
17#include <Ecore_Evas.h>
18#include "evas-3d-primitives.h"
19
20#define WIDTH 400
21#define HEIGHT 400
22
23typedef struct _Scene_Data
24{
25 Eo *scene;
26 Eo *root_node;
27 Eo *camera_node;
28 Eo *light_node;
29 Eo *mesh_node;
30
31 Eo *camera;
32 Eo *light;
33 Eo *mesh;
34 Eo *material_rocks;
35 Eo *material_wood;
36 Eo *texture_rocks;
37 Eo *texture_rocks_n;
38 Eo *texture_wood;
39 Eo *texture_four_n;
40} Scene_Data;
41
42static Ecore_Evas *ecore_evas = NULL;
43static Evas *evas = NULL;
44static Eo *background = NULL;
45static Eo *image = NULL;
46
47static void
48_on_key_down(void *data, Evas *e EINA_UNUSED, Evas_Object *eo EINA_UNUSED, void *event_info)
49{
50 Scene_Data *scene = (Scene_Data *)data;
51 Evas_Event_Key_Down *ev = event_info;
52
53 if (!strcmp("n", ev->key))
54 eo_do(scene->mesh, evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_NORMAL_MAP));
55
56 if (!strcmp("p", ev->key))
57 eo_do(scene->mesh, evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_PARALLAX_OCCLUSION));
58}
59
60static void
61_on_delete(Ecore_Evas *ee EINA_UNUSED)
62{
63 ecore_main_loop_quit();
64}
65
66static void
67_on_canvas_resize(Ecore_Evas *ee)
68{
69 int w, h;
70
71 ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
72 eo_do(background, evas_obj_size_set(w, h));
73 eo_do(image, evas_obj_size_set(w, h));
74}
75
76static Eina_Bool
77_animate_scene(void *data)
78{
79 static float angle = 0.0f;
80 Scene_Data *scene = (Scene_Data *)data;
81
82 angle += 0.5;
83
84 eo_do(scene->mesh_node,
85 evas_3d_node_orientation_angle_axis_set(angle, 0.0, 1.0, 0.0));
86
87 eo_do(scene->mesh_node, evas_3d_node_mesh_frame_set(scene->mesh, 50 * (1 + sin(angle / 180.0 * M_PI))));
88 /* Rotate */
89 if (angle > 360.0) angle = 0.0f;
90
91 return EINA_TRUE;
92}
93
94static void
95_camera_setup(Scene_Data *data)
96{
97 data->camera = eo_add(EVAS_3D_CAMERA_CLASS, evas);
98
99 eo_do(data->camera,
100 evas_3d_camera_projection_perspective_set(60.0, 1.0, 2.0, 50.0));
101
102 data->camera_node = eo_add(EVAS_3D_NODE_CLASS, evas,
103 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_CAMERA));
104 eo_do(data->camera_node,
105 evas_3d_node_camera_set(data->camera),
106 evas_3d_node_position_set(0.0, 0.0, 10.0),
107 evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0,
108 EVAS_3D_SPACE_PARENT, 0.0, 1.0, 0.0));
109 eo_do(data->root_node, evas_3d_node_member_add(data->camera_node));
110}
111
112static void
113_light_setup(Scene_Data *data)
114{
115 data->light = eo_add(EVAS_3D_LIGHT_CLASS, evas);
116 eo_do(data->light,
117 evas_3d_light_ambient_set(0.2, 0.2, 0.2, 1.0),
118 evas_3d_light_diffuse_set(1.0, 1.0, 1.0, 1.0),
119 evas_3d_light_specular_set(1.0, 1.0, 1.0, 1.0));
120
121 data->light_node = eo_add(EVAS_3D_NODE_CLASS, evas,
122 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_LIGHT));
123 eo_do(data->light_node,
124 evas_3d_node_light_set(data->light),
125 evas_3d_node_position_set(-20.0, 20.0, 50.0),
126 evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0,
127 EVAS_3D_SPACE_PARENT, 0.0, 1.0, 0.0));
128 eo_do(data->root_node, evas_3d_node_member_add(data->light_node));
129}
130
131static void
132_mesh_setup(Scene_Data *data)
133{
134 /* Setup material. */
135 data->material_rocks = eo_add(EVAS_3D_MATERIAL_CLASS, evas);
136
137 data->texture_rocks = eo_add(EVAS_3D_TEXTURE_CLASS, evas);
138 data->texture_rocks_n = eo_add(EVAS_3D_TEXTURE_CLASS, evas);
139 eo_do(data->texture_rocks, evas_3d_texture_file_set("rocks.jpg", NULL),
140 evas_3d_texture_wrap_set(EVAS_3D_WRAP_MODE_REPEAT,
141 EVAS_3D_WRAP_MODE_REPEAT));
142 eo_do(data->texture_rocks_n, evas_3d_texture_file_set("rocks_NM_height.tga", NULL),
143 evas_3d_texture_wrap_set(EVAS_3D_WRAP_MODE_REPEAT,
144 EVAS_3D_WRAP_MODE_REPEAT));
145
146 eo_do(data->material_rocks,
147 evas_3d_material_texture_set(EVAS_3D_MATERIAL_NORMAL, data->texture_rocks_n),
148 evas_3d_material_texture_set(EVAS_3D_MATERIAL_DIFFUSE, data->texture_rocks),
149 evas_3d_material_texture_set(EVAS_3D_MATERIAL_AMBIENT, data->texture_rocks),
150 evas_3d_material_texture_set(EVAS_3D_MATERIAL_SPECULAR, data->texture_rocks),
151 evas_3d_material_enable_set(EVAS_3D_MATERIAL_AMBIENT, EINA_TRUE),
152 evas_3d_material_enable_set(EVAS_3D_MATERIAL_NORMAL, EINA_TRUE),
153 evas_3d_material_enable_set(EVAS_3D_MATERIAL_DIFFUSE, EINA_TRUE),
154 evas_3d_material_enable_set(EVAS_3D_MATERIAL_SPECULAR, EINA_TRUE),
155
156 evas_3d_material_color_set(EVAS_3D_MATERIAL_AMBIENT, 0.1, 0.1, 0.1, 1.0),
157 evas_3d_material_color_set(EVAS_3D_MATERIAL_DIFFUSE, 1.0, 1.0, 1.0, 1.0),
158 evas_3d_material_color_set(EVAS_3D_MATERIAL_SPECULAR, 1.0, 1.0, 1.0, 1.0),
159 evas_3d_material_shininess_set(100.0));
160
161 data->material_wood = eo_add(EVAS_3D_MATERIAL_CLASS, evas);
162
163 data->texture_wood = eo_add(EVAS_3D_TEXTURE_CLASS, evas);
164 data->texture_four_n = eo_add(EVAS_3D_TEXTURE_CLASS, evas);
165 eo_do(data->texture_wood, evas_3d_texture_file_set("wood.jpg", NULL),
166 evas_3d_texture_wrap_set(EVAS_3D_WRAP_MODE_REPEAT,
167 EVAS_3D_WRAP_MODE_REPEAT));
168 eo_do(data->texture_four_n, evas_3d_texture_file_set("four_NM_height.tga", NULL),
169 evas_3d_texture_wrap_set(EVAS_3D_WRAP_MODE_REPEAT,
170 EVAS_3D_WRAP_MODE_REPEAT));
171
172 eo_do(data->material_wood,
173 evas_3d_material_texture_set(EVAS_3D_MATERIAL_NORMAL, data->texture_four_n),
174 evas_3d_material_texture_set(EVAS_3D_MATERIAL_DIFFUSE, data->texture_wood),
175 evas_3d_material_texture_set(EVAS_3D_MATERIAL_AMBIENT, data->texture_wood),
176 evas_3d_material_texture_set(EVAS_3D_MATERIAL_SPECULAR, data->texture_wood),
177 evas_3d_material_enable_set(EVAS_3D_MATERIAL_AMBIENT, EINA_TRUE),
178 evas_3d_material_enable_set(EVAS_3D_MATERIAL_NORMAL, EINA_TRUE),
179 evas_3d_material_enable_set(EVAS_3D_MATERIAL_DIFFUSE, EINA_TRUE),
180 evas_3d_material_enable_set(EVAS_3D_MATERIAL_SPECULAR, EINA_TRUE),
181
182 evas_3d_material_color_set(EVAS_3D_MATERIAL_AMBIENT, 0.1, 0.1, 0.1, 1.0),
183 evas_3d_material_color_set(EVAS_3D_MATERIAL_DIFFUSE, 1.0, 1.0, 1.0, 1.0),
184 evas_3d_material_color_set(EVAS_3D_MATERIAL_SPECULAR, 1.0, 1.0, 1.0, 1.0),
185 evas_3d_material_shininess_set(100.0));
186
187 /* Setup mesh. */
188 data->mesh = eo_add(EVAS_3D_MESH_CLASS, evas);
189 evas_3d_add_cube_frame(data->mesh, 0);
190
191 evas_3d_add_cube_frame(data->mesh, 100);
192 eo_do(data->mesh,
193 evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_PARALLAX_OCCLUSION),
194 evas_3d_mesh_frame_material_set(0, data->material_rocks));
195
196 eo_do(data->mesh,
197 evas_3d_mesh_frame_material_set(100, data->material_wood));
198
199 data->mesh_node = eo_add(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 evas_3d_node_scale_set(3, 3, 3));
204}
205
206static void
207_scene_setup(Scene_Data *data)
208{
209 data->scene = eo_add(EVAS_3D_SCENE_CLASS, evas);
210 eo_do(data->scene,
211 evas_3d_scene_size_set(WIDTH, HEIGHT);
212 evas_3d_scene_background_color_set(0.0, 0.0, 0.0, 0.0));
213
214 data->root_node = eo_add(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
226int
227main(void)
228{
229 //Unless Evas 3D supports Software renderer, we set gl backened forcely.
230 setenv("ECORE_EVAS_ENGINE", "opengl_x11", 1);
231
232 Scene_Data data;
233
234 if (!ecore_evas_init()) return 0;
235
236 ecore_evas = ecore_evas_new(NULL, 10, 10, WIDTH, HEIGHT, NULL);
237
238 if (!ecore_evas) return 0;
239
240 ecore_evas_callback_delete_request_set(ecore_evas, _on_delete);
241 ecore_evas_callback_resize_set(ecore_evas, _on_canvas_resize);
242 ecore_evas_show(ecore_evas);
243
244 evas = ecore_evas_get(ecore_evas);
245
246 _scene_setup(&data);
247
248 /* Add a background rectangle objects. */
249 background = eo_add(EVAS_RECTANGLE_CLASS, evas);
250 eo_do(background,
251 evas_obj_color_set(0, 0, 0, 255),
252 evas_obj_size_set(WIDTH, HEIGHT),
253 evas_obj_visibility_set(EINA_TRUE));
254
255 /* Add an image object for 3D scene rendering. */
256 image = evas_object_image_filled_add(evas);
257 eo_do(image,
258 evas_obj_size_set(WIDTH, HEIGHT),
259 evas_obj_visibility_set(EINA_TRUE),
260 evas_object_focus_set(image, EINA_TRUE));
261
262 /* Set the image object as render target for 3D scene. */
263 eo_do(image, evas_obj_image_scene_set(data.scene));
264
265 /* Add animation timer callback. */
266 ecore_timer_add(0.01, _animate_scene, &data);
267 evas_object_event_callback_add(image, EVAS_CALLBACK_KEY_DOWN, _on_key_down, &data);
268
269 /* Enter main loop. */
270 ecore_main_loop_begin();
271
272 ecore_evas_free(ecore_evas);
273 ecore_evas_shutdown();
274
275 return 0;
276}
diff --git a/src/examples/evas/four_NM_height.tga b/src/examples/evas/four_NM_height.tga
new file mode 100644
index 0000000..08bb3a2
--- /dev/null
+++ b/src/examples/evas/four_NM_height.tga
Binary files differ
diff --git a/src/examples/evas/rocks.jpg b/src/examples/evas/rocks.jpg
new file mode 100644
index 0000000..7383271
--- /dev/null
+++ b/src/examples/evas/rocks.jpg
Binary files differ
diff --git a/src/examples/evas/rocks_NM_height.tga b/src/examples/evas/rocks_NM_height.tga
new file mode 100644
index 0000000..7c8ee3b
--- /dev/null
+++ b/src/examples/evas/rocks_NM_height.tga
Binary files differ
diff --git a/src/examples/evas/wood.jpg b/src/examples/evas/wood.jpg
new file mode 100644
index 0000000..cff2cb2
--- /dev/null
+++ b/src/examples/evas/wood.jpg
Binary files differ