Sign in to follow this  
Inian

OpenGL Camera Matrix (Eye matrix)

Recommended Posts

Hello everyone. Currently I'm working on a FPS camera in OpenGL and I'm having some issues. First I was using gluLookAt() to set my camera but I recently realized that I need the matrix it created for my shaders (to move between spaces). So I changed it and I'm now using a 4x4 matrix that I create myself and set up and load with glLoadMatrixf(). But this also gave me a problem, it seems that now when I try to rotate my camera, it seems to rotate around the centre of my scene and not around itself. If I remove the translation part of the matrix and just use the rotation part it works fine and I can add the translation after with glTranslatef(), but then I don't get the all so usefull matrix for my shaders since the translation part won't be in it. So my question/problem is, how can I get a matrix that has the translation part and rotation part yet behaves like my code currently does? Any help will be appreciated. Thanks in advance. PS. I'm using a math lib from paulsprojects for matrixes and vectors. viewmat is a member variable. DS. Code as follows: void Camera::SetCamera() { VECTOR3D upvec; upvec.x = 0.0; upvec.y = 1.0; upvec.z = 0.0; look.Normalize(); VECTOR3D sidevec; sidevec = look.CrossProduct(upvec); sidevec.Normalize(); upvec = sidevec.CrossProduct(look); upvec.Normalize(); viewmat.SetEntry(0, sidevec.x); viewmat.SetEntry(4, sidevec.y); viewmat.SetEntry(8, sidevec.z); viewmat.SetEntry(1, upvec.x); viewmat.SetEntry(5, upvec.y); viewmat.SetEntry(9, upvec.z); viewmat.SetEntry(2, -look.x); viewmat.SetEntry(6, -look.y); viewmat.SetEntry(10, -look.z); //viewmat.SetEntry(12, -pos.x); //viewmat.SetEntry(13, -pos.y); //viewmat.SetEntry(14, -pos.z);//not working as I want it to.. //VECTOR3D transvec(-pos.x,-pos.y,-pos.z); //viewmat.SetTranslationPart(transvec);//same as above.. glLoadMatrixf(viewmat); glTranslatef(-pos.x,-pos.y,-pos.z);//move the camera after to rotation.. }

Share this post


Link to post
Share on other sites
The equation for creating the OpenGL view matrix from the local camera-space matrix looks like this:



You have the RT part correct for rotation, but the translation part is computed using -RTd. So you're just forgetting to pre-multiply by RT. The side effect is that the camera rotates in the world frame instead of the local frame.

Share this post


Link to post
Share on other sites
Try:
viewmat.SetEntry(12, Dot(-pos,sideVec));
viewmat.SetEntry(13, Dot(-pos,upVec));
viewmat.SetEntry(14, Dot(-pos,-look));

//VECTOR3D transvec(-pos.x,-pos.y,-pos.z);
//viewmat.SetTranslationPart(transvec);//same as above..

//glLoadMatrixf(viewmat);
Remember that you're setting up an object matrix for your camera and inverting it at the same time. The dot products above have the effect of inverting the translation elements of the camera matrix.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zipster
The equation for creating the OpenGL view matrix from the local camera-space matrix looks like this:



You have the RT part correct for rotation, but the translation part is computed using -RTd. So you're just forgetting to pre-multiply by RT. The side effect is that the camera rotates in the world frame instead of the local frame.
FYI Zipster, your equations don't display clearly for those of us who use the 'classic black' gdnet theme :-)

Share this post


Link to post
Share on other sites
So... painfully... obvious...

Thanks guys, this got it fixed. I went with Jyk's solution due to the way the matrixes are implemented since it was the simplest.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
FYI Zipster, your equations don't display clearly for those of us who use the 'classic black' gdnet theme :-)

You can only imagine my dilemma when I had to choose between black text for a white background or white text for a black background! You can make it out if you highlight it... but I'll go with a solid background from now on even though I despise them :)

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