# OpenGL camera transformation

This topic is 4399 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to make a simple 3d renderer, without dx or ogl. I have all objects in world space as standard, and I use right = +x, up = +y, forward = +z, which is left-handed I believe. With the camera at (0,0,0) and facing the positive z-axis, all objects are rendered how they should. However, the camera should ideally be able to move around and rotate. I can handle the code for moving, but I can't figure out how to transform the points accordingly. The camera simply has two 3d vectors, one for the eyepoint, and the other for the lookAt point. The up vector is by default (0,1,0). I have functions for getting the view, side, and up vectors based on the eye and lookAt. However since I can't get the transformations to work, I can't be sure they are right. My current process is to subtract each point by the camera's eyepoint, setting it at the origin. The part I can't get is properly rotating the view vector to point along the positive z-axis. I've looked online extensively, and everything I find either tells me how to use directx or opengl's functions, or it didn't work for me. I have implementations of 3d vectors, 4d vectors, 3x3 matrices, 4x4 matrices, tons of stuff tried and failed. Alternatively, not a problem per se, but a question. Currently, my implementation of projection is just to divide the x and y coords by the z coord, something I remember hearing a while ago. It works, and moving the camera around (the eyepoint... it still has to look straight ahead) seems realistic. However, it tends to stretch things along the z-axis, ie. things look longer than they really are. I have seen many projection transformations in my searches for view transformations, and although I didn't try any of them, I still wonder: is my implementation acceptable and/or what is the best way to do projection?

##### Share on other sites
In most rendering APIs, the concept of a camera is more of a convenience than an integral part of the renderer. Basically, you have a chain of transformations -- model-to-world, world-to-view, view-to-clip, and something like clip-to-screen, and the renderer just deals in terms of these transformations. Luckily, the camera fits into this scheme very easily -- it generates the world-to-view transformation.

To generate the world-to-view transformation, you take the position and orientation of the camera in world space and compute a transformation (view-to-world) from that, and finally invert that to get the world-to-view transformation.

That's the basics. I left out a lot of details.

##### Share on other sites
Thanks for your response, but I knew all that... what I need IS that world to view transformation, given the sort of data I have about the camera.

##### Share on other sites
How to Compute the View Matrix from the Camera's Look-At and Eye Positions, and an Up Vector

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

To generate the camera's orientation/position matrix, combine the local x, y, and z axes, and eye position. You compute the local axes like this:
    z = normalize( lookAt - eye );    x = normalize( cross( up, z ) );    y = cross( z, x );
The x, y, and z vectors go into the first, second, and third columns (if you are using column vectors) of the matrix. The eye position goes into the 4th column (if you are using column vectors). If you are using row vectors, then they go into the rows of the matrix. Now you have your camera's orientation/position matrix, which conveniently happens to be the view-to-world matrix. To make a world-to-view matrix, you simply invert it.

Now, rather than doing a general 4x4 matrix inversion, you can take advantage of some of the properties of the matrix in order to invert it quickly. First, let's simplify the notation. The matrix can be represented like this:
    R  T    0  1
where R is the upper-left 3x3 part of the 4x4 matrix, T is the upper-right 3x1 part of the 4x4 matix, 0 is the 1x3 row of zeros on the bottom, and 1 is the 1 in the lower-right corner. Note that R and T conveniently correspond to the rotation and translation portions of the matrix. If you are using row vectors, then the matrices here are shown transposed. That doesn't affect the math, it just affects the notation.

The inverse of this 2x2 matrix is:
    R-1 T'    0  1
where T' = -R-1T. In addition, since R is orthonormal (or orthogonal, or whatever the correct terminology is), R-1 = RT. The result is:
    RT T'    0  1
where T' = -RTT, and so no inversion is actually necessary. This is your world-to-view matrix.

One more possible optimization... T' = -RTT can also be computed like this:
    T'X = -(RX dot T)    T'Y = -(RY dot T)    T'Z = -(RZ dot T)
where RX, RY, and RZ are the first, second, and third columns (or rows) of R -- the x, y, and z axes you computed at the beginning!

Note: It doesn't matter how the camera's orientation/position matrix is computed. The matrix inversion presented above will work for any transformation matrix that involves only rotation and translation.

##### Share on other sites
Thanks a lot for the detailed reply! I will implement this and hopefully it works fine.

I don't know a ton about transformation matrices, but I think that the top 3x3, or R, is just a rotation matrix, and T is a translation matrix, just combined into the 4x4 matrix that way. Could I simply perform the translation of -T, then multiply R * [point] to rotate the point? The inverse of a 3x3 matrix isn't hard to calculate, although your optimizations make a 4x4's easy also.

##### Share on other sites
Quote:
 Original post by deej21Could I simply perform the translation of -T, then multiply R * [point] to rotate the point?

Not R, RT. Transforming a point by the view matrix looks like this:
    | v' |  =  | RT  -RTT |  | v |    | 1  |  =  | 0    1   |  | 1 |
which simplifies to v' = RTv - RTT, or v' = RT(v - T).

Quote:
 Original post by deej21The inverse of a 3x3 matrix isn't hard to calculate, although your optimizations make a 4x4's easy also.

Don't forget that the inverse of a rotation matrix is simply its transpose.

##### Share on other sites
Sweet, that worked out great. Thanks a lot, johnbolton.
Just a note, it also worked just using the 3x3 matrix. I just subtracted the eyepoint, then multiplied by the transposed rotation matrix. This eliminates the need to calculate T'

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631419
• Total Posts
2999983
×