• Create Account

# D3DXMatrixLookAtLH internally

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1neeker  Members   -  Reputation: 182

Like
0Likes
Like

Posted 27 August 2013 - 04:20 PM

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?

Edited by neeker, 27 August 2013 - 04:28 PM.

### #2nonoptimalrobot  Members   -  Reputation: 408

Like
5Likes
Like

Posted 27 August 2013 - 06:32 PM

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)



### #3Juliean  Crossbones+   -  Reputation: 1547

Like
1Likes
Like

Posted 27 August 2013 - 07:30 PM

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);
}


### #4kubera  Members   -  Reputation: 663

Like
0Likes
Like

Posted 27 August 2013 - 11:42 PM

You would consider using XNAMath

### #5Burnt_Fyr  Members   -  Reputation: 794

Like
0Likes
Like

Posted 28 August 2013 - 08:32 AM

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.

### #6neeker  Members   -  Reputation: 182

Like
0Likes
Like

Posted 28 August 2013 - 09:21 AM

Perfect!  Thank you!

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS