Understanding Geometry Transformations in DirectX
Transformations are fundamental to 3D graphics, allowing you to manipulate objects in space. In DirectX, this is primarily achieved using matrix operations applied to vertex data.
Types of Transformations
The three basic types of geometric transformations are:
- Translation: Moving an object from one point to another.
- Rotation: Turning an object around an axis.
- Scaling: Resizing an object.
Matrix Representation
In DirectX, transformations are represented using 4x4 matrices. These matrices combine translation, rotation, and scaling into a single operation. This is often referred to as an affine transformation.
A point or vector (x, y, z, w) is transformed by multiplying it with the transformation matrix:
[x'] [m11 m12 m13 m14] [x]
[y'] = [m21 m22 m23 m24] * [y]
[z'] [m31 m32 m33 m34] [z]
[w'] [m41 m42 m43 m44] [w]
Common Transformation Matrices
DirectX provides helper functions to create these matrices:
Translation Matrix
Moves vertices by a specified offset (tx, ty, tz).
D3DXMATRIX matTranslate;
D3DXMatrixTranslation(&matTranslate, tx, ty, tz);
Rotation Matrix
Rotates vertices around an axis (e.g., X, Y, Z) by a given angle.
D3DXMATRIX matRotateX, matRotateY, matRotateZ;
D3DXMatrixRotationX(&matRotateX, angleX);
D3DXMatrixRotationY(&matRotateY, angleY);
D3DXMatrixRotationZ(&matRotateZ, angleZ);
Scaling Matrix
Resizes vertices by specified factors (sx, sy, sz).
D3DXMATRIX matScale;
D3DXMatrixScaling(&matScale, sx, sy, sz);
Combining Transformations
To apply multiple transformations (e.g., scale, then rotate, then translate), you multiply their respective matrices. The order of multiplication is important.
For a common transformation pipeline (scale, then rotate, then translate):
D3DXMATRIX matCombined;
D3DXMatrixTransformation(&matCombined, &scaleVector, &rotationQuat, &translationVector);
// Or manually:
matCombined = matScale * matRotate * matTranslate;
World, View, and Projection Matrices
In the DirectX rendering pipeline, these transformations are categorized into three main matrices:
- World Matrix: Positions and orients objects in the 3D scene.
- View Matrix: Defines the camera's position and orientation.
- Projection Matrix: Defines the viewing frustum and how 3D coordinates are projected onto the 2D screen (e.g., perspective or orthographic).
These matrices are combined and applied to vertex data during the vertex shader stage.
| Matrix Type | Purpose |
|---|---|
| World | Object's position, rotation, and scale in the scene. |
| View | Camera's position and orientation. |
| Projection | Defines the viewing frustum and projection type. |
Coordinate Systems
Understanding coordinate systems (object space, world space, view space, clip space, screen space) is crucial for correctly applying transformations.