summaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorBogdan Devichev <b.devichev@samsung.com>2015-03-18 14:47:50 +0100
committerCedric BAIL <cedric@osg.samsung.com>2015-03-18 15:22:21 +0100
commitdb7d448ca277d6df02881db4b4d22ea382206862 (patch)
treecdd79779e90e04ac695123db7b7fd44916c30a92 /src/examples
parentca111165e9af8651e60d068dad188564717d0ee6 (diff)
evas: pick example uses sphere primitives.
Reviewers: cedric, Hermet, raster Subscribers: cedric Differential Revision: https://phab.enlightenment.org/D2169 Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/evas/evas-3d-pick.c217
1 files changed, 9 insertions, 208 deletions
diff --git a/src/examples/evas/evas-3d-pick.c b/src/examples/evas/evas-3d-pick.c
index 360e72c..bcd9c84 100644
--- a/src/examples/evas/evas-3d-pick.c
+++ b/src/examples/evas/evas-3d-pick.c
@@ -6,7 +6,7 @@
6 * and parameters which can be got from this function. 6 * and parameters which can be got from this function.
7 * 7 *
8 * @verbatim 8 * @verbatim
9 * gcc -o evas-3d-pick evas-3d-pick.c `pkg-config --libs --cflags evas ecore ecore-evas eo` -lm 9 * gcc -o evas-3d-pick evas-3d-pick.c evas-3d-primitives.c `pkg-config --libs --cflags evas ecore ecore-evas eo` -lm
10 * @endverbatim 10 * @endverbatim
11 */ 11 */
12 12
@@ -24,41 +24,12 @@
24#include <Ecore.h> 24#include <Ecore.h>
25#include <Ecore_Evas.h> 25#include <Ecore_Evas.h>
26#include "evas-common.h" 26#include "evas-common.h"
27#include "evas-3d-primitives.h"
27 28
28#define WIDTH 400 29#define WIDTH 400
29#define HEIGHT 400 30#define HEIGHT 400
30 31
31static const char *earth_image = PACKAGE_EXAMPLES_DIR EVAS_IMAGE_FOLDER "/wood.jpg"; 32static const char *image_path = PACKAGE_EXAMPLES_DIR EVAS_IMAGE_FOLDER "/wood.jpg";
32
33typedef struct _vec4
34{
35 float x;
36 float y;
37 float z;
38 float w;
39} vec4;
40
41typedef struct _vec3
42{
43 float x;
44 float y;
45 float z;
46} vec3;
47
48typedef struct _vec2
49{
50 float x;
51 float y;
52} vec2;
53
54typedef struct _vertex
55{
56 vec3 position;
57 vec3 normal;
58 vec3 tangent;
59 vec4 color;
60 vec3 texcoord;
61} vertex;
62 33
63static Ecore_Evas *ecore_evas = NULL; 34static Ecore_Evas *ecore_evas = NULL;
64static Evas *evas = NULL; 35static Evas *evas = NULL;
@@ -72,11 +43,7 @@ static Eo *mesh_node = NULL;
72static Eo *mesh = NULL; 43static Eo *mesh = NULL;
73static Eo *material = NULL; 44static Eo *material = NULL;
74static Eo *texture_diffuse = NULL; 45static Eo *texture_diffuse = NULL;
75 46static const vec2 tex_scale = {1, 1};
76static int vertex_count = 0;
77static vertex *vertices = NULL;
78static int index_count = 0;
79static unsigned short *indices = NULL;
80 47
81static Eina_Bool 48static Eina_Bool
82_animate_scene(void *data) 49_animate_scene(void *data)
@@ -111,150 +78,6 @@ _on_canvas_resize(Ecore_Evas *ee)
111 evas_object_move(image, 0, 0); 78 evas_object_move(image, 0, 0);
112} 79}
113 80
114static inline vec3
115_normalize(const vec3 *v)
116{
117 double l = sqrt(v->x * v->x + v->y * v->y + v->z * v->z);
118 vec3 vec;
119
120 vec.x = v->x / l;
121 vec.y = v->y / l;
122 vec.z = v->z / l;
123
124 return vec;
125}
126
127static void
128_sphere_fini()
129{
130 free(vertices);
131 free(indices);
132}
133
134static void
135_sphere_init(int precision)
136{
137 int i, j;
138 unsigned short *index;
139
140 vertex_count = (precision + 1) * (precision + 1);
141 index_count = precision * precision * 6;
142
143 /* Allocate buffer. */
144 vertices = malloc(sizeof(vertex) * vertex_count);
145 indices = malloc(sizeof(unsigned short) * index_count);
146
147 for (i = 0; i <= precision; i++)
148 {
149 double lati = (M_PI * (double)i) / (double)precision;
150 double y = cos(lati);
151 double r = fabs(sin(lati));
152
153 for (j = 0; j <= precision; j++)
154 {
155 double longi = (M_PI * 2.0 * j) / precision;
156 vertex *v = &vertices[i * (precision + 1) + j];
157
158 if (j == 0 || j == precision) v->position.x = 0.0;
159 else v->position.x = r * sin(longi);
160
161 v->position.y = y;
162
163 if (j == 0 || j == precision) v->position.z = r;
164 else v->position.z = r * cos(longi);
165
166 v->normal = v->position;
167
168 if (v->position.x > 0.0)
169 {
170 v->tangent.x = -v->normal.y;
171 v->tangent.y = v->normal.x;
172 v->tangent.z = v->normal.z;
173 }
174 else
175 {
176 v->tangent.x = v->normal.y;
177 v->tangent.y = -v->normal.x;
178 v->tangent.z = v->normal.z;
179 }
180
181 v->color.x = v->position.x;
182 v->color.y = v->position.y;
183 v->color.z = v->position.z;
184 v->color.w = 1.0;
185
186 if (j == precision) v->texcoord.x = 1.0;
187 else if (j == 0) v->texcoord.x = 0.0;
188 else v->texcoord.x = (double)j / (double)precision;
189
190 if (i == precision) v->texcoord.y = 1.0;
191 else if (i == 0) v->texcoord.y = 0.0;
192 else v->texcoord.y = 1.0 - (double)i / (double)precision;
193 }
194 }
195
196 index = &indices[0];
197
198 for (i = 0; i < precision; i++)
199 {
200 for (j = 0; j < precision; j++)
201 {
202 *index++ = i * (precision + 1) + j;
203 *index++ = i * (precision + 1) + j + 1;
204 *index++ = (i + 1) * (precision + 1) + j;
205
206 *index++ = (i + 1) * (precision + 1) + j;
207 *index++ = i * (precision + 1) + j + 1;
208 *index++ = (i + 1) * (precision + 1) + j + 1;
209 }
210 }
211
212 for (i = 0; i < index_count; i += 3)
213 {
214 vertex *v0 = &vertices[indices[i + 0]];
215 vertex *v1 = &vertices[indices[i + 1]];
216 vertex *v2 = &vertices[indices[i + 2]];
217
218 vec3 e1, e2;
219 float du1, du2, dv1, dv2, f;
220 vec3 tangent;
221
222 e1.x = v1->position.x - v0->position.x;
223 e1.y = v1->position.y - v0->position.y;
224 e1.z = v1->position.z - v0->position.z;
225
226 e2.x = v2->position.x - v0->position.x;
227 e2.y = v2->position.y - v0->position.y;
228 e2.z = v2->position.z - v0->position.z;
229
230 du1 = v1->texcoord.x - v0->texcoord.x;
231 dv1 = v1->texcoord.y - v0->texcoord.y;
232
233 du2 = v2->texcoord.x - v0->texcoord.x;
234 dv2 = v2->texcoord.y - v0->texcoord.y;
235
236 f = 1.0 / (du1 * dv2 - du2 * dv1);
237
238 tangent.x = f * (dv2 * e1.x - dv1 * e2.x);
239 tangent.y = f * (dv2 * e1.y - dv1 * e2.y);
240 tangent.z = f * (dv2 * e1.z - dv1 * e2.z);
241
242 v0->tangent = tangent;
243 }
244
245 for (i = 0; i <= precision; i++)
246 {
247 for (j = 0; j <= precision; j++)
248 {
249 if (j == precision)
250 {
251 vertex *v = &vertices[i * (precision + 1) + j];
252 v->tangent = vertices[i * (precision + 1)].tangent;
253 }
254 }
255 }
256}
257
258static void 81static void
259_on_mouse_down(void *data EINA_UNUSED, Evas *e EINA_UNUSED, Evas_Object *obj, 82_on_mouse_down(void *data EINA_UNUSED, Evas *e EINA_UNUSED, Evas_Object *obj,
260 void *event_info) 83 void *event_info)
@@ -327,43 +150,21 @@ main(void)
327 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_CAMERA)); 150 evas_3d_node_constructor(EVAS_3D_NODE_TYPE_CAMERA));
328 eo_do(camera_node, 151 eo_do(camera_node,
329 evas_3d_node_camera_set(camera), 152 evas_3d_node_camera_set(camera),
330 evas_3d_node_position_set(0.0, 0.0, 5.0), 153 evas_3d_node_position_set(0.0, 0.0, 2.5),
331 evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0, 154 evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0,
332 EVAS_3D_SPACE_PARENT, 0.0, 1.0, 0.0)); 155 EVAS_3D_SPACE_PARENT, 0.0, 1.0, 0.0));
333 eo_do(root_node, evas_3d_node_member_add(camera_node)); 156 eo_do(root_node, evas_3d_node_member_add(camera_node));
334 157
335 /* Add the cube mesh. */ 158 /* Add the cube mesh. */
336 _sphere_init(100);
337 159
338 mesh = eo_add(EVAS_3D_MESH_CLASS, evas); 160 mesh = eo_add(EVAS_3D_MESH_CLASS, evas);
339 eo_do(mesh, 161 evas_3d_add_sphere_frame(mesh, 0, 100, tex_scale);
340 evas_3d_mesh_vertex_count_set(vertex_count),
341 evas_3d_mesh_frame_add(0),
342 evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_POSITION,
343 sizeof(vertex),
344 &vertices[0].position),
345 evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_NORMAL,
346 sizeof(vertex),
347 &vertices[0].normal),
348 evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_TANGENT,
349 sizeof(vertex),
350 &vertices[0].tangent),
351 evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_COLOR,
352 sizeof(vertex), &vertices[0].color),
353 evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_TEXCOORD,
354 sizeof(vertex),
355 &vertices[0].texcoord),
356
357 evas_3d_mesh_index_data_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
358 index_count, &indices[0]),
359 evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES));
360 162
361 material = eo_add(EVAS_3D_MATERIAL_CLASS, evas); 163 material = eo_add(EVAS_3D_MATERIAL_CLASS, evas);
362 eo_do(mesh, evas_3d_mesh_frame_material_set(0, material));
363 164
364 texture_diffuse = eo_add(EVAS_3D_TEXTURE_CLASS, evas); 165 texture_diffuse = eo_add(EVAS_3D_TEXTURE_CLASS, evas);
365 eo_do(texture_diffuse, 166 eo_do(texture_diffuse,
366 evas_3d_texture_file_set(earth_image, NULL), 167 evas_3d_texture_file_set(image_path, NULL),
367 evas_3d_texture_filter_set(EVAS_3D_TEXTURE_FILTER_LINEAR, 168 evas_3d_texture_filter_set(EVAS_3D_TEXTURE_FILTER_LINEAR,
368 EVAS_3D_TEXTURE_FILTER_LINEAR)); 169 EVAS_3D_TEXTURE_FILTER_LINEAR));
369 eo_do(material, 170 eo_do(material,
@@ -386,7 +187,8 @@ main(void)
386 187
387 eo_do(root_node, evas_3d_node_member_add(mesh_node)); 188 eo_do(root_node, evas_3d_node_member_add(mesh_node));
388 eo_do(mesh_node, evas_3d_node_mesh_add(mesh)); 189 eo_do(mesh_node, evas_3d_node_mesh_add(mesh));
389 eo_do(mesh, evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_DIFFUSE)); 190 eo_do(mesh, evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_DIFFUSE),
191 evas_3d_mesh_frame_material_set(0, material));
390 192
391 /* Set up scene. */ 193 /* Set up scene. */
392 eo_do(scene, 194 eo_do(scene,
@@ -415,7 +217,6 @@ main(void)
415 217
416 ecore_evas_free(ecore_evas); 218 ecore_evas_free(ecore_evas);
417 ecore_evas_shutdown(); 219 ecore_evas_shutdown();
418 _sphere_fini();
419 220
420 return 0; 221 return 0;
421} 222}