PDA

View Full Version : can a spotlight revolve around an object??

07-08-2001, 04:08 PM
is there away to make a spotlight revolve around a sphere? How would i do this

DFrey
07-08-2001, 04:21 PM
You do it just like you'd make any other thing revolve around the sphere. Remember that the light position is transformed by the modelview matrix, and the spot direction is transformed by the inverse modelview matrix.

07-08-2001, 05:10 PM
what do you mean??

Bob
07-09-2001, 07:06 AM
You cannot tell OpenGL to rotate the light about an object or any other point, you must rotate it yourself.

For each frame, you need to recalculate the position and direction of the spotlight, and report it to OpenGL before drawing.

07-09-2001, 08:19 AM
could it work even if i put it in a push/pop matrix

DFrey
07-09-2001, 08:58 AM
Here's an example of a spotlight revolving around a sphere:

#include <gl/glut.h>

int speed = 2;
float rotl;

float eye[] = {0.0f, 0.0f, 6.0f};
float LightAmbient[]= { 0.2f, 0.2f, 0.2f, 1.0f };
float LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
float LightPosition[]= { 0.0f, 0.0f, 3.0f, 1.0f };
float LightDirection[]= { 0.0f, 0.0f, -1.0f, 1.0f };

void glutResize(int width, int height)
{
glMatrixMode(GL_PROJECTION);
gluPerspective(55.0, (float)width/(float)height, 1.0, 300.0);
glMatrixMode(GL_MODELVIEW);
}

void glutKeyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
case 'f':

case 'a':
case 'A':
speed++;
return;
case 'z':
case 'Z':
speed--;
return;
}
}

void glutDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glTranslatef (-eye[0], -eye[1], -eye[2]);

glPushMatrix();

glRotatef(rotl, 1.0f, 0.0f, 0.0f);
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION,LightDirection);

glPopMatrix();

rotl += speed;
if(rotl<0)
rotl+=360;
else
if(rotl>360)
rotl-=360;

glColor3f(0.5f, 0, 1);

gluSphere(sphere,2.0f,128,128);

glPopMatrix();

glutSwapBuffers();
}

int main(int argc, char *argv[])
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(400, 300);
glutInitWindowPosition(100, 100);
glutCreateWindow("Revolving Light demo");

glutReshapeFunc(glutResize);
glutDisplayFunc(glutDisplay);
glutIdleFunc(glutDisplay);
glutKeyboardFunc(glutKeyboard);

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT1);
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 20.0);

glEnable(GL_COLOR_MATERIAL);

glClearColor(0.5, 0.4, 0.0, 1.0f);

glutMainLoop();

return 0;
}

<edit> Some bugs were in the original code

[This message has been edited by DFrey (edited 07-09-2001).]