Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am using GLSL version 1.20 with OpenGL 2.1 .

I am trying to compute when a fragment falls into the area of a spot light. I have already set all the light values with glLightfv and glLightf, also the material. This is how I initialize the spot direction and spot cutoff:

float spotCutoff= 5.0;
vector<GLfloat> spotDirection= vector<GLfloat>{0,0,0,1};
// I use [0,0,0,1] because the object is in the [0,0,0] position
// but I also have tried (in vain) many other values.
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spotDirection.data());
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,spotCutoff);

In the fragment shader I use a function to know if the fragment is inside this cone:

bool isRayInsideCone(vec3 Light)
{
    // Light is taken from the vertex shader and normalized.
    // It is exactly computed as normalize(lightPos - eyePos)
    // With lightPos= gl_LightSource[0].position.xyz ,
    // and eyePos= gl_ModelViewMatrix * gl_Vertex (re-normalized in 
    // the fragment shader).
    vec3 D= normalize(gl_LightSource[0].spotDirection);
    return dot(-Light,D) > gl_LightSource[0].spotCutoff;
}

But the problem is that this function returns false for every fragment. I can test this by just doing something like this:

if(isRayInsideCone(Light))
{
    gl_FragColor= some_color;
}
else
{
    gl_FragColor= other_color;
}

I just guess what I'm doing of wrong.

share|improve this question
1  
I don't know how glLightfwv works, but directions are usually non-zero vectors. Your vector {0,0,0,1} seems to have x,y,z zero. You can not normalize zero vector. If you compute a dot product with it, it will be zero. – Ivan Kuckir Feb 5 at 0:45

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.