Drawing
Coordinate spaces and the view matrix
A vertex is stored in its mesh's own coordinates. Before it becomes a pixel, it passes through five more coordinate systems. Each step is one matrix or one simple formula. This page explains every space, which way its axes point, and how the camera's view matrix is built. The last part shows how RASM does it.
In short
- The chain is object → world → view → clip → NDC → pixels. The model, view and projection matrices do the first three steps. The GPU does the last two.
- A right-handed, Y-up world has X right, Y up and Z toward you. A camera there looks down its own −Z.
- The model matrix is T · R · S: scale first, then rotate, then move.
- The view matrix is the inverse of the camera's own world matrix. For a camera without scale it is RT · translate(−eye).
- A quaternion stores a rotation in 4 numbers. Its inverse is its conjugate, and it blends smoothly (slerp).
- Vulkan's clip space has Y down; OpenGL, Direct3D and Metal have Y up. Engines fix this with a negative viewport height or a flip in the projection.
1The chain of spaces
A space is a coordinate system: an origin and three axes. The same point has different numbers in different spaces. Rendering moves every vertex through a fixed chain of them. Each arrow below is one transform.
The same scene, as each space sees it:
Object (model, local)
Origin: the mesh's own pivot.
The numbers in the file. One mesh can be placed many times.
World
Origin: the scene's origin.
All objects, lights and cameras in one space. Lighting and physics often work here.
View (eye, camera)
Origin: the camera.
The world as the camera sees it. The camera sits at 0 and looks down −Z.
Clip
Origin: —
4D homogeneous coordinates. The GPU clips triangles against −w ≤ x, y ≤ w here.
NDC (normalized device coordinates)
Origin: the screen's center.
Clip divided by w. The visible part is a cube: x and y in −1..1, depth in 0..1 (or −1..1 in OpenGL).
Framebuffer (window, screen)
Origin: a corner of the image.
Pixel coordinates, and a depth value for the depth test.
2Axes and handedness
Three axes at right angles can be arranged in two ways that are mirror images of each other. Point X to the right and Y up. Then Z points either toward you or away from you. The first is right-handed, the second left-handed. Curl the fingers of that hand from X to Y, and the thumb shows Z.
Neither choice is better. What matters is that every part of a program agrees. Tools and file formats chose differently:
| Tool or format | Handedness | Up axis |
|---|---|---|
| glTF 2.0 | right | +Y (a model's front faces +Z) |
| OpenGL tradition, Maya | right | +Y |
| Blender, 3ds Max | right | +Z |
| Unity | left | +Y |
| Unreal Engine | left | +Z |
Converting between two of them is a fixed matrix:
A different up axis: a rotation
A rotation keeps the winding of every triangle.
A different handedness: a mirror
One negated axis. A mirror also reverses the winding of every triangle, so it changes face culling. See Face culling and winding order.
3Object to world: the model matrix
An object is placed in the world with three parts: a scale S, a rotation R and a translation T. With column vectors (the convention of OpenGL, glm and most math books), a point is transformed as p′ = M · p, and the model matrix is
M = T · R · S p_world = T · (R · (S · p_object))
Read it from right to left: the matrix next to the point acts first. So the object is scaled around its own pivot, then turned, then moved. The order matters:
Swap the rotation and the translation, and the object lands somewhere else:
R · T: move, then rotate
The object turns around the world origin instead of its own pivot.
T · R: rotate, then move
The object turns around its own pivot, then moves. This is the order in M = T · R · S.
A hierarchy (a wheel on a car, a node tree in a glTF file) multiplies the matrices of all parents:
Row or column vectors
Some texts (classic Direct3D, DirectXMath) write p′ = p · M, with the point as a row. Then every matrix is the transpose, and products are read left to right: M = S · R · T. It is the same math with the other notation.
Row-major or column-major
A separate question: how the 16 numbers sit in memory. glm and GLSL store columns one after the other, so m[3] is the translation column. Mixing up the storage order transposes the matrix without any error message.
4Storing a rotation: quaternions
A rotation can be stored in several ways. Games most often use a unit quaternion: four numbers (w, x, y, z) with length 1. A rotation by the angle θ around the unit axis a is
q = ( cos(θ/2), sin(θ/2) · a ) rotate a vector: v' = q · v · q*
Why a quaternion: 4 numbers, and no special angles where it breaks.
Combine
Two rotations in a row are one product: q2 · q1 (q1 acts first).
Undo
The conjugate q* = (w, −x, −y, −z) turns back.
Blend
Slerp blends two rotations at an even speed.
Normalize
Rounding makes the length drift from 1 after many products: normalize it from time to time.
The other ways to store a rotation:
Euler angles (yaw, pitch, roll)
3 numbers. Good for user input and editors.
Problem: gimbal lock: at pitch ±90° two axes line up and one degree of freedom is lost. Blending is uneven.
3×3 matrix
9 numbers. Good for transforming many points; the GPU.
Problem: drifts away from a pure rotation; hard to blend
Axis and angle
4 numbers. Good for building a rotation.
Problem: combining two is awkward
Unit quaternion
4 numbers. Good for storing, combining and blending.
Problem: must be turned into a matrix for the GPU
5The view matrix
A camera is an object like any other. It has a position eye and a rotation R, so it has a world matrix C = T(eye) · R. That matrix takes points from the camera's space to the world. Rendering needs the opposite: points from the world into the camera's space. So the view matrix is the inverse:
V = C⁻¹ = (T · R)⁻¹ = R⁻¹ · T⁻¹ = Rᵀ · T(−eye)
Two facts make this cheap. The inverse of a translation is the translation by the negative vector. The inverse of a rotation matrix is its transpose (for a quaternion: its conjugate). No general 4×4 inverse is needed.
Building it with lookAt
Often the camera is given as "stand at eye, look at target, with up roughly up". The three axes of the camera follow from cross products:
If f is parallel to up, f × up is zero: looking straight up or down breaks lookAt.
The rows of RT are the camera's axes, and the last column is RT · (−eye):
| r.x r.y r.z −r·eye |
V = | u.x u.y u.z −u·eye |
| −f.x −f.y −f.z f·eye |
| 0 0 0 1 |
Each row asks one question about a world point p: how far is it to the right (r), up (u), and behind (−f) the camera? A point in front of the camera gets a negative z.
Try it from above. The picture builds this matrix from the two orange handles and applies it to one fixed point. The last two numbers use the projection of the worked example.
6Clip space, NDC and pixels
The projection matrix turns view space into clip space. Its main job is to put the distance in front of the camera into w, so that the GPU can divide by it: far things get smaller. The details are in Projection and reversed-Z depth. Here we only need the last two steps, which the GPU does after the vertex shader:
NDC: (x_n, y_n, z_n) = (x_c, y_c, z_c) / w_c
pixels: x_p = vx + (x_n + 1) / 2 · width
y_p = vy + (y_n + 1) / 2 · height (the API's own y direction)
depth = minDepth + z_n · (maxDepth − minDepth)
7Y up or Y down
The APIs do not agree on which way NDC y points:
OpenGL
NDC y = +1 is the top. NDC depth −1..1. Pixel row 0 is at the bottom.
Direct3D 11/12
NDC y = +1 is the top. NDC depth 0..1. Pixel row 0 is at the top.
Metal
NDC y = +1 is the top. NDC depth 0..1. Pixel row 0 is at the top.
Vulkan
NDC y = +1 is the bottom. NDC depth 0..1. Pixel row 0 is at the top.
Most math libraries (glm among them) build OpenGL-style matrices, where +Y is up in clip space. Feed such a matrix to Vulkan unchanged and the image comes out upside down. There are three common fixes:
No fix
Vulkan maps y = +1 to the bottom row. The image is upside down on screen, and every winding is reversed.
A: viewport flip
y = top + h, height = −h. Clip space stays Y-up, the same as on the other APIs. Shaders that compute clip positions by hand need no change.
Needs: Vulkan 1.1 (or VK_KHR_maintenance1)
B: projection flip
P[1][1] = −P[1][1]. Clip space becomes Y-down.
Cost: any full-screen pass or hand-written clip position must flip y too
Flip the final image
Render upside down and flip the final image.
Cost: a copy; rarely used
Fixes A and B both give an upright image, so a counter-clockwise triangle stays counter-clockwise on screen.
Mouse positions have the same problem the other way round: windows report y growing downward. Code that maps the mouse into NDC-like coordinates flips it: yn = 1 − 2 · ymouse / height.
8Worked example
One vertex, all the way to its pixel. The mesh point is p = (1, 0, 0). The object stands at (0, 0, −2), turned 90° around +Y. The camera is at (5, 0, 0) and looks at the origin. The projection has a 90° vertical field of view, aspect 2, near 1 and far 100 (standard 0..1 depth). The image is 800 × 400 pixels.
- Object(1, 0, 0)
- World(0, 0, −3)
- View(3, 0, −5)
- Clip(1.5, 0, 4.04, 5)
- NDC(0.3, 0, 0.808)
- Pixel(520, 200)
1. Object space
The point is one unit to the right of the mesh's pivot.
2. World space
First the rotation (the scale is 1), then the translation, as in M = T · R · S.
3. View space
This is the picture in section 5.
4. Clip space
w is now the distance in front of the camera. The GPU clips against it.
5. NDC
Depth 0.808 at distance 5 of 100 already: depth values crowd toward the far end. That is the topic of the projection page.
6. Pixels
The vertex lands at pixel (520, 200), with depth 0.808.
9Pitfalls
- The image is upside down, or every face is culled
- A Y-up projection on Vulkan without a flip (section 7), or a flip done twice.
- Objects turn around the world origin instead of their center
- The matrix order is wrong: R · T instead of T · R.
- The model is mirrored, or inside out
- Data from a tool with the other handedness, converted with a rotation only.
- A matrix "does nothing" or does something strange
- Row-major data read as column-major: the matrix is transposed.
- Lighting looks wrong on scaled objects
- Normals transformed with the model matrix. They need the inverse transpose (Normal mapping).
- Far from the origin, vertices jitter
- A 32-bit float has only 24 bits of precision: at 100 km, steps are about 8 mm. Large worlds render relative to the camera (Large worlds).
- The camera spins wildly at the top or bottom
- lookAt with forward parallel to up, or Euler angles at gimbal lock. Clamp the pitch (Camera controllers).
10In RASM
RASM uses the glTF convention: a right-handed, Y-up world, and a camera that looks down its local −Z. Transforms are a position in doubles, a quaternion and a scale. Clip space is Y-up on every backend; the Vulkan backend flips Y with a negative viewport height (fix A). The GPU never sees world space: each view draws in its camera's render space, the world moved so that the camera is at the origin (Large worlds).
Where RASM follows the textbook, and where it differs
| Part | This page | RASM |
|---|---|---|
| Handedness, up axis | a choice; glTF is right-handed, Y-up | Right-handed, Y-up, like glTF. Imported glTF and OBJ data is used as it is, with no mirroring (D8). |
| Camera direction | looks down −Z | The same. Free-fly and trackball code use rotation * (0, 0, -1) as forward. |
| Model matrix | T · R · S, column vectors | Transform::toMatrix builds exactly T · R · S with glm (column vectors, column-major), in doubles. The instance's world matrix is that times SubMesh's transform; its row keeps the translation as a high and a low float. |
| Rotation | a unit quaternion | Transform holds a glm::quat, identity by default (B22). Transform::rotate multiplies the new turn on the left, so it turns around a world axis. |
| View matrix | RT · T(−eye), or lookAt | getViewMatrix: mat4_cast(conjugate(rotation)) * translate(-position), a glm::dmat4. No general inverse, and no lookAt: the controllers produce the rotation directly. The camera's scale is not used. The views draw with it moved into render space (toRenderMatrix), where it is the rotation alone. |
| Y flip for Vulkan | fix A, B or a final flip | Fix A, inside VulkanCommandBuffer::setViewport: y = y + height, height = -height. Callers pass a normal top-left rectangle. |
| Hand-written clip positions | must follow the clip convention | Clip space is Y-up, so statsOverlay.slang maps pixel rows with 1.0 - pixel.y / screen.y * 2.0, and occlusion culling maps NDC back to rows with 0.5 - y * 0.5. |
| Mouse | flip y | toInputNdc in cameraInput.cpp flips it, so mouse motion is Y-up too (Camera controllers). |
| Normals | inverse transpose | Computed once per instance on the CPU (normalMatrix in the instance row), not per vertex. |
Where it lives in code
| What | Where |
|---|---|
| Position, rotation, scale | Transform in include/rasm/transform.h; Transform::toMatrix, Transform::rotate in src/scene/transform.cpp |
| Instance world matrix | Renderer::writeInstanceRows in src/renderer/gpuScene.cpp |
| View matrix | Renderer::getViewMatrix in src/scene/camera.cpp |
| Matrices into the frame | Renderer::prepareSceneView in src/renderer/gpuScene.cpp (SceneData's view and projection) |
| Object to clip | vertMain in shaders/passes/rasm.slang |
| The Y flip | VulkanCommandBuffer::setViewport in src/rhi/vulkan/vulkanCommandBuffer.cpp |
| glm's depth range | GLM_FORCE_DEPTH_ZERO_TO_ONE in src/CMakeLists.txt (B2) |
- Clip space is Y-up everywhere. Pass a normal, top-left rectangle to
setViewport; the backend flips it. Do not flip again in a projection. - A shader that writes a clip position by hand uses Y-up (+1 is the top row).
- Do not mirror imported data to change handedness. A mirror flips the winding, and face culling depends on it (Face culling).
- glm config macros belong in CMake, not in a
.cpp(B2). - Transform math is covered by
tests/transformTests.cppandtests/cameraTests.cpp.
Further reading
- Tomas Akenine-Möller and others, Real-Time Rendering, 4th edition: chapter 4, Transforms.
- Eric Lengyel, Foundations of Game Engine Development, Volume 1: Mathematics (2016).
- Ken Shoemake, Animating Rotation with Quaternion Curves, SIGGRAPH 1985: quaternions and slerp.
- The Vulkan specification: the sections "Coordinate Transformations" and "Controlling the Viewport".
- The glTF 2.0 specification: "Coordinate System and Units".