Rotating a light position

Started by
3 comments, last by silvermace 19 years, 6 months ago
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
Advertisement
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.
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
How would I find the cubes transformation matrix?
or the translated and rotated modelview matrix?
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.
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
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website

This topic is closed to new replies.

Advertisement