PDA

View Full Version : Spot Lights



lara
11-11-2002, 07:41 AM
Hi,
following is an example from the red book, which talks about lighting a shpere with a directional light and a spot light.
But I dont see the spot light. Is it because the parameters are not set right, or it is masked by the first directional light?
lara
void init(void)
{
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
GLfloat light1_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
GLfloat light1_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light1_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light1_position[] = { -2.0, 2.0, 1.0, 1.0 };
GLfloat spot_direction[] = { -1.0, -1.0, 0.0 };

glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5);
glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5);
glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2);

glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 45.0);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction);
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0);

}

void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere (1.0, 20, 16);
glFlush ();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho (-1.5*(GLfloat)w/(GLfloat)h,
1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

yakuza
11-11-2002, 08:21 AM
It looks like you set up the spotlight as Light1, but you never enable it, you only enable Light0

try: glEnable(GL_LIGHT1);

lara
11-11-2002, 08:27 AM
Oh, I probably forgot to add that on the there..but I do enable it..it still does not show....is there anything i can do, so that i can distinclty make out the effect of a spot light

11-11-2002, 09:29 AM
You can't see openGL lights, they just specify how polygons are shaded. To make a ligh like you are thinking you probably need a volumetric effect to see the light beams, and some sort of geometry to look like a light.

lara
11-11-2002, 09:33 AM
I think i did not state my question right this time...of course i understand that you cannot see a spot light..and it only helps in shading etc.
but atleast one should be able to see the shading on there...i just cannot make out any difference even after adding the light1 in the code. In other words, if I just comment out the light1, and only have light0 in my code, it still gives the same effect.
I expected the spot light to light up some are on the screen...
Lara

[This message has been edited by lara (edited 11-11-2002).]

Deiussum
11-11-2002, 10:51 AM
OpenGL lights are calculated on a vertex by vertex basis. So if you have a big quad, with just 4 vertices, and a spotlight that shines right in the middle, none of the vertices get lit by the spotlight, and you effectively won't see it. If you break that quad into a bunch of smaller quads, you'll be able to see the spotlight effect better. Perhaps you're running into something similar?