You are using an out of date browser. It may not display this or other websites correctly.

You should upgrade or use an alternative browser.

You should upgrade or use an alternative browser.

- Thread starter NitroGL
- Start date

If you have only the texture coords it'll be harder to calculate the tangent space vectors, but shouldn't be too hard either I guess (haven't tried though).

Otherwise, for simple demos like my Volumetric lighting and stencilshadows demos I only support quads, which makes the problem trivial, you only need to normalize the vector from the vertex 0 to vertex 1 to get the vector in the S direction, similarily from vertex 0 to 3 for the T direction. I suppose you know how to get the normal too

I already know how to calculate them, I was just wondering how you do it.

This is how I do it:

v0.x=vertex1.x-vertex0.x;

v0.y=vertex1.y-vertex0.y;

v0.z=vertex1.z-vertex0.z;

v0.w=vertex1.t-vertex0.t;

v1.x=vertex2.x-vertex0.x;

v1.y=vertex2.y-vertex0.y;

v1.z=vertex2.z-vertex0.z;

v1.w=vertex2.t-vertex0.t;

tangent.x=-(v0.w*v1.x-v0.x*v1.w);

tangent.y=-(v0.w*v1.y-v0.y*v1.w);

tangent.z=-(v0.w*v1.z-v0.z*v1.w);

It doesn't always get a correct tangent vector, but it's easier to use with different vertex layouts.

I've done a vertex class that allows you to write most vector operations as you'd do when expressing it mathematically.

If I were to do someting similer I'd do

Vertex v0,v1,vertex0, vertex1, tangent;

v0 = vertex1 - vertex0;

v1 = vertex2 - vertex0;

tangent = cross(v0, v1);

Just curious... Is there a benefit in precalculating the tangent vectors?..

I'm calculating them on the fly in the vertex shader, by using a vector from the vertex to the light position ( I need to calculate it anyway ) crossed with the world UP vector, I then recalculate the up vector by crossing the vertex normal and the tangent vector. It seems to work fine this way.. so far. It reduces the vertex storage costs and doesn't require any extra cycles over the method you guys are using. What's your opinions on this method?

I'm calculating them on the fly in the vertex shader, by using a vector from the vertex to the light position ( I need to calculate it anyway ) crossed with the world UP vector, I then recalculate the up vector by crossing the vertex normal and the tangent vector. It seems to work fine this way.. so far. It reduces the vertex storage costs and doesn't require any extra cycles over the method you guys are using. What's your opinions on this method?

Last edited: