Jump to content

  • Log In with Google      Sign In   
  • Create Account

Whats wrong with my bump-map shader?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 ic0de   Members   -  Reputation: 909

Like
0Likes
Like

Posted 06 December 2011 - 10:40 PM

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?

you know you program too much when you start ending sentences with semicolons;


Sponsor:

#2 Hodgman   Moderators   -  Reputation: 31798

Like
0Likes
Like

Posted 06 December 2011 - 10:59 PM

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.

#3 ic0de   Members   -  Reputation: 909

Like
0Likes
Like

Posted 06 December 2011 - 11:19 PM

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

you know you program too much when you start ending sentences with semicolons;


#4 Kaptein   Prime Members   -  Reputation: 2179

Like
0Likes
Like

Posted 07 December 2011 - 09:59 AM

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

#5 ic0de   Members   -  Reputation: 909

Like
0Likes
Like

Posted 07 December 2011 - 02:59 PM

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

you know you program too much when you start ending sentences with semicolons;


#6 Kaptein   Prime Members   -  Reputation: 2179

Like
1Likes
Like

Posted 08 December 2011 - 11:51 AM

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...




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS