glsl moving light

Started by
2 comments, last by adam17 18 years, 8 months ago
i have a small test scene setup with 2 static lights in it. whenever i run the program with the glsl shaders i wrote, the highlighted area moves whenever i move. i know how to fix it in the ogl pipeline but im not sure how to do it in glsl. heres the code.

//vertex program
uniform sampler2D diffmap, lightmap, normalmap, specmap;

varying vec3 lightDir;
varying vec3 normal;

void main()
{	
	lightDir = normalize(vec3(gl_LightSource[0].position));
	normal = normalize(gl_Normal * gl_NormalMatrix);
	
	gl_Position = ftransform();	
	gl_TexCoord[0] = gl_MultiTexCoord0;
}


//fragment program
uniform sampler2D diffmap, lightmap, normalmap, specmap;

varying vec3 lightDir;
varying vec3 normal;

void main()
{							
	vec4 dmap = texture2D(diffmap, gl_TexCoord[0].st) * gl_FrontMaterial.diffuse;
	vec4 smap = texture2D(specmap, gl_TexCoord[0].st);
	//vec4 lmap = texture2D(lightmap, gl_TexCoord[0].st);
	//vec4 nmap = texture2D(normalmap, gl_TexCoord[0].st);
	
	float intensity = max(dot(normal, lightDir), 0.0);
	
	//vec4 matEmission = gl_FrontMaterial.emission;
	
	gl_FragColor = dmap * intensity;
}
i have a feeling the problem is in my vertex program but im not quite sure. when i disable the shader the ogl pipeline renders properly without the light moving.
Advertisement
You could try to replace this line

lightDir = normalize(vec3(gl_LightSource[0].position));

within your vertex shader with that:

vec4 ecPos=gl_ModelViewMatrix*gl_Vertex;vec3 aux=vec3(gl_LightSource[0].position-ecPos); lightDir=normalize(aux);


And make sure that LightSource[0] is the right one. I've spent a couple of hours trying to fix my shader because the light wasn't working right to find out that I've been using the wrong LightSource ;)

Greets

Chris
Try this:
vertex:
...
lightDir =vec3(gl_LightSource[0].position - gl_Vertex.xyz * gl_ModelViewMatrix);
...
fragment:
...
float att = light_radius * light_radius /(length(lightDir));
lightDir = normalize(lightDir);
float intensity = max(dot(normal, lightDir), 0.0)/att;

...

Not sure,but it works for me.
i tried those changes and for some reason the light still moves with me

This topic is closed to new replies.

Advertisement