Camera
In the last chapter the view matrix pushed the world three units away, and that was our whole camera. Direct3D has no idea what a camera is, and neither does OpenGL. What we can do is move the entire world the opposite way to how a camera would move, and the result on screen is indistinguishable from moving the camera. This chapter turns that idea into a learn/camera.asm, which every later lesson uses.
Camera/View space
View space is the world as the camera sees it, with the camera at the origin. To build the matrix that takes world coordinates there, we need four things in world space: where the camera is, which way it looks, and which ways are its right and its up. Those last three are unit vectors at right angles to each other, so together with the position they form a little coordinate system of the camera's own:
1. Camera position
The position is just a point in world space. The last chapter's view matrix put the camera three units back along +z, which is towards us in our right-handed world: the camera is at (0, 0, 3).
2. Camera direction
Next, which way it looks. Say it looks at the origin. Subtracting two points gives the vector from one to the other, so target - position points from the camera at its target, and normalized it is the
3. Right axis
The right vector is the camera's +x axis. The cross product gives us a vector perpendicular to two others, so if we can find a second vector besides front, we are done. The trick is to use the world's up, (0, 1, 0). It is not the camera's up unless the camera looks straight ahead, but it does not need to be: it only needs to lie in the plane that front and the camera's up share, and for any camera that is not looking straight up or down, it does. Then
\[\hat{R} = \text{normalize}(\text{front} \times \text{up}_{world})\]points to the camera's right. Swap the two and it points left.
4. Up axis
With front and right known, the camera's true up is perpendicular to both, which is one more cross product: \(\hat{U} = \hat{R} \times \text{front}\). Building a set of perpendicular axes from a couple of starting vectors like this is a form of the
Look At
Here is a useful fact about matrices: given three perpendicular unit axes, a matrix whose rows are those axes takes any vector into the coordinate system they define. Each row dotted with the vector gives the vector's component along that axis. Add a translation that first moves the camera's position to the origin, and we have a matrix that takes world space into the camera's view space, the
\(\color{red}R\) is the right vector, \(\color{green}U\) the up vector, \(\color{blue}D\) the backward-pointing direction from step 2, and \(\color{purple}P\) the camera's position. Read it right to left: first move the world so the camera's position is at the origin (that is why the translation is negated), then rotate it so the camera's axes line up with x, y and z. Both halves do the opposite of what the camera did, which is exactly the "move the world instead" idea.
math.asm builds this matrix in glm::lookAt, from a position, a target and a world up, all three passed by address:
; rcx: out, rdx: eye, r8: target, r9: up (all vec3)
;
; The view matrix of a camera at `eye` looking at `target`.
align 16
m4_look_at proc out_ref:qword, eye:qword, target:qword, up:qword
local f:vec3, s:vec3, u:vec3
lea rcx, f ; f = normalize(target - eye)
mov rdx, target
mov r8, eye
call v3_sub
lea rcx, f
lea rdx, f
call v3_normalize
lea rcx, s ; s = normalize(cross(f, up))
lea rdx, f
mov r8, up
call v3_cross
lea rcx, s
lea rdx, s
call v3_normalize
lea rcx, u ; u = cross(s, f)
lea rdx, s
lea r8, f
call v3_cross
mov rcx, out_ref
call m4_identity
mov rdx, eye
v3load xmm4, [rdx]
; row 0: s, -dot(s, eye)
v3load xmm0, s
movups [rcx], xmm0
dpps xmm0, xmm4, 71h
xorps xmm0, math_sign_mask
movss real4 ptr [rcx + 12], xmm0
; row 1: u, -dot(u, eye)
v3load xmm0, u
movups [rcx + 16], xmm0
dpps xmm0, xmm4, 71h
xorps xmm0, math_sign_mask
movss real4 ptr [rcx + 28], xmm0
; row 2: -f, dot(f, eye)
v3load xmm0, f
movaps xmm1, xmm0
xorps xmm1, math_sign_mask
movups [rcx + 32], xmm1
dpps xmm0, xmm4, 71h
movss real4 ptr [rcx + 44], xmm0
ret
m4_look_at endp
It is steps 2 to 4 in order: f is front, s the right vector ("side"), u the camera's up, which needs no normalizing since it is the cross product of two perpendicular unit vectors. The three rows are R, U and D, with D written as -f. Each row is stored whole, four floats with one movups; dpps with the eye, negated with xorps where the formula has a minus. So a camera at (0, 0, 3) looking at the origin, with up (0, 1, 0), gives exactly the translation by (0, 0, -3) of the last chapter.
Before taking input, a quick way to get a feel for it is to swing the camera around the scene. Keep the target at the origin and move the position around a circle of radius 10, using a sine and a cosine of the time for x and z: the eye at \((10 \sin t, 0, 10 \cos t)\), handed to
Walk around
To steer the camera ourselves we stop thinking in terms of a target and keep a position + front, however we move. That is how the camera asks for its view matrix, in learn/camera.asm:
; rcx: out mat4, rdx: &camera
align 16
camera_view proc out_ref:qword, cam:qword
local target:vec3
mov rdx, cam
lea rcx, target
lea r8, [rdx].camera.front
lea rdx, [rdx].camera.position
call v3_add
mov rax, cam
lea rdx, [rax].camera.position
lea r8, target
lea r9, [rax].camera.up
invoke m4_look_at, out_ref, rdx, r8, r9
ret
camera_view endp
cam is the address of a camera structure, and [rdx].camera.front is a field of the structure rdx points at: C's c->front. lea computes that field's address without reading it, which is what leas matters: rdx is the camera until the last moment, and only then becomes the position's address. In the rdx as the second argument, r8 as the third and r9 as the fourth, so nothing is overwritten on the way.
Moving is now adding to the position. Forward and backward go along front; left and right go along the right vector, the cross product of front and up, which gives the sideways "strafe" movement everyone knows from games. LearnOpenGL checks keys with learn/window.asm, built on Win32's
; rcx: virtual key -> rax: 1 while it is held. The top bit of GetAsyncKeyState is set while a key
; is down, which makes the SHORT negative.
align 16
key_down proc vkey:qword
invoke GetForegroundWindow
cmp rax, window
jne up
invoke GetAsyncKeyState, vkey
test ax, ax
jns up
mov eax, 1
ret
up:
xor eax, eax
ret
key_down endp
and ifs, it walks a table of six keys, one for each direction the camera knows, in the order of the CAMERA_ constants:
CAMERA_FORWARD equ 0
CAMERA_BACKWARD equ 1
CAMERA_RIGHT equ 2
CAMERA_LEFT equ 3
CAMERA_UP equ 4
CAMERA_DOWN equ 5
; ...
; the key for each CAMERA_ direction, in order
camera_keys byte 'W', 'S', 'D', 'A', VK_SPACE, VK_SHIFT
lea rbx, camera_keys
xor esi, esi
keys:
movzx eax, byte ptr [rbx + rsi]
invoke key_down, rax
test eax, eax
jz next_key
mov rax, dt_ref
movss xmm2, real4 ptr [rax]
invoke camera_process_keyboard, cam, rsi
next_key:
inc esi
cmp esi, 6
jb keys
Letter keys are identified by their capital letter's character code, so 'W' is the W key; other keys have VK_ names. Besides LearnOpenGL's four, Space and Shift move straight up and down, which makes it easier to get above or below the crates. The index into the table is the direction, so a held key passes its own index to the camera, along with the frame's duration as the third argument, a float, in xmm2. The camera does the actual moving:
; rcx: &camera, rdx: CAMERA_FORWARD ... CAMERA_DOWN, xmm2: seconds since the last frame
align 16
camera_process_keyboard proc cam:qword, direction:qword, seconds:qword
local step:vec3
mov rcx, cam
mulss xmm2, [rcx].camera.speed ; how far this frame
mov eax, dword ptr direction
lea rdx, [rcx].camera.front
cmp eax, CAMERA_RIGHT
jb along
lea rdx, [rcx].camera.right
cmp eax, CAMERA_UP
jb along
lea rdx, [rcx].camera.world_up
along:
; forward, right and up (even) add the step; backward, left and down (odd) subtract it
test eax, 1
jz forwards
xorps xmm2, math_sign_mask
forwards:
lea rcx, step
call v3_scale
mov rcx, cam
lea rdx, [rcx].camera.position
lea r8, step
mov rcx, rdx
call v3_add
ret
camera_process_keyboard endp
The order of the constants does the work of a switch: directions 0 and 1 move along front, 2 and 3 along right, 4 and 5 along the world up, and the odd ones move backwards, by negating the step. The step, speed times seconds, is still in xmm2 when
Every vector it moves along is a unit vector, so a step is always step units long whatever way the camera faces. That matters for the right vector in particular: the cross product of front and world up gets shorter as front tilts towards up or down, and without normalizing it (the camera stores it normalized) strafing would slow down as you looked at the sky.
Movement speed
If each frame moved the camera a fixed amount, its speed would depend on the frame rate: a machine drawing 300 frames a second would fly five times faster than one drawing 60. Our lessons present with a sync interval of 1, vcall swap_chain, IDXGISwapChain_Present, 1, 0 in
The usual cure is learn/window.asm's
; -> xmm0: seconds since the window was created: what glfwGetTime is to LearnOpenGL. Stopped at
; --time while taking a --shot.
align 16
get_time proc
local now:qword
cmp window_shot_path, 0
je running
movss xmm0, window_shot_time
ret
running:
invoke QueryPerformanceCounter, ADDR now
mov rax, now
sub rax, window_perf_start
cvtsi2sd xmm0, rax
cvtsi2sd xmm1, window_perf_frequency
divsd xmm0, xmm1
cvtsd2ss xmm0, xmm0
ret
get_time endp
Counting from the window's creation rather than from when Windows started keeps the number small, which matters for a 32-bit float: after a few days of uptime, raw seconds would no longer have the precision to tell one frame from the next. At the top of every frame the render loop works out the delta:
; per-frame time logic
; --------------------
call get_time
movss xmm1, xmm0
subss xmm0, last_frame
movss delta_time, xmm0
movss last_frame, xmm1
and its address is passed to xmm2. With the camera's speed of 2.5, that is 2.5 units a second on any machine:
invoke camera_process_input, ADDR cam, ADDR delta_time
Passing the float by address keeps the call an ordinary xmm register first.
Look around
Moving without turning gets boring quickly. To look around we have to change front, and for that we use the mouse. Turning mouse movement into a direction takes a little trigonometry. If it does not interest you, the code is short and can be used as it is; come back to the explanation when you want it.
Euler angles
Pitch is how far we look up or down, yaw how far we look left or right, and roll how far we tilt sideways, as an aircraft banks. A fly camera like ours needs only yaw and pitch, and from those two angles we can work out front.
Start with a reminder about right-angled triangles. If the long side has length 1, the side next to an angle has length \(\cos\) of the angle and the side opposite it has length \(\sin\):
Now look down on the scene from above, along the y axis, so that the x and z axes form the floor. Put the yaw angle at the origin, measured from the x axis. A unit-length front vector is the long side of a triangle whose x side is \(\cos(\text{yaw})\) and whose z side is \(\sin(\text{yaw})\):
Pitch works the same way, but in the vertical plane: the y component of front is \(\sin(\text{pitch})\), and the part along the floor shrinks to \(\cos(\text{pitch})\):
Since the floor part is now only \(\cos(\text{pitch})\) long, both floor components get multiplied by it, and the whole thing is:
\[\text{front} = \begin{pmatrix} \cos(\text{yaw}) \cos(\text{pitch}) \\ \sin(\text{pitch}) \\ \sin(\text{yaw}) \cos(\text{pitch}) \end{pmatrix}\]In the camera, along with the right and up vectors that follow from it (the angles are kept in degrees, and converted first):
; rcx: &camera. front, right and up from yaw and pitch.
align 16
camera_update_vectors proc cam:qword
local yaw:real4, pitch:real4, cos_pitch:real4
USING rbx
SaveRegs
mov rbx, cam
movss xmm0, [rbx].camera.yaw
call radians
movss yaw, xmm0
movss xmm0, [rbx].camera.pitch
call radians
movss pitch, xmm0
call cosf
movss cos_pitch, xmm0
; front = normalize(cos(yaw) cos(pitch), sin(pitch), sin(yaw) cos(pitch))
movss xmm0, yaw
call cosf
mulss xmm0, cos_pitch
movss [rbx].camera.front.x, xmm0
movss xmm0, pitch
call sinf
movss [rbx].camera.front.y, xmm0
movss xmm0, yaw
call sinf
mulss xmm0, cos_pitch
movss [rbx].camera.front.z, xmm0
lea rcx, [rbx].camera.front
mov rdx, rcx
call v3_normalize
; right = normalize(cross(front, world_up)), up = normalize(cross(right, front))
lea rcx, [rbx].camera.right
lea rdx, [rbx].camera.front
lea r8, [rbx].camera.world_up
call v3_cross
lea rcx, [rbx].camera.right
mov rdx, rcx
call v3_normalize
lea rcx, [rbx].camera.up
lea rdx, [rbx].camera.right
lea r8, [rbx].camera.front
call v3_cross
lea rcx, [rbx].camera.up
mov rdx, rcx
call v3_normalize
RestoreRegs
ret
camera_update_vectors endp
The camera's address is kept in rbx, which survives the calls to math.asm allows.
The right and up are steps 3 and 4 from the start of the chapter, recomputed whenever the angles change. With a yaw of 0 the formula points front along +x. Our scenes are laid out down -z, so a camera that should look straight into them needs a yaw of -90 degrees, as the comment in the camera structure says. Our lesson starts the camera a little to the left of and above the crates and turned towards them, with camera_start vec3 <-1.5, 1.0, 4.0> in its data:
lea rcx, cam
lea rdx, camera_start
movf xmm2, -70.0 ; yaw
movf xmm3, -10.0 ; pitch
call camera_create
that is, at (-1.5, 1, 4), with a yaw of -70 degrees (20 degrees to the right of straight down -z) and a pitch of -10 (looking slightly down). Yaw and pitch are the third and fourth arguments, so they go in xmm2 and xmm3, and the camera's and the position's addresses in rcx and rdx.
Mouse input
Yaw and pitch come from the mouse: moving it sideways changes the yaw, moving it up and down changes the pitch. Each frame we compare where the mouse is with where it was the frame before, and turn by the difference.
LearnOpenGL hides and captures the cursor with learn/window.asm's
; Screen y grows downwards, so moving the mouse up is a positive dy here: pitch up.
invoke GetCursorPos, ADDR cursor
mov eax, cursor.x
sub eax, window_last_cursor.x
cvtsi2ss xmm0, eax
movss window_mouse_dx, xmm0
mov eax, window_last_cursor.y
sub eax, cursor.y
cvtsi2ss xmm0, eax
movss window_mouse_dy, xmm0
mov eax, cursor.x
mov window_last_cursor.x, eax
mov eax, cursor.y
mov window_last_cursor.y, eax
and
; Hold the right mouse button to look around.
invoke key_down, VK_RBUTTON
test eax, eax
jz no_look
movss xmm1, window_mouse_dx
movss xmm2, window_mouse_dy
invoke camera_process_mouse, cam
no_look:
xmm1 and xmm2, and
Notice that window_last_cursor is updated every frame, whether the button is held or not, and also while another window has the focus (the offsets are then set to zero). That avoids the problem LearnOpenGL solves with its firstMouse flag: when the cursor is first captured, the first offset is measured from some stale position and the view jumps. Here the previous position is never more than a frame old, so the first frame of a drag turns the camera only by however far the mouse moved in that frame.
The camera turns the offsets into angles:
; rcx: &camera, xmm1: dx, xmm2: dy (pixels; up is positive)
;
; Pitch stops short of straight up and down, where the view would flip.
align 16
camera_process_mouse proc cam:qword
mov rcx, cam
mulss xmm1, [rcx].camera.sensitivity
addss xmm1, [rcx].camera.yaw
movss [rcx].camera.yaw, xmm1
mulss xmm2, [rcx].camera.sensitivity
movss xmm0, [rcx].camera.pitch
addss xmm0, xmm2
movf xmm1, -89.0
movf xmm2, 89.0
call clampf
mov rcx, cam
movss [rcx].camera.pitch, xmm0
invoke camera_update_vectors, cam
ret
camera_process_mouse endp
sensitivity is 0.1, a tenth of a degree per pixel; without it the camera would spin wildly. Yaw is left free, so we can turn round as often as we like. Pitch is clamped to 89 degrees either way, and the reason goes back to step 3. At a pitch of exactly 90, front would point straight along the world up, and the cross product of two parallel vectors is zero.
Zoom
Last, the scroll wheel zooms. In the last chapter we saw that the field of view sets how much of the scene fits on the screen. Make it smaller and a smaller part of the world is spread over the same screen, which looks like zooming in, exactly like a telephoto lens.
A turn of the wheel is an event, not a state we can poll, so it arrives in learn/window.asm's window procedure as a WM_MOUSEWHEEL message, where LearnOpenGL would register a scroll callback:
on_wheel:
; The high word of wparam is the distance turned, 120 per notch.
mov rax, wparam
shr eax, 16
movsx eax, ax
cvtsi2ss xmm0, eax
movf xmm1, 120.0
divss xmm0, xmm1
addss xmm0, window_scroll
movss window_scroll, xmm0
xor eax, eax
ret
The distance is a signed 16-bit number, positive when the wheel turns away from you, in units of 120 per notch (finer-grained wheels report fractions of a notch). movsx sign-extends the 16 bits to 32, so a turn towards you stays negative. We collect it in window_scroll, and xmm1, and resets it:
movss xmm1, window_scroll
invoke camera_process_scroll, cam
mov dword ptr window_scroll, 0
; rcx: &camera, xmm1: wheel notches. Zooming is narrowing the field of view.
align 16
camera_process_scroll proc cam:qword
mov rcx, cam
addss xmm1, xmm1 ; 2 degrees a notch
movss xmm0, [rcx].camera.zoom
subss xmm0, xmm1
movf xmm1, 1.0
movf xmm2, 45.0
call clampf
mov rcx, cam
movss [rcx].camera.zoom, xmm0
ret
camera_process_scroll endp
Each notch narrows the field of view by 2 degrees, from 45 degrees at the widest down to 1. The projection matrix is now rebuilt every frame with the camera's zoom as its field of view, the same call as in the last chapter with cam.zoom in place of 45, and the view matrix comes from the camera:
; view from the camera; projection with the camera's zoom as its field of view
invoke camera_view, ADDR c.view, ADDR cam
movf xmm0, 100.0
movss real4 ptr [rsp + 32], xmm0 ; z_far
movss xmm0, cam.zoom
call radians
movaps xmm1, xmm0 ; fovy
cvtsi2ss xmm2, scr_width
cvtsi2ss xmm0, scr_height
divss xmm2, xmm0 ; aspect
movf xmm3, 0.1 ; z_near
lea rcx, c.projection
call m4_perspective
camera.asm also has this as a procedure, learn/cube.asm's
That is everything. The ten crates are the same as in the last chapter, standing still this time so that the only motion is ours. Fly in among them with W A S D, hold the right button to look around, and scroll to zoom:
Camera class
All of this is in includes/learn/camera.asm, so that later lessons can use a camera without repeating it, just as LearnOpenGL moves its camera into a Camera class. Assembly has structures and procedures rather than classes, so the camera is a structure:
camera struct
position vec3 <>
front vec3 <>
up vec3 <>
right vec3 <>
world_up vec3 <>
yaw real4 ? ; degrees; -90 looks down -z
pitch real4 ? ; degrees
speed real4 ? ; units per second
sensitivity real4 ? ; degrees per pixel of mouse movement
zoom real4 ? ; vertical field of view, degrees
camera ends
with procedures that take its address:
This is a fly camera built on Euler angles, which suits looking around a scene, but every kind of camera has its own catches. Ours cannot look straight up or down, and its fixed world up of (0, 1, 0) means it cannot roll: a flight-simulator camera that banks needs a different approach, usually with quaternions. A first-person camera that walks rather than flies needs movement confined to the floor, which is one of the exercises below.
The full program is here, its shaders here, and the camera here.
Additional resources
- GetAsyncKeyState and WM_MOUSEWHEEL: Microsoft's reference for the two ways our lessons read input.
- Acquiring high-resolution time stamps: how
QueryPerformanceCounter works and why to prefer it. - LearnOpenGL's Camera: the OpenGL version of this chapter, with GLFW's callbacks and a captured cursor.
Exercises
- Turn the fly camera into a first-person camera that stays on the floor: looking around works as before, but W A S D move only in the xz plane, and Space and Shift do nothing. (Moving along front with its y component removed is not quite enough: normalize it again, with
v3_normalize .) - Write your own look-at as the product of the two matrices in the formula at the start of this chapter, built from
m4_identity and the R, U and D vectors, and use it in place ofm4_look_at incamera_view . Nothing should change on screen. - Make the camera behave like LearnOpenGL's: always looking, without holding a button. Hide the cursor with
ShowCursor , and every frame move it back to the centre of the window withSetCursorPos , measuring the offset from the centre instead of from window_last_cursor (inwindow_poll , or in a copy of it).