Jump to content
  • Advertisement
Sign in to follow this  
adam17

glsl moving light

This topic is 4842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!