Ok so I’m trying to emulate the behaviour of a flashlight. For the time being I’m just trying to get a spotlight to show up on a sphere. Now the code below works, but it only works when the GL_SPOT_CUTOFF is set to 180 which creates a uniform distribution of light – not what I’m going for. If I try to set it to any other value then it just stops working. Here is the code
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_pos [4] = {5, 2, -10, 1.0};
glLightfv (GL_LIGHT0, GL_SPECULAR, mat_specular);
glLightfv (GL_LIGHT0, GL_POSITION, light_pos);
glLightf (GL_LIGHT0, GL_SPOT_CUTOFF, 20.0f);
glLightf (GL_LIGHT0, GL_SPOT_EXPONENT, 100.0);
GLfloat spotDir [] = {0.0, 0.0, 0.0};
glLightfv (GL_LIGHT0, GL_SPOT_DIRECTION, spotDir);
glutSolidSphere (2, 20, 20);
glFlush ();
glutSwapBuffers ();
I’ve enabled lighting and have done all the initialization stuff. All the surface normals are calculated from glutSolidSphere so don’t know what else could be failing…any ideas?
The problem is your
spotDir. It is a vector, not a point in your world coordinates.Try to change it to:
I am lazy, so I will not normalize this vector ;D
This will point your spotlight to the sphere center.
Hope it helps!