11. Coordinate Spaces and the Camera

Over the last few chapters we built a real toolbox: matrices that scale, rotate and move things (chapters 8 and 9), and inverse matrices that undo them (chapter 10).
In this chapter I'll put that toolbox to work on the most important object in any 3D program: the camera.
We'll follow one vertex on its trip from a model file to a pixel, learn what "changing coordinate spaces" really means, and build the famous lookAt view matrix from scratch.
By the end you'll be able to write a first-person camera and an orbit camera yourself.

11.1 The journey of a vertex

Imagine telling a friend where your coffee mug is.
You could say "10 cm to the left of my keyboard", or "in room 302 on the third floor", or give its GPS coordinates.
It's the same mug every time.
Only the reference frame changed: where you put the origin, and which directions you call the axes.

A 3D program does exactly this, several times for every vertex, every frame.
Each reference frame is called a space, and a matrix moves a point from one space to the next:

The chain of spaces from object space to screen space and the matrix or step between each pair

Space Where is the origin? What it's used for
Object (local) the model's pivot, chosen by the artist vertex data stored in the file
World one fixed point of the scene placing objects, physics, lights
View (camera) the camera's eye the camera looks down −Z here
Clip no simple origin (4D homogeneous) the GPU cuts away what's off-screen
NDC the center of the screen x and y squeezed into −1 to 1
Screen a corner of the window actual pixels

We write the first three arrows ourselves in the vertex shader.
The GPU does the last two for us.
You already know the model matrix: it's the 𝐓𝐑𝐒 matrix from chapter 9.
The projection matrix gets its own chapter next.
This chapter is about the view matrix, and about the one idea hiding behind every arrow in that diagram: change of basis.

11.2 Change of basis: the formula

Here is the picture.
The grid below has the usual x and y axes, but someone else uses tilted axes 𝐛1 and 𝐛2 instead.
Both of you can describe the same point P, you'll just write down different numbers.

The tilted axes b1 and b2 and the point P reached by walking 2 b1 plus 1 b2

Write the new basis vectors in the old coordinates and put them into a matrix as its columns:

𝐁=[𝐛1𝐛2𝐛3]

If a point has coordinates 𝐩new measured along the new axes, then its old coordinates are:

𝐩old=𝐁𝐩new

To go the other way, undo 𝐁 with its inverse (chapter 10):

𝐩new=𝐁−1𝐩old

And if the new axes are orthonormal (each one has length 1, and they're all perpendicular to each other), the inverse is simply the transpose:

𝐁−1=𝐁⊤so𝐩new=𝐁⊤𝐩old

When the new frame also has its own origin 𝐨 (written in old coordinates), we use the 4×4 trick from chapter 9 and put 𝐨 into the fourth column:

𝐩old=[𝐛1𝐛2𝐛3𝐨0001]𝐩new

11.3 How change of basis works

What does 𝐩new=(2,1) mean?
It means "walk 2 steps along 𝐛1, then 1 step along 𝐛2".
In old coordinates that walk is 2𝐛1+1𝐛2.
Remember chapter 6: a matrix times a vector is exactly a linear combination of the matrix's columns.
So 2𝐛1+1𝐛2=𝐁𝐩new, and that's the whole formula.

Going back is a question: "how many steps along 𝐛1 is this point?"
For an orthonormal basis, chapter 4 already answered it: the dot product with a unit vector measures how far a vector reaches along that direction.
So the new coordinates are just 𝐛1⋅𝐩 and 𝐛2⋅𝐩.
Doing several dot products at once is a matrix whose rows are 𝐛1 and 𝐛2, and that matrix is 𝐁⊤.
This is why the transpose undoes an orthonormal matrix, the same fact we used for rotations in chapter 10.

A worked 2D example

Take the new axes

𝐛1=[0.60.8],𝐛2=[−0.80.6]

First let's check that they're orthonormal:

‖𝐛1‖=0.36+0.64=1,‖𝐛2‖=0.64+0.36=1,𝐛1⋅𝐛2=−0.48+0.48=0

Good: both have length 1 and they're perpendicular.
(They're just the normal x and y axes rotated by about 53°.)
Now a point has new coordinates 𝐩new=(2,1).
New to old:

𝐩old=2[0.60.8]+1[−0.80.6]=[1.2−0.81.6+0.6]=[0.42.2]

And back again, old to new, with the transpose (rows are 𝐛1 and 𝐛2):

𝐁⊤𝐩old=[0.60.8−0.80.6][0.42.2]=[0.24+1.76−0.32+1.32]=[21]

We got (2,1) back, so the round trip works.
If the new axes were not orthonormal (say one of them had length 2), the transpose would give a wrong answer and you'd need the real inverse from chapter 10.

Here's the sentence I want you to remember from this section:

A matrix whose columns are the axes (and origin) of frame A, written in frame B's coordinates, converts points from A to B, and its inverse converts from B back to A.

The model matrix fits this exactly: its columns are the object's axes and position written in world coordinates, so it converts object space to world space.

11.4 The camera as an object

A camera is just an object in the world.
It has a position, the eye 𝐞, and an orientation given by three unit vectors:

  • 𝐫: right
  • 𝐮: up
  • 𝐟: forward, the direction the camera looks

These three are an orthonormal basis.
Now remember the convention of this whole series: in view space the camera looks down −Z.
With +X to the right and +Y up, a right-handed system puts +Z pointing out of the screen, towards you.
So "into the screen" is −Z, and the camera's own +Z axis points backwards, along −𝐟.
You can check the handedness: 𝐫×𝐮 is like 𝐢×𝐣=𝐤, which points backwards, so 𝐫×𝐮=−𝐟.

The camera seen from the side: u points up, f points forward and the camera +Z points backwards

Using the rule from the last section, the matrix that converts camera coordinates into world coordinates has the camera's axes and its position as columns:

𝐂=[rxux−fxexryuy−fyeyrzuz−fzez0001]

This is the camera's world matrix, exactly like any other object's model matrix.
It's a rotation 𝐑 (the first three columns) followed by a translation to the eye: 𝐂=𝐓(𝐞)𝐑.

11.5 The view matrix and lookAt: the formula

But we don't want camera to world.
We have vertices in world space and we want to know where they are relative to the camera: world to camera.
That's the inverse:

𝐕=𝐂−1

This is the view matrix.
There's a neat way to think about it: the GPU never moves a camera.
Instead, the view matrix moves the whole world so that the camera ends up at the origin, looking down −Z.
Stepping the camera 1 m to the right is the same as sliding the entire world 1 m to the left.

We don't need the general 4×4 inverse here, because chapter 10 gave us the cheap rules: "socks and shoes" (𝐀𝐁)−1=𝐁−1𝐀−1, a rotation's inverse is its transpose, and a translation's inverse is the opposite translation:

𝐕=(𝐓(𝐞)𝐑)−1=𝐑−1𝐓(𝐞)−1=𝐑⊤𝐓(−𝐞)

The rows of 𝐑⊤ are 𝐫, 𝐮 and −𝐟.
Multiplying by 𝐓(−𝐞) first subtracts the eye, then the rows dot the result, so the translation column becomes 𝐑⊤(−𝐞):

𝐕=[rxryrz−𝐫⋅𝐞uxuyuz−𝐮⋅𝐞−fx−fy−fz𝐟⋅𝐞0001]

Read it row by row and it makes sense: the new x of a point is "how far to the right of the eye is it", the new y is "how far above", and the new z is "how far behind" (negative means in front).

Of course nobody wants to type in 𝐫, 𝐮 and 𝐟 by hand.
The lookAt function builds them from three easy things: where the camera is (𝐞), what it looks at (the target 𝐭), and roughly which way is up (𝐮𝐩, usually the world's (0,1,0)):

𝐟=𝐭−𝐞‖𝐭−𝐞‖,𝐫=𝐟×𝐮𝐩‖𝐟×𝐮𝐩‖,𝐮=𝐫×𝐟

11.6 How lookAt works, step by step

Let me justify each of the three lines.

  1. Forward is the arrow from the eye to the target (chapter 2: target minus eye), normalized so it has length 1 (chapter 3).
  2. Right must be perpendicular to forward and to up, and a cross product (chapter 5) gives exactly a vector perpendicular to both. We normalize it because forward and the up hint are usually not perpendicular (a camera looking down at a slant), so the length of the cross product is sinθ, which is less than 1.
  3. True up is perpendicular to right and forward. It's already unit length, because 𝐫 and 𝐟 are unit vectors at 90° and 1⋅1⋅sin90°=1. This is the camera's real up, tilted along with the camera, not the world's up.

Let's check the directions with the simplest camera: looking down −Z with up = +Y.
Then 𝐫=(−𝐤)×𝐣=−(𝐤×𝐣)=−(−𝐢)=𝐢, which is +X, to the right.
And 𝐮=𝐢×(−𝐤)=−(𝐢×𝐤)=−(−𝐣)=𝐣, which is up.
The order inside the cross products matters (chapter 5: swapping them flips the sign), and this order is the one that matches our right-handed system.

A worked example

Camera at 𝐞=(3,0,4), looking at the origin 𝐭=(0,0,0), up hint (0,1,0).

Top view of the lookAt example with the eye at 3, 0, 4 looking at the origin

Step 1, forward.

𝐭−𝐞=(−3,0,−4),‖𝐭−𝐞‖=9+0+16=5,𝐟=(−0.6,0,−0.8)

Step 2, right. With the cross product formula (aybz−azby,azbx−axbz,axby−aybx) and 𝐛=(0,1,0):

𝐟×𝐮𝐩=(0⋅0−(−0.8)⋅1,(−0.8)⋅0−(−0.6)⋅0,(−0.6)⋅1−0⋅0)=(0.8,0,−0.6)

Its length is 0.64+0.36=1 already (because this camera looks perfectly horizontally), so 𝐫=(0.8,0,−0.6).

Step 3, true up.

𝐫×𝐟=(0⋅(−0.8)−(−0.6)⋅0,(−0.6)(−0.6)−0.8⋅(−0.8),0.8⋅0−0⋅(−0.6))=(0,1,0)

Step 4, the translation column.

−𝐫⋅𝐞=−(0.8⋅3+0⋅0+(−0.6)⋅4)=−(2.4−2.4)=0
−𝐮⋅𝐞=−(0⋅3+1⋅0+0⋅4)=0
𝐟⋅𝐞=(−0.6)⋅3+0⋅0+(−0.8)⋅4=−1.8−3.2=−5

Step 5, assemble. Rows 𝐫, 𝐮, −𝐟:

𝐕=[0.80−0.6001000.600.8−50001]

Step 6, check it.
The target (the origin) should be straight ahead at distance 5.
Multiplying (0,0,0,1) just picks the last column: (0,0,−5).
Straight ahead, 5 units away, exactly right.
The eye itself: x=0.8⋅3−0.6⋅4=0, y=0, z=0.6⋅3+0.8⋅4−5=0, so the camera sits at the origin of view space.
Now a point 𝐩=(4,2,−3):

x=0.8⋅4+0⋅2−0.6⋅(−3)+0=3.2+1.8=5
y=0⋅4+1⋅2+0⋅(−3)+0=2
z=0.6⋅4+0⋅2+0.8⋅(−3)−5=2.4−2.4−5=−5

So in view space 𝐩 is at (5,2,−5): 5 units to the right, 2 up, and 5 ahead.
Keep this point in mind, in chapter 12 we'll find out which pixel it lands on.

When lookAt breaks

If the camera looks straight up or straight down, forward is parallel to the up hint.
Then 𝐟×𝐮𝐩 is the zero vector (chapter 5: parallel vectors have a zero cross product).
Our normalize from chapter 3 returns the zero vector instead of dividing by zero, so 𝐫 and 𝐮 both become zero, the matrix squashes everything flat (its determinant is 0, chapter 10), and your screen goes blank or flickers.
The fixes are simple: never let the camera look exactly along the up hint (see pitch clamping below), or pass a different up hint for that case, like (0,0,−1) for a top-down map camera.

11.7 First-person and orbit cameras

First-person camera from yaw and pitch

In a first-person game you don't store a target point.
You store two angles that the mouse changes:

  • yaw θ: turning left and right, a rotation around the world's up axis Y
  • pitch ϕ: looking up and down, a rotation around the camera's right axis

Start with the forward direction (0,0,−1), pitch it around X, then yaw it around Y, using rotateX and rotateY from chapter 8: 𝐟=𝐑y(θ)𝐑x(ϕ)(0,0,−1).
The pitch step gives (0,sinϕ,−cosϕ), and the yaw step then turns that around Y.
Multiplying it out gives:

𝐟=[−sinθcosϕsinϕ−cosθcosϕ]

It's always unit length, using sin2+cos2=1 from chapter 1 twice:

‖𝐟‖2=cos2ϕ(sin2θ+cos2θ)+sin2ϕ=cos2ϕ+sin2ϕ=1

Worked example: yaw 60°, pitch 30°.
We need sin60°≈0.866, cos60°=0.5, sin30°=0.5 and cos30°≈0.866.

fx=−0.866⋅0.866=−0.75,fy=0.5,fz=−0.5⋅0.866=−0.433

Check: 0.5625+0.25+0.1875=1.
The camera looks left of −Z (because positive yaw is counter-clockwise seen from above, by the right-hand rule) and 30° upwards.
That also means that when the mouse moves right, yaw should decrease.

Clamping the pitch. At pitch ±90°, forward becomes (0,±1,0), parallel to the world's up, which is exactly the broken case from the last section.
Past 90° the view flips upside down.
So every first-person camera clamps the pitch to a bit less than 90°.
Here is a small camera update function (forwardFromYawPitch is the formula above written as code, you'll find it in section 11.10):

struct FpsCamera {
  Vec3 eye{0, 2, 0};
  float yaw = 0.0f, pitch = 0.0f;
};

// mouseDx, mouseDy: how far the mouse moved this frame, in pixels
inline Mat4 updateFpsCamera(FpsCamera &cam, float mouseDx, float mouseDy, float sensitivity) {
  cam.yaw -= mouseDx * sensitivity;   // mouse right -> turn right
  cam.pitch -= mouseDy * sensitivity; // screen y grows downwards
  const float limit = radians(89.0f);
  if (cam.pitch > limit) cam.pitch = limit;
  if (cam.pitch < -limit) cam.pitch = -limit;

  Vec3 forward = forwardFromYawPitch(cam.yaw, cam.pitch);
  return lookAt(cam.eye, cam.eye + forward, {0, 1, 0});
}

A small tip for walking: move along forwardFromYawPitch(yaw, 0.0f), the forward direction without pitch.
Otherwise looking at the floor makes you walk slower, the same kind of bug as the diagonal movement bug from chapter 3.
The strafe direction is normalize(cross(forward, up)).

Orbit camera

Model viewers and strategy games often circle around a target instead.
Same two angles plus a distance d, and the eye is placed behind the target, against the forward direction:

𝐞=𝐭−d𝐟

Then lookAt(eye, target, up) builds the view matrix.
Dragging the mouse changes yaw and pitch, the scroll wheel changes d.
A negative pitch looks down, so the eye ends up above the target.
For example, target (0,1,0), d=4, yaw 0 and pitch −30° give 𝐟=(0,−0.5,−0.866) and 𝐞=(0,1,0)−4(0,−0.5,−0.866)=(0,3,3.464): above and behind the target.

11.8 Use in graphics programming: P V M

Every vertex goes through the whole chain.
Reading right to left like always (chapter 7):

𝐩clip=𝐏𝐕𝐌𝐩object

The three matrices change at different rates:

  • 𝐌 is different for every object.
  • 𝐕 changes once per frame, when the camera moves.
  • 𝐏 changes only when the window is resized or the field of view changes.

Because matrix multiplication is associative (chapter 7), you can multiply some of them on the CPU and send fewer matrices.
A common setup sends 𝐏𝐕 once per frame and 𝐌 per object, because the shader usually needs the world position for lighting anyway.
Here is the straightforward version with all three:

struct Matrices {
  float4x4 model;
  float4x4 view;
  float4x4 proj;
};
ConstantBuffer<Matrices> matrices;

struct VertexOut {
  float4 position : SV_Position;
  float3 worldPos : TEXCOORD0;
};

[shader("vertex")]
VertexOut vertexMain(float3 position : POSITION) {
  VertexOut o;
  float4 world = mul(matrices.model, float4(position, 1.0));
  float4 viewPos = mul(matrices.view, world);
  o.position = mul(matrices.proj, viewPos);
  o.worldPos = world.xyz;
  return o;
}
#version 450
layout(set = 0, binding = 0) uniform Matrices {
  mat4 model;
  mat4 view;
  mat4 proj;
};

layout(location = 0) in vec3 inPosition;
layout(location = 0) out vec3 outWorldPos;

void main() {
  vec4 world = model * vec4(inPosition, 1.0);
  vec4 viewPos = view * world;
  gl_Position = proj * viewPos;
  outWorldPos = world.xyz;
}
struct Matrices {
  float4x4 model;
  float4x4 view;
  float4x4 proj;
};
ConstantBuffer<Matrices> matrices;

struct VertexOut {
  float4 position : SV_Position;
  float3 worldPos : TEXCOORD0;
};

VertexOut vertexMain(float3 position : POSITION) {
  VertexOut o;
  float4 world = mul(matrices.model, float4(position, 1.0));
  float4 viewPos = mul(matrices.view, world);
  o.position = mul(matrices.proj, viewPos);
  o.worldPos = world.xyz;
  return o;
}

A few more everyday uses of what we just built:

  • Camera position for lighting. Specular highlights need the eye position in world space. It's the fourth column of 𝐕−1, which is the camera's world matrix 𝐂. Compute it on the CPU and send it as a uniform.
  • Billboards. Particles and name tags should always face the camera. The first two rows of the view matrix are the camera's right and up in world space, so you can build the quad's corners from them.
  • Skyboxes. The sky must never get closer when you walk. Copy the view matrix and replace its fourth column with (0,0,0,1), so only the rotation is left: sky.c[3] = {0, 0, 0, 1};
  • Fog and sorting. In view space, −z is the distance in front of the camera. It's handy for fog and for sorting transparent objects from back to front.

Here's the billboard function.
Notice the indexing difference from chapter 6: Slang and HLSL read a row as view[0], while GLSL indexes column first, so the same row is spread across view[0][0], view[1][0] and view[2][0].

float3 billboardCorner(float4x4 view, float3 center, float2 corner, float size) {
  float3 right = float3(view[0][0], view[0][1], view[0][2]);
  float3 up = float3(view[1][0], view[1][1], view[1][2]);
  return center + (corner.x * right + corner.y * up) * size;
}
vec3 billboardCorner(mat4 view, vec3 center, vec2 corner, float size) {
  vec3 right = vec3(view[0][0], view[1][0], view[2][0]);
  vec3 up = vec3(view[0][1], view[1][1], view[2][1]);
  return center + (corner.x * right + corner.y * up) * size;
}
float3 billboardCorner(float4x4 view, float3 center, float2 corner, float size) {
  float3 right = float3(view[0][0], view[0][1], view[0][2]);
  float3 up = float3(view[1][0], view[1][1], view[1][2]);
  return center + (corner.x * right + corner.y * up) * size;
}

11.9 Other uses

Changing reference frames is everywhere once you start looking:

  • Robotics. Every joint, gripper and camera on a robot has its own frame. "Where is the cup, seen from the gripper?" is a change of basis. ROS keeps a whole tree of these frames in its tf library.
  • AR and VR. The headset tracks your head's position and orientation and builds a view matrix from them every frame, one for each eye, shifted by half the distance between your eyes.
  • GPS and drones. GPS gives positions in an Earth-centered frame. Drones and self-driving cars convert them into a local East-North-Up frame around a reference point, which is a rotation plus a translation.
  • Astronomy. Star positions are given in equatorial, ecliptic or galactic coordinates, and fixed rotation matrices convert between them.
  • Aircraft. Pilots and flight software use a body frame (forward, right wing, down) attached to the plane. Its orientation is described by yaw, pitch and roll, which brings us to chapter 13.

11.10 Code

Here is lookAt as it appears in linalg.hh.
Compare it line by line with section 11.5: rows 𝐫, 𝐮, −𝐟, and the translation column −𝐫⋅𝐞, −𝐮⋅𝐞, 𝐟⋅𝐞.

// ビュー行列:カメラがeyeからtargetを見る。カメラは自分の-Z方向を向く。
// View matrix: camera at eye looking at target. The camera looks down its own -Z axis.
inline Mat4 lookAt(Vec3 eye, Vec3 target, Vec3 up) {
  Vec3 f = normalize(target - eye); // 前 / forward
  Vec3 r = normalize(cross(f, up)); // 右 / right
  Vec3 u = cross(r, f);             // 上 / up

  Mat4 m = Mat4::identity();
  m.at(0, 0) = r.x;  m.at(0, 1) = r.y;  m.at(0, 2) = r.z;  m.at(0, 3) = -dot(r, eye);
  m.at(1, 0) = u.x;  m.at(1, 1) = u.y;  m.at(1, 2) = u.z;  m.at(1, 3) = -dot(u, eye);
  m.at(2, 0) = -f.x; m.at(2, 1) = -f.y; m.at(2, 2) = -f.z; m.at(2, 3) = dot(f, eye);
  return m;
}

And a test program with the forwardFromYawPitch helper from section 11.7.
It also checks that the inverse of the view matrix (chapter 10) really is the camera's world matrix, by reading the eye position from its fourth column.

#include <cstdio>
#include "linalg.hh"

// yaw: turn around +Y, pitch: look up (+) or down (-). yaw = pitch = 0 looks down -Z.
inline Vec3 forwardFromYawPitch(float yaw, float pitch) {
  float cp = std::cos(pitch);
  return {-std::sin(yaw) * cp, std::sin(pitch), -std::cos(yaw) * cp};
}

static void printVec(const char *name, Vec4 v) {
  printf("%-8s (%6.3f, %6.3f, %6.3f, %6.3f)\n", name, v.x, v.y, v.z, v.w);
}

static void printMat(const Mat4 &m) {
  for (int row = 0; row < 4; ++row)
    printf("  [%6.3f %6.3f %6.3f %6.3f]\n", m.at(row, 0), m.at(row, 1), m.at(row, 2), m.at(row, 3));
}

int main() {
  // a camera at (3, 0, 4) looking at the origin
  Mat4 view = lookAt({3, 0, 4}, {0, 0, 0}, {0, 1, 0});
  printf("view matrix:\n");
  printMat(view);

  printVec("origin", view * Vec4{0, 0, 0, 1});
  printVec("eye", view * Vec4{3, 0, 4, 1});
  printVec("p", view * Vec4{4, 2, -3, 1});

  // the camera's world matrix is the inverse of the view matrix
  Mat4 camWorld = inverse(view);
  printVec("camPos", camWorld.c[3]);

  // first-person camera: yaw 60 degrees, pitch 30 degrees
  Vec3 f = forwardFromYawPitch(radians(60.0f), radians(30.0f));
  printf("forward  (%6.3f, %6.3f, %6.3f) length %.3f\n", f.x, f.y, f.z, length(f));

  Vec3 eye{0, 2, 0};
  Mat4 fps = lookAt(eye, eye + f, {0, 1, 0});
  printVec("ahead", fps * Vec4{eye.x + 2 * f.x, eye.y + 2 * f.y, eye.z + 2 * f.z, 1});
  return 0;
}

Compile it like before with g++ -std=c++20 main.cc -o main and run it:

view matrix:
  [ 0.800  0.000 -0.600 -0.000]
  [ 0.000  1.000  0.000 -0.000]
  [ 0.600 -0.000  0.800 -5.000]
  [ 0.000  0.000  0.000  1.000]
origin   ( 0.000,  0.000, -5.000,  1.000)
eye      ( 0.000,  0.000,  0.000,  1.000)
p        ( 5.000,  2.000, -5.000,  1.000)
camPos   ( 3.000,  0.000,  4.000,  1.000)
forward  (-0.750,  0.500, -0.433) length 1.000
ahead    ( 0.000,  0.000, -2.000,  1.000)

Don't worry about the -0.000 entries.
Floating point numbers have a sign bit even for zero, so −(2.4−2.4) comes out as a "negative zero", which compares equal to 0.
Depending on your compiler you may see 0.000 there instead.
The last line is a nice check: a point 2 units along the camera's forward direction ends up at (0,0,−2), straight down −Z.

In the next chapter we'll put a projection matrix in front of the view matrix and combine them into one view-projection matrix, like this:

Mat4 viewProj = perspectiveGl(radians(60.0f), 16.0f / 9.0f, 0.1f, 100.0f) * view;

11.11 Summary

  • A space is a choice of origin and axes. A vertex travels object → world → view → clip → NDC → screen, through M, V, P, the divide by w and the viewport transform.
  • A matrix whose columns are frame A's axes (and origin) written in frame B converts A to B. Its inverse converts B to A.
  • For an orthonormal basis the inverse is the transpose, because the new coordinates are just dot products with the axes.
  • The camera is an object with eye 𝐞 and axes right, up, forward. It looks down its own −Z.
  • The view matrix is the inverse of the camera's world matrix: rows 𝐫, 𝐮, −𝐟 and translation −𝐫⋅𝐞, −𝐮⋅𝐞, 𝐟⋅𝐞.
  • lookAt builds forward from target minus eye, right from cross(f, up) and true up from cross(r, f). It breaks when forward is parallel to up.
  • Yaw and pitch give a forward vector with sin and cos. Clamp the pitch below 90°.
  • Every vertex is transformed by 𝐏𝐕𝐌, applied right to left.

11.12 Challenges

  1. ★ A 2D frame has axes 𝐛1=(0,1) and 𝐛2=(−1,0). A point has coordinates (3,2) in this frame: what are its old coordinates? Then convert the old point (1,4) into the new frame.
  2. ★ Check that 𝐛1=(0.6,0,0.8), 𝐛2=(0,1,0), 𝐛3=(−0.8,0,0.6) are orthonormal and right-handed (𝐛1×𝐛2=𝐛3), then find the coordinates of (5,1,5) in this basis.
  3. ★ Build the view matrix for eye (0,3,4), target (0,0,0) and up (0,1,0) by hand. Write down the length of 𝐟×𝐮𝐩 before normalizing it, and explain why it isn't 1.
  4. ★ Compute forwardFromYawPitch for yaw 90° and pitch −45°, with exact values (use 2/2). Which way is the camera looking?
  5. ★★ Write orbitEye(Vec3 target, float distance, float yaw, float pitch). For target (0,1,0), distance 4, yaw 0 and pitch −30° it should return about (0,3,3.464), and lookAt(eye, target, {0, 1, 0}) * Vec4{0, 1, 0, 1} should print (0,0,−4,1).
  6. ★★ Write void cameraAxes(const Mat4 &view, Vec3 &right, Vec3 &up, Vec3 &forward) that reads the three axes from the rows of a view matrix. For the camera in section 11.6 it should print right (0.8,0,−0.6), up (0,1,0) and forward (−0.6,0,−0.8).
  7. ★★★ lookAt can't roll the camera (tilt the horizon), because it always uses the world's up. Add a roll angle: after computing 𝐫 and 𝐮, rotate them inside their own plane with the 2D rotation from chapter 8. What happens if you pass up (0,−1,0) instead, and why?


コメント(0)

まだコメントはありません。