Sign in to follow this  
Laval B

LookAt matrix

Recommended Posts

Laval B    12387

I'm working on a simple problem which is giving me some trouble right now. The problem is to build the lookat matrix for a camera in 3D. I ussume a left-handed coordinate system, i.e. z+ is pointing toward the screen. So, Eye being the point where the camera stands, At the point the camera is looking at, i construct an orthonormal basis for this new system like so :

Z = Normalize(At - Eye)
X = Normalize(Up x Z)
Y = Normalize(Z x X)

where x is the cross-product of two vectors and up is the original world ip vector (usually (0,1,0)).

Now, to build the matrix containing the rotation part of the transformation, i just build the matrix (X, Y, Z) where X, Y, X are the column vectors calculates above. This gives the following matrix :
[left] [X.x Y.x Z.x 0][/left][left] [X.y Y.y Z.z 0][/left][left] [X.z Y.z Z.z 0][/left][left] [0 0 0 1][/left]

To takes into account the translation to Eye, i then multiply this matrix by

[left] [1 0 0 -Eye.x][/left][left] [0 1 0 -Eye.y][/left][left] [0 0 1 -Eye.z][/left][left] [0 0 0 1 ][/left][left]
[/left][left]which gives [/left][left]
[/left][left][left] [X.x Y.x Z.x -(X.x*Eye.x + Y.x*Eye.y + Z.x*Eye.z)][/left][left] [X.y Y.y Z.z -(X.y*Eye.x + Y.y*Eye.y + Z.y*Eye.z)][/left][left] [X.z Y.z Z.z -(Z.z*Eye.x + Y.z*Eye.y + Z.z*Eye.z)][/left][left] [0 0 0 1 ][/left][left]
[/left][left]The problem i have is that, when i look at the MSDN page for [font="Consolas, Courier, monospace"][size="4"]D3DXMatrixLookAtLH, [/size][/font]the translation part had dot products. Yes, i'm using column major[/left][left]matrices with column vectors, but it should be the same. In order to have those dot products, i would need to write my initial rotation matrix this way :[/left][left]
[/left][left][left] [X.x X.y X.z 0][/left][left] [Y.x Y.y Y.z 0][/left][left] [Z.x Z.y Z.z 0][/left][left] [0 0 0 1][/left][left]
[/left][left]Did i write my rotation matrix wrong ?[/left][left]

Share this post

Link to post
Share on other sites
quasar3d    814
your matrix takes the camera axes to world space axes, but you actually want to convert from world to camera space, so you have to invert your matrix, which, since your matrix is orthonormal, is just its transpose (the rotation part that is).

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