Jump to content
  • Advertisement
Sign in to follow this  

Creating a camera (view) matrix

This topic is 4914 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'm implementing my own transform pipeline and am trying to figure out how to derive a camera/view matrix. Here's my understanding of the view matrix: - translate the position vector to the origin - align right, look, and up vectors with x,y,z - apply this matrix to all vertices Is this correct? What is this called in a more general algebraic term, as in, which section/topic would this be under in my linear algebra text book. I can't quite grasp how to arbitrarly position my camera and then derive a matrix that will position it at the origin. :( Thanks for any help

Share this post

Link to post
Share on other sites
I'm not sure exactly how everything works but I believe this is what's going on.

If you define a matrix with these axis's

xAxis.x yAxis.x zAxis.x
xAxis.y yAxis.y zAxis.y
xAxis.z yAxis.z zAxis.z

You'll see that you have three vector's in each column, the xAxis vector, the yAxis vector and the zAxis vector. These vector's as the name points out defines the vector for each axis in your world, if there is no rotation you should have these axis's(normal cartesian axis) x = <1,0,0> y = <0,1,0> and z<0,0,1> to give you this matrix

1 0 0
0 1 0
0 0 1

Now say you want the world rotated on the zAxis by 90 degree's, well rotating the x vector by 90 degree's will give have it pointing up at <0,1,0> and the y axis pointing left at <-1,0,0> so your new matrix will be

0, 1 0
-1 0 0
0, 0 1

Now to build a camera class you want take your camera's orientation and build your axis's based on it's orientation, so if your camera was simply rotated to point down 90 degree's you would use the above matrix to rotate the axis back to the origin.

So to do this, we can build a matrix called the look at matrix based on three variables the camera's eye position, what the camera is looking at(how far away it is) and the camera's up vector.

So the easiest axis to make is the z axis(which way the camera is pointing at). Take the position of the camera and subtract it from what its looking at so

zAxis = vEye - vAt;

*Note: You must do from Eye - At not At - Eye, to give you a backwards vector because you always want to build your matrix rotation opposite the way the camera is rotating, remember the transformation pipeline is about bringing vertices back to the origin to render.

Once we have the zAxis we can get the right axis or xAxis by doing the cross product of the camera's up vector with the zAxis.

xAxis = cross( vUp, zAxis )
normalize( xAxis )

The last axis remaining is the yAxis, since we have the z and x, z cross x will give us y.

yAxis = cross( zAxis, xAxis )
normalize( yAxis )

So now we have built our axis matrix, the last thing we need to take into consideration is the translation vector of the matrix. In row major the translation vector is across the bottom on _41, _42, _43. If we want to get the distance of how far the at point is from our camera, we can simply project the eye vector onto each of our axis's. Projecting these points will give us the displacement on each axis. The last step is to negate these components because we need to translate back to the origin, everything is always backwards.


_41 = -dot(xAxis, eye), _42 = -dot(yAxis, eye), _43 = -dot(zAxis, eye).

Once you have this matrix if you want to translate the camera further, you simply add your -translatioin vector onto that last row.

Also one last note, these axis matrices I've been refering to all along are actually rotation matrices in disguise. If you plug in numbers for a rotation matrix with 0 angle you'll get the identity matrix, or the three x,y,z axis. If you plug in 90 degree's you'll get the matrix I showed above.

Hope that helped

Share this post

Link to post
Share on other sites
Original post by GaryNas
I'm ... trying to figure out how to derive a camera/view matrix.

The view matrix is simply the inverse of the camera's position/orientation matrix.

For example, if an D3D camera is at location [3,4,5] and facing down the X axis (and Y is up), its position/orientation matrix is this:
 0  0 -1  0
0 1 0 0
1 0 0 0
3 4 5 1
and the view matrix is simply the inverse of that:
 0  0  1  0
0 1 0 0
-1 0 0 0
5 -4 -3 1
There is a shortcut to finding the inverse, but just do the inverse first, worry about the shortcut later.

[Edited by - JohnBolton on May 31, 2005 12:33:10 AM]

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!