Lighting problem

Okay I have a fairly standard bump-mapping shader. Everything works as it should with one big exception. The light position is totally incorrect to what is specified with glLightfv().
No matter what I specify, the light position reflected by the final output of the shader seems “locked” to one position. I’ve even tried using a manually specified vec3(), instead of the built-in gl_LightSource[x], that didn’t work either.

Here is an example with the shader turned off:

and here it is with it turned on:

no matter what I do I cannot get any other light position than what you see in the above image. I’ve been racking my brain for so long. I know I must be missing something key.

Here is my vertex shader:

varying	vec3 g_lightVec;
varying	vec3 g_viewVec;
varying vec2 texCoord;

void main()
{
	gl_Position = ftransform();
	texCoord = gl_MultiTexCoord0.xy;

	vec3 tangent;
	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);
	
	vec3 n = normalize(gl_NormalMatrix * gl_Normal);
	vec3 t = normalize(gl_NormalMatrix * tangent);
	vec3 b = cross(n,t);
	
	vec3 vVert = (gl_ModelViewMatrix * gl_Vertex);
	vec3 tVec = gl_LightSource[0].position.xyz - vVert;
	
	g_lightVec.x = dot(tVec,t);
	g_lightVec.y = dot(tVec,b);
	g_lightVec.z = dot(tVec,n);
	
	tVec = -vVert;
	g_viewVec.x = dot(tVec,t);
	g_viewVec.y = dot(tVec,b);
	g_viewVec.z = dot(tVec,n);

}
 

and here is how I setup my light (it’s a directional light):


float ambientLight0[] = { 0.9f, 0.9f, 0.9f, 1.0f };
float diffuseLight0[] = { 0.9f, 0.7f, 0.45f, 1.0f };
float specularLight0[] = { 0.4f, 0.4f, 0.4f, 1.0f };
float emissiveLight0[] = {0.5f,0.5f,0.5f,1.0f};
float position0[] = { 0.0f, 8.0f, -1.0f, 0.0f };
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight0);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight0);
glLightfv(GL_LIGHT0, GL_EMISSION, emissiveLight0);
glLightfv(GL_LIGHT0, GL_POSITION, position0);

so what am I missing here?

Pixel shader, please? You do call glLightfv after view transform only, don’t you?

right. here’s the pixel shader.


uniform sampler2D NormalHeight;
uniform sampler2D Base;
uniform sampler2D SpecularMap;

uniform float invRad;

varying	vec3 g_lightVec;
varying	vec3 g_viewVec;
varying vec2 texCoord;

void main()
{
		float dSqr = dot(g_lightVec,g_lightVec);
		float att = clamp(1.0-invRad*sqrt(dSqr),0.0,1.0);
		vec3 lVec = g_lightVec * inversesqrt(dSqr);
		vec3 vVec = normalize(g_viewVec);
		vec4 base = texture2D(Base,texCoord);
		vec3 bump = normalize(texture2D(NormalHeight, texCoord).xyz * 2.0-1.0);
		vec4 vAmbient = gl_LightSource[0].ambient * gl_FrontMaterial.ambient;
		float diffuse = max(dot(lVec, bump), 0.0);
		vec4 vDiffuse = gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse * diffuse;
		vec4 specColor = texture2D(SpecularMap,texCoord);
		float specular = pow(clamp(dot(reflect(-lVec,bump),vVec),0.0,1.0),16.0); // change 16 to the material shininess value gl_FrontMaterial.shininess
		vec4 vSpecular = gl_LightSource[0].specular * gl_FrontMaterial.specular * specular * specColor;
		vSpecular.a = 1.0;
		
		gl_FragColor = (vAmbient*base+vDiffuse*base+vSpecular)*att;
				
}

I call glLightfv, in my engine’s init() function, so before any individual object transformations. Is that maybe where I’m going wrong? Do I need to set the position every CPU cycle? Literature I’ve read suggests you only need to do this if your light source is going to move.

Anybody have any suggestions?

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.