index.xml (16265B)
1 <?xml version="1.0" encoding="utf-8" standalone="yes"?> 2 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"> 3 <channel> 4 <title>OpenGL on Electrosoup</title> 5 <link>https://electrosoup.com/tags/opengl/</link> 6 <description>Recent content in OpenGL on Electrosoup</description> 7 <generator>Hugo -- gohugo.io</generator> 8 <language>en-US</language> 9 <managingEditor>christianermann@gmail.com (Christian Ermann)</managingEditor> 10 <webMaster>christianermann@gmail.com (Christian Ermann)</webMaster> 11 <lastBuildDate>Mon, 10 Oct 2022 00:31:00 -0400</lastBuildDate> 12 <atom:link href="https://electrosoup.com/tags/opengl/index.xml" rel="self" type="application/rss+xml" /> 13 <item> 14 <title>Understanding Camera Look-At.</title> 15 <link>https://electrosoup.com/posts/look-at/</link> 16 <pubDate>Mon, 10 Oct 2022 00:31:00 -0400</pubDate><author>christianermann@gmail.com (Christian Ermann)</author> 17 <guid>https://electrosoup.com/posts/look-at/</guid> 18 <description><p>Often times, we want to position the camera in our scene at a particular point,
such as the head of the character in a first-person shooter or the cockpit of a
plane in a flight simulator. We also want the camera to look in the direction
that the player is trying to move.</p></description> 19 <content:encoded><![CDATA[<p>Often times, we want to position the camera in our scene at a particular point, 20 such as the head of the character in a first-person shooter or the cockpit of a 21 plane in a flight simulator. We also want the camera to look in the direction 22 that the player is trying to move.</p> 23 <p>To do this, we need to find the forward, right, and up vectors that correspond 24 to the desired view.</p> 25 <p>If we let $\vec{p}$ be the camera’s position and $\vec{t}$ be the position of 26 what we want to look at, the camera’s look direction is 27 $$\vec{l}=\vec{t}-\vec{p}\,.$$ 28 Conventionally, the camera looks down the negative forward axis so 29 $$\vec{f}=-\vec{l}\,.$$</p> 30 <p>In order to determine the right vector, we need a reference up direction, 31 $\vec{up}$ (which isn’t necessarily orthogonal to $\vec{f}$). This enables us 32 to calculate 33 $$\vec{r} = \vec{f} \times \vec{up}$$ 34 and 35 $$\vec{u} = \vec{r} \times \vec{f}\,.$$</p> 36 <p>Now we can use the camera position $\vec{p}$ and normalized forms of $\vec{f}$, 37 $\vec{r}$, and $\vec{u}$ to fill in our 38 <a href="/posts/view-matrix/">view matrix</a>:</p> 39 <p>$$ 40 V 41 = 42 \begin{bmatrix} 43 r_{x} & r_{y} & r_{z} & -(\vec{r} \cdot \vec{p}) \\ 44 u_{x} & u_{y} & u_{z} & -(\vec{u} \cdot \vec{p}) \\ 45 f_{x} & f_{y} & f_{z} & -(\vec{f} \cdot \vec{p}) \\ 46 0 & 0 & 0 & 1 47 \end{bmatrix} 48 $$</p> 49 ]]></content:encoded> 50 </item> 51 <item> 52 <title>Deriving the View Matrix</title> 53 <link>https://electrosoup.com/posts/view-matrix/</link> 54 <pubDate>Sun, 21 Nov 2021 13:34:08 -0800</pubDate><author>christianermann@gmail.com (Christian Ermann)</author> 55 <guid>https://electrosoup.com/posts/view-matrix/</guid> 56 <description><p>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.</p></description> 57 <content:encoded><![CDATA[<p>In computer graphics, the view matrix is used to transform points from 58 world space into camera space. Below is an explanation of how to derive this 59 matrix.</p> 60 <p>Let $V$ be the view matrix. 61 If $\vec{r}$, $\vec{u}$, and $\vec{f}$ denote the right, up, and forward 62 vectors of the camera and $\vec{p}$ is the camera’s position, $V$ should 63 satisfy the following properties: 64 $$ 65 \begin{matrix} 66 V_{1*} \times 67 \begin{bmatrix} 68 r_{x} \\ 69 r_{y} \\ 70 r_{z} \\ 71 0 72 \end{bmatrix} 73 = 74 \begin{bmatrix} 75 1 \\ 76 0 \\ 77 0 \\ 78 0 79 \end{bmatrix},& 80 V_{2*} \times 81 \begin{bmatrix} 82 u_{x} \\ 83 u_{y} \\ 84 u_{z} \\ 85 0 86 \end{bmatrix} 87 = 88 \begin{bmatrix} 89 0 \\ 90 1 \\ 91 0 \\ 92 0 93 \end{bmatrix},& 94 V_{3*} \times 95 \begin{bmatrix} 96 f_{x} \\ 97 f_{y} \\ 98 f_{z} \\ 99 0 100 \end{bmatrix} 101 = 102 \begin{bmatrix} 103 0 \\ 104 0 \\ 105 1 \\ 106 0 107 \end{bmatrix},& 108 V_{4*} \times 109 \begin{bmatrix} 110 p_{x} \\ 111 p_{y} \\ 112 p_{z} \\ 113 1 114 \end{bmatrix} 115 = 116 \begin{bmatrix} 117 0 \\ 118 0 \\ 119 0 \\ 120 1 121 \end{bmatrix} 122 \end{matrix} 123 $$ 124 In words, the view matrix should translate the camera to the origin and rotate 125 it to align with the $\vec{x}$, $\vec{y}$, and $\vec{z}$ axes.</p> 126 <p>This can be expressed more concisely as the product of two matrices: 127 $$ 128 V \times 129 \begin{bmatrix} 130 r_{x} & u_{x} & f_{x} & p_{x} \\ 131 r_{y} & u_{y} & f_{y} & p_{y} \\ 132 r_{z} & u_{z} & f_{z} & p_{z} \\ 133 0 & 0 & 0 & 1 134 \end{bmatrix} 135 = 136 \begin{bmatrix} 137 1 & 0 & 0 & 0 \\ 138 0 & 1 & 0 & 0 \\ 139 0 & 0 & 1 & 0 \\ 140 0 & 0 & 0 & 1 141 \end{bmatrix} 142 $$</p> 143 <p>To make this equation easier to solve, we can split the matrix containing our 144 camera parameters into the product of a translation matrix $T$ and a rotation 145 matrix $R$: 146 $$ 147 V \times (T \times R) = I 148 $$ 149 where 150 $$ 151 \begin{matrix} 152 T = 153 \begin{bmatrix} 154 1 & 0 & 0 & p_{x} \\ 155 0 & 1 & 0 & p_{y} \\ 156 0 & 0 & 1 & p_{z} \\ 157 0 & 0 & 0 & 1 158 \end{bmatrix},& 159 R = 160 \begin{bmatrix} 161 r_{x} & u_{x} & f_{x} & 0 \\ 162 r_{y} & u_{y} & f_{y} & 0 \\ 163 r_{z} & u_{z} & f_{z} & 0 \\ 164 0 & 0 & 0 & 1 165 \end{bmatrix} 166 \end{matrix} 167 $$</p> 168 <p>Solving for $V$ reveals that $V=R^{-1} \times T^{-1}$: 169 $$ 170 V = 171 \begin{bmatrix} 172 r_{x} & r_{y} & r_{z} & 0 \\ 173 u_{x} & u_{y} & u_{z} & 0 \\ 174 f_{x} & f_{y} & f_{z} & 0 \\ 175 0 & 0 & 0 & 1 176 \end{bmatrix} 177 \times 178 \begin{bmatrix} 179 1 & 0 & 0 & -p_{x} \\ 180 0 & 1 & 0 & -p_{y} \\ 181 0 & 0 & 1 & -p_{z} \\ 182 0 & 0 & 0 & 1 183 \end{bmatrix} 184 $$</p> 185 <p>And with some minor simplification, 186 $$ 187 V 188 = 189 \begin{bmatrix} 190 r_{x} & r_{y} & r_{z} & -(\vec{r} \cdot \vec{p}) \\ 191 u_{x} & u_{y} & u_{z} & -(\vec{u} \cdot \vec{p}) \\ 192 f_{x} & f_{y} & f_{z} & -(\vec{f} \cdot \vec{p}) \\ 193 0 & 0 & 0 & 1 194 \end{bmatrix} 195 $$</p> 196 <p>When we’re ready to render our scene, we can apply $V$ to each vertex $\vec{q}$ 197 by calculating 198 $$ 199 V \times \vec{q} 200 $$ 201 in our vertex shader.</p> 202 ]]></content:encoded> 203 </item> 204 <item> 205 <title>Unbind Your Vertex Array Objects.</title> 206 <link>https://electrosoup.com/posts/unbind/</link> 207 <pubDate>Wed, 25 Aug 2021 14:33:02 -0500</pubDate><author>christianermann@gmail.com (Christian Ermann)</author> 208 <guid>https://electrosoup.com/posts/unbind/</guid> 209 <description><p>When I first started learning about OpenGL, I always made sure to unbind each resource as soon as I was done using it. Over time I became more lenient with unbinding, as in many cases leaving resources bound is okay and leads to fewer API calls.</p></description> 210 <content:encoded><![CDATA[<p>When I first started learning about OpenGL, I always made sure to unbind each resource as soon as I was done using it. Over time I became more lenient with unbinding, as in many cases leaving resources bound is okay and leads to fewer API calls.</p> 211 <p>In addition, when I’m developing on my PC I have access to the newer Direct State Access (DSA) functions, so binding resources isn’t something I have to worry about as much. However, I do a fair amount of programming on my macbook which means I don’t always have access to the DSA functions.</p> 212 <p>I was working on a project recently that involved the vertex and index data for a mesh frequently changing. The actual mesh struct has a bit more going on, but we can think of it like this for now:</p> 213 214 215 216 217 <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-c" data-lang="c"><span class="line"><span class="ln">1</span><span class="cl"><span class="k">typedef</span> <span class="k">struct</span> <span class="p">{</span> 218 </span></span><span class="line"><span class="ln">2</span><span class="cl"> <span class="n">Vec3</span> <span class="o">*</span><span class="n">vertices</span><span class="p">;</span> 219 </span></span><span class="line"><span class="ln">3</span><span class="cl"> <span class="n">GLuint</span> <span class="o">*</span><span class="n">indices</span><span class="p">;</span> 220 </span></span><span class="line"><span class="ln">4</span><span class="cl"> <span class="n">GLuint</span> <span class="n">vao</span><span class="p">;</span> 221 </span></span><span class="line"><span class="ln">5</span><span class="cl"> <span class="n">GLuint</span> <span class="n">vbo</span><span class="p">;</span> 222 </span></span><span class="line"><span class="ln">6</span><span class="cl"> <span class="n">GLuint</span> <span class="n">ebo</span><span class="p">;</span> 223 </span></span><span class="line"><span class="ln">7</span><span class="cl"><span class="p">}</span> <span class="n">Mesh</span><span class="p">;</span></span></span></code></pre></div> 224 <p>So when it comes time to update the vertex or index data, we map the vertex buffer object to the vertices pointer and the element buffer object to the indices pointer. Then we can write all of our vertex and index data directly to the buffers, and then unmap them when we’re all done and need to draw.</p> 225 226 227 228 229 <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-c" data-lang="c"><span class="line"><span class="ln"> 1</span><span class="cl"><span class="p">...</span> 230 </span></span><span class="line"><span class="ln"> 2</span><span class="cl"> 231 </span></span><span class="line"><span class="ln"> 3</span><span class="cl"><span class="nf">glBindBuffer</span><span class="p">(</span><span class="n">GL_ARRAY_BUFFER</span><span class="p">,</span> <span class="n">mesh</span><span class="o">-></span><span class="n">vbo</span><span class="p">);</span> 232 </span></span><span class="line"><span class="ln"> 4</span><span class="cl"><span class="n">mesh</span><span class="o">-></span><span class="n">vertices</span> <span class="o">=</span> <span class="nf">glMapBuffer</span><span class="p">(</span><span class="n">GL_ARRAY_BUFFER</span><span class="p">,</span> <span class="n">GL_WRITE_ONLY</span><span class="p">);</span> 233 </span></span><span class="line"><span class="ln"> 5</span><span class="cl"> 234 </span></span><span class="line"><span class="ln"> 6</span><span class="cl"><span class="nf">glBindBuffer</span><span class="p">(</span><span class="n">GL_ELEMENT_ARRAY_BUFFER</span><span class="p">,</span> <span class="n">mesh</span><span class="o">-></span><span class="n">ebo</span><span class="p">);</span> 235 </span></span><span class="line"><span class="ln"> 7</span><span class="cl"><span class="n">mesh</span><span class="o">-></span><span class="n">indices</span> <span class="o">=</span> <span class="nf">glMapBuffer</span><span class="p">(</span><span class="n">GL_ELEMENT_ARRAY_BUFFER</span><span class="p">,</span> <span class="n">GL_WRITE_ONLY</span><span class="p">);</span> 236 </span></span><span class="line"><span class="ln"> 8</span><span class="cl"> 237 </span></span><span class="line"><span class="ln"> 9</span><span class="cl"><span class="c1">// While we have the buffers mapped, we can write data directly to 238 </span></span></span><span class="line"><span class="ln">10</span><span class="cl"><span class="c1">// mesh->vertices and mesh->indices. 239 </span></span></span><span class="line"><span class="ln">11</span><span class="cl"><span class="c1"></span> 240 </span></span><span class="line"><span class="ln">12</span><span class="cl"><span class="nf">glBindBuffer</span><span class="p">(</span><span class="n">GL_ARRAY_BUFFER</span><span class="p">,</span> <span class="n">mesh</span><span class="o">-></span><span class="n">vbo</span><span class="p">);</span> 241 </span></span><span class="line"><span class="ln">13</span><span class="cl"><span class="nf">glUnmapBuffer</span><span class="p">(</span><span class="n">GL_ARRAY_BUFFER</span><span class="p">);</span> 242 </span></span><span class="line"><span class="ln">14</span><span class="cl"><span class="n">mesh</span><span class="o">-></span><span class="n">vertices</span> <span class="o">=</span> <span class="nb">NULL</span> 243 </span></span><span class="line"><span class="ln">15</span><span class="cl"> 244 </span></span><span class="line"><span class="ln">16</span><span class="cl"><span class="nf">glBindBuffer</span><span class="p">(</span><span class="n">GL_ELEMENT_ARRAY_BUFFER</span><span class="p">,</span> <span class="n">mesh</span><span class="o">-></span><span class="n">ebo</span><span class="p">);</span> 245 </span></span><span class="line"><span class="ln">17</span><span class="cl"><span class="nf">glUnmapBuffer</span><span class="p">(</span><span class="n">GL_ELEMENT_ARRAY_BUFFER</span><span class="p">);</span> 246 </span></span><span class="line"><span class="ln">18</span><span class="cl"><span class="n">mesh</span><span class="o">-></span><span class="n">indices</span> <span class="o">=</span> <span class="nb">NULL</span> 247 </span></span><span class="line"><span class="ln">19</span><span class="cl"> 248 </span></span><span class="line"><span class="ln">20</span><span class="cl"><span class="c1">// Now that the buffers are unmapped, we can draw the mesh again. 249 </span></span></span><span class="line"><span class="ln">21</span><span class="cl"><span class="c1"></span> 250 </span></span><span class="line"><span class="ln">22</span><span class="cl"><span class="p">...</span></span></span></code></pre></div> 251 <p>Leaving the buffers bound in the above snippet didn’t cause problems and seemed a reasonable thing to do to reduce unnecessary OpenGL calls. 252 The issues I was running into were actually caused by how I was drawing each mesh:</p> 253 254 255 256 257 <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-c" data-lang="c"><span class="line"><span class="ln">1</span><span class="cl"><span class="p">...</span> 258 </span></span><span class="line"><span class="ln">2</span><span class="cl"> 259 </span></span><span class="line"><span class="ln">3</span><span class="cl"><span class="nf">glBindVertexArray</span><span class="p">(</span><span class="n">mesh</span><span class="o">-></span><span class="n">vao</span><span class="p">);</span> 260 </span></span><span class="line"><span class="ln">4</span><span class="cl"><span class="nf">glDrawElements</span><span class="p">(...);</span> 261 </span></span><span class="line"><span class="ln">5</span><span class="cl"> 262 </span></span><span class="line"><span class="ln">6</span><span class="cl"><span class="p">...</span></span></span></code></pre></div> 263 <p>Since I wasn’t unbinding the mesh’s vertex array object when I finished drawing it, when I updated another mesh’s vertex or index data and bound that mesh’s buffer objects, the vertex array object I forgot to unbind would now be using the incorrect buffers to draw with.</p> 264 <p>Luckily, there was a simple fix, and now I know to be more careful with unbinding vertex array objects:</p> 265 266 267 268 269 <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-c" data-lang="c"><span class="line"><span class="ln">1</span><span class="cl"><span class="p">...</span> 270 </span></span><span class="line"><span class="ln">2</span><span class="cl"> 271 </span></span><span class="line"><span class="ln">3</span><span class="cl"><span class="nf">glBindVertexArray</span><span class="p">(</span><span class="n">mesh</span><span class="o">-></span><span class="n">vao</span><span class="p">);</span> 272 </span></span><span class="line"><span class="ln">4</span><span class="cl"><span class="nf">glDrawElements</span><span class="p">(...);</span> 273 </span></span><span class="line"><span class="ln">5</span><span class="cl"><span class="nf">glBindVertexArray</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span> 274 </span></span><span class="line"><span class="ln">6</span><span class="cl"> 275 </span></span><span class="line"><span class="ln">7</span><span class="cl"><span class="p">...</span></span></span></code></pre></div> 276 ]]></content:encoded> 277 </item> 278 </channel> 279 </rss>