New to graphics? You can skip this post for now and go straight to part 9, Rasterisation: nothing later in the series depends on it. Come back when you move between engines or graphics APIs, or when something appears mirrored, upside down or out of place.
Introduction#
The maths in part 4, part 5 and part 6 is the same everywhere, but the way it’s written down isn’t. Books, engines and graphics APIs make different choices, and mixing them up gives objects in the wrong place, mirrored scenes or nothing at all. This post separates the four choices that cause the most confusion. (A point going into a matrix multiplication is usually called a vector: just a list of numbers, such as (x, y, z).)
- Row vectors or column vectors: which side of the matrix the point goes on
- Row-major or column-major storage: what order the 16 numbers sit in memory
- Left-handed or right-handed coordinates: which way z points
- The NDC conventions: the depth range and which way is up
The key idea to take away: these are independent choices. Each one is easy on its own; the trouble comes from assuming two of them always go together.
Choice 1: Row Vectors or Column Vectors#
So far this series has written points as columns on the right of the matrix: p′ = M × p. The translation then sits in the last column, and in a chain of matrices the one on the right happens first.
You can equally write points as rows on the left: p′ = p × M. For the same answer, the matrix must be flipped along its diagonal, or transposed: rows become columns. The translation then sits in the bottom row, and the chain reads left to right: p × S × R × T means scale, then rotate, then translate.
Neither is more correct. OpenGL’s documentation, GLSL (the language OpenGL and WebGL shaders are written in, covered in part 7) and most maths textbooks use column vectors. Microsoft’s maths library for Direct3D uses row vectors, as did older Direct3D documentation. HLSL, the shader language for Direct3D, lets you use either.
Choice 2: Row-Major or Column-Major Storage#
Memory is one long line of numbers, so a 4×4 matrix is laid out as 16 numbers in a row (as floats, short for floating-point numbers: how computers store numbers with a decimal point). Row-major storage writes the first row, then the second, and so on; column-major writes the first column, then the second.
This is a separate choice from choice 1: how numbers are stored, not how the maths is written. It matters because when your program hands 16 numbers to a shader, the shader must know their order. GLSL (and so WebGL) and HLSL both read matrices column-major by default, though both can be told otherwise.
The demo below shows a transformation (turn and move a crate) written both ways, stored both ways, and then uploaded to your graphics card exactly as stored, to be used by a shader that does matrix × vector, the standard GLSL way.
The same transform under different conventions, and what WebGL makes of the 16 floats
Written on paper:
In memory, in the order they're uploaded (float 0 first):
Colours show what each number means: red, green and blue for where the x, y and z axes go, and orange for the translation. Try all four combinations. You’ll find:
- Column vectors + column-major works: the translation lands in floats 12, 13 and 14.
- Row vectors + row-major also works, and produces exactly the same 16 floats. The matrix on paper is transposed, but it’s stored row by row, so the two flips cancel out.
- The other two combinations put the translation in floats 3, 7 and 11, and the crate breaks.
That’s why a matrix built by a row-vector maths library often goes straight into a shader that does matrix × vector unchanged: the memory is already right. It’s also why “I’ll transpose it” fixes so many bugs, and why the fix is so confusing when you don’t know which choice actually differed.
The graphics card has no idea what a “matrix” is. To it, a matrix handed to a shader is just 16 floats in memory, and the shader’s multiplication becomes a handful of multiply-and-add steps reading those floats in the order the shading language’s rules say. So the convention is a setting of the shading language and API: GLSL and HLSL both have keywords to choose row-major or column-major, and OpenGL can transpose a matrix as it’s uploaded.
Choice 3: Left-Handed or Right-Handed#
Take x to the right and y up. Should z point towards you, out of the screen, or away from you, into it? Hold up your right hand with the thumb along x and index finger along y: your middle finger points towards you. That’s a right-handed system. Do the same with your left hand and it points away: left-handed.
Handedness also decides which way a positive rotation turns: in a right-handed system, curl your right fingers round an axis with the thumb along it, and positive rotations turn the way they curl.
The same numbers in a right-handed and a left-handed system
Switching systems mirrors the picture: same coordinates, same rotation matrix, but z and the direction of turning are flipped. So putting a model from a left-handed modelling program into a right-handed engine without converting gives a mirror image, and back-face culling (part 3) culls the wrong side, because mirroring flips the winding order.
This one varies too: OpenGL’s tradition (and this site’s demos) is right-handed in world and view space; Direct3D’s older helper libraries offered both but leaned left-handed; the Unity engine is left-handed with y up; the Blender modelling program is right-handed with z up. Using z for “up” instead of y is yet another independent choice.
Choice 4: NDC Depth Range and Which Way Is Up#
Finally, the graphics APIs themselves disagree about the box that clip space maps to (part 5). WebGPU, in the table, is the newer graphics API for web browsers. “Pixel row 0” is which end of the picture the API counts rows from.
| NDC depth (z) | NDC y | Pixel row 0 | |
|---|---|---|---|
| OpenGL, WebGL | −1 to +1 | up | bottom |
| Direct3D | 0 to 1 | up | top |
| Vulkan | 0 to 1 | down | top |
| Metal, WebGPU | 0 to 1 | up | top |
So a projection matrix built for OpenGL is slightly wrong for Direct3D: it squeezes depth into the wrong range. That’s why maths libraries offer separate projection functions, and why moving drawing code between APIs usually involves a “flip y” somewhere. (Recent versions of OpenGL can also be switched to the 0 to 1 depth range, which helps depth precision, as part 11 explains.)
A Checklist for New Code#
When you meet an unfamiliar engine, library or tutorial, ask:
- Are points rows or columns? (Where’s the translation: bottom row or last column?)
- How are matrices stored in memory, and how does the shader read them?
- Is the coordinate system left- or right-handed, and which axis is up?
- Which NDC depth range and y direction does the API use?
Questions You Might Have#
Which conventions should I use?
Whichever your maths library, engine or existing code already uses, consistently everywhere. Neither choice is more correct; problems only come from mixing them.
Summary#
- Row vs column vectors is about how the maths is written; it transposes the matrix.
- Row-major vs column-major is about how numbers are stored; it’s a separate choice.
- Column vectors + column-major and row vectors + row-major give the same 16 floats.
- Handedness decides which way z points and which way rotations turn; mixing them mirrors the scene.
- APIs differ in NDC depth range and y direction.
Next, part 9 follows the transformed triangles into the rasteriser, which decides exactly which pixels they cover.
Further Reading#
- Fabian Giesen, Row major vs. column major, row vectors vs. column vectors: why these are two separate choices.
- Microsoft, Matrix ordering in HLSL and DirectXMath getting started: HLSL’s packing rules and DirectXMath’s row-vector convention.
- Khronos, Data Type (GLSL): how GLSL stores and multiplies matrices.
- Matthew Wellings, Vulkan’s coordinate system: what changed from OpenGL, and how to adapt a projection matrix.