Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

GLSL Normal Mapping

Started by weasalmongler May 25, 2009 at 6:03 AM 0 replies 2.6k views
Original Post
weasalmongler
weasalmongler
Hi all, I've been working on implementing normal mapping into my game and generally everything is ok, but there is a strange problem for some models which means the light direction is different. I.e. 90% of the models in the scene are lit from one direction, and the remaining 10% are lit from a light source 90 degrees rotated. All things should be lit from the same direction. I am using directional lights to light the scene. Light direction is passed in to the opengl light position. I have also drawn the tangent space basis vectors at every vertex and they look like they are being generated correctly. Here is my vertex and pixel shader:

varying vec3 vecLightDir;
varying vec4 vecColour;

uniform sampler2D tex0;

attribute vec3 tangent;
attribute vec3 bitangent;

void main ()
{
	gl_TexCoord[0] = gl_MultiTexCoord0;
	vecColour = gl_Color;	

	//Transform light vector into tangent space
	mat3 tangentspace = mat3(tangent, bitangent, gl_Normal);
	tangentspace = gl_NormalMatrix * tangentspace;

	vec3 templightdir = normalize(gl_LightSource[0].position.xyz);
	vecLightDir = templightdir * tangentspace;	//multiply by inverse rotmat (multiplied in other order = transposed)
	vecLightDir.xy = -vecLightDir.xy; //correct the normal direction

	//Do the transform
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}




varying vec3 vecLightDir;
varying vec4 vecColour;

uniform sampler2D tex0;
uniform sampler2D tex1;

void main ()
{
	vec3 N, halfV;
	float NdotL, NdotHV;
	vec4 color;
	
	N = normalize(texture2D(tex1, gl_TexCoord[0].st).rgb*2.0 - 1.0);
	NdotL = max(dot(normalize(vecLightDir), N), 0.0);
	
	color = clamp(vec4(0.2, 0.2, 0.2, 1.0) + vec4(0.8, 0.8, 0.8, 1.0) * NdotL, 0.0, 1.0)*vecColour;
	
	gl_FragColor = color * texture2D(tex0, gl_TexCoord[0].st);
}



Can anyone see anything wrong with my code? I'm not sure why it doesn't work. Thanks in advance.
zedz
zedz
this is very hacky
vecLightDir.xy = -vecLightDir.xy; //correct the normal direction

to solve stuff like this its best to do, something like

varying vec3 normal;
normal = gl_Normal;

gl_FragColor.xyz = vec3( normal * 0.5 + 0.5);

etc to see if youre supplying the shader with the correct info first

[Edited by - zedz on May 25, 2009 3:07:41 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.