• Advertisement
Sign in to follow this  

Rotating a light position

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


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

Share this post


Link to post
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 this post


Link to post
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 matrix
vec3 lightPos = {...}; //in object space

// first get OpenGL to generate our transformation matrix
glPushMatrix();
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement