D3DXMatrixLookAtLH internally

Started by
4 comments, last by neeker 10 years, 7 months ago

Hi,

I have a project that to remove all dependency on d3dx9, so I have to manually implement the functionality of D3DXMatrixLookAtLH. I found some great resources for implementing D3DXMatrixPerspectiveFovLH, but I can seem to find anything D3DXMatrixLookAtLH. I've tried looking at the return values and it's not making much sense right now. Does anyone have an idea how the final matrix is built?

Advertisement

This: http://msdn.microsoft.com/en-us/library/windows/desktop/bb205342(v=vs.85).aspx contains a description of the algorithm. Note that the matrix is identical to a object-to-world space transform that has been inverted.

The gist of the algorithm is to compute the local x, y and z axis of the camera in world space and then drop them into a matrix such the camera is placed at the origin. Transforms containing rotations and translations only are constructed as follows:


| ux  uy  uz  0 |
| vx  vy  vz  0 |
| nx  ny  nz  0 |
| tx  ty  tz  1 |

// x-axis of basis (ux, uy, uz) 
// y-axix of basis (vx, vy, vz)
// z-axis of basis (nx, ny, nz)
// origin of basis (tx, ty, tz)

That's a simple world transform; you are building a view matrix so you actually want the inverse which is computed as follows:


|        ux          vx         nx  0 |
|        uy          vy         ny  0 |
|        uz          vz         nz  0 |
| -(t dot u)  -(t dot v) -(t dot n) 1 |

// u = (ux, uy, uz)
// v = (vx, vy, vz)
// n = (nx, ny, nz)
// t = (tx, ty, tz)

What nonoptimalrobot said, but if you want to see an implementation example:


		Matrix MatLookAt(const Vector3& vEye, const Vector3& vAt, const Vector3& vUp)
		{
			Vector3 vZ = (vAt - vEye).normal();
			Vector3 vX = (vUp.Cross(vZ)).normal();
			Vector3 vY = vZ.Cross(vX);

			return Matrix(	vX.x,			vY.x,			vZ.x,			0.0f,
							vX.y,			vY.y,			vZ.y,			0.0f,
							vX.z,			vY.z,			vZ.z,			0.0f,
							-vX.Dot(vEye), -vY.Dot(vEye),	-vZ.Dot(vEye),	1.0f);
		}

You would consider using XNAMath

I hate to downvote, Kubera, but with XNA dead in the water, that is about as useful a suggestion as using d3dx math. I would recommend DirectXMath as a current, and more future proof library.

Perfect! Thank you!

This topic is closed to new replies.

Advertisement