MSDN Documentation

Microsoft Developer Network

Matrices in DirectX

Matrices are fundamental to 3D graphics programming, particularly within DirectX. They are used extensively for representing transformations such as translation, rotation, scaling, and projection. Understanding how to define, manipulate, and apply matrices is crucial for any DirectX developer.

What is a Matrix?

A matrix is a rectangular array of numbers, symbols, or expressions arranged in rows and columns. In DirectX, we commonly deal with square matrices, most often 4x4 matrices, to represent 3D transformations in homogeneous coordinates.

Matrix Representation

A 4x4 matrix is typically represented as:

M
m11m12m13m14
m21m22m23m24
m31m32m33m34
m41m42m43m44

Each element in the matrix (e.g., m11, m12) holds a specific value that contributes to the transformation. The indices (row, column) often start from 1 in mathematical notation, but in programming contexts (like C++ arrays or structures), they are typically 0-indexed.

Common Matrix Types in DirectX

DirectX provides specific structures and functions for handling common matrix operations. The primary structure for 4x4 matrices is DirectX::XMFLOAT4X4.

Identity Matrix

The identity matrix is a special square matrix that, when multiplied by another matrix, leaves the other matrix unchanged. It represents no transformation.

Identity
1000
0100
0010
0001

In DirectX, you can use DirectX::XMMatrixIdentity() to get an identity matrix.

Translation Matrix

A translation matrix shifts an object in 3D space along the X, Y, and Z axes. The translation values (tx, ty, tz) are placed in the last column of the fourth row.

Translation
1000
0100
0010
txtytz1

DirectX provides DirectX::XMMatrixTranslation(tx, ty, tz).

Scaling Matrix

A scaling matrix resizes an object along the X, Y, and Z axes. The scaling factors (sx, sy, sz) are placed on the main diagonal.

Scaling
sx000
0sy00
00sz0
0001

Use DirectX::XMMatrixScaling(sx, sy, sz).

Rotation Matrix

Rotation matrices rotate an object around an axis. These are more complex, involving trigonometric functions (sine and cosine). DirectX provides functions for rotation around the X, Y, and Z axes, as well as arbitrary axes.

For example, a rotation around the Z-axis by an angle θ:

Rotation Z
cos(θ)sin(θ)00
-sin(θ)cos(θ)00
0010
0001

Functions like DirectX::XMMatrixRotationX(angle), DirectX::XMMatrixRotationY(angle), and DirectX::XMMatrixRotationZ(angle) are available.

Matrix Operations

Common matrix operations include:

Performance Considerations

DirectXMath library uses SIMD (Single Instruction, Multiple Data) instructions for optimized matrix and vector operations. Always prefer using the DirectXMath types (e.g., XMMATRIX, XMVECTOR) and functions for performance-critical code.

Applying Transformations

To transform a vertex's position (represented as a 4D vector) by a matrix, you perform matrix-vector multiplication:

new_position = vertex_position * transformation_matrix;

In DirectX, this is often achieved by multiplying the vertex position vector by a combined world, view, and projection matrix.

Conclusion

Matrices are the workhorses of 3D transformations in DirectX. By mastering their definition and manipulation, you gain the power to position, orient, and project your 3D scenes effectively.