view-matrix.md (3753B)
1 --- 2 title: "Deriving the View Matrix" 3 date: 2021-11-21T13:34:08-08:00 4 draft: false 5 tags: ["Graphics", "Matrices", "OpenGL"] 6 math: true 7 --- 8 9 In computer graphics, the view matrix is used to transform points from 10 world space into camera space. Below is an explanation of how to derive this 11 matrix. 12 13 Let $V$ be the view matrix. 14 If $\vec{r}$, $\vec{u}$, and $\vec{f}$ denote the right, up, and forward 15 vectors of the camera and $\vec{p}$ is the camera's position, $V$ should 16 satisfy the following properties: 17 $$ 18 \begin{matrix} 19 V_{1*} \times 20 \begin{bmatrix} 21 r_{x} \\\ 22 r_{y} \\\ 23 r_{z} \\\ 24 0 25 \end{bmatrix} 26 = 27 \begin{bmatrix} 28 1 \\\ 29 0 \\\ 30 0 \\\ 31 0 32 \end{bmatrix},& 33 V_{2*} \times 34 \begin{bmatrix} 35 u_{x} \\\ 36 u_{y} \\\ 37 u_{z} \\\ 38 0 39 \end{bmatrix} 40 = 41 \begin{bmatrix} 42 0 \\\ 43 1 \\\ 44 0 \\\ 45 0 46 \end{bmatrix},& 47 V_{3*} \times 48 \begin{bmatrix} 49 f_{x} \\\ 50 f_{y} \\\ 51 f_{z} \\\ 52 0 53 \end{bmatrix} 54 = 55 \begin{bmatrix} 56 0 \\\ 57 0 \\\ 58 1 \\\ 59 0 60 \end{bmatrix},& 61 V_{4*} \times 62 \begin{bmatrix} 63 p_{x} \\\ 64 p_{y} \\\ 65 p_{z} \\\ 66 1 67 \end{bmatrix} 68 = 69 \begin{bmatrix} 70 0 \\\ 71 0 \\\ 72 0 \\\ 73 1 74 \end{bmatrix} 75 \end{matrix} 76 $$ 77 In words, the view matrix should translate the camera to the origin and rotate 78 it to align with the $\vec{x}$, $\vec{y}$, and $\vec{z}$ axes. 79 80 This can be expressed more concisely as the product of two matrices: 81 $$ 82 V \times 83 \begin{bmatrix} 84 r_{x} & u_{x} & f_{x} & p_{x} \\\ 85 r_{y} & u_{y} & f_{y} & p_{y} \\\ 86 r_{z} & u_{z} & f_{z} & p_{z} \\\ 87 0 & 0 & 0 & 1 88 \end{bmatrix} 89 = 90 \begin{bmatrix} 91 1 & 0 & 0 & 0 \\\ 92 0 & 1 & 0 & 0 \\\ 93 0 & 0 & 1 & 0 \\\ 94 0 & 0 & 0 & 1 95 \end{bmatrix} 96 $$ 97 98 To make this equation easier to solve, we can split the matrix containing our 99 camera parameters into the product of a translation matrix $T$ and a rotation 100 matrix $R$: 101 $$ 102 V \times (T \times R) = I 103 $$ 104 where 105 $$ 106 \begin{matrix} 107 T = 108 \begin{bmatrix} 109 1 & 0 & 0 & p_{x} \\\ 110 0 & 1 & 0 & p_{y} \\\ 111 0 & 0 & 1 & p_{z} \\\ 112 0 & 0 & 0 & 1 113 \end{bmatrix},& 114 R = 115 \begin{bmatrix} 116 r_{x} & u_{x} & f_{x} & 0 \\\ 117 r_{y} & u_{y} & f_{y} & 0 \\\ 118 r_{z} & u_{z} & f_{z} & 0 \\\ 119 0 & 0 & 0 & 1 120 \end{bmatrix} 121 \end{matrix} 122 $$ 123 124 Solving for $V$ reveals that $V=R^{-1} \times T^{-1}$: 125 $$ 126 V = 127 \begin{bmatrix} 128 r_{x} & r_{y} & r_{z} & 0 \\\ 129 u_{x} & u_{y} & u_{z} & 0 \\\ 130 f_{x} & f_{y} & f_{z} & 0 \\\ 131 0 & 0 & 0 & 1 132 \end{bmatrix} 133 \times 134 \begin{bmatrix} 135 1 & 0 & 0 & -p_{x} \\\ 136 0 & 1 & 0 & -p_{y} \\\ 137 0 & 0 & 1 & -p_{z} \\\ 138 0 & 0 & 0 & 1 139 \end{bmatrix} 140 $$ 141 142 And with some minor simplification, 143 $$ 144 V 145 = 146 \begin{bmatrix} 147 r_{x} & r_{y} & r_{z} & -(\vec{r} \cdot \vec{p}) \\\ 148 u_{x} & u_{y} & u_{z} & -(\vec{u} \cdot \vec{p}) \\\ 149 f_{x} & f_{y} & f_{z} & -(\vec{f} \cdot \vec{p}) \\\ 150 0 & 0 & 0 & 1 151 \end{bmatrix} 152 $$ 153 154 When we're ready to render our scene, we can apply $V$ to each vertex $\vec{q}$ 155 by calculating 156 $$ 157 V \times \vec{q} 158 $$ 159 in our vertex shader. 160