• 11
• 9
• 12
• 9
• 11

# GLSL Bump Map Question

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

## Recommended Posts

I'm teaching myself shaders (GLSL) with RenderMonkey. I'm trying to get my mind around Bump Maps but I'm having some issues. Two things 1. I cant figure out what Tangent is supposed to be. 2. I do not think I'm calculating HalfAngle correctly. I get an 'ok' output. It doesnt quite look right. The reason why I say I dont think I'm calculating HalfAngle correctly because when I rotate the cube the bumpmaping will 'disappear' from the face at certain angles

varying vec3 LightDir;
varying vec3 EyeDir;
varying vec3 HalfAngle;
uniform vec3 LightPosition;

attribute vec3 Tangent;

void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
EyeDir = vec3(gl_ModelViewMatrix * gl_Vertex);

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_MultiTexCoord1;

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

vec3 tl;
tl.x = dot(LightPosition, t);
tl.y = dot(LightPosition, b);
tl.z = dot(LightPosition, n);
LightDir = normalize(tl);

vec3 te;
te.x = dot(EyeDir, t);
te.y = dot(EyeDir, b);
te.z = dot(EyeDir, n);
EyeDir = normalize(te);

HalfAngle = normalize(tl + te);



##### Share on other sites
>>1. I cant figure out what Tangent is supposed to be.
2. I do not think I'm calculating HalfAngle correctly.

I get an 'ok' output. It doesnt quite look right. The reason why I say I dont think I'm calculating HalfAngle correctly <<

ild say its more likely to do with the fact that u dont know what the tangent as it is which is integral to bumpmapping, google 'bumpmapping opengl' or something theres plenty of info on the net, esp the nvidia site. getting bumpmapping going with glsl is basically really no different to anyother method.

also >>I do not think I'm calculating HalfAngle correctly.<< this is only necessary for specular, ie its not necessary for plain bumpmapping, get normal bumpmapping working first before u tackle this

##### Share on other sites
Well I re-read what I did and I still cant seem to see what Tangent is. Every example I look at is different from the next in terms of what they do.

  vec3 t  = vec3(0, gl_TexCoord[0].y, 0);   vec3 n  = vec3(  gl_Normal.x,   gl_Normal.y,   gl_Normal.z);   vec3 b = vec3(cross(t, n));

is what I have now.

##### Share on other sites
thats not a correct tangent (well it could be correct in some cases eg like a normal of vec3(0,0,1) is correct in some cases but normally it can be any direction)

to workout a triangles tangent matrix, u need to supply, 3 vertices,3 texture coordinates + the tris normal, do some calculations with these + then u have a 3x3 matrix. made up of the tangent, bitangent + normal vectors.
ie u want this tangent vector + not vec3 t = vec3(0, gl_TexCoord[0].y, 0); (i wish it was that easy but its not, it requires some math first)

(check the nvidia devleoper site for better info on the math behind bumpmapping)

##### Share on other sites
basically the bump map ( normal map ) is a set of normal vectors encoded into a texture using RGB values as vectors.
The texture is defined in local coordinates, much like the way a mesh is usually defined in its local coordinates. It is part of the 3D pipeline to transform a mesh into world coordinates before rendering, so it should not be too much of a problem to see how you need to transform the lighting vector(s) into normal map space.

imagine a light vector from (x,0,0) in the direction (-x,0,0), and a plane oriented arbitrarily in the beam of light.

if you rotate the entire plane until it's normal faces upwards (0,1,0) for positive y, and still apply the same lighting vector then the normal map will faulty if it is transformed back to normal after lighting since the light will appear to be coming from the wrong direction. Therefore you need to transform the light vector in the same way as the plane. This is logical since the normal map contains vector directions based on the "up" vector (0,1,0), or whatever type of coordinate system you are using.

the tangent and the binormal are created so that there is a matrix M = [ T B N ] such that a vector multiplied by it would then be in the space of the plane.

The tangent basically runs along the plane of the polygon - the binormal does too, and the normal faces away from it.

take the vectors and transform them via the matrix, and the lighting model looks correct.

##### Share on other sites
I think you should read the paper

MathematicsOfPerPixelLighting.pdf by Cass Everett

there is loads in there about well-behaved tangents and the possible artifacts.
basically the tangents and binormals should point in the same direction.

H = (L + V)/2

where V is viewer location and L is light direction.
[/edit]