i am having a little trouble with my per-pixel lighting under GLSL. below is a pic of the problem.
OpenGL fixed Function Lighting
GLSL Lighting
vert shader:
varying vec4 diffuse,ambient;
varying vec3 normal,lightDir,halfVector;
varying vec2 Texcoord;
void main()
{
normal = normalize(gl_NormalMatrix * gl_Normal)* -1.0;
lightDir = normalize(vec3(gl_LightSource[0].position));
halfVector = normalize(gl_LightSource[0].halfVector.xyz);
diffuse = gl_LightSource[0].diffuse;
ambient = gl_LightSource[0].ambient;
gl_Position = ftransform();
Texcoord = gl_MultiTexCoord0.xy;
}
frag shder:
uniform sampler2D baseMap;
varying vec4 diffuse,ambient;
varying vec3 normal,lightDir,halfVector;
varying vec2 Texcoord;
void main()
{
vec3 n,halfV,viewV,ldir;
float NdotL,NdotHV;
vec4 color = ambient;
vec4 tex = texture2D( baseMap, Texcoord );
n = normalize(normal);
NdotL = max(dot(n,lightDir),0.0);
if (NdotL > 0.0) {
halfV = normalize(halfVector);
NdotHV = max(dot(n,halfV),0.0);
color += diffuse * NdotL;
}
gl_FragColor = tex + color;
}
code to set up lighting
glEnable(GL_LIGHTING);
glEnable( GL_LIGHT0 );
glShadeModel(GL_SMOOTH);
GLfloat Ambient[4];
GLfloat diffuse_light0[] = { 1.0f, 1.0f, 1.0f, 1.0f };
Ambient[0] = 192.0f / 255.0f;
Ambient[1] = 192.0f / 255.0f;
Ambient[2] = 192.0f / 255.0f;
Ambient[3] = 1.0f;
glLightfv( GL_LIGHT0, GL_AMBIENT, Ambient);
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse_light0);
every frame i update the light's position with(i have a moving camera):
GLfloat position_light0[] = {1.0f,1.0f,1.0f,0};
glLightfv( GL_LIGHT0, GL_POSITION,position_light0 );
as you can see from the first picture the lighting works just fine when i disable the shader but when i enable the shader(pic 2) it's just all bright and wrong. This shader is from lighthouse tut's. anyone notice what i am doing wrong??