Sign in to follow this  

Problem with matrix lookat method

Recommended Posts

I made my own matrix lookat method that functions just like D3DXMatrixLookAtLH. Yet when I use it in place of that directx method I end up seeing nothing but black in the game viewport. Is there an obvious flaw in this code?

I am doing this for a left handed, row major matrix. In the method, the matrix dest is intended to be initialized as a lookat transform matrix. Thanks for any help!

INLINE void MatrixLookAtLH (Matrix& dest, Vector3D& eye, Vector3D& at, Vector3D up)
Vector3D tmp;
Vector3D xAxis;
Vector3D yAxis;
Vector3D zAxis;

tmp = at - eye;
tmp.Normalize ();
zAxis = tmp;

tmp = up;
tmp.Cross (zAxis, tmp);
tmp.Normalize ();
xAxis = tmp;

tmp = zAxis;
tmp.Cross (xAxis, tmp);
yAxis = tmp;

dest.Init (xAxis.x, yAxis.x, zAxis.x, 0,
xAxis.y, yAxis.y, zAxis.y, 0,
xAxis.z, yAxis.z, zAxis.z, 0,
-xAxis.Dot (eye), -yAxis.Dot (eye), -zAxis.Dot (eye), 1);

Share this post

Link to post
Share on other sites
A couple of suggestions:

- You might try swapping the arguments to one or more of the calls to Cross(). Generally speaking, the relationship between the basis vectors is x = yXz, y = zXx, and z = xXy. The cross product is non-commutative, so if you get things backwards the overall results may be incorrect.

- Make sure you understand the difference between row/column [i]major[/i] and row/column [i]vectors[/i], and make sure that the conventions you're using are compatible with Direct3D (in other words, that the matrix is laid out in memory in the order that Direct3D expects).

- This probably isn't the problem, but the cross product should be a binary function that doesn't modify its arguments rather than a member function that writes the result to the object on which it's called. (Arguably, at least.) Implementing the cross product in this way will make your code cleaner and easier to follow.

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