 # Sphere tangents

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);
Vec3f vertex2 = shape.vertex.at(triangle);
Vec3f vertex3 = shape.vertex.at(triangle);

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

Vec2f tex1 = shape.texture.at(triangle);
Vec2f tex2 = shape.texture.at(triangle);
Vec2f tex3 = shape.texture.at(triangle);

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

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

Vec3f tangent;

tangent = coef * ((v1 * st2)  + (v2 * -st1));
tangent = coef * ((v1 * st2)  + (v2 * -st1));
tangent = coef * ((v1 * st2)  + (v2 * -st1));

``````

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

With an analytic surface like a sphere, it is possible to calculate the tangents of a point on the sphere with differential calculus. You have to choose first a parametric representation 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.

Assuming this sphere is centered around (0,0,0), you could use also spherical coordinates (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.

Thanks, i think i found a good solution, taking yours comments 