PDA

View Full Version : Sphere tangents



SimoPI
03-04-2010, 06:27 PM
Hi, i hope is the right section.

I need to know how to compute sphere tangents. For the normals i saw i can approximate them with vertices coordinates.

But for tangents i don't find nothing. I did this:



Vec3s triangle = shape.triangle.at(k);
Vec3f vertex1 = shape.vertex.at(triangle[0]);
Vec3f vertex2 = shape.vertex.at(triangle[1]);
Vec3f vertex3 = shape.vertex.at(triangle[2]);

Vec3f v1 = vertex3-vertex1;
Vec3f v2 = vertex2-vertex1;


Vec2f tex1 = shape.texture.at(triangle[0]);
Vec2f tex2 = shape.texture.at(triangle[1]);
Vec2f tex3 = shape.texture.at(triangle[2]);

Vec2f st1 = tex3-tex1;
Vec2f st2 = tex2-tex1;

float coef = 1/ (st1[0] * st2[1] - st2[0] * st1[1]);

Vec3f tangent;

tangent[0] = coef * ((v1[0] * st2[1]) + (v2[0] * -st1[1]));
tangent[1] = coef * ((v1[1] * st2[1]) + (v2[1] * -st1[1]));
tangent[2] = coef * ((v1[2] * st2[1]) + (v2[2] * -st1[1]));



It works for 3d models but not for a sphere...

trinitrotoluene
03-04-2010, 08:35 PM
With an analytic surface like a sphere (http://en.wikipedia.org/wiki/Sphere), it is possible to calculate the tangents of a point on the sphere with differential calculus (http://en.wikipedia.org/wiki/Differential_calculus). You have to choose first a parametric representation (http://en.wikipedia.org/wiki/Parametric_equation) of the sphere S(u,v) = [ f(u,v) i,g(u,v) j,h(u,v) k ] (not unique). Then calculate the partial derivative of the parametric representation for u and v and you have your 2 tangent vectors.

But be careful, some parametric representation of a sphere (like the one given in the above link) can give tangent vector of length 0 at different place on the sphere, so choose another one.

Stanley L
03-05-2010, 01:47 PM
Assuming this sphere is centered around (0,0,0), you could use also spherical coordinates (http://en.wikipedia.org/wiki/Spherical_coordinate_system) (r, theta, phi) and just add a quarter turn to theta or phi to get a unit-length vector tangent to the original point.


r = sqrt(x^2 + y^2 + z^2)
theta = acos(z/r)
phi = atan2(y,x)

then add pi/2 to theta or phi

tangent.x = sin(theta)cos(phi)
tangent.y = sin(theta)sin(phi)
tangent.z = cos(theta)

trinitrotoluene
03-05-2010, 06:11 PM
Assuming this sphere is centered around (0,0,0), you could use also spherical coordinates (http://en.wikipedia.org/wiki/Spherical_coordinate_system) (r, theta, phi) and just add a quarter turn to theta or phi to get a unit-length vector tangent to the original point.


r = sqrt(x^2 + y^2 + z^2)
theta = acos(z/r)
phi = atan2(y,x)

then add pi/2 to theta or phi

tangent.x = sin(theta)cos(phi)
tangent.y = sin(theta)sin(phi)
tangent.z = cos(theta)


Sometimes it seem this will give the wrong tangents.

Example: for theta = 0 and phi = 0 this give the point (0,0,1) on the sphere. So the first tangent is



theta = pi/2
phi = 0
tangent.x = sin(pi/2)*cos(0) = 1
tangent.y = sin(pi/2)*sin(0) = 0
tangent.z = cos(pi/2) = 0

The vector [1,0,0] which is a valid tangent for the point (0,0,1) for a sphere.

But for the second tangent give


theta = 0
phi = pi/2
tangent.x = sin(0)*cos(pi/2) = 0
tangent.y = sin(0)*sin(pi/2) = 0
tangent.z = cos(0) = 1


The vector [0,0,1] is not a valid tangent for the point (0,0,1)

If I use theta = pi/2 and phi = pi/2 this give a second valid tangent vector [0,1,0] but now this doesn't work for retrieving the tangents for the point (0,1,0) on the sphere.

So you need at least to check for all possibilities [(theta,phi+pi/2);(theta+pi/2,phi);(theta+pi/2),phi+pi/2)] and verify the dot product of the resulting vector with the normal of the sphere. If it is 0 then the resulting vector is a valid tangent. But maybe it exist a point on the sphere where this algorithm can give false tangents for all the combinaisons or two identics tangents.

SimoPI
03-06-2010, 05:13 PM
Thanks, i think i found a good solution, taking yours comments :)