Jump to content
  • Advertisement
Sign in to follow this  

GLSL Tangent Space

This topic is 3546 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I've made some more progress on the normal mapping shader. It now works for x-axis rotations, but not y-axis ones. The code is from some links people gave me: http://www.ozone3d.net/tutorials/bump_mapping_p3.php getting the tangent space like: http://www.ozone3d.net/tutorials/mesh_deformer_p2.php#tangent_space. What follows is my vertex shader:
varying vec3 lightVec; 
varying vec3 eyeVec;
varying vec2 texCoord;
attribute vec3 vTangent; 

void main(void)
    gl_Position = ftransform();
    texCoord = gl_MultiTexCoord0.xy;

    vec3 c1 = cross(gl_Normal, vec3(0.0, 0.0, 1.0)); 
    vec3 c2 = cross(gl_Normal, vec3(0.0, 1.0, 0.0)); 
        vTangent = c1;	
        vTangent = c2;	
    vTangent = normalize(vTangent);

    vec3 n = normalize(gl_NormalMatrix * gl_Normal);
    vec3 t = normalize(gl_NormalMatrix * vTangent);
    vec3 b = cross(n, t);

    vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);
    vec3 tmpVec = gl_LightSource[0].position.xyz - vVertex;

    lightVec.x = dot(tmpVec, t);
    lightVec.y = dot(tmpVec, b);
    lightVec.z = dot(tmpVec, n);

    tmpVec = -vVertex;
    eyeVec.x = dot(tmpVec, t);
    eyeVec.y = dot(tmpVec, b);
    eyeVec.z = dot(tmpVec, n);

Again, this only works when the object is rotated around the x (horizontal) axis. When rotated around the y-axis, it does not work, so the transformation did not take place correctly. Please help, -Geometrian

Share this post

Link to post
Share on other sites
I'm thinking I should just scrap all of this normal mapping code I've gotten so far, because the critical part of the normal mapping--transforming the normals or light--doesn't work for me, but everything else I can do.

Most of the normal mapping procedures I've seen require knowledge of the geometry and computations on the CPU to find the TBN matrix. They also transform the light's position, instead of the normals' position, which is obviously faster.

I want to make a normal mapping shader that uses resources entirely in the GPU; not on the CPU. It will work with no knowledge of adjacent geometry. I know this can be done--googling "normal map teapot" gives multiple hits showing a normal mapped teapot--the teapot is an inbuilt primitive, so no knowledge of the geometry is known, yet these pictures show the normal mapping executed correctly.

Similarly, I'm working from prebuilt primitives, such as the GLUT teapot, so this sort of normal mapping is essential.

What I'm looking for is a shader (preferably GLSL) which will transform the normals into object space so that they can be used. This calculation will be done in the vertex shader for every vertex with no knowledge of the surrounding geometry. The texture coordinates are known, and the tangent and bitangent vectors should be parallel to the texture's u and v axes. All of this shouldn't be too complicated, but I'm totally at a loss how to do it.

[EDIT: if this cannot be done for whatever reason--how is the TBN matrix calculated if knowledge of the surrounding geometry is not available? It is done somehow]

Any help of any kind would be gladly appreciated.

[Edited by - Geometrian on October 26, 2008 7:06:38 PM]

Share this post

Link to post
Share on other sites
Just for the record, most "teapot demos" use a loaded model, rather than the teapot data set to generate the surface/s. So make sure you know the background before jumping into conclusions about that sort of stuff :)

Share this post

Link to post
Share on other sites
I guess it could be done with geometry shader as long as you can get adjacency information. I think you need to supply your data as GL_TRIANGLES_ADJACENCY.
Anyway, you are saying that your use GLUT teapot, which isn't up to date so it probably uses GL_TRIANGLES or something.
Why don't you use some file format that can store tangents that are precomputed?
It is better than considered any other bull shit.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!