#### Archived

This topic is now archived and is closed to further replies.

# Dissecting D3DXMatrixLookAtLH?

## Recommended Posts

Cyelince    122
Has anyone Dissected the D3DXMatrixLookAtLH function or know how to make something with the same effect...? I am trying to make a 3D object look at another one, but I want to use the D3DXMatrixLookAtLH, and would like to know how they made it anyway...I saw it posted up here one time, but it''s gone now or I just can''t find it. Thank you, Cyelince

##### Share on other sites
Mithrandir    607
Fron the d3d8 SDK docs:
DirectX Graphics->Understanding DirectX Graphics->Direct3D Rendering Pipeline->Fixed Function Vertex and Pixel Processing->Transformation and Lighting Engine->The View Transformation->What is the view transformation?

"Another approach involves creating the composite view matrix directly. The D3DXMatrixLookAtLH and D3DXMatrixLookAtRH helper functions use this technique. This approach uses the camera''s world space position and a look-at point in the scene to derive vectors that describe the orientation of the camera space coordinate axes. The camera position is subtracted from the look-at point to produce a vector for the camera''s direction vector (vector n). Then the cross product of the vector n and the y-axis of world space is taken and normalized to produce a right vector (vector u). Next, the cross product of the vectors u and n is taken to determine an up vector (vector v). The right (u), up (v), and view-direction (n) vectors describe the orientation of the coordinate axes for camera space in terms of world space. The x, y, and z translation factors are computed by taking the negative of the dot product between the camera position and the u, v, and n vectors.

These values are put into the following matrix to produce the view matrix.

  [ux vx nx 0][uy vy ny 0][uz vz nz 0][-(u.c) -(v.c) -(n.c) 1]

In this matrix, u, v, and n are the up, right, and view-direction vectors, and c is the camera''s world space position. This matrix contains all the elements needed to translate and rotate vertices from world space to camera space. After creating this matrix, you can also apply a matrix for rotation around the z-axis to allow the camera to roll."

from the next page:

"The following code example, illustrates the D3DXMatrixLookAtLH function.

  D3DXMATRIX* WINAPI D3DXMatrixLookAtLH ( D3DXMATRIX *pOut, const D3DXVECTOR3 *pEye, const D3DXVECTOR3 *pAt, const D3DXVECTOR3 *pUp ){#if DBG if(!pOut || !pEye || !pAt || !pUp) return NULL;#endif D3DXVECTOR3 XAxis, YAxis, ZAxis; // Get the z basis vector, which points straight ahead; the // difference from the eye point to the look-at point. This is the // direction of the gaze (+z). D3DXVec3Subtract(&ZAxis, pAt, pEye); // Normalize the z basis vector. D3DXVec3Normalize(&ZAxis, &ZAxis); // Compute the orthogonal axes from the cross product of the gaze // and the pUp vector. D3DXVec3Cross(&XAxis, pUp, &ZAxis); D3DXVec3Normalize(&XAxis, &XAxis); D3DXVec3Cross(&YAxis, &ZAxis, &XAxis); // Start building the matrix. The first three rows contain the // basis vectors used to rotate the view to point at the look-at // point. The fourth row contains the translation values. // Rotations are still about the eyepoint. pOut->_11 = XAxis.x; pOut->_21 = XAxis.y; pOut->_31 = XAxis.z; pOut->_41 = -D3DXVec3Dot(&XAxis, pEye); pOut->_12 = YAxis.x; pOut->_22 = YAxis.y; pOut->_32 = YAxis.z; pOut->_42 = -D3DXVec3Dot(&YAxis, pEye); pOut->_13 = ZAxis.x; pOut->_23 = ZAxis.y; pOut->_33 = ZAxis.z; pOut->_43 = -D3DXVec3Dot(&ZAxis, pEye); pOut->_14 = 0.0f; pOut->_24 = 0.0f; pOut->_34 = 0.0f; pOut->_44 = 1.0f; return pOut;}

"