Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Camera Matrix Calculation...

This topic is 2837 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 want to replace my Camera class which used gluLookAt with a modernized OpenGL 3 style one...
at first I thought, ok, why not just rewrite gluLookAt... but I quickly realized that with some matrix trickery I shouldn't need to.

My camera stores a position and a set of euler angles (x = pitch, y = yaw, z = roll)

So I thought, if I just follow the rules of transformation, then I should be able to create three matrices...
- third_person_offset_matrix
- rotation_matrix
- translation_matrix

and the camera matrix should just be...
camera_matrix = third_person_offset_matrix * rotation_matrix * translation_matrix;

This works except for one part... my position only gets modified in the axis aligned directions...
so if I press forward but am looking to the right, my camera moves left.

Then I thought, what if I grabbed the view vector from the rotation matrix and used that somehow...
but scrapped that after some trial and error.

I have yet to bust out the graph paper and linear algebra book because I am at work, but figured that this is a common problem...
and after some googling it is, but there are so many implementations and seeing as I'm using OpenGL...
- most answers involve gluLookAt blah...

Here's some code...

void Camera::GenerateCameraMatrices(void){
EG::Base::Math::Transformationf offset_matrix;
EG::Base::Math::Transformationf translation_matrix;
EG::Base::Math::Transformationf rotation_matrix_x;
EG::Base::Math::Transformationf rotation_matrix_y;
//EG::Base::Math::Transformationf rotation_matrix_z;

// Offset (For 3rd Person Cameras)

// Camera Position

// Rotation
rotation_matrix_x.Rotate(rotation.X(), 1.0f, 0.0f, 0.0f);
rotation_matrix_y.Rotate(rotation.Y(), 0.0f, 1.0f, 0.0f);
//rotation_matrix_z.Rotate(rotation.Z(), 0.0f, 0.0f, 1.0f);

EG::Base::Math::Transformationf rotation_matrix((/*rotation_matrix_z * */rotation_matrix_x * rotation_matrix_y).Data());
camera_matrix.Set(offset_matrix * rotation_matrix * translation_matrix);

Does anyone have any quick thoughts?

Thanks in advance!! ^_^

Share this post

Link to post
Share on other sites
Is this for a FPS or 6DOF style camera?

Why not just store the matrix directly 'as' the camera data instead of storing angles.

Lets take a FPS camera for example. You have one matrix for position/heading, and another matrix for the pitch (because pitch does not effect movement).

When you want to "move forward", you just apply a forward translation to your position matrix.

If you want to "look left", than you just apply a rotation about the Y axis to the position matrix.

When you look up and down this doesn't effect your position at all, so you can store a separate matrix for this.

Then when you want the final camera, it's just :

camera matrix = pitch matrix * position matrix

Then invert the camera matrix to get the view matrix.
You can tweak this if you want 6DOF or other kind of matrix.

Share this post

Link to post
Share on other sites
This is pretty much what I'm doing I think... and yes, this is for a standard FPS camera... fore now.

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!