cedric pushed a commit to branch master. http://git.enlightenment.org/core/efl.git/commit/?id=b803714e406f5b447388640cefb2eeb4ca8decf7
commit b803714e406f5b447388640cefb2eeb4ca8decf7 Author: Bogdan Devichev <[email protected]> Date: Wed Apr 15 17:01:40 2015 +0200 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 <[email protected]> --- src/examples/evas/evas-3d-primitives.c | 112 +++++++++++++++++++++++++-------- 1 file changed, 87 insertions(+), 25 deletions(-) diff --git a/src/examples/evas/evas-3d-primitives.c b/src/examples/evas/evas-3d-primitives.c index f737002..d2297fa 100644 --- a/src/examples/evas/evas-3d-primitives.c +++ b/src/examples/evas/evas-3d-primitives.c @@ -278,45 +278,107 @@ void evas_3d_add_sphere_frame(Eo *mesh, int frame, int p, vec2 tex_scale) { int vcount, icount, vccount, i, j; - icount = p * p * 6; + unsigned short *index; + vccount = p + 1; vcount = vccount * vccount; + icount = p * p * 6; ALLOCATE_VERTEX_DATA - double dtheta, dfi, sinth, costh, fi, theta, sinfi, cosfi; - dtheta = M_PI / p; - dfi = 2 * M_PI / p; - - for (j = 0; j < vccount; j++) + /* Calculate vertices position of the sphere mesh by using + splitting of sphere by latitude and longitude. */ + for (i = 0; i <= p; i++) { - theta = j * dtheta; - sinth = sin(theta); - costh = cos(theta); - for (i = 0; i < vccount; i++) - { - fi = i * dfi; - sinfi = sin(fi); - cosfi = cos(fi); - normals[i + j * vccount].x = sinth * sinfi; - normals[i + j * vccount].y = costh; - normals[i + j * vccount].z = sinth * cosfi; + double lati, z, r, point_r; - vertices[i + j * vccount].x = normals[i + j * vccount].x / 2; - vertices[i + j * vccount].y = normals[i + j * vccount].y / 2; - vertices[i + j * vccount].z = normals[i + j * vccount].z / 2; + point_r = 0.00001;//non-zero little value for correct tangents calculation. - tangents[i + j * vccount].x = normals[i + j * vccount].z; - tangents[i + j * vccount].y = normals[i + j * vccount].y; - tangents[i + j * vccount].z = -normals[i + j * vccount].x; + lati = ((M_PI - 2 * point_r) * (double)i) / (double)p; + z = cos(lati + point_r); + r = fabs(sin(lati + point_r)); - tex_coord[i + j * vccount].x = i / (float)(vccount - 1) * tex_scale.x; - tex_coord[i + j *vccount].y = tex_scale.y - j / (float)(vccount - 1) * tex_scale.y; + for (j = 0; j <= p; j++) + { + double longi; + int num = (i * (p + 1)) + j; + + longi = (M_PI * 2.0 * (double)j) / (double)p; + + normals[num].x = r * sin(longi); + normals[num].y = r * cos(longi); + normals[num].z = z; + + vertices[num].x = normals[num].x / 2; + vertices[num].y = normals[num].y / 2; + vertices[num].z = normals[num].z / 2; + + if (vertices[num].x > 0.0) + { + tangents[num].x = -normals[num].z; + tangents[num].y = normals[num].y; + tangents[num].z = normals[num].x; + } + else + { + tangents[num].x = normals[num].z; + tangents[num].y = normals[num].y; + tangents[num].z = -normals[num].x; + } + + tex_coord[num].x = i / (float)(vccount - 1) * tex_scale.x; + tex_coord[num].y = tex_scale.y - j / (float)(vccount - 1) * tex_scale.y; } } _generate_grid_indices(indices, p); + /* Triangulation of sphere mesh in appliance with buffer of indices. */ + for (i = 0; i < icount; i += 3) + { + vec3 e1, e2; + float du1, du2, dv1, dv2, f; + vec3 tangent; + int num0, num1, num2; + + num0 = indices[i + 0]; + num1 = indices[i + 1]; + num2 = indices[i + 2]; + + e1.x = vertices[num1].x - vertices[num0].x; + e1.y = vertices[num1].y - vertices[num0].y; + e1.z = vertices[num1].z - vertices[num0].z; + + e2.x = vertices[num2].x - vertices[num0].x; + e2.y = vertices[num2].y - vertices[num0].y; + e2.z = vertices[num2].z - vertices[num0].z; + + du1 = tex_coord[num1].x - tex_coord[num0].x; + dv1 = tex_coord[num1].y - tex_coord[num0].y; + + du2 = tex_coord[num2].x - tex_coord[num0].x; + dv2 = tex_coord[num2].y - tex_coord[num0].y; + + f = 1.0 / ((du1 * dv2) - (du2 * dv1)); + + tangent.x = f * ((dv2 * e1.x) - (dv1 * e2.x)); + tangent.y = f * ((dv2 * e1.y) - (dv1 * e2.y)); + tangent.z = f * ((dv2 * e1.z) - (dv1 * e2.z)); + + tangents[num0] = tangent; + } + + /* Coupling between vertices by calculation of tangent parametr correct value. */ + for (i = 0; i <= p; i++) + { + for (j = 0; j <= p; j++) + { + if (j == p) + { + tangents[(i * (p + 1)) + j] = tangents[i * (p + 1)]; + } + } + } SET_VERTEX_DATA(frame) } --
