summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBogdan Devichev <b.devichev@samsung.com>2015-04-15 17:01:40 +0200
committerCedric BAIL <cedric@osg.samsung.com>2015-05-07 09:53:08 +0200
commitb803714e406f5b447388640cefb2eeb4ca8decf7 (patch)
treea0f29906caec603111da08ede3f7d67471be8d96 /src
parent330159d2558b83f8018db09a37b530bac54711c9 (diff)
evas: sphere is rotated to standard axis and fix tangents of sphere for Evas_3D examples.
Reviewers: Hermet, raster, cedric Reviewed By: cedric Subscribers: cedric Differential Revision: https://phab.enlightenment.org/D2333 Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
Diffstat (limited to 'src')
-rw-r--r--src/examples/evas/evas-3d-primitives.c112
1 files changed, 87 insertions, 25 deletions
diff --git a/src/examples/evas/evas-3d-primitives.c b/src/examples/evas/evas-3d-primitives.c
index f7370024b0..d2297faae5 100644
--- a/src/examples/evas/evas-3d-primitives.c
+++ b/src/examples/evas/evas-3d-primitives.c
@@ -278,45 +278,107 @@ void
278evas_3d_add_sphere_frame(Eo *mesh, int frame, int p, vec2 tex_scale) 278evas_3d_add_sphere_frame(Eo *mesh, int frame, int p, vec2 tex_scale)
279{ 279{
280 int vcount, icount, vccount, i, j; 280 int vcount, icount, vccount, i, j;
281 icount = p * p * 6; 281 unsigned short *index;
282
282 vccount = p + 1; 283 vccount = p + 1;
283 vcount = vccount * vccount; 284 vcount = vccount * vccount;
285 icount = p * p * 6;
284 286
285 ALLOCATE_VERTEX_DATA 287 ALLOCATE_VERTEX_DATA
286 288
287 double dtheta, dfi, sinth, costh, fi, theta, sinfi, cosfi; 289 /* Calculate vertices position of the sphere mesh by using
288 dtheta = M_PI / p; 290 splitting of sphere by latitude and longitude. */
289 dfi = 2 * M_PI / p; 291 for (i = 0; i <= p; i++)
290
291 for (j = 0; j < vccount; j++)
292 { 292 {
293 theta = j * dtheta; 293 double lati, z, r, point_r;
294 sinth = sin(theta);
295 costh = cos(theta);
296 for (i = 0; i < vccount; i++)
297 {
298 fi = i * dfi;
299 sinfi = sin(fi);
300 cosfi = cos(fi);
301 normals[i + j * vccount].x = sinth * sinfi;
302 normals[i + j * vccount].y = costh;
303 normals[i + j * vccount].z = sinth * cosfi;
304 294
305 vertices[i + j * vccount].x = normals[i + j * vccount].x / 2; 295 point_r = 0.00001;//non-zero little value for correct tangents calculation.
306 vertices[i + j * vccount].y = normals[i + j * vccount].y / 2;
307 vertices[i + j * vccount].z = normals[i + j * vccount].z / 2;
308 296
309 tangents[i + j * vccount].x = normals[i + j * vccount].z; 297 lati = ((M_PI - 2 * point_r) * (double)i) / (double)p;
310 tangents[i + j * vccount].y = normals[i + j * vccount].y; 298 z = cos(lati + point_r);
311 tangents[i + j * vccount].z = -normals[i + j * vccount].x; 299 r = fabs(sin(lati + point_r));
312 300
313 tex_coord[i + j * vccount].x = i / (float)(vccount - 1) * tex_scale.x; 301 for (j = 0; j <= p; j++)
314 tex_coord[i + j *vccount].y = tex_scale.y - j / (float)(vccount - 1) * tex_scale.y; 302 {
303 double longi;
304 int num = (i * (p + 1)) + j;
305
306 longi = (M_PI * 2.0 * (double)j) / (double)p;
307
308 normals[num].x = r * sin(longi);
309 normals[num].y = r * cos(longi);
310 normals[num].z = z;
311
312 vertices[num].x = normals[num].x / 2;
313 vertices[num].y = normals[num].y / 2;
314 vertices[num].z = normals[num].z / 2;
315
316 if (vertices[num].x > 0.0)
317 {
318 tangents[num].x = -normals[num].z;
319 tangents[num].y = normals[num].y;
320 tangents[num].z = normals[num].x;
321 }
322 else
323 {
324 tangents[num].x = normals[num].z;
325 tangents[num].y = normals[num].y;
326 tangents[num].z = -normals[num].x;
327 }
328
329 tex_coord[num].x = i / (float)(vccount - 1) * tex_scale.x;
330 tex_coord[num].y = tex_scale.y - j / (float)(vccount - 1) * tex_scale.y;
315 } 331 }
316 } 332 }
317 333
318 _generate_grid_indices(indices, p); 334 _generate_grid_indices(indices, p);
319 335
336 /* Triangulation of sphere mesh in appliance with buffer of indices. */
337 for (i = 0; i < icount; i += 3)
338 {
339 vec3 e1, e2;
340 float du1, du2, dv1, dv2, f;
341 vec3 tangent;
342 int num0, num1, num2;
343
344 num0 = indices[i + 0];
345 num1 = indices[i + 1];
346 num2 = indices[i + 2];
347
348 e1.x = vertices[num1].x - vertices[num0].x;
349 e1.y = vertices[num1].y - vertices[num0].y;
350 e1.z = vertices[num1].z - vertices[num0].z;
351
352 e2.x = vertices[num2].x - vertices[num0].x;
353 e2.y = vertices[num2].y - vertices[num0].y;
354 e2.z = vertices[num2].z - vertices[num0].z;
355
356 du1 = tex_coord[num1].x - tex_coord[num0].x;
357 dv1 = tex_coord[num1].y - tex_coord[num0].y;
358
359 du2 = tex_coord[num2].x - tex_coord[num0].x;
360 dv2 = tex_coord[num2].y - tex_coord[num0].y;
361
362 f = 1.0 / ((du1 * dv2) - (du2 * dv1));
363
364 tangent.x = f * ((dv2 * e1.x) - (dv1 * e2.x));
365 tangent.y = f * ((dv2 * e1.y) - (dv1 * e2.y));
366 tangent.z = f * ((dv2 * e1.z) - (dv1 * e2.z));
367
368 tangents[num0] = tangent;
369 }
370
371 /* Coupling between vertices by calculation of tangent parametr correct value. */
372 for (i = 0; i <= p; i++)
373 {
374 for (j = 0; j <= p; j++)
375 {
376 if (j == p)
377 {
378 tangents[(i * (p + 1)) + j] = tangents[i * (p + 1)];
379 }
380 }
381 }
320 SET_VERTEX_DATA(frame) 382 SET_VERTEX_DATA(frame)
321} 383}
322 384