DirectX Math: Matrices
Matrices are fundamental data structures in computer graphics, used extensively for representing transformations such as translation, rotation, and scaling, as well as for storing other structured data. The DirectX Math library provides robust support for matrix operations.
Matrix Types
The library defines several matrix types, primarily differentiated by their dimensions:
DirectX::XMMATRIX
: This is the primary 4x4 matrix type used for most 3D graphics transformations. It is typically aligned for optimal performance on the CPU.- Other matrix types might exist for specific purposes or lower dimensions, but 4x4 is the most common.
Creating Matrices
Matrices can be created in several ways:
Identity Matrix
An identity matrix has ones on the diagonal and zeros elsewhere. It represents no transformation.
auto identityMatrix = DirectX::XMMatrixIdentity();
Translation Matrix
A translation matrix shifts objects by a specified amount along the X, Y, and Z axes.
DirectX::XMVECTOR translation = DirectX::XMVectorSet(10.0f, 5.0f, 2.0f, 0.0f);
auto translationMatrix = DirectX::XMMatrixTranslationFromVector(translation);
Rotation Matrices
Rotation matrices rotate objects around an axis. Common functions include rotation around the X, Y, or Z axes.
float angleRadians = DirectX::XMConvertToRadians(45.0f); // 45 degrees
auto rotationXMatrix = DirectX::XMMatrixRotationX(angleRadians);
auto rotationYMatrix = DirectX::XMMatrixRotationY(angleRadians);
auto rotationZMatrix = DirectX::XMMatrixRotationZ(angleRadians);
Scaling Matrix
A scaling matrix resizes objects along the specified axes.
DirectX::XMVECTOR scale = DirectX::XMVectorSet(2.0f, 1.0f, 0.5f, 1.0f);
auto scaleMatrix = DirectX::XMMatrixScalingFromVector(scale);
Projection Matrices
Projection matrices are crucial for defining the camera's view frustum and projecting 3D objects onto the 2D screen. This includes perspective and orthogonal projections.
// Perspective projection
float aspectRatio = 16.0f / 9.0f;
float nearPlane = 0.1f;
float farPlane = 1000.0f;
float fieldOfView = DirectX::XMConvertToRadians(60.0f); // 60 degrees FOV
auto perspectiveMatrix = DirectX::XMMatrixPerspectiveFovLH(fieldOfView, aspectRatio, nearPlane, farPlane);
// Orthogonal projection
float orthoWidth = 800.0f;
float orthoHeight = 600.0f;
auto orthogonalMatrix = DirectX::XMMatrixOrthographicLH(orthoWidth, orthoHeight, nearPlane, farPlane);
Matrix Operations
The DirectX Math library provides optimized functions for common matrix operations:
Matrix Multiplication
Matrix multiplication is used to combine transformations. The order of multiplication matters (e.g., M1 * M2 is generally not the same as M2 * M1).
DirectX::XMMATRIX matrixA = DirectX::XMMatrixTranslation(1.0f, 0.0f, 0.0f);
DirectX::XMMATRIX matrixB = DirectX::XMMatrixRotationY(DirectX::XMConvertToRadians(90.0f));
DirectX::XMMATRIX combinedMatrix = DirectX::XMMatrixMultiply(matrixA, matrixB);
Matrix Transposition
Transposing a matrix swaps its rows and columns.
DirectX::XMMATRIX originalMatrix = DirectX::XMMatrixIdentity(); // Assume it's populated
DirectX::XMMATRIX transposedMatrix = DirectX::XMMatrixTranspose(originalMatrix);
Matrix Inverse
The inverse of a matrix is used to "undo" a transformation. Not all matrices are invertible.
DirectX::XMMATRIX transformMatrix = DirectX::XMMatrixTranslation(5.0f, 5.0f, 0.0f);
DirectX::XMMATRIX inverseMatrix;
if (DirectX::XMMatrixInverse(&inverseMatrix, transformMatrix))
{
// Successfully calculated the inverse
}
Matrix Decomposition
It's often useful to decompose a transformation matrix back into its constituent parts (translation, rotation, scale).
DirectX::XMMATRIX complexMatrix = /* ... a matrix combining multiple transformations ... */;
DirectX::XMVECTOR scale, rotationQuaternion, translation;
DirectX::XMMatrixDecompose(&scale, &rotationQuaternion, &translation, complexMatrix);
Matrix Layout and Conventions
DirectX Math uses column-major order by default for many operations that interact with hardware, but its internal representation and many functions work with a row-major concept for ease of use. When dealing with external systems (like shaders, which are often column-major), be mindful of potential layout differences. The library provides functions to handle these conversions.
__declspec(align(16))
) when using DirectX Math's vector types for optimal performance. The library's functions handle alignment internally for their return types.
A view matrix defines the camera's position and orientation in the world.
DirectX::XMVECTOR eyePosition = DirectX::XMVectorSet(0.0f, 5.0f, -10.0f, 1.0f);
DirectX::XMVECTOR lookAtPosition = DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f);
DirectX::XMVECTOR upDirection = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
DirectX::XMMATRIX viewMatrix = DirectX::XMMatrixLookAtLH(eyePosition, lookAtPosition, upDirection);