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.

Object space the mesh file's numbers model World space one space for the scene view View space camera at 0, looks −Z projection Clip space 4D: (x, y, z, w) ÷ w NDC a cube: x, y in −1..1 viewport Pixels framebuffer, plus depth model, view, projection: in the vertex shader (your code) ÷ w and viewport: fixed GPU hardware
The vertex shader outputs a clip-space position. Everything after that is done by the GPU itself, with settings the application gives it (the viewport).

The same scene, as each space sees it:

pivot

Object (model, local)

Origin: the mesh's own pivot.

The numbers in the file. One mesh can be placed many times.

origin

World

Origin: the scene's origin.

All objects, lights and cameras in one space. Lighting and physics often work here.

−Z camera at 0

View (eye, camera)

Origin: the camera.

The world as the camera sees it. The camera sits at 0 and looks down −Z.

−w w clipped

Clip

Origin: —

4D homogeneous coordinates. The GPU clips triangles against −w ≤ x, y ≤ w here.

(−1, −1) (1, 1)

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.

Right-handed, Y-up +Y +X +Z (toward you) view: −Z X × Y = Z with the right-hand rule. Left-handed, Y-up +Y +X +Z (away) view: +Z The same picture, with Z mirrored.
In both, a camera with no rotation shows +X to the right and +Y up on the screen. Only the sign of the view direction differs.

Neither choice is better. What matters is that every part of a program agrees. Tools and file formats chose differently:

Tool or formatHandednessUp axis
glTF 2.0right+Y (a model's front faces +Z)
OpenGL tradition, Mayaright+Y
Blender, 3ds Maxright+Z
Unityleft+Y
Unreal Engineleft+Z

Converting between two of them is a fixed matrix:

123 123

A different up axis: a rotation

A rotation keeps the winding of every triangle.

123 123

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:

Object space 1. scale (2, 1) 2. rotate 90° 3. translate (2, 0.5) pivot at the origin twice as long turns around the pivot pivot now at (2, 0.5)
Seen along the Z axis, 25 pixels per unit. Scale first: if the rotation came first, scaling X would stretch the turned arrow sideways instead of along its length.

Swap the rotation and the translation, and the object lands somewhere else:

1. move 2. swings around the origin

R · T: move, then rotate

The object turns around the world origin instead of its own pivot.

1. rotate 2. move

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:

world origin M car car pivot M wheel in car wheel pivot M wheel = M car · M wheel in car
Mwheel = Mcar · Mwheel in car. A child under a parent with a non-uniform scale can end up with shear, which no single T · R · S can describe. That is why engines often store full matrices for world transforms.
· p′ = M · p · p′ = p · M

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.

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 memory order: column after column m[3] = cells 12 … 15: the translation column

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*
axis a v v′ θ q: four numbers, length 1 wxyz cos(θ/2) sin(θ/2) · a.x sin(θ/2) · a.y sin(θ/2) · a.z how far around which axis q and −q are the same rotation (θ and θ + 360°).
The half angle is not a typo: q · v · q* applies q twice, once on each side.

Why a quaternion: 4 numbers, and no special angles where it breaks.

q₁ q₂

Combine

Two rotations in a row are one product: q2 · q1 (q1 acts first).

q q*

Undo

The conjugate q* = (w, −x, −y, −z) turns back.

t = 0 t = 1 t = 0.5

Blend

Slerp blends two rotations at an even speed.

drifted length 1

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.

r.xu.x… r.yu.y… r.zu.z…

3×3 matrix

9 numbers. Good for transforming many points; the GPU.

Problem: drifts away from a pure rotation; hard to blend

axis angle

Axis and angle

4 numbers. Good for building a rotation.

Problem: combining two is awkward

w x y z

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.

World space (seen from above) +X −Z origin object (0, 0, −3) camera right looks camera (5, 0, 0) V View space +X (camera right) −Z (forward) camera (0, 0, 0) object (3, 0, −5) 5 ahead 3 right
The view matrix does not move the camera. It moves the whole world, so that the camera ends up at the origin, looking down −Z. 22 pixels per unit.

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:

eye target f up (hint) r u Steps f = normalize(target − eye) r = normalize(f × up) u = r × f r, u, −f are the camera's X, Y, Z in world space. u is at right angles to f, even when the up hint is not.
The picture is 3D: r points out of the page toward you. In a right-handed system, forward × up gives right.
lookAt breaks straight up and straight down

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.

the camera, and what it can see the point drag: the camera, and where it looks
Drag the camera around the point: the point never moves in the world, but it moves in view space. Then turn the camera away until the point leaves the image, and put the point behind the camera.

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)
NDC −1 x +1 viewport Framebuffer (vx, vy) width height Pixel centers (0, 0) (0.5, 0.5) (1.5, 0.5) (2, 2)
The viewport rectangle (vx, vy, width, height) and the depth range come from the application. Pixel centers sit at half-integers: the top-left pixel covers 0..1 and its center is (0.5, 0.5).

7Y up or Y down

The APIs do not agree on which way NDC y points:

+y row 0 depth −1..1

OpenGL

NDC y = +1 is the top. NDC depth −1..1. Pixel row 0 is at the bottom.

+y row 0 depth 0..1

Direct3D 11/12

NDC y = +1 is the top. NDC depth 0..1. Pixel row 0 is at the top.

+y row 0 depth 0..1

Metal

NDC y = +1 is the top. NDC depth 0..1. Pixel row 0 is at the top.

+y row 0 depth 0..1

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:

clip: +y up screen: upside down

No fix

Vulkan maps y = +1 to the bottom row. The image is upside down on screen, and every winding is reversed.

clip: +y up height < 0

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)

clip: +y down screen: upright

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

rendered copy flipped copy

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.

  1. Object(1, 0, 0)
  2. World(0, 0, −3)
  3. View(3, 0, −5)
  4. Clip(1.5, 0, 4.04, 5)
  5. NDC(0.3, 0, 0.808)
  6. Pixel(520, 200)

1. Object space

+X −Z pivot p p = (1, 0, 0) The number stored in the mesh. Top view: X right, −Z up the page. 40 pixels per unit.

The point is one unit to the right of the mesh's pivot.

2. World space

+X −Z origin pivot (0, 0, −2) p (0, 0, −3) R · p = (cos 90°, 0, −sin 90°) = (0, 0, −1) T · (R · p) = (0, 0, −1) + (0, 0, −2) p_world = (0, 0, −3) A turn by +90° around +Y takes +X to −Z: counter-clockwise, seen from above.

First the rotation (the scale is 1), then the translation, as in M = T · R · S.

3. View space

f = normalize((0,0,0) − (5,0,0)) = (−1, 0, 0) r = f × (0,1,0) = (0, 0, −1) u = r × f = (0, 1, 0) x = r·p − r·eye = 3 − 0 = 3 y = u·p − u·eye = 0 − 0 = 0 z = −f·p + f·eye = 0 − 5 = −5 p_view = (3, 0, −5) 3 units to the right of the camera, 5 units in front of it (z is negative).

This is the picture in section 5.

4. Clip space

focal = 1 / tan(90° / 2) = 1 x_c = focal / aspect · x = 0.5 · 3 = 1.5 y_c = focal · y = 0 z_c = −100/99 · z − 100/99 = 4.0404 w_c = −z = 5 p_clip = (1.5, 0, 4.04, 5) Inside: −5 ≤ 1.5 ≤ 5, and 0 ≤ 4.04 ≤ 5. −w = −5 0 w = 5 x = 1.5 0 w = 5 z = 4.04 The z row is the standard 0..1 depth for near 1, far 100 (see the projection page).

w is now the distance in front of the camera. The GPU clips against it.

5. NDC

(−1, −1) (1, 1) (1.5, 0, 4.04) / 5 p_ndc = (0.3, 0, 0.808) x = 0.3: a bit right of the center. Depth 0.808 for a point at distance 5.

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

(0, 0) (800, 400) (520, 200) x_p = (0.3 + 1) / 2 · 800 = 520 y_p = (1 − 0) / 2 · 400 = 200 Row 0 at the top, so y is flipped: the API or the viewport does this.

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).

CPU Transform::toMatrix entity T · R · S × sub-mesh, doubles getViewMatrix moved to render space: R only getProjectionMatrix fitted to the target's aspect Instance row world[3] + translationLow SceneData view · projection · origin GPU vertMain P · V · (world − origin) · pos Viewport, height < 0 Y-up clip → rows top-down The camera's scale is ignored. The world matrix of an instance is the entity's Transform times the sub-mesh's own matrix (for glTF, the node hierarchy, baked at import).
The CPU writes the matrices into GPU tables; the vertex shader multiplies them for every vertex. See The GPU scene for the tables.

Where RASM follows the textbook, and where it differs

PartThis pageRASM
Handedness, up axisa choice; glTF is right-handed, Y-upRight-handed, Y-up, like glTF. Imported glTF and OBJ data is used as it is, with no mirroring (D8).
Camera directionlooks down −ZThe same. Free-fly and trackball code use rotation * (0, 0, -1) as forward.
Model matrixT · R · S, column vectorsTransform::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.
Rotationa unit quaternionTransform 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 matrixRT · T(−eye), or lookAtgetViewMatrix: 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 Vulkanfix A, B or a final flipFix A, inside VulkanCommandBuffer::setViewport: y = y + height, height = -height. Callers pass a normal top-left rectangle.
Hand-written clip positionsmust follow the clip conventionClip 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.
Mouseflip ytoInputNdc in cameraInput.cpp flips it, so mouse motion is Y-up too (Camera controllers).
Normalsinverse transposeComputed once per instance on the CPU (normalMatrix in the instance row), not per vertex.

Where it lives in code

WhatWhere
Position, rotation, scaleTransform in include/rasm/transform.h; Transform::toMatrix, Transform::rotate in src/scene/transform.cpp
Instance world matrixRenderer::writeInstanceRows in src/renderer/gpuScene.cpp
View matrixRenderer::getViewMatrix in src/scene/camera.cpp
Matrices into the frameRenderer::prepareSceneView in src/renderer/gpuScene.cpp (SceneData's view and projection)
Object to clipvertMain in shaders/passes/rasm.slang
The Y flipVulkanCommandBuffer::setViewport in src/rhi/vulkan/vulkanCommandBuffer.cpp
glm's depth rangeGLM_FORCE_DEPTH_ZERO_TO_ONE in src/CMakeLists.txt (B2)
Keep these true
  • 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.cpp and tests/cameraTests.cpp.

Further reading