Jump to content
  • Advertisement
Sign in to follow this  
TraxMate

OpenGL Spotlight doesn't work (GLSL)

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

Hi I'm trying to write a spotlight shader using the code from OpenGL Shading Language 3rd edition as a reference but it doesn't work. Everything becomes completely black, the ambient colors isn't even there and I don't know why it won't work (I have no problems with point lights).

Here's my shader code (no specular values yet):
void Spotlight(int i, out vec4 Ambient, out vec4 Diffuse)
{
vec3 LightDir = Lights.Position.xyz - EyePosition;
float Distance = length(LightDir);
LightDir = normalize(LightDir);

float Att = 1.0 / (Lights.ConstAtt +
Lights.LinearAtt * Distance +
Lights.QuadraticAtt * Distance * Distance);

float SpotAtt = 0.0;
float SpotDot = dot(-LightDir, normalize(vec3(Lights.SpotDirection)));
if(SpotDot > Lights.SpotCosCutoff)
SpotAtt = pow(SpotDot, Lights.SpotExponent);

Att *= SpotAtt;
float Lambert = max(0.0, dot(EyeNormal, LightDir));

Ambient += Lights.Ambient * Att;
Diffuse += Lights.Diffuse * Lambert * Att;
}


And these are the values I pass as uniforms:
Light.Position = Vector4(0.0, 10.0, 0.0, 1.0) * Camera.GetCameraMatrix();
Light.Ambient = Vector4(1.0f);
Light.Diffuse = Vector4(1.0f);
Light.Specular = Vector4(1.0f);
Light.ConstAtt = 1.0f;
Light.LinearAtt = 0.0f;
Light.QuadraticAtt = 0.0f;

Light.SpotDirection = Vector4(0.0, -10.0, 0.0, 1.0);
Light.SpotCosCutoff = 45;
Light.SpotExponent = 30;

I'm trying to have a spotlight at position (0, 10, 0) to shine down at the floor (0, -10, 0), but as I said earlier it's just black.

Share this post


Link to post
Share on other sites
Advertisement
Your formula for calculating the Lambertian term looks strange... but it doesn't make scene why even ambient term is not working.

For debugging your program,

1. you may firstly comment out all the things that looks complex, for example, every thing except the ambient light and remove the attenuation term.
2. If it still doesn't works, check if your uniforms are passed to your shader correctly.

Share this post


Link to post
Share on other sites

Your formula for calculating the Lambertian term looks strange... but it doesn't make scene why even ambient term is not working.

For debugging your program,

1. you may firstly comment out all the things that looks complex, for example, every thing except the ambient light and remove the attenuation term.
2. If it still doesn't works, check if your uniforms are passed to your shader correctly.


Why does the lambertian term look strange? Anyways I checked to see if all values are being passed and for some reason the SpotExponent float is always 0 :s. I'm using uniform buffers to pass the data and the light struct is exactly the same in both the shader and C++ code.

Removing everything that has to do with spotlight works but then it's just a regular point light...

EDIT: I rearranged the members position inside the struct and now SpotExponent isn't 0 anymore but I get very strange results instead (image here)

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!