Bump Mapping Problem

Started by
3 comments, last by Achilleos 16 years, 2 months ago
Hi, i am currently working on a normal bump shader for my game engine. The bump effect seems to be dependend on the model view matrix... but i dont know why. The shader are very simple and the tangent calculation should be correct. To better discribe the problem i have recorded a short video clip (xvid) which can be downloaded here. Ignore the error messages at the bottom of the window, i just do no bump mapping in the ground shader... so the uniforms are unused. The video shows some interaction and the wrong bump mapping effect. In the model chooser window on the left side the tangents (light blue) and the normals (yellow) of the torus are shown. And finally, here ist the shader pair: Vertex Shader

attribute vec3 Tangent;
varying vec3 LightDir;

void main()
{
  //Put the vertex in the position passed
  gl_Position = ftransform(); 
  
  // Construct a 3x3 matrix from the geometry’s inverse tangent, binormal, and normal
  vec3 normal = normalize(gl_NormalMatrix * gl_Normal);
  vec3 tangent = normalize(gl_NormalMatrix * Tangent);
  vec3 binormal = cross(normal, tangent);
  mat3 TBN = mat3(tangent, binormal, normal);
  
  //Rotate the light into tangent space
  vec4 ecposition = gl_ModelViewMatrix * gl_Vertex;
  vec3 lightDirection = gl_LightSource[0].position.xyz - ecposition.xyz;
  LightDir = TBN * normalize(lightDirection);
  
  //Use the first set of texture coordinates in the fragment shader 
  gl_TexCoord[0] = gl_MultiTexCoord0;
}

Fragment Shader

uniform sampler2D DiffuseMap;
uniform sampler2D NormalMap;
varying vec3 LightDir;

void main() 
{  
  //Get the color of the bump-map
  vec3 BumpNormal = vec3(texture2D(NormalMap, gl_TexCoord[0].xy));
  
  //Expand the bump-map into a normalized signed vector
  BumpNormal = (BumpNormal -0.5) * 2.0;
  
  //Find the dot product between the light direction and the normal
  float NdotL = max(dot(BumpNormal, LightDir), 0.0);
  
  //Calculate the final color gl_FragColor
  vec4 Idiff = gl_FrontLightProduct[0].diffuse * NdotL;
  vec4 Iamb = gl_FrontLightProduct[0].ambient;
  vec4 Ifog = gl_FrontLightProduct[0].diffuse;
  
  vec4 finalColor = max(Iamb, Idiff) * texture2D(DiffuseMap, gl_TexCoord[0].xy);
  float fogDistance = pow(gl_FragCoord.z, 128.0) - 0.5;
  gl_FragColor = mix(finalColor, Ifog * gl_Fog.color, clamp(fogDistance, 0.0, 1.0));
}

Please help...
Advertisement
You may want to check what space you want to be in, world, object, ect...

and
change
vec3 ecposition = vec3(gl_ModelViewMatrix * gl_Vertex);

//check this here for correct space
vec3 lightDirection = gl_LightSource[0].position.xyz - ecposition.xyz;


LightDir = normalize(lightDirection) * TBN;

Quote:Original post by MARS_999
LightDir = normalize(lightDirection) * TBN;


Thanks for your help! I visited the site every 10 minutes to check if anyone had an answer for me... and finally, just before i lost the hope...
The first bug in the setup (seems that there are more than one) is solved with changing the order of lightDir and TBN multiplication. Now the effect is no longer relativ to the cam position or object rotation.

One bug is left. The bump direction seem to change over the surface.
Maybe this is caused by using t-tangents instead s-tangents or something similar. Here is a picture of the current result on the left and the reference image from this tutorial on the right. I will try to change vertex order for tangent calculation when i am back home.

wrong bump direction

Thanks so far!
hmm.. just switching the binormal and tangents in the matrix is equal to changing the s- and t-tangent and changes does not solve the problem. as well as inverting them...

any ideas?
Solved.
I never bound the vertex attribute...

This topic is closed to new replies.

Advertisement