Sign in to follow this  
endfx

Rotating a light position

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this