summaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorDmytro Dadyka <d.dadyka@samsung.com>2015-02-20 17:57:18 +0100
committerCedric BAIL <cedric@osg.samsung.com>2015-02-20 18:36:56 +0100
commitf377770cc16585ce0e6d7777f8e2090ca38dda21 (patch)
tree73ec37bdb0f699fa6879fa9bdc8fd505440523a2 /src/examples
parent789633b321dbb0669b93ba2da3bbaaede3de6233 (diff)
evas: Evas_3D - add common code for primitive shape.
Reviewers: Hermet, cedric Subscribers: cedric Differential Revision: https://phab.enlightenment.org/D2012 Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/evas/Makefile.am2
-rw-r--r--src/examples/evas/evas-3d-primitives.c458
-rw-r--r--src/examples/evas/evas-3d-primitives.h55
-rw-r--r--src/examples/evas/evas-3d-shadows.c19
4 files changed, 360 insertions, 174 deletions
diff --git a/src/examples/evas/Makefile.am b/src/examples/evas/Makefile.am
index 9cb4284..300f978 100644
--- a/src/examples/evas/Makefile.am
+++ b/src/examples/evas/Makefile.am
@@ -248,7 +248,7 @@ evas_3d_cube_rotate_LDADD = $(ECORE_EVAS_COMMON_LDADD) @EFL_PTHREAD_LIBS@
248evas_3d_cube_rotate_CPPFLAGS = $(ECORE_EVAS_COMMON_CPPFLAGS) 248evas_3d_cube_rotate_CPPFLAGS = $(ECORE_EVAS_COMMON_CPPFLAGS)
249 249
250EXTRA_PROGRAMS += evas_3d_shadows 250EXTRA_PROGRAMS += evas_3d_shadows
251evas_3d_shadows_SOURCES = evas-3d-shadows.c 251evas_3d_shadows_SOURCES = evas-3d-shadows.c evas-3d-primitives.c evas-3d-primitives.h
252evas_3d_shadows_LDADD = $(ECORE_EVAS_COMMON_LDADD) @EFL_PTHREAD_LIBS@ 252evas_3d_shadows_LDADD = $(ECORE_EVAS_COMMON_LDADD) @EFL_PTHREAD_LIBS@
253evas_3d_shadows_CPPFLAGS = $(ECORE_EVAS_COMMON_CPPFLAGS) 253evas_3d_shadows_CPPFLAGS = $(ECORE_EVAS_COMMON_CPPFLAGS)
254 254
diff --git a/src/examples/evas/evas-3d-primitives.c b/src/examples/evas/evas-3d-primitives.c
index 6892054..ab96cd8 100644
--- a/src/examples/evas/evas-3d-primitives.c
+++ b/src/examples/evas/evas-3d-primitives.c
@@ -1,131 +1,192 @@
1#include <Eo.h> 1#define EFL_EO_API_SUPPORT
2#include <Evas.h> 2#define EFL_BETA_API_SUPPORT
3#include <Ecore.h> 3
4#include <Ecore_Evas.h> 4#include "evas-3d-primitives.h"
5#include <math.h> 5
6 6// TODO Use an external library of linear algebra.
7#define SET_VERTEX_DATA eo_do(mesh, evas_3d_mesh_vertex_count_set(vcount),\ 7inline void
8 evas_3d_mesh_frame_add(0);\ 8_vec3_subtract(vec3 *out, const vec3 *a, const vec3 *b)
9 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_POSITION,\
10 sizeof(vec3), &vertices[0]);\
11 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_NORMAL,\
12 sizeof(vec3), &normals[0]));\
13 free(vertices);\
14 free(normals);
15
16typedef struct _vec3
17{ 9{
18 float x; 10 out->x = a->x - b->x;
19 float y; 11 out->y = a->y - b->y;
20 float z; 12 out->z = a->z - b->z;
21} vec3; 13}
22 14
23static const float cube_vertices[] = 15inline void
16_vec3_copy(vec3 *dst, const vec3 *src)
17{
18 dst->x = src->x;
19 dst->y = src->y;
20 dst->z = src->z;
21}
22
23inline void
24_vec3_cross_product(vec3 *out, const vec3 *a, const vec3 *b)
25{
26 vec3 tmp;
27
28 tmp.x = a->y * b->z - a->z * b->y;
29 tmp.y = a->z * b->x - a->x * b->z;
30 tmp.z = a->x * b->y - a->y * b->x;
31
32 _vec3_copy(out, &tmp);
33}
34
35inline void
36_vec3_normalize(vec3 *out)
37{
38 float size = out->x * out->x + out->y *out->y + out->z * out->z;
39 size = sqrt(size);
40 out->x /= size;
41 out->y /= size;
42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, 0.0, 1.0, -1.0, 0.0, 0.0,
57 -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, 1.0, 1.0, -1.0, 0.0, 0.0,
58 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, 0.0, 0.0, -1.0, 0.0, 0.0,
59 -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 -1.0, -1.0, 1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, -1.0, 0.0, 0.0,
60}; 82};
61 83
62static const unsigned short cube_indices[] = 84const unsigned short cube_indices[] =
63{ 85{
64 /* Front */
65 0, 1, 2, 2, 1, 3, 86 0, 1, 2, 2, 1, 3,
66
67 /* Back */
68 4, 5, 6, 6, 5, 7, 87 4, 5, 6, 6, 5, 7,
69
70 /* Left */
71 8, 9, 10, 10, 9, 11, 88 8, 9, 10, 10, 9, 11,
72
73 /* Right */
74 12, 13, 14, 14, 13, 15, 89 12, 13, 14, 14, 13, 15,
75
76 /* Top */
77 16, 17, 18, 18, 17, 19, 90 16, 17, 18, 18, 17, 19,
78
79 /* Bottom */
80 20, 21, 22, 22, 21, 23 91 20, 21, 22, 22, 21, 23
81}; 92};
82 93
83static const float square_vertices[] = 94const float square_vertices[] =
84{ 95{
85 1.0, -1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 96 -1.0, 1.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0,
86 -1.0, -1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 97 1.0, 1.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
87 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 98 -1.0, -1.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
88 -1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 99 1.0, -1.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0,
89}; 100};
90 101
91static const unsigned short square_indices[] = {0, 1, 2, 2, 1, 3}; 102const unsigned short square_indices[] = {0, 1, 2, 2, 1, 3};
103
104#define ALLOCATE_VERTEX_DATA \
105 vec3 *vertices = malloc(sizeof(vec3) * vcount); \
106 vec3 *normals = malloc(sizeof(vec3) * vcount); \
107 vec2 *tex_coord = malloc(sizeof(vec2) * vcount); \
108 vec3 *tangents = malloc(sizeof(vec3) * vcount); \
109 unsigned short *indices = malloc(sizeof(short) * icount);
110
111#define SET_VERTEX_DATA(frame) \
112 eo_do(mesh, evas_3d_mesh_vertex_count_set(vcount), \
113 evas_3d_mesh_frame_add(frame), \
114 evas_3d_mesh_frame_vertex_data_copy_set(frame, EVAS_3D_VERTEX_POSITION, \
115 sizeof(vec3), &vertices[0]), \
116 evas_3d_mesh_frame_vertex_data_copy_set(frame, EVAS_3D_VERTEX_NORMAL, \
117 sizeof(vec3), &normals[0]), \
118 evas_3d_mesh_frame_vertex_data_copy_set(frame, EVAS_3D_VERTEX_TEXCOORD, \
119 sizeof(vec2), &tex_coord[0]), \
120 evas_3d_mesh_frame_vertex_data_copy_set(frame, EVAS_3D_VERTEX_TANGENT, \
121 sizeof(vec3), &tangents[0]), \
122 evas_3d_mesh_index_data_copy_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT, \
123 icount , &indices[0])); \
124 free(vertices); \
125 free(normals); \
126 free(tangents); \
127 free(tex_coord); \
128 free(indices);
129
130#define SET_VERTEX_DATA_FROM_ARRAY(mesh, frame, v_array, v_count, i_array, i_count) \
131 eo_do(mesh, \
132 evas_3d_mesh_vertex_count_set(v_count), \
133 evas_3d_mesh_frame_add(frame), \
134 evas_3d_mesh_frame_vertex_data_set(frame, EVAS_3D_VERTEX_POSITION, \
135 15 * sizeof(float), &v_array[ 0]), \
136 evas_3d_mesh_frame_vertex_data_set(frame, EVAS_3D_VERTEX_NORMAL, \
137 15 * sizeof(float), &v_array[ 3]), \
138 evas_3d_mesh_frame_vertex_data_set(frame, EVAS_3D_VERTEX_COLOR, \
139 15 * sizeof(float), &v_array[ 6]), \
140 evas_3d_mesh_frame_vertex_data_set(frame, EVAS_3D_VERTEX_TEXCOORD, \
141 15 * sizeof(float), &v_array[10]), \
142 evas_3d_mesh_frame_vertex_data_set(frame, EVAS_3D_VERTEX_TANGENT, \
143 15 * sizeof(float), &v_array[12]), \
144 evas_3d_mesh_index_data_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT, \
145 i_count, &i_array[0]), \
146 evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES));
147
148vec3 _get_func_normal(Surface *func, float x, float y)
149{
150 vec3 v00, v01, v10, d1, d2, normal;
151
152 v00 = func(x, y);
153 v01 = func(x, y + 0.01);
154 v10 = func(x + 0.01, y);
155 _vec3_subtract(&d1, &v00, &v01);
156 _vec3_subtract(&d2, &v01, &v10);
92 157
93static void 158 _vec3_cross_product(&normal, &d1, &d2);
94_set_grid_indices(Eo *mesh, int count) 159
160 _vec3_normalize(&normal);
161
162 return normal;
163}
164
165void
166_generate_grid_indices(unsigned short *indices, int count)
95{ 167{
96 int i, j; 168 int i, j;
97 unsigned short *index, *indices; 169 unsigned short *index = &indices[0];
98 int vccount = count + 1; 170 int vccount = count + 1;
99 int icount = count * count * 6;
100
101 indices = malloc(sizeof(short) * icount);
102 index = indices;
103 171
104 for(j = 0; j < count; j++) 172 for(j = 0; j < count; j++)
105 for(i = 0; i < count; i++) 173 for(i = 0; i < count; i++)
106 { 174 {
107 *index++ = i + vccount * j; 175 *index++ = (unsigned short)(i + vccount * j);
108 *index++ = i + vccount * (j + 1); 176 *index++ = i + vccount * (j + 1);
109 *index++ = i + 1 + vccount * (j + 1); 177 *index++ = i + 1 + vccount * (j + 1);
110 178
111 *index++ = i + vccount * j; 179 *index++ = i + vccount * j;
112 *index++ = i + 1 + vccount * j; 180 *index++ = i + 1 + vccount * j;
113 *index++ = i + vccount * (j + 1) + 1; 181 *index++ = i + vccount * (j + 1) + 1;
114 } 182 }
115 eo_do(mesh,
116 evas_3d_mesh_index_data_copy_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
117 icount , &indices[0]));
118 free(indices);
119} 183}
120 184
121static void 185void
122_set_tape_indices(Eo *mesh, int count) 186_generate_tape_indices(unsigned short *indices, int count)
123{ 187{
124 unsigned short *index, *indices;
125 int i, vccount = count + 1; 188 int i, vccount = count + 1;
126 int icount = count * 6; 189 unsigned short *index = &indices[0];
127 indices = malloc(sizeof(short) * icount);
128 index = indices;
129 190
130 for(i = 0; i < count; i++) 191 for(i = 0; i < count; i++)
131 { 192 {
@@ -133,31 +194,26 @@ _set_tape_indices(Eo *mesh, int count)
133 *index++ = i + 1; 194 *index++ = i + 1;
134 *index++ = i + 1 + vccount; 195 *index++ = i + 1 + vccount;
135 196
136 *index++ = i; 197 *index++ = i;
137 *index++ = i + vccount; 198 *index++ = i + vccount;
138 *index++ = i + vccount + 1; 199 *index++ = i + vccount + 1;
139 } 200 }
140 eo_do(mesh,
141 evas_3d_mesh_index_data_copy_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
142 icount , &indices[0]));
143 free(indices);
144} 201}
145 202
146static void 203void
147_set_ball(Eo *mesh, int p) 204evas_3d_add_sphere_frame(Eo *mesh, int frame, int p, vec2 tex_scale)
148{ 205{
149 int vcount, vccount, i, j; 206 int vcount, icount, vccount, i, j;
150 double dtheta, dfi, sinth, costh, fi, theta, sinfi, cosfi; 207 icount = p * p * 6;
151
152 vccount = p + 1; 208 vccount = p + 1;
153 vcount = vccount * vccount; 209 vcount = vccount * vccount;
154 210
211 ALLOCATE_VERTEX_DATA
212
213 double dtheta, dfi, sinth, costh, fi, theta, sinfi, cosfi;
155 dtheta = M_PI / p; 214 dtheta = M_PI / p;
156 dfi = 2 * M_PI / p; 215 dfi = 2 * M_PI / p;
157 216
158 vec3 *vertices = malloc(sizeof(vec3) * vcount);
159 vec3 *normals = malloc(sizeof(vec3) * vcount);
160
161 for (j = 0; j < vccount; j++) 217 for (j = 0; j < vccount; j++)
162 { 218 {
163 theta = j * dtheta; 219 theta = j * dtheta;
@@ -168,125 +224,199 @@ _set_ball(Eo *mesh, int p)
168 fi = i * dfi; 224 fi = i * dfi;
169 sinfi = sin(fi); 225 sinfi = sin(fi);
170 cosfi = cos(fi); 226 cosfi = cos(fi);
171 vertices[i + j * vccount].x = sinth * cosfi; 227 vertices[i + j * vccount].x = sinth * sinfi;
172 vertices[i + j * vccount].y = sinth * sinfi; 228 vertices[i + j * vccount].y = costh;
173 vertices[i + j * vccount].z = costh; 229 vertices[i + j * vccount].z = sinth * cosfi;
174 230
175 normals[i + j * vccount].x = vertices[i + j * vccount].x; 231 normals[i + j * vccount] = vertices[i + j * vccount];
176 normals[i + j * vccount].y = vertices[i + j * vccount].y; 232
177 normals[i + j * vccount].z = vertices[i + j * vccount].z; 233 tangents[i + j * vccount].x = vertices[i + j * vccount].z;
234 tangents[i + j * vccount].y = vertices[i + j * vccount].y;
235 tangents[i + j * vccount].z = -vertices[i + j * vccount].x;
236
237 tex_coord[i+ j * vccount].x = i / (float)(vccount-1) * tex_scale.x;
238 tex_coord[i+ j *vccount].y = tex_scale.y - j / (float)(vccount-1) * tex_scale.y;
178 } 239 }
179 } 240 }
180 241
181 _set_grid_indices(mesh, p); 242 _generate_grid_indices(indices, p);
182 243
183 SET_VERTEX_DATA 244 SET_VERTEX_DATA(frame)
184} 245}
185 246
186static void 247void
187_set_cylinder(Eo *mesh, int p) 248evas_3d_add_func_surface_frame(Eo *mesh, int frame, Surface func, int p, vec2 tex_scale)
188{ 249{
189 int vcount, vccount, i; 250 int vcount, icount, vccount, i, j;
190 double dfi, fi, sinfi, cosfi; 251 icount = p * p * 6;
252 vccount = p + 1;
253 vcount = vccount * vccount;
254
255 ALLOCATE_VERTEX_DATA
256
257 double v, u, d = 1.0 / p;
191 258
259 for (j = 0; j < vccount; j++)
260 {
261 u = j * d - 0.5;
262 for (i = 0; i < vccount; i++)
263 {
264 v = i * d - 0.5;
265 vertices[i + j * vccount] = func(v, u);
266 normals[i + j * vccount] = _get_func_normal(func, v, u);
267
268 tex_coord[i+ j * vccount].x = i / (float)(vccount-1) * tex_scale.x;
269 tex_coord[i+ j *vccount].y = tex_scale.y - j / (float)(vccount-1) * tex_scale.y;
270 }
271 }
272
273 _generate_grid_indices(indices, p);
274 SET_VERTEX_DATA(frame)
275}
276
277void
278evas_3d_add_torus_frame(Eo *mesh, int frame, float rratio, int p, vec2 tex_scale)
279{
280 int vcount, icount, vccount, i, j;
281 icount = p * p * 6;
282 vccount = p + 1;
283 vcount = vccount * vccount;
284
285 ALLOCATE_VERTEX_DATA
286
287 double d, sinth, costh, fi, theta, sinfi, cosfi;
288
289 d = 2 * M_PI / p;
290
291 for (j = 0; j < vccount; j++)
292 {
293 theta = j * d;
294 sinth = sin(theta);
295 costh = cos(theta);
296 for (i = 0; i < vccount; i++)
297 {
298 fi = i * d;
299 sinfi = sin(fi);
300 cosfi = cos(fi);
301 vertices[i + j * vccount].x = (1 + rratio * cosfi) * costh;
302 vertices[i + j * vccount].y = (1 + rratio * cosfi) * sinth;
303 vertices[i + j * vccount].z = rratio * sinfi;
304
305 normals[i + j * vccount].x = cosfi * costh;
306 normals[i + j * vccount].y = cosfi * sinth;
307 normals[i + j * vccount].z = sinfi;
308
309 tangents[i + j * vccount].x = - sinfi * costh;
310 tangents[i + j * vccount].y = - sinfi * sinth;
311 tangents[i + j * vccount].z = cosfi;
312
313 _vec3_normalize(&normals[i + j * vccount]);
314
315 tex_coord[i+ j * vccount].x = i / (float)(vccount-1) * tex_scale.x;
316 tex_coord[i+ j *vccount].y = tex_scale.y - j / (float)(vccount-1) * tex_scale.y;
317 }
318 }
319
320 _generate_grid_indices(indices, p);
321
322 SET_VERTEX_DATA(frame)
323}
324
325void
326evas_3d_add_cylinder_frame(Eo *mesh, int frame, int p, vec2 tex_scale)
327{
328 int vcount, icount, vccount, i;
329 icount = p * 6;
192 vccount = p + 1; 330 vccount = p + 1;
193 vcount = 2 * vccount; 331 vcount = 2 * vccount;
194 332
195 dfi = 2 * M_PI / p; 333 ALLOCATE_VERTEX_DATA
196 334
197 vec3 *vertices = malloc(sizeof(vec3) * vcount); 335 double dfi, fi, sinfi, cosfi;
198 vec3 *normals = malloc(sizeof(vec3) * vcount); 336 dfi = 2 * M_PI / p;
199 337
200 for (i = 0; i < vccount; i++) 338 for (i = 0; i < vccount; i++)
201 { 339 {
202 fi = i * dfi; 340 fi = i * dfi;
203 sinfi = sin(fi); 341 sinfi = sin(fi);
204 cosfi = cos(fi); 342 cosfi = cos(fi);
205 vertices[i + vccount].x = vertices[i].x = cosfi; 343 vertices[i + vccount].x = vertices[i].x = sinfi ;
206 vertices[i].y = -0.5; 344 vertices[i].y = -0.5;
207 vertices[i + vccount].z = vertices[i].z = sinfi; 345 vertices[i + vccount].z = vertices[i].z = cosfi;
208 vertices[i + vccount].y = 0.5; 346 vertices[i + vccount].y = 0.5;
209 347
210 normals[i + vccount].x = normals[i].x = cosfi; 348 normals[i + vccount].x = normals[i].x = sinfi;
211 normals[i + vccount].y = normals[i].y = 0; 349 normals[i + vccount].y = normals[i].y = 0;
212 normals[i + vccount].z = normals[i].z = sinfi; 350 normals[i + vccount].z = normals[i].z = cosfi;
351
352 tangents[i + vccount].x = tangents[i].x = cosfi;
353 tangents[i + vccount].y = tangents[i].y = 0;
354 tangents[i + vccount].z = tangents[i].z = -sinfi;
355
356 tex_coord[i].x = i / (float)(vccount-1) * tex_scale.x;
357 tex_coord[i].y = 0;
358 tex_coord[i+ vccount].x = i / (float)(vccount-1) * tex_scale.x;
359 tex_coord[i+ vccount].y = tex_scale.y;
213 } 360 }
214 361
215 _set_tape_indices(mesh, p); 362 _generate_tape_indices(indices, p);
216 363
217 SET_VERTEX_DATA 364 SET_VERTEX_DATA(frame)
218} 365}
219 366
220static void 367void
221_set_cone(Eo *mesh, int p) 368evas_3d_add_cone_frame(Eo *mesh, int frame, int p, vec2 tex_scale)
222{ 369{
223 int vcount, vccount, i; 370 int vcount, icount, vccount, i;
224 double dfi, fi, sinfi, cosfi; 371 double dfi, fi, sinfi, cosfi;
225 372
373 icount = p * 6;
226 vccount = p + 1; 374 vccount = p + 1;
227 vcount = 2 * vccount; 375 vcount = 2 * vccount;
228 376
229 dfi = 2 * M_PI / p; 377 dfi = 2.0 * M_PI / p;
230 378
231 vec3 *vertices = malloc(sizeof(vec3) * vcount); 379 ALLOCATE_VERTEX_DATA
232 vec3 *normals = malloc(sizeof(vec3) * vcount);
233 380
234 for (i = 0; i < vccount; i++) 381 for (i = 0; i < vccount; i++)
235 { 382 {
236 fi = i * dfi; 383 fi = i * dfi;
237 sinfi = sin(fi); 384 sinfi = sin(fi);
238 cosfi = cos(fi); 385 cosfi = cos(fi);
239 vertices[i].x = cosfi; 386 vertices[i].x = sinfi;
240 vertices[i].y = 0; 387 vertices[i].y = 0;
241 vertices[i].z = sinfi; 388 vertices[i].z = cosfi;
242 vertices[i + vccount].x = 0; 389 vertices[i + vccount].x = 0;
243 vertices[i + vccount].y = 1; 390 vertices[i + vccount].y = 1;
244 vertices[i + vccount].z = 0; 391 vertices[i + vccount].z = 0;
245 392
246 normals[i + vccount].x = normals[i].x = cosfi * 0.71; 393 normals[i + vccount].x = normals[i].x = sinfi * 0.71;
247 normals[i + vccount].y = normals[i].y = 0.71; 394 normals[i + vccount].y = normals[i].y = 0.71;
248 normals[i + vccount].z = normals[i].z = sinfi * 0.71; 395 normals[i + vccount].z = normals[i].z = cosfi * 0.71;
396
397 tangents[i + vccount].x = tangents[i].x = cosfi;
398 tangents[i + vccount].y = tangents[i].y = 0;
399 tangents[i + vccount].z = tangents[i].z = -sinfi;
400
401 tex_coord[i].x = i / (float)(vccount-1) * tex_scale.x;
402 tex_coord[i].y = 0;
403 tex_coord[i+ vccount].x = tex_coord[i].x;
404 tex_coord[i+ vccount].y = tex_scale.y;
249 } 405 }
250 406
251 _set_tape_indices(mesh, p); 407 _generate_tape_indices(indices, p);
252 408
253 SET_VERTEX_DATA 409 SET_VERTEX_DATA(frame)
254} 410}
255 411
256static void 412void
257_set_square(Eo *mesh) 413evas_3d_add_square_frame(Eo *mesh, int frame)
258{ 414{
259 eo_do(mesh, evas_3d_mesh_vertex_count_set(4), 415 SET_VERTEX_DATA_FROM_ARRAY(mesh, frame, square_vertices, 4, square_indices, 6)
260 evas_3d_mesh_frame_add(0);
261 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_POSITION,
262 8 * sizeof(float), &square_vertices[0]);
263 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_NORMAL,
264 8 * sizeof(float), &square_vertices[3]),
265 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_TEXCOORD,
266 8 * sizeof(float), &square_vertices[6]);
267 evas_3d_mesh_index_data_copy_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
268 6 , &square_indices[0]);
269 evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES);
270 evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_PHONG));
271} 416}
272 417
273static void 418void
274_set_cube(Eo *mesh) 419evas_3d_add_cube_frame(Eo *mesh, int frame)
275{ 420{
276 eo_do(mesh, 421 SET_VERTEX_DATA_FROM_ARRAY(mesh, frame, cube_vertices, 24, cube_indices, 36)
277 evas_3d_mesh_vertex_count_set(24),
278 evas_3d_mesh_frame_add(0),
279
280 evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_POSITION,
281 12 * sizeof(float), &cube_vertices[0]),
282 evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_NORMAL,
283 12 * sizeof(float), &cube_vertices[3]),
284 evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_COLOR,
285 12 * sizeof(float), &cube_vertices[6]),
286 evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_TEXCOORD,
287 12 * sizeof(float), &cube_vertices[10]),
288
289 evas_3d_mesh_index_data_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
290 36, &cube_indices[0]),
291 evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES));
292} 422}
diff --git a/src/examples/evas/evas-3d-primitives.h b/src/examples/evas/evas-3d-primitives.h
new file mode 100644
index 0000000..3281f5d
--- /dev/null
+++ b/src/examples/evas/evas-3d-primitives.h
@@ -0,0 +1,55 @@
1#ifndef EVAS_3D_PRIMITIVES_H
2#define EVAS_3D_PRIMITIVES_H
3
4#include <Eo.h>
5#include <Evas.h>
6#include <math.h>
7
8// TODO Use an external library of linear algebra.
9typedef struct _vec3
10{
11 float x;
12 float y;
13 float z;
14} vec3;
15
16typedef struct _vec2
17{
18 float x;
19 float y;
20} vec2;
21
22/* The type of user-defined parametric surface function.*/
23typedef vec3 (Surface)(float x, float y);
24
25/* Set frame as sphere. */
26void
27evas_3d_add_sphere_frame(Eo *mesh, int frame, int precision, vec2 tex_scale);
28
29/* Set frame as user defined parametric surface.A parametric surface is a
30 * surface in the Euclidean space R3 which is defined by a parametric equation
31 * with two parameters. */
32void
33evas_3d_add_func_surface_frame(Eo *mesh, int frame, Surface func, int precision, vec2 tex_scale);
34
35/* Set frame as sphere as torus */
36void
37evas_3d_add_torus_frame(Eo *mesh, int frame, float rratio, int precision, vec2 tex_scale);
38
39/* Set frame as cylinder. */
40void
41evas_3d_add_cylinder_frame(Eo *mesh, int frame, int precision, vec2 tex_scale);
42
43/* Set frame as cone. */
44void
45evas_3d_add_cone_frame(Eo *mesh, int frame, int precision, vec2 tex_scale);
46
47/* Set frame as square. */
48void
49evas_3d_add_square_frame(Eo *mesh, int frame);
50
51/* Set frame as cube. */
52void
53evas_3d_add_cube_frame(Eo *mesh, int frame);
54
55#endif // EVAS_3D_PRIMITIVES_H
diff --git a/src/examples/evas/evas-3d-shadows.c b/src/examples/evas/evas-3d-shadows.c
index 86903f6..710507c 100644
--- a/src/examples/evas/evas-3d-shadows.c
+++ b/src/examples/evas/evas-3d-shadows.c
@@ -5,7 +5,7 @@
5 * @see evas_3d_scene_shadows_enable_set(Eina_Bool _shadows_enabled) 5 * @see evas_3d_scene_shadows_enable_set(Eina_Bool _shadows_enabled)
6 * @see evas_3d_object_callback_register 6 * @see evas_3d_object_callback_register
7 * 7 *
8 * Compile with gcc -o evas-3d-shadows evas-3d-shadows.c `pkg-config --libs --cflags efl evas ecore ecore-evas eo eina` -lm 8 * Compile with gcc -o gcc -o evas-3d-shadows evas-3d-shadows.c evas-3d-primitives.c `pkg-config --libs --cflags efl evas ecore ecore-evas eo eina` -lm
9 */ 9 */
10 10
11#ifdef HAVE_CONFIG_H 11#ifdef HAVE_CONFIG_H
@@ -23,8 +23,8 @@
23#include <Ecore_Evas.h> 23#include <Ecore_Evas.h>
24#include <Eina.h> 24#include <Eina.h>
25#include <math.h> 25#include <math.h>
26#include "evas-3d-primitives.c" 26#include "evas-3d-primitives.h"
27#include "evas-3d-common.h" 27#include "evas-common.h"
28 28
29#define WIDTH 1024 29#define WIDTH 1024
30#define HEIGHT 1024 30#define HEIGHT 1024
@@ -37,6 +37,7 @@
37 37
38static const char *model_path = PACKAGE_EXAMPLES_DIR EVAS_3D_MODEL_FOLDER "/sonic.md2"; 38static const char *model_path = PACKAGE_EXAMPLES_DIR EVAS_3D_MODEL_FOLDER "/sonic.md2";
39static const char *image_path = PACKAGE_EXAMPLES_DIR EVAS_3D_IMAGE_FOLDER "/sonic.png"; 39static const char *image_path = PACKAGE_EXAMPLES_DIR EVAS_3D_IMAGE_FOLDER "/sonic.png";
40static const vec2 tex_scale = {1, 1};
40 41
41Ecore_Evas *ecore_evas = NULL; 42Ecore_Evas *ecore_evas = NULL;
42Evas *evas = NULL; 43Evas *evas = NULL;
@@ -171,7 +172,7 @@ static void
171_sphere_setup(Body_3D *sphere) 172_sphere_setup(Body_3D *sphere)
172{ 173{
173 sphere->mesh = eo_add(EVAS_3D_MESH_CLASS, evas); 174 sphere->mesh = eo_add(EVAS_3D_MESH_CLASS, evas);
174 _set_ball(sphere->mesh, 50); 175 evas_3d_add_sphere_frame(sphere->mesh, 0, 50, tex_scale);
175 _body_material_set(sphere, 1, 0.0, 0.0); 176 _body_material_set(sphere, 1, 0.0, 0.0);
176 177
177 sphere->node = 178 sphere->node =
@@ -185,7 +186,7 @@ static void
185_cone_setup(Body_3D *cone) 186_cone_setup(Body_3D *cone)
186{ 187{
187 cone->mesh = eo_add(EVAS_3D_MESH_CLASS, evas); 188 cone->mesh = eo_add(EVAS_3D_MESH_CLASS, evas);
188 _set_cone(cone->mesh, 100); 189 evas_3d_add_cone_frame(cone->mesh, 0, 100, tex_scale);
189 _body_material_set(cone, 0.8, 0.5, 0.5); 190 _body_material_set(cone, 0.8, 0.5, 0.5);
190 191
191 cone->node = 192 cone->node =
@@ -200,7 +201,7 @@ static void
200_cylinder_setup(Body_3D *cylinder) 201_cylinder_setup(Body_3D *cylinder)
201{ 202{
202 cylinder->mesh = eo_add(EVAS_3D_MESH_CLASS, evas); 203 cylinder->mesh = eo_add(EVAS_3D_MESH_CLASS, evas);
203 _set_cylinder(cylinder->mesh, 50); 204 evas_3d_add_cylinder_frame(cylinder->mesh, 0, 50, tex_scale);
204 _body_material_set(cylinder, 0.0, 0.0, 1.0); 205 _body_material_set(cylinder, 0.0, 0.0, 1.0);
205 206
206 cylinder->node = 207 cylinder->node =
@@ -214,9 +215,9 @@ static void
214_square_setup(Body_3D *square) 215_square_setup(Body_3D *square)
215{ 216{
216 square->mesh = eo_add(EVAS_3D_MESH_CLASS, evas); 217 square->mesh = eo_add(EVAS_3D_MESH_CLASS, evas);
217 _set_square(square->mesh); 218 evas_3d_add_square_frame(square->mesh, 0);
218 219
219 _body_material_set(square, 0.9, 1, 1); 220 _body_material_set(square, 0.4, 0.4, 0.4);
220 221
221 square->node = 222 square->node =
222 eo_add(EVAS_3D_NODE_CLASS, evas, 223 eo_add(EVAS_3D_NODE_CLASS, evas,
@@ -231,7 +232,7 @@ static void
231_box_setup(Body_3D *box) 232_box_setup(Body_3D *box)
232{ 233{
233 box->mesh = eo_add(EVAS_3D_MESH_CLASS, evas); 234 box->mesh = eo_add(EVAS_3D_MESH_CLASS, evas);
234 _set_cube(box->mesh); 235 evas_3d_add_cube_frame(box->mesh, 0);
235 236
236 _body_material_set(box, 0, 1, 0); 237 _body_material_set(box, 0, 1, 0);
237 238