index.html (7345B)
1 <!DOCTYPE html> 2 <html lang="en-US"> 3 4 <head> 5 <meta http-equiv="X-Clacks-Overhead" content="GNU Terry Pratchett" /> 6 <meta charset="utf-8"> 7 <meta name="viewport" content="width=device-width, initial-scale=1.0" /> 8 <link rel="shortcut icon" href="https://electrosoup.com/favicon.png" /> 9 <title>Deriving the View Matrix | Electrosoup</title> 10 <meta name="title" content="Deriving the View Matrix" /> 11 <meta name="description" content="In computer graphics, the view matrix is used to transform points from 12 world space into camera space. Below is an explanation of how to derive this 13 matrix." /> 14 <meta name="author" content="" /> 15 <meta name="keywords" content="Graphics,Matrices,OpenGL," /> 16 17 18 19 20 21 22 23 <meta property="og:url" content="https://electrosoup.com/posts/view-matrix/"> 24 <meta property="og:site_name" content="Electrosoup"> 25 <meta property="og:title" content="Deriving the View Matrix"> 26 <meta property="og:description" content="In computer graphics, the view matrix is used to transform points from world space into camera space. Below is an explanation of how to derive this matrix."> 27 <meta property="og:locale" content="en_US"> 28 <meta property="og:type" content="article"> 29 <meta property="article:section" content="posts"> 30 <meta property="article:published_time" content="2021-11-21T13:34:08-08:00"> 31 <meta property="article:modified_time" content="2021-11-21T13:34:08-08:00"> 32 <meta property="article:tag" content="Graphics"> 33 <meta property="article:tag" content="Matrices"> 34 <meta property="article:tag" content="OpenGL"> 35 36 37 38 39 <meta name="twitter:card" content="summary"> 40 <meta name="twitter:title" content="Deriving the View Matrix"> 41 <meta name="twitter:description" content="In computer graphics, the view matrix is used to transform points from world space into camera space. Below is an explanation of how to derive this matrix."> 42 43 44 45 46 <meta itemprop="name" content="Deriving the View Matrix"> 47 <meta itemprop="description" content="In computer graphics, the view matrix is used to transform points from world space into camera space. Below is an explanation of how to derive this matrix."> 48 <meta itemprop="datePublished" content="2021-11-21T13:34:08-08:00"> 49 <meta itemprop="dateModified" content="2021-11-21T13:34:08-08:00"> 50 <meta itemprop="wordCount" content="521"> 51 <meta itemprop="keywords" content="Graphics,Matrices,OpenGL"> 52 53 54 <link href="/herman.min.css" rel="stylesheet"> 55 56 57 58 59 60 61 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css" integrity="sha384-nB0miv6/jRmo5UMMR1wu3Gz6NLsoTkbqJghGIsx//Rlm+ZU03BU6SQNC66uf4l5+" crossorigin="anonymous"> 62 <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.js" integrity="sha384-7zkQWkzuo3B5mTepMUcHkMB5jZaolc2xDwL6VFqjFALcbeS9Ggm/Yr2r3Dy4lfFg" crossorigin="anonymous"></script> 63 <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/contrib/auto-render.min.js" integrity="sha384-43gviWU0YVjaDtb/GhzOouOXtZMP/7XUzwPTstBeZFe/+rCMvRwr4yROQP43s0Xk" crossorigin="anonymous" 64 onload="renderMathInElement(document.body);"></script> 65 66 67 </head> 68 69 <body> 70 <header><a class="skip-link" href="#main-content">Skip to main content</a> 71 72 <nav> 73 <a href="/">Main</a> 74 75 <a href="/posts/">Posts</a> 76 77 <a href="https://git.electrosoup.com">Code</a> 78 79 <a href="https://christianermann.dev">Resume</a> 80 81 82 83 84 85 86 87 88 89 </nav> 90 </header> 91 <main id="main-content"> 92 93 <h1>Deriving the View Matrix</h1> 94 <p class="byline"> 95 96 <time datetime='2021-11-21' pubdate> 97 2021-11-21 98 </time> 99 100 </p> 101 102 <content> 103 <p>In computer graphics, the view matrix is used to transform points from 104 world space into camera space. Below is an explanation of how to derive this 105 matrix.</p> 106 <p>Let $V$ be the view matrix. 107 If $\vec{r}$, $\vec{u}$, and $\vec{f}$ denote the right, up, and forward 108 vectors of the camera and $\vec{p}$ is the camera’s position, $V$ should 109 satisfy the following properties: 110 $$ 111 \begin{matrix} 112 V_{1*} \times 113 \begin{bmatrix} 114 r_{x} \\ 115 r_{y} \\ 116 r_{z} \\ 117 0 118 \end{bmatrix} 119 = 120 \begin{bmatrix} 121 1 \\ 122 0 \\ 123 0 \\ 124 0 125 \end{bmatrix},& 126 V_{2*} \times 127 \begin{bmatrix} 128 u_{x} \\ 129 u_{y} \\ 130 u_{z} \\ 131 0 132 \end{bmatrix} 133 = 134 \begin{bmatrix} 135 0 \\ 136 1 \\ 137 0 \\ 138 0 139 \end{bmatrix},& 140 V_{3*} \times 141 \begin{bmatrix} 142 f_{x} \\ 143 f_{y} \\ 144 f_{z} \\ 145 0 146 \end{bmatrix} 147 = 148 \begin{bmatrix} 149 0 \\ 150 0 \\ 151 1 \\ 152 0 153 \end{bmatrix},& 154 V_{4*} \times 155 \begin{bmatrix} 156 p_{x} \\ 157 p_{y} \\ 158 p_{z} \\ 159 1 160 \end{bmatrix} 161 = 162 \begin{bmatrix} 163 0 \\ 164 0 \\ 165 0 \\ 166 1 167 \end{bmatrix} 168 \end{matrix} 169 $$ 170 In words, the view matrix should translate the camera to the origin and rotate 171 it to align with the $\vec{x}$, $\vec{y}$, and $\vec{z}$ axes.</p> 172 <p>This can be expressed more concisely as the product of two matrices: 173 $$ 174 V \times 175 \begin{bmatrix} 176 r_{x} & u_{x} & f_{x} & p_{x} \\ 177 r_{y} & u_{y} & f_{y} & p_{y} \\ 178 r_{z} & u_{z} & f_{z} & p_{z} \\ 179 0 & 0 & 0 & 1 180 \end{bmatrix} 181 = 182 \begin{bmatrix} 183 1 & 0 & 0 & 0 \\ 184 0 & 1 & 0 & 0 \\ 185 0 & 0 & 1 & 0 \\ 186 0 & 0 & 0 & 1 187 \end{bmatrix} 188 $$</p> 189 <p>To make this equation easier to solve, we can split the matrix containing our 190 camera parameters into the product of a translation matrix $T$ and a rotation 191 matrix $R$: 192 $$ 193 V \times (T \times R) = I 194 $$ 195 where 196 $$ 197 \begin{matrix} 198 T = 199 \begin{bmatrix} 200 1 & 0 & 0 & p_{x} \\ 201 0 & 1 & 0 & p_{y} \\ 202 0 & 0 & 1 & p_{z} \\ 203 0 & 0 & 0 & 1 204 \end{bmatrix},& 205 R = 206 \begin{bmatrix} 207 r_{x} & u_{x} & f_{x} & 0 \\ 208 r_{y} & u_{y} & f_{y} & 0 \\ 209 r_{z} & u_{z} & f_{z} & 0 \\ 210 0 & 0 & 0 & 1 211 \end{bmatrix} 212 \end{matrix} 213 $$</p> 214 <p>Solving for $V$ reveals that $V=R^{-1} \times T^{-1}$: 215 $$ 216 V = 217 \begin{bmatrix} 218 r_{x} & r_{y} & r_{z} & 0 \\ 219 u_{x} & u_{y} & u_{z} & 0 \\ 220 f_{x} & f_{y} & f_{z} & 0 \\ 221 0 & 0 & 0 & 1 222 \end{bmatrix} 223 \times 224 \begin{bmatrix} 225 1 & 0 & 0 & -p_{x} \\ 226 0 & 1 & 0 & -p_{y} \\ 227 0 & 0 & 1 & -p_{z} \\ 228 0 & 0 & 0 & 1 229 \end{bmatrix} 230 $$</p> 231 <p>And with some minor simplification, 232 $$ 233 V 234 = 235 \begin{bmatrix} 236 r_{x} & r_{y} & r_{z} & -(\vec{r} \cdot \vec{p}) \\ 237 u_{x} & u_{y} & u_{z} & -(\vec{u} \cdot \vec{p}) \\ 238 f_{x} & f_{y} & f_{z} & -(\vec{f} \cdot \vec{p}) \\ 239 0 & 0 & 0 & 1 240 \end{bmatrix} 241 $$</p> 242 <p>When we’re ready to render our scene, we can apply $V$ to each vertex $\vec{q}$ 243 by calculating 244 $$ 245 V \times \vec{q} 246 $$ 247 in our vertex shader.</p> 248 249 </content> 250 <p> 251 252 <a class="blog-tags" href="/tags/graphics/">#graphics</a> 253 254 <a class="blog-tags" href="/tags/matrices/">#matrices</a> 255 256 <a class="blog-tags" href="/tags/opengl/">#opengl</a> 257 258 </p> 259 260 261 <p> 262 <a href='mailto:christianermann@gmail.com?subject=Reply%20to%20"Deriving%20the%20View%20Matrix"'> 263 Reply to this post by email ↪ 264 </a> 265 </p> 266 267 268 269 </main> 270 <footer><footer> 271 272 <a href="https://git.electrosoup.com">Code</a>. 273 274 <a href="https://christianermann.dev">Resume</a>. 275 276 <a href='https://electrosoup.com/index.xml'>RSS</a>. 277 <p>Copyright © 2026 Christian Ermann. | Made with <a href="https://github.com/clente/hugo-bearcub">Bear Cub</a></p> 278 </footer> 279 </footer> 280 281 282 </body> 283 284 </html>