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.

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:

    gl_FragColor= some_color;
    gl_FragColor= other_color;

I just guess what I'm doing of wrong.

  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Ivan Kuckir Feb 5 '13 at 0:45

As stated by Iva Kuckir You are passing an invalid spot direction.

The direction [0,0,0] is no direction, you need to set it to some other value. For example[1,0,0] in which case your spotlight is facing the positive X axis.

This is basically because you cant normalize a zero-vector.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.