# Whats wrong with my bump-map shader?

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

## Recommended Posts

so I wrote a normal mapping shader in glsl but it doesn't appear to work, the normal map screws lighting up royally.

vertex:
 varying vec3 v; void main(void) { v = vec3(gl_ModelViewMatrix * gl_Vertex); gl_TexCoord[0] = gl_MultiTexCoord0; gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; } 

fragment:
 varying vec2 vTexCoord; varying vec3 v; uniform sampler2D Texture; uniform sampler2D normalTexture; void main(void) { vec3 normal = normalize(texture2D(normalTexture, gl_TexCoord[0].st).rgb * 2.0 - 1.0); normal = normalize (normal); vec3 lightpos = normalize(gl_LightSource[0].position.xyz - v); vec3 eyevec = normalize(-v); vec3 reflection = normalize(-reflect(lightpos, normal)); vec4 ambient, specular, diffuse; vec4 texcolor = texture2D(Texture, vec2(gl_TexCoord[0])); vec4 finalcolor; float L_Term = dot(normal, lightpos); ambient = (gl_FrontLightModelProduct.sceneColor * gl_FrontMaterial.ambient) + gl_FrontLightProduct[0].ambient * gl_FrontMaterial.ambient; if(L_Term > 0.0) { specular = gl_LightSource[0].specular * gl_FrontMaterial.specular * (pow( max(dot(reflection, eyevec), 0.0), gl_FrontMaterial.shininess )); diffuse = gl_FrontLightProduct[0].diffuse * gl_FrontMaterial.diffuse * max(dot(normal, lightpos), 0.0); finalcolor = (texcolor) * (ambient + specular + diffuse); } else { finalcolor = (texcolor) * ambient; } gl_FragColor = finalcolor; } 

so what did I do wrong?

##### Share on other sites
At the moment, your code is written to use world-space normal-maps. Is that intended?

You don't have vertex streams for normals/tangents/binormals, which means you're unable to properly decode a tangent-space normal map (the kind where flat areas a bluish).

Also, your calculation of eyevec seems suspect - this should be a vector between the pixel and the eye, but it seems as if you're getting a direction between the origin and the eye.

##### Share on other sites

At the moment, your code is written to use world-space normal-maps. Is that intended?

You don't have vertex streams for normals/tangents/binormals, which means you're unable to properly decode a tangent-space normal map (the kind where flat areas a bluish).

Also, your calculation of eyevec seems suspect - this should be a vector between the pixel and the eye, but it seems as if you're getting a direction between the origin and the eye.

Thanks

How can I calculate the tangent/binormal? Can I do this inside the shader? And also what would be the "correct" way to calculate eyevec

##### Share on other sites
you need to provide tangents
you can find binormal with cross(normal, tangent)

and eye vector is usually v_eye = normalize(-positon.xyz) * tbnMatrix; in the vertex shader
where position is the world-space position of vertices

youll have to do the same to v_light... v_light = normalize(lightposition.xyz) * tbn;
aka converting it to tangent / texture space

youll have to rotate normal, tangent and binormal, using gl_NormalMatrix
and normalize them, you can do that in the same line as you make the tbn matrix:

tbn matrix is: mat3 tbn = gl_NormalMatrix * mat3(tangent, binormal, normal);

notice how i multiply matrix AT THE END, instead of the usual at the start
its the same as transpose of tbn * vector

##### Share on other sites

you need to provide tangents
you can find binormal with cross(normal, tangent)

and eye vector is usually v_eye = normalize(-positon.xyz) * tbnMatrix; in the vertex shader
where position is the world-space position of vertices

youll have to do the same to v_light... v_light = normalize(lightposition.xyz) * tbn;
aka converting it to tangent / texture space

youll have to rotate normal, tangent and binormal, using gl_NormalMatrix
and normalize them, you can do that in the same line as you make the tbn matrix:

tbn matrix is: mat3 tbn = gl_NormalMatrix * mat3(tangent, binormal, normal);

notice how i multiply matrix AT THE END, instead of the usual at the start
its the same as transpose of tbn * vector

I read somewhere that I could find the tangent in the vertex shader like so:

 vec3 c1 = cross( gl_Normal, vec3(0.0, 0.0, 1.0) ); vec3 c2 = cross( gl_Normal, vec3(0.0, 1.0, 0.0) ); if( length(c1) > length(c2) ) { tangent = c1; } else { tangent = c2; } tangent = normalize(tangent); 

Is this sufficient to use in my bump map shader

##### Share on other sites
probably not, you most likely will have to provide tangents along with normals... using attributes
its probably best if you move from standard vertex struct to a user defined one, using vertexattrib and VAOs
if theres anything you need help with on that part i can post some of my code and compare it
openGL is hillariously undocumented, and more often than not i seem to be reading the specifications for each and every thing i have to use =)

either way, make sure you have tangents to start with and go from there
im sure someone can explain why trying to make tangents is a bad idea, but i dont know why...

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 14
• 30
• 9
• 16
• 12