OpenGL - Calculating camera view matrix
Posted
by
Karle
on Game Development
See other posts from Game Development
or by Karle
Published on 2014-05-27T00:08:28Z
Indexed on
2014/05/27
3:43 UTC
Read the original article
Hit count: 403
Problem
I am calculating the model, view and projection matrices independently to be used in my shader as follows:
gl_Position = projection * view * model * vec4(in_Position, 1.0);
When I try to calculate my camera's view matrix the Z axis is flipped and my camera seems like it is looking backwards.
My program is written in C# using the OpenTK library.
Translation (Working)
I've created a test scene as follows:
From my understanding of the OpenGL coordinate system they are positioned correctly.
The model matrix is created using:
Matrix4 translation = Matrix4.CreateTranslation(modelPosition);
Matrix4 model = translation;
The view matrix is created using:
Matrix4 translation = Matrix4.CreateTranslation(-cameraPosition);
Matrix4 view = translation;
Rotation (Not-Working)
I now want to create the camera's rotation matrix. To do this I use the camera's right, up and forward vectors:
// Hard coded example orientation:
// Normally calculated from up and forward
// Similar to look-at camera.
Vector3 r = Vector.UnitX;
Vector3 u = Vector3.UnitY;
Vector3 f = -Vector3.UnitZ;
Matrix4 rot = new Matrix4(
r.X, r.Y, r.Z, 0,
u.X, u.Y, u.Z, 0,
f.X, f.Y, f.Z, 0,
0.0f, 0.0f, 0.0f, 1.0f);
This results in the following matrix being created:
I know that multiplying by the identity matrix would produce no rotation. This is clearly not the identity matrix and therefore will apply some rotation.
I thought that because this is aligned with the OpenGL coordinate system is should produce no rotation. Is this the wrong way to calculate the rotation matrix?
I then create my view matrix as:
// OpenTK is row-major so the order of operations is reversed:
Matrix4 view = translation * rot;
Rotation almost works now but the -Z/+Z axis has been flipped, with the green cube now appearing closer to the camera. It seems like the camera is looking backwards, especially if I move it around.
My goal is to store the position and orientation of all objects (including the camera) as:
Vector3 position;
Vector3 up;
Vector3 forward;
Apologies for writing such a long question and thank you in advance. I've tried following tutorials/guides from many sites but I keep ending up with something wrong.
Edit: Projection Matrix Set-up
Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(
(float)(0.5 * Math.PI),
(float)display.Width / display.Height,
0.1f,
1000.0f);
© Game Development or respective owner