What a machine computes

Four numbers and a window

A projection matrix is built from six numbers and one of them is not a number at all. Four sides carry the focal length and the principal point; the near and far planes move nothing a reader can see; and the bottom row, (0, 0, 1, 0), is the only place the depth divides — set it to (0, 0, 0, 1) and the same machine draws a parallel projection.

Worth reading first: The divide is postponed, not avoided · A pixel is not a point.

The divide is postponed builds a projection matrix from a camera and two distances and checks that it reproduces this site’s pinhole to five parts in a hundred trillion. This essay asks the question the other way round: given the matrix, which of its numbers are the camera?

The usual presentation makes the question hard to ask. A matrix built from a field of view and an aspect ratio has the answer hidden inside two derived quantities. Built from a general frustum — six numbers, four sides and two planes — it is visible.

A frustum is four numbers and a windowThe camera's own section, looking down on it. The two slanted lines are the left and right sides: divide either by the near plane and it is a direction, -0.4663 and 0.4663, which between them carry the focal length 739.9 px and the principal point 345. The two crossbars are the near and far planes, and moving either of them moves no mark in the picture at all.the eyenear 0.90 mfar 4.20 mfocal 740 pxprincipal 345, 210
Fig. 1 The camera’s own section, looking down on it. Two slanted lines are the left and right sides of the frustum; two crossbars are the near and far planes.
One projection, two routes: divide by depth, or multiply and divide laterThe same box through the site's pinhole and through a 4×4 projection matrix with the divide postponed until after clip space. The worst disagreement over all twelve edges is 4.0e-14 px, which is the noise floor of double precision rather than an approximation.x/z, y/z — the pinholeM·p, then divide by wworst disagreement 4.0e-14 px over 8 verticescorrect from 21 cm, at 160 mm wide42° across · near 0.1 m, far 1000 m
Fig. 2 The check that licenses everything here: the matrix pipeline and this collection’s own pinhole put every point on the same pixel.

Six numbers, and what each of them is for

The frustum’s six are the left, right, top and bottom sides — measured on the near plane — and the near and far distances themselves. Written that way the matrix has no field of view in it and no aspect ratio, and both of those turn out to be derived quantities that exist only when the frustum is symmetric.

The convention needs one note, because getting it backwards is this site’s oldest recorded gotcha. top is above bottom in a reader’s sense and smaller as a number, because these camera coordinates run y down the page, which is the computer-vision convention this collection uses throughout. A frustum built with the two swapped produces a picture that is perfectly consistent, mirrored vertically, and passes every assertion that asks whether a mark is where it should be relative to other marks.

A frustum is four numbers and a windowThe camera's own section, looking down on it. The two slanted lines are the left and right sides: divide either by the near plane and it is a direction, -0.4663 and 0.4663, which between them carry the focal length 739.9 px and the principal point 345. The two crossbars are the near and far planes, and moving either of them moves no mark in the picture at all.the eyenear 0.40 mfar 6.00 mfocal 740 pxprincipal 345, 210
Fig. 3 The same camera with both planes moved. The two slanted sides are at exactly the same angles, because they are ratios and the near plane has cancelled.

The four sides are ratios

The sides are quoted on the near plane, which is what every graphics interface does and what makes them look like distances. They are not distances. Divide any of them by the near plane and it cancels:

leftnear=pxf,rightnear=Wpxf\frac{\text{left}}{\text{near}} = \frac{-p_x}{f}, \qquad \frac{\text{right}}{\text{near}} = \frac{W - p_x}{f}

which are the directions the leftmost and rightmost pixels look along. For the camera in the figure they are −0.4663 and +0.4663, and between them they carry the focal length of 739.85 pixels and the principal point at the middle of a 690-pixel frame, and nothing else.

That is the first claim and it is checkable rather than descriptive. Build the matrix from the six frustum numbers and build it from the camera, and the two agree entry for entry — every one of the sixteen, at every near plane tried, to under a part in a hundred trillion. Move the near plane by a factor of two hundred and the two lateral rows do not change at all.

So an off-centre frustum is not a distorted view. It is the same eye looking through a window that is not centred on it, and the site already has that statement in another vocabulary: the principal point is not the centre is about a shift lens, which moves the principal point without tilting the picture plane. An off-centre frustum is a shift lens written as a matrix.

Level, tilted, shiftedTilting the camera up to fit the building in makes the verticals converge by 4.55°. Shifting the lens up instead moves the principal point 120 px off centre, frames the same view, and leaves the verticals parallel to 0e+0° — because the picture plane never tilts.level — the top is cut off0.00° of spreadtilted 13°4.55° of spreadshifted 120 px0.00° of spreada shift moves every point by exactly the shift120.0 px, and no direction at all
Fig. 4 The same thing as an instrument. A shift lens moves the principal point without tilting the picture plane, which is why it keeps verticals parallel.

The two planes are not camera parameters

Nothing a reader can see moves when the near or far plane moves. Every x and every y in the picture is unchanged, because neither plane appears in the first two rows.

What the planes decide is which points exist — the clipper keeps what is between them — and how the ones that do get ordered, since the third row maps depth into a stored code. Both are real and neither is visible in a still picture of a scene entirely inside the frustum.

That is worth stating plainly because the two planes are usually presented alongside the field of view as though all three were properties of the camera. They are not the same kind of thing at all: a field of view is a fact about an instrument, and a near plane is a decision about a computation. The next two essays are about what each of the planes costs, and the answers are wildly unequal.

A frustum is four numbers and a windowThe camera's own section, looking down on it. The two slanted lines are the left and right sides: divide either by the near plane and it is a direction, -0.6285 and 0.3041, which between them carry the focal length 739.9 px and the principal point 465. The two crossbars are the near and far planes, and moving either of them moves no mark in the picture at all.the eyenear 0.90 mfar 4.20 mfocal 740 pxprincipal 465, 210
Fig. 5 The principal point moved a long way off centre. The frustum is lopsided and the camera is unchanged; this is a shift lens written as four numbers.

What the four sides can be, and cannot

The sides are four independent numbers, and it is worth asking what arrangements of them are legitimate, because two of the four constraints are not obvious.

Left below right, top above bottom. A frustum with zero or negative width has no picture in it, and the construction refuses rather than producing a matrix that divides by zero somewhere downstream. That refusal matters more than it sounds: a degenerate frustum built from a tile of zero width would otherwise produce a matrix full of infinities, and every point put through it would land at a plausible-looking coordinate.

Near strictly in front of the eye. A near plane at zero is refused. It is the one place in the whole construction where a limit does not exist: the sides are quoted on the near plane, so a near plane at the eye makes all four of them zero and the ratios that carry the camera become nothing over nothing.

The sides need not straddle the axis. This is the one that surprises. Both left and right may be positive, which is a frustum entirely to one side of the optical axis, and the matrix is perfectly well-formed. That is what a tile of the right-hand edge of a picture is, and it is what a projector aimed above its own axis is — a real instrument, drawing a real picture, whose field of view in the usual sense does not contain its own axis.

And near and far are otherwise free. Any positive pair with far beyond near gives a valid matrix. There is no constraint tying them to the scene, and this is exactly why they can be moved without moving a mark.

Clip space: the box the frustum becomes, and the plane the segment stops atA plan section through the frustum. The segment runs from 6.60 m in front of the eye to 2.99 m behind it, and the clipper stops it on the near plane at 0.40 m. Clipping happens here, before the divide, because both ends are still finite numbers at this stage.near 0.4 mtoward the far planethe eyein front3.0 m behind the eyeclipped hereeverything below the eye's line has a negative w, and dividing by it flips both signsplan section · near 0.4 m, far 26 mthe segment crosses the eye plane at t = 0.647
Fig. 6 What the postponed divide is protecting: clip space, where a point behind the eye is still a perfectly good finite point and the divide has not yet ruined it.

The bottom row

The last row of a projection matrix is (0, 0, 1, 0), and it is the only place the depth enters as a divisor. It says: the fourth coordinate of the result is the depth. Everything downstream divides by it, and the divide is what makes the picture a perspective.

Set that row to (0, 0, 0, 1) instead. Now w is 1 for every point, the divide does nothing, and what comes out is an affine map of the scene — a parallel projection, drawn by the same machine through the same window.

One entry is the whole differenceThe same box through the same matrix, with the bottom row (0, 0, 1, 0) and with it flattened to (0, 0, 0, 1). Flattened, w is 1 for every point, the divide does nothing, and the result preserves midpoints to 6e-14 px — a parallel projection. Unflattened, the midpoint of the same segment misses the midpoint of its marks by 32.9 px. The eye taken to infinity is one entry of a matrix.parallel drift 6e-14 px · perspective drift 32.9 pxcorrect from 17 cm, at 160 mm widedrawn true at 6 m
Fig. 7 The same box through the same matrix, with the bottom row (0, 0, 1, 0) and with it flattened to (0, 0, 0, 1).

It really is one: the flattened matrix preserves midpoints to a few parts in a hundred million million of a pixel, which is the definition parallel projection is not primitive uses, and the same segment through the unflattened matrix misses its own midpoint by nearly thirty-three pixels.

One rescaling is needed when the row is flattened, and it is not a fudge. The lateral entries are ratios that were going to be divided by the depth and now are not, so the drawing has to be told which depth it is true at — the same “one plane is drawn at true size” decision the oblique systems make by decree. That plane is the only parameter the conversion has.

One cube in 4 parallel drawing systemsEvery one preserves midpoints exactly. What separates them is the axis scales, printed beneath each — isometric's are all 0.8165, which is equal and is not 1.elevationx 1.000y 1.000z 0.000cavalierx 1.000y 1.000z 1.000isometricx 0.816y 0.816z 0.816dimetricx 0.943y 0.943z 0.471axis scales measured from the drawingall 4 preserve midpoints
Fig. 8 Where the flattened matrix lands: an orthographic projection along the camera’s own axis, which is one point of the axonometric family rather than a free choice.

What the flattened matrix is, in the field’s own terms

Flattening the row does not produce a parallel projection. It produces the orthographic projection along the camera’s own optical axis, at whatever scale the chosen true-depth plane fixes — which is one point of the two-parameter family the axonometric ladder is about, and not a free one.

To get an oblique projection out of the same machine, more than the bottom row has to change. An oblique system’s rays are not perpendicular to the picture plane, and that shows up as off-diagonal entries in the top two rows: a term coupling z into x and into y, which is exactly a shear. So cavalier and cabinet are the flattened matrix with two extra entries filled in, and their kernel is not the optical axis.

Which is the same statement oblique is a shear makes from the drawing-board side, arriving as an entry pattern. The matrix formulation is worth having because it makes the three families’ relationship a matter of which entries are non-zero: a full bottom row is perspective, a flattened one with a diagonal top-left is orthographic, and a flattened one with off-diagonal terms is oblique. Three fields of this site, distinguished by the sparsity pattern of one four-by-four array.

One entry is the whole differenceThe same box through the same matrix, with the bottom row (0, 0, 1, 0) and with it flattened to (0, 0, 0, 1). Flattened, w is 1 for every point, the divide does nothing, and the result preserves midpoints to 6e-14 px — a parallel projection. Unflattened, the midpoint of the same segment misses the midpoint of its marks by 32.9 px. The eye taken to infinity is one entry of a matrix.parallel drift 6e-14 px · perspective drift 32.9 pxcorrect from 17 cm, at 160 mm widedrawn true at 6 m
Fig. 9 The flattened matrix’s drawing alone. It preserves midpoints, which is the definition this collection uses for a parallel projection.

Which makes the limit a matrix entry

The eye taken to infinity says a parallel projection is a photograph from infinitely far away with the lens lengthened to match, and watches the limit happen. This is the same statement with the limit already taken.

The two accounts have to agree, and the agreement is measurable. Move a real pinhole back to distance D with its focal length grown as D·k so the subject stays the same size, and difference its picture against the flattened matrix’s. The departure falls as D to the power 1.007 — one over the distance, fitted rather than assumed.

The limit, and the rate it is reached atA cube photographed from ever further away with the focal length grown to match, differenced against the flattened matrix's own picture. The departure falls as D to the power -1.007 — one over the distance, fitted rather than assumed — so a picture is a *thousand* times nearer to parallel at a kilometre than at a metre, and never actually parallel at any distance.-2.50-2-1.50-111.5022.50distance of the eye (powers of ten, in units of the box)worst departure from the parallel picture (powers of ten, px)fitted slope -1.007one over the distancea limit, never an arrival
Fig. 10 A cube photographed from ever further away with the focal length grown to match, differenced against the flattened matrix’s picture. The slope is the rate.

An exponent of one rather than two is worth a sentence. The leading term in the departure is the scene’s depth range divided by the eye’s distance, so halving the depth of the object is exactly as good as doubling the distance — which is why a long lens flattens a shallow subject convincingly and a deep one not at all.

Fitting that exponent turned up a trap the collection’s convergence gate now guards. Measured against a reference camera four times beyond the last rung of the ladder the exponent came out 1.13; measured against one four orders beyond it, 1.01. The reference was itself a perspective picture, and its own error was being subtracted from every rung. A limit measured against a nearby stand-in is a limit of the stand-in.

A frustum is four numbers and a windowThe camera's own section, looking down on it. The two slanted lines are the left and right sides: divide either by the near plane and it is a direction, -0.2771 and 0.6555, which between them carry the focal length 739.9 px and the principal point 205. The two crossbars are the near and far planes, and moving either of them moves no mark in the picture at all.the eyenear 0.90 mfar 4.20 mfocal 740 pxprincipal 205, 210
Fig. 11 The window moved the other way. Nothing about the eye has changed and the picture is a different crop of the same projection.
Where the sample sits inside the pixel, and what a recovery calls the differenceSampling at the pixel's corner instead of its centre translates every mark by half a pixel in each axis — 0.7071 px — and the site's own recovery reads that as a principal point 0.707 px from where it should be, with the focal length changed by 4.5e-13 px. The other half-pixel error, mapping the viewport onto W−1 pixels instead of W, does the opposite: it leaves the principal point alone and shortens the focal length by 1.30 px.one pixel is an areacentrescornersprincipal point moves0.707 pxfocal length changes by4.5e-13 pxan edge-versus-centre viewport1.303 pxa half-pixel convention is a principal-point error; an off-by-one viewport is a focal-length error8 vertices, all shifted by the same 0.7071 pxspread across marks 0.0e+0 px
Fig. 12 The other end of the same window: where inside a pixel the sample sits is a convention, and getting it wrong is a principal-point error rather than a blur.

The window metaphor, taken literally

The word window is doing real work here and not decoration, so it is worth pushing until it breaks.

A person at a fixed place in a room, looking through a rectangular hole in a wall, sees a picture. The four sides of the hole are the four sides of the frustum: they decide what is in view, and moving the person changes the picture while moving the hole changes only the framing. That is the sense in which the four sides carry the principal point — an off-centre hole is a hole with the eye not opposite its middle, and the picture through it is a crop of the picture that a centred hole would give, not a different projection.

The near and far planes have no counterpart in the room, and that is the honest limit of the metaphor. Nothing in a wall says stop drawing things closer than ninety centimetres. They exist because the computation needs somewhere to put the depth: a stored depth has to be a bounded quantity mapped into a fixed range of codes, and the two planes are what fix the range.

So the metaphor is exact for the four numbers that are the camera and empty for the two that are not, which is a reasonable way to remember which is which. Anything a person leaning through a window could change is in the sides; anything only a computation cares about is in the planes.

Where a depth buffer's codes actually goThe share of a 24-bit buffer used up within each distance, for a near plane at 0.1 m and a far plane at 1000 m. Ninety per cent of the range is spent inside the first metre; the last nine hundred metres share what is left.near 0.1 m · far 1000 m · 24-bit codeswithin 20 cm50.0%within 50 cm80.0%within 1 m90.0%within 5 m98.0%within 50 m99.8%half the codes are gone by 0.20 m — the harmonic mean of the two planesshare of the depth buffer used up within each distancearithmetic midpoint would be 500 m
Fig. 13 What the two planes decide, which is the whole of the next two essays: how the stored depth is spread, and therefore how well the scene is ordered.
The limit, and the rate it is reached atA cube photographed from ever further away with the focal length grown to match, differenced against the flattened matrix's own picture. The departure falls as D to the power -1.007 — one over the distance, fitted rather than assumed — so a picture is a *thousand* times nearer to parallel at a kilometre than at a metre, and never actually parallel at any distance.-2.50-2-1.50-111.5022.50distance of the eye (powers of ten, in units of the box)worst departure from the parallel picture (powers of ten, px)fitted slope -1.007one over the distancea limit, never an arrival
Fig. 14 The rate again, as the closing measurement of the bottom row: one over the eye’s distance, fitted rather than assumed.

So the accounting is

Six numbers and one row, and they divide into three kinds:

Four sides — the camera. Focal length and principal point, and recoverable from a picture: this site takes cameras out of pictures throughout the foundations field, and what comes back is exactly these.

Two planes — a window on depth. Not recoverable from a picture, because nothing in the picture depends on them. Choices about what the computation keeps and how well it orders it.

One row — whether it is a perspective at all. Two values, and the choice between them is the choice between the construction field and the parallel field, made in one matrix entry.

Squeeze the pixels and every alarm this site has stays silentThe same box, drawn by a camera whose pixels are 2.00 times wider than they are tall. The recovery is shown the drawn edges and nothing else; it returns a focal length of 219.9 px against a true 436.4, a principal point 48.1 px away — and a spread across its three independent estimates of 0.0e+0, with bundle residuals of 9.1e-13 px.pixels 2.00 : 1recovered focal length219.9 pxthe camera's actual one436.4 pxwhat the recovery returnsspread across three estimates 0.0e+0worst bundle residual 9.1e-13 pxboth are what a wrong picture would trippixel aspect 2.00 unmodelledfocal length 49.6% short, every diagnostic clean
Fig. 15 The one thing the two lateral rows can disagree about: a pixel that is not square shows up as two different focal lengths, and an unmodelled one is absorbed into a confident wrong camera.
A frustum is four numbers and a windowThe camera's own section, looking down on it. The two slanted lines are the left and right sides: divide either by the near plane and it is a direction, -0.4663 and 0.4663, which between them carry the focal length 739.9 px and the principal point 345. The two crossbars are the near and far planes, and moving either of them moves no mark in the picture at all.the eyenear 1.60 mfar 3.20 mfocal 740 pxprincipal 345, 210
Fig. 16 A shallow window on depth. The two crossbars have closed right up and the two slanted sides are where they always were.

Reading the matrix back

Because the four sides carry the camera and nothing else, the camera can be read out of a matrix by inverting one line of arithmetic — which is a small thing and is the reason this decomposition is worth having at all.

From the first row’s two entries, 2·near/(right − left) and −(right + left)/(right − left), the focal length is the first times the frame’s half-width, and the principal point is where the second says the axis crosses. The second row gives the same two numbers again, from the height, and the two answers must agree for square pixels; where they do not, the difference is a pixel aspect, which is exactly the quantity the pixel that is not square is about and which an unmodelled version of absorbs into a confident wrong camera.

So a projection matrix is a camera plus two window parameters, and the separation is not a matter of interpretation — the entries are in different rows. That is the whole reason this essay can make a claim about which numbers are the camera rather than an argument about which ones feel like it.

There is one thing the reading cannot recover and it is worth naming. Nothing in the matrix says where the eye is or which way it is pointing; those live in the view transform that runs before it, and the projection matrix is written in the camera’s own coordinates throughout. So “the camera” here means the intrinsic camera — focal length, principal point, pixel shape — and the extrinsic half is somewhere else entirely. The site’s own recoveries take both out of a picture, and they take them out by different routes for the same reason.

The same lens behind five sensorsA 50 mm lens subtends 39.6° across full frame and 8.7° across a phone sensor. The focal length is the same in every one of these; what changes is the rectangle behind it. Two setups matched on angle rather than on focal length are correct from the same distance — 222 mm for a 160 mm print — whatever their formats.full frame · 39.6°APS-C · 26.6°Micro Four Thirds · 19.6°1 inch · 15.0°phone (1/1.7″) · 8.7°one 50 mm lens · the angle is a property of the rectangle behind itMicro Four Thirds: 2.00× diagonal, 2.08× wide, 1.85× tall50 mm across five formats39.6° down to 8.7°
Fig. 17 Why a field of view is derived rather than given: a focal length becomes an angle only once a rectangle of stated width is named.
A frustum is four numbers and a windowThe camera's own section, looking down on it. The two slanted lines are the left and right sides: divide either by the near plane and it is a direction, -0.5474 and 0.3852, which between them carry the focal length 739.9 px and the principal point 405. The two crossbars are the near and far planes, and moving either of them moves no mark in the picture at all.the eyenear 0.90 mfar 4.20 mfocal 740 pxprincipal 405, 210
Fig. 18 A modest shift, where the two half-angles differ by a few degrees and a single field of view is already the wrong description.

What this says about the field of view

The tidiest consequence is a small correction to how a rendered camera is usually described.

A field of view is not one of the six numbers. It is a derived quantity: for a symmetric frustum it is 2·arctan((right − left) / 2·near), and it exists only because the frustum happened to be symmetric. Give the frustum an off-centre principal point and there is no single field of view — there is a left half-angle and a right half-angle, and they differ.

That is not a defect of the description; it is the description catching up with the instrument. A shift lens has exactly that asymmetry, a tilted projector has it, and every tile of a tiled render has it. The essays that follow are all cases where the symmetric description has nothing to say and the six numbers do.

And it explains why a focal length is not an angle needs a rectangle named before it can quote a field of view. A focal length lives in the four sides; an angle needs the sides and the width they are measured across, and the width is the thing the sensor supplies. The frustum contains both, which is why it needs six numbers where a focal length needs one.

What links here

Computed from the collection, not written here: the essays that point at this one.

Reads more easily once this is understood

Essays that name this one as worth reading first.

Shares its objects with

Essays that name at least two of the same things, and that neither author linked.

Named objects

A flat tag is an object no other essay names yet.

Camera matrixClip spacedegrees of freedomFocal lengthHomogeneous coordinatesOrthographic limitParallel projectionPrincipal pointProjective limitView frustum