Hey Humus....

NitroGL

Active member
I'm just wondering... How are you calculating the tangent space basis for your bump mapping demos?
 
In for instance my GameEngine demo I get the tangent space vectors from the .t3d file exported from the Unreal editor. This format doesn't give you any texture coordinates, instead you get those vectors and is supposed to calculate the texture coords yourself, which isn't hard to do anyway. Very handy for bumpmapping :)
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 :)
 
Ah ok.

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.
 
Oki. Just curious, you haven't thought about implementing a vertex/vector class, with overloaded operators for +,-,* etc? It all gets much easier that way.

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);
 
Yeah, I've thought of doing that, but I don't use C++ in my demos yet.

I'll probably do that if/when I switch to C++ (there are various reasons I'm not right now).
 
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?
 
Last edited:
Back
Top