Jump to content
  • Advertisement
Sign in to follow this  
ic0de

Whats wrong with my bump-map shader?

This topic is 2563 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

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

here's my shader :

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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...

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!