Matrix.LookAtLH(Vector3,Vector3,Vector3) Method (Microsoft.DirectX)

How Do I...?

Builds a left-handed look-at matrix.

Definition

Visual BasicPublic Shared Function LookAtLH( _
    ByVal cameraPosition As Vector3, _
    ByVal cameraTarget As Vector3, _
    ByVal cameraUpVector As Vector3 _
) As Matrix
C#public static Matrix LookAtLH(
    Vector3 cameraPosition,
    Vector3 cameraTarget,
    Vector3 cameraUpVector
);
C++public:
static Matrix LookAtLH(
    Vector3 cameraPosition,
    Vector3 cameraTarget,
    Vector3 cameraUpVector
);
JScriptpublic static function LookAtLH(
    cameraPosition : Vector3,
    cameraTarget : Vector3,
    cameraUpVector : Vector3
) : Matrix;

Parameters

cameraPosition Microsoft.DirectX.Vector3
A Vector3 structure that defines the camera point. This value is used in translation.
cameraTarget Microsoft.DirectX.Vector3
A Vector3 structure that defines the camera look-at target.
cameraUpVector Microsoft.DirectX.Vector3
A Vector3 structure that defines the up direction of the current world, usually [0, 1, 0].

Return Value

Microsoft.DirectX.Matrix
A Matrix structure that is a left-handed look-at matrix.

Remarks

This method uses the following formula to compute the returned matrix.

zaxis = normal(cameraTarget - cameraPosition)
xaxis = normal(cross(cameraUpVector, zaxis))
yaxis = cross(zaxis, xaxis)

 xaxis.x           yaxis.x           zaxis.x          0
 xaxis.y           yaxis.y           zaxis.y          0
 xaxis.z           yaxis.z           zaxis.z          0
-dot(xaxis, cameraPosition)  -dot(yaxis, cameraPosition)  -dot(zaxis, cameraPosition)  1

How Do I...?

Set Up a View Matrix

This example demonstrates how to initialize the view transformation matrix, which transforms world coordinates into camera or view space.

In the following C# code example, the vector components of the Vector3 structure form the arguments for the LookAtLH method, which builds a left-handed (LH) look-at matrix. The View transformation matrix is set to be equal to this look-at matrix.

The three input vectors represent the following, respectively:

  1. The eye point: [0, 3, -5].
  2. The camera look-at target: the origin [0, 0, 0].
  3. The current world's up-direction: usually [0, 1, 0].

              [C#]
              
using Microsoft.DirectX.Direct3D;

Device device = null;  // Create rendering device.

// Set up the view matrix. A view matrix can be defined given an eye point,
//   a point to view, and a direction for which way is up. Here, you set 
//   the eye five units back along the z-axis and up three units, view the
//   origin, and define "up" to be in the y-direction.

device.Transform.View = Microsoft.DirectX.Matrix.LookAtLH(
                            new Vector3(0.0f, 3.0f, -5.0f),
                            new Vector3(0.0f, 0.0f, 0.0f),
                            new Vector3(0.0f, 1.0f, 0.0f));

See Also

Show: