# Rotating a light position

This topic is 5386 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a cube with a spotlight attatched to the bottom of it. The spotlight shines on anything thats directly below it. Problem is, this cube is mobile. When I translate or rotate the cube the light must stay with it. Now I know how to keep the light with the cube during a translation, but I'm not sure how to keep the light with the cube after a rotation (using glRotatef() ) Is there a way to get the cube coordinates after a glRotate() is performed on it? Or better yet, is there a way to rotate a point light source using glRotate()? Thanks

##### Share on other sites
why dont you multiply the lights position vector by the translated and rotated modelview matrix?

EDIT: or better yet, by the cube's transform matrix.

##### Share on other sites
How would I find the cubes transformation matrix?
or the translated and rotated modelview matrix?

##### Share on other sites
Actually, is there a way I can pull the cube coordinates from the modelview matrix? that would really be good, then I can just set
the position of the light that way.

##### Share on other sites
i think you should create your own matrix, its pretty easy using some lazy-ass openGL calls ;)

you'll need a mmult (matrix mult) function for matrix times vector.

float m[16]; //our transform matrixvec3 lightPos = {...}; //in object space// first get OpenGL to generate our transformation matrixglPushMatrix();glLoadIdentity();  glTranslatef( cube.x, cube.y, cube.z );  glRotatef( cube.angleX, 1, 0, 0 ); // just for x axis ATM.   // at this stage, OpenGL has done all the matrix math for us!  glGetFloatv( GL_MODELVIEW_MATRIX, &m );glPopMatrix();  glPushMatrix();    lightPos = mmult(lightPos, m);    glLightfv( GL_LIGHT0, GL_POSITION, &lightPos[0] );    glMultMatrixf(m);    drawCube();  glPopMatrix();
anyway, something like that :D

• 21
• 13
• 9
• 17
• 13