Jump to content
  • Advertisement
Sign in to follow this  
sssss

Problem with lighting GLSL

This topic is 3034 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

Here is my code, and it appears all dark

Vertex Shader

uniform vec4 Difusse;
uniform vec4 Specular;
uniform vec4 Ambient;
uniform vec4 lightDir;
uniform vec4 view_position;
uniform vec4 lightPos;
uniform float Ks,Ka,Kd;
varying vec3 normal;
varying vec4 color;

void main(void)
{
gl_Position = ftransform();
vec3 lightVector=lightPos.xyz-gl_Position.xyz;
float distances=length(lightVector);
lightVector=normalize(lightVector);
vec3 halfVector=normalize(lightVector+view_position.xyz);
normal=normalize(gl_NormalMatrix*gl_Normal);
float NdotLV=dot(lightDir.xyz,normal);
float NdotHV=dot(halfVector,normal);
float att=1/(Ks+Kd*distances+Ka*distances*distances);
color=NdotLV*Difusse*att+Ambient*att;

}


Fragment Shader


varying vec4 color;

void main(void)
{
gl_FragColor = vec4( color);
}


lightPos is -55.0,-55.0,-32.0
Kd is 0.8
Ka and Ks are 1.0

Share this post


Link to post
Share on other sites
Advertisement
1) You should comment your code.
2) You should put some debug info in your program (not the shaders) to help narrow the many many possibilities of what can go horribly wrong here.


Maybe one of your positional variables is putting the object or the view port in unexpected places.

Maybe you're mixing up global and local coordinates.

Maybe your normals are all messed up.

Maybe it is required that a vertex shader produces the color the normal way (by setting glColor or something, I don't remember the exact variable name); and that in your drivers take a short cut and not even run the fragment shader when the vertex shader produces no output.

Share this post


Link to post
Share on other sites
Are you sure this is compiled properly ?

Because you declare "varying vec3 normal" in your vertex shader but not in your fragment shader and I'm not sure it is allowed in glsl.

One more thing: I think you're confusing space: gl_Position doesn't give you the 3D world position of your vertex, but the position once transformed AND projected, so the way you compute your lightVector doesn't make sense.

I encourage you to read the lighting tutorial on lighthouse3d website which correspond exactly to what you want to do, and is very well detailed.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!