electrosoup.com

git clone git://git.electrosoup.com/electrosoup.com
Log | Files | Refs

index.xml (25326B)


      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>C on Electrosoup</title>
      5     <link>https://electrosoup.com/tags/c/</link>
      6     <description>Recent content in C 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>Wed, 23 Oct 2024 16:29:40 -0700</lastBuildDate>
     12     <atom:link href="https://electrosoup.com/tags/c/index.xml" rel="self" type="application/rss+xml" />
     13     <item>
     14       <title>Writing a G-code Interpreter</title>
     15       <link>https://electrosoup.com/posts/gcode-interpreter/</link>
     16       <pubDate>Wed, 23 Oct 2024 16:29:40 -0700</pubDate><author>christianermann@gmail.com (Christian Ermann)</author>
     17       <guid>https://electrosoup.com/posts/gcode-interpreter/</guid>
     18       <description>&lt;p&gt;I&amp;rsquo;m currently working on a control system for a laser galvanometer (galvo) that&#xA;I want to use in a stereolithography (SLA) resin printer. I don&amp;rsquo;t want to write&#xA;a custom slicer, as I feel like model slicing can be incredibly complicated and&#xA;existing free software (Cura, Slic3r, etc.) has many advanced features I don&amp;rsquo;t&#xA;want to reimplement. However, this means my control system needs to understand&#xA;G-code, the language that these slicers output.&lt;/p&gt;</description>
     19       <content:encoded><![CDATA[<p>I&rsquo;m currently working on a control system for a laser galvanometer (galvo) that
     20 I want to use in a stereolithography (SLA) resin printer. I don&rsquo;t want to write
     21 a custom slicer, as I feel like model slicing can be incredibly complicated and
     22 existing free software (Cura, Slic3r, etc.) has many advanced features I don&rsquo;t
     23 want to reimplement. However, this means my control system needs to understand
     24 G-code, the language that these slicers output.</p>
     25 <p>As stated on the <a href="https://reprap.org/wiki/G-code">reprap wiki</a>, &ldquo;G-code is a
     26 list of fields that are separated by white spaces or line breaks. A field can
     27 be interpreted as a command, parameter, or for any other special purpose.&rdquo; For
     28 now, we&rsquo;ll ignore any special purposes. Here is an example of G-code that
     29 describes a movement of the tool/hotend/laser to a specific location:</p>
     30 
     31 
     32 
     33 
     34 
     35 <pre tabindex="0"><code class="language-gcode" data-lang="gcode">    G1 X10.4 Y7.4 Z8.3</code></pre><p>I was able to parse commands with parameters using a two-stage procedure:</p>
     36 <pre><code>1. Retrieve the next white-space delimited token.
     37 2. Accumulate tokens until two separate command tokens are found.
     38 </code></pre>
     39 <p>This second step is important because we can&rsquo;t begin executing a command until
     40 we&rsquo;ve found all of its parameters and we don&rsquo;t know if we&rsquo;ve found all of its
     41 parameters until we find the next command.</p>
     42 <p>Once a command and all of its parameters have been parsed, it is passed to the
     43 controller which updates the state of the machine. For each command that the
     44 controller receives, it has to update its internal state, and in the case of
     45 physical movement commands, send the proper signal to the laser galvo. Most
     46 physical movement commands utilize interpolation so a timed control loop is
     47 utilized to ensure the laser follows as close to the intended path as possible.</p>
     48 <h2 id="system-diagram">System Diagram</h2>
     49 
     50 
     51 
     52 <div class="goat svg-container ">
     53   
     54     <svg
     55       xmlns="http://www.w3.org/2000/svg"
     56       font-family="Menlo,Lucida Console,monospace"
     57       
     58         viewBox="0 0 384 249"
     59       >
     60       <g transform='translate(8,16)'>
     61 <path d='M 80,16 L 296,16' fill='none' stroke='currentColor'></path>
     62 <path d='M 16,64 L 112,64' fill='none' stroke='currentColor'></path>
     63 <path d='M 256,64 L 368,64' fill='none' stroke='currentColor'></path>
     64 <path d='M 128,80 L 240,80' fill='none' stroke='currentColor'></path>
     65 <path d='M 16,96 L 112,96' fill='none' stroke='currentColor'></path>
     66 <path d='M 256,96 L 368,96' fill='none' stroke='currentColor'></path>
     67 <path d='M 80,144 L 296,144' fill='none' stroke='currentColor'></path>
     68 <path d='M 0,192 L 120,192' fill='none' stroke='currentColor'></path>
     69 <path d='M 256,192 L 368,192' fill='none' stroke='currentColor'></path>
     70 <path d='M 136,208 L 240,208' fill='none' stroke='currentColor'></path>
     71 <path d='M 0,224 L 120,224' fill='none' stroke='currentColor'></path>
     72 <path d='M 256,224 L 368,224' fill='none' stroke='currentColor'></path>
     73 <path d='M 0,192 L 0,224' fill='none' stroke='currentColor'></path>
     74 <path d='M 16,64 L 16,96' fill='none' stroke='currentColor'></path>
     75 <path d='M 64,32 L 64,48' fill='none' stroke='currentColor'></path>
     76 <path d='M 64,160 L 64,176' fill='none' stroke='currentColor'></path>
     77 <path d='M 112,64 L 112,96' fill='none' stroke='currentColor'></path>
     78 <path d='M 120,192 L 120,224' fill='none' stroke='currentColor'></path>
     79 <path d='M 256,64 L 256,96' fill='none' stroke='currentColor'></path>
     80 <path d='M 256,192 L 256,224' fill='none' stroke='currentColor'></path>
     81 <path d='M 312,32 L 312,48' fill='none' stroke='currentColor'></path>
     82 <path d='M 312,112 L 312,128' fill='none' stroke='currentColor'></path>
     83 <path d='M 368,64 L 368,96' fill='none' stroke='currentColor'></path>
     84 <path d='M 368,192 L 368,224' fill='none' stroke='currentColor'></path>
     85 <path d='M 312,48 L 312,56' fill='none' stroke='currentColor'></path>
     86 <path d='M 312,104 L 312,112' fill='none' stroke='currentColor'></path>
     87 <path d='M 64,48 L 64,56' fill='none' stroke='currentColor'></path>
     88 <polygon points='80.000000,48.000000 68.000000,42.400002 68.000000,53.599998' fill='currentColor' transform='rotate(90.000000, 64.000000, 48.000000)'></polygon>
     89 <path d='M 64,176 L 64,184' fill='none' stroke='currentColor'></path>
     90 <polygon points='80.000000,176.000000 68.000000,170.399994 68.000000,181.600006' fill='currentColor' transform='rotate(90.000000, 64.000000, 176.000000)'></polygon>
     91 <polygon points='248.000000,80.000000 236.000000,74.400002 236.000000,85.599998' fill='currentColor' transform='rotate(0.000000, 240.000000, 80.000000)'></polygon>
     92 <polygon points='248.000000,208.000000 236.000000,202.399994 236.000000,213.600006' fill='currentColor' transform='rotate(0.000000, 240.000000, 208.000000)'></polygon>
     93 <path d='M 80,16 A 16,16 0 0,0 64,32' fill='none' stroke='currentColor'></path>
     94 <path d='M 296,16 A 16,16 0 0,1 312,32' fill='none' stroke='currentColor'></path>
     95 <path d='M 80,144 A 16,16 0 0,0 64,160' fill='none' stroke='currentColor'></path>
     96 <path d='M 312,128 A 16,16 0 0,1 296,144' fill='none' stroke='currentColor'></path>
     97 <text text-anchor='middle' x='24' y='212' fill='currentColor' style='font-size:1em'>c</text>
     98 <text text-anchor='middle' x='32' y='84' fill='currentColor' style='font-size:1em'>t</text>
     99 <text text-anchor='middle' x='32' y='212' fill='currentColor' style='font-size:1em'>o</text>
    100 <text text-anchor='middle' x='40' y='84' fill='currentColor' style='font-size:1em'>o</text>
    101 <text text-anchor='middle' x='40' y='212' fill='currentColor' style='font-size:1em'>n</text>
    102 <text text-anchor='middle' x='48' y='84' fill='currentColor' style='font-size:1em'>k</text>
    103 <text text-anchor='middle' x='48' y='212' fill='currentColor' style='font-size:1em'>t</text>
    104 <text text-anchor='middle' x='56' y='84' fill='currentColor' style='font-size:1em'>e</text>
    105 <text text-anchor='middle' x='56' y='212' fill='currentColor' style='font-size:1em'>r</text>
    106 <text text-anchor='middle' x='64' y='84' fill='currentColor' style='font-size:1em'>n</text>
    107 <text text-anchor='middle' x='64' y='212' fill='currentColor' style='font-size:1em'>o</text>
    108 <text text-anchor='middle' x='72' y='84' fill='currentColor' style='font-size:1em'>i</text>
    109 <text text-anchor='middle' x='72' y='212' fill='currentColor' style='font-size:1em'>l</text>
    110 <text text-anchor='middle' x='80' y='84' fill='currentColor' style='font-size:1em'>z</text>
    111 <text text-anchor='middle' x='80' y='212' fill='currentColor' style='font-size:1em'>l</text>
    112 <text text-anchor='middle' x='88' y='84' fill='currentColor' style='font-size:1em'>e</text>
    113 <text text-anchor='middle' x='88' y='212' fill='currentColor' style='font-size:1em'>e</text>
    114 <text text-anchor='middle' x='96' y='84' fill='currentColor' style='font-size:1em'>r</text>
    115 <text text-anchor='middle' x='96' y='212' fill='currentColor' style='font-size:1em'>r</text>
    116 <text text-anchor='middle' x='128' y='132' fill='currentColor' style='font-size:1em'>c</text>
    117 <text text-anchor='middle' x='136' y='132' fill='currentColor' style='font-size:1em'>o</text>
    118 <text text-anchor='middle' x='144' y='4' fill='currentColor' style='font-size:1em'>m</text>
    119 <text text-anchor='middle' x='144' y='132' fill='currentColor' style='font-size:1em'>m</text>
    120 <text text-anchor='middle' x='144' y='196' fill='currentColor' style='font-size:1em'>c</text>
    121 <text text-anchor='middle' x='152' y='4' fill='currentColor' style='font-size:1em'>o</text>
    122 <text text-anchor='middle' x='152' y='132' fill='currentColor' style='font-size:1em'>m</text>
    123 <text text-anchor='middle' x='152' y='196' fill='currentColor' style='font-size:1em'>t</text>
    124 <text text-anchor='middle' x='160' y='4' fill='currentColor' style='font-size:1em'>r</text>
    125 <text text-anchor='middle' x='160' y='132' fill='currentColor' style='font-size:1em'>a</text>
    126 <text text-anchor='middle' x='160' y='196' fill='currentColor' style='font-size:1em'>r</text>
    127 <text text-anchor='middle' x='168' y='4' fill='currentColor' style='font-size:1em'>e</text>
    128 <text text-anchor='middle' x='168' y='68' fill='currentColor' style='font-size:1em'>t</text>
    129 <text text-anchor='middle' x='168' y='132' fill='currentColor' style='font-size:1em'>n</text>
    130 <text text-anchor='middle' x='168' y='196' fill='currentColor' style='font-size:1em'>l</text>
    131 <text text-anchor='middle' x='176' y='68' fill='currentColor' style='font-size:1em'>o</text>
    132 <text text-anchor='middle' x='176' y='132' fill='currentColor' style='font-size:1em'>d</text>
    133 <text text-anchor='middle' x='184' y='4' fill='currentColor' style='font-size:1em'>p</text>
    134 <text text-anchor='middle' x='184' y='68' fill='currentColor' style='font-size:1em'>k</text>
    135 <text text-anchor='middle' x='184' y='196' fill='currentColor' style='font-size:1em'>s</text>
    136 <text text-anchor='middle' x='192' y='4' fill='currentColor' style='font-size:1em'>a</text>
    137 <text text-anchor='middle' x='192' y='68' fill='currentColor' style='font-size:1em'>e</text>
    138 <text text-anchor='middle' x='192' y='132' fill='currentColor' style='font-size:1em'>w</text>
    139 <text text-anchor='middle' x='192' y='196' fill='currentColor' style='font-size:1em'>i</text>
    140 <text text-anchor='middle' x='200' y='4' fill='currentColor' style='font-size:1em'>r</text>
    141 <text text-anchor='middle' x='200' y='68' fill='currentColor' style='font-size:1em'>n</text>
    142 <text text-anchor='middle' x='200' y='132' fill='currentColor' style='font-size:1em'>/</text>
    143 <text text-anchor='middle' x='200' y='196' fill='currentColor' style='font-size:1em'>g</text>
    144 <text text-anchor='middle' x='208' y='4' fill='currentColor' style='font-size:1em'>a</text>
    145 <text text-anchor='middle' x='208' y='196' fill='currentColor' style='font-size:1em'>n</text>
    146 <text text-anchor='middle' x='216' y='4' fill='currentColor' style='font-size:1em'>m</text>
    147 <text text-anchor='middle' x='216' y='132' fill='currentColor' style='font-size:1em'>p</text>
    148 <text text-anchor='middle' x='216' y='196' fill='currentColor' style='font-size:1em'>a</text>
    149 <text text-anchor='middle' x='224' y='4' fill='currentColor' style='font-size:1em'>s</text>
    150 <text text-anchor='middle' x='224' y='132' fill='currentColor' style='font-size:1em'>a</text>
    151 <text text-anchor='middle' x='224' y='196' fill='currentColor' style='font-size:1em'>l</text>
    152 <text text-anchor='middle' x='232' y='4' fill='currentColor' style='font-size:1em'>?</text>
    153 <text text-anchor='middle' x='232' y='132' fill='currentColor' style='font-size:1em'>r</text>
    154 <text text-anchor='middle' x='240' y='132' fill='currentColor' style='font-size:1em'>a</text>
    155 <text text-anchor='middle' x='248' y='132' fill='currentColor' style='font-size:1em'>m</text>
    156 <text text-anchor='middle' x='256' y='132' fill='currentColor' style='font-size:1em'>s</text>
    157 <text text-anchor='middle' x='272' y='84' fill='currentColor' style='font-size:1em'>a</text>
    158 <text text-anchor='middle' x='272' y='212' fill='currentColor' style='font-size:1em'>l</text>
    159 <text text-anchor='middle' x='280' y='84' fill='currentColor' style='font-size:1em'>c</text>
    160 <text text-anchor='middle' x='280' y='212' fill='currentColor' style='font-size:1em'>a</text>
    161 <text text-anchor='middle' x='288' y='84' fill='currentColor' style='font-size:1em'>c</text>
    162 <text text-anchor='middle' x='288' y='212' fill='currentColor' style='font-size:1em'>s</text>
    163 <text text-anchor='middle' x='296' y='84' fill='currentColor' style='font-size:1em'>u</text>
    164 <text text-anchor='middle' x='296' y='212' fill='currentColor' style='font-size:1em'>e</text>
    165 <text text-anchor='middle' x='304' y='84' fill='currentColor' style='font-size:1em'>m</text>
    166 <text text-anchor='middle' x='304' y='212' fill='currentColor' style='font-size:1em'>r</text>
    167 <text text-anchor='middle' x='312' y='84' fill='currentColor' style='font-size:1em'>u</text>
    168 <text text-anchor='middle' x='320' y='84' fill='currentColor' style='font-size:1em'>l</text>
    169 <text text-anchor='middle' x='320' y='212' fill='currentColor' style='font-size:1em'>g</text>
    170 <text text-anchor='middle' x='328' y='84' fill='currentColor' style='font-size:1em'>a</text>
    171 <text text-anchor='middle' x='328' y='212' fill='currentColor' style='font-size:1em'>a</text>
    172 <text text-anchor='middle' x='336' y='84' fill='currentColor' style='font-size:1em'>t</text>
    173 <text text-anchor='middle' x='336' y='212' fill='currentColor' style='font-size:1em'>l</text>
    174 <text text-anchor='middle' x='344' y='84' fill='currentColor' style='font-size:1em'>o</text>
    175 <text text-anchor='middle' x='344' y='212' fill='currentColor' style='font-size:1em'>v</text>
    176 <text text-anchor='middle' x='352' y='84' fill='currentColor' style='font-size:1em'>r</text>
    177 <text text-anchor='middle' x='352' y='212' fill='currentColor' style='font-size:1em'>o</text>
    178 </g>
    179 
    180     </svg>
    181   
    182 </div>
    183 <h2 id="source-code">Source Code</h2>
    184 <p>The source code for the G-code interpreter can be found
    185 <a href="https://git.christianermann.dev/gcode-interpreter">here</a>. You may have issues
    186 accessing the link if your ISP doesn&rsquo;t support IPv6 yet.</p>
    187 <p>Also, the names in the code are different than what&rsquo;s shown in the diagram, so
    188 here&rsquo;s the mapping to help out with understanding the code:</p>
    189 <table>
    190   <thead>
    191       <tr>
    192           <th style="text-align: left">Diagram</th>
    193           <th style="text-align: left">Source File</th>
    194       </tr>
    195   </thead>
    196   <tbody>
    197       <tr>
    198           <td style="text-align: left">tokenizer</td>
    199           <td style="text-align: left">token.c</td>
    200       </tr>
    201       <tr>
    202           <td style="text-align: left">accumulator</td>
    203           <td style="text-align: left">command.c</td>
    204       </tr>
    205       <tr>
    206           <td style="text-align: left">controller</td>
    207           <td style="text-align: left">machine.c</td>
    208       </tr>
    209   </tbody>
    210 </table>
    211 <p>I&rsquo;ll try to keep this updated if the mapping changes as I continue to work on
    212 this project.</p>
    213 ]]></content:encoded>
    214     </item>
    215     <item>
    216       <title>Unbind Your Vertex Array Objects.</title>
    217       <link>https://electrosoup.com/posts/unbind/</link>
    218       <pubDate>Wed, 25 Aug 2021 14:33:02 -0500</pubDate><author>christianermann@gmail.com (Christian Ermann)</author>
    219       <guid>https://electrosoup.com/posts/unbind/</guid>
    220       <description>&lt;p&gt;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.&lt;/p&gt;</description>
    221       <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>
    222 <p>In addition, when I&rsquo;m developing on my PC I have access to the newer Direct State Access (DSA) functions, so binding resources isn&rsquo;t something I have to worry about as much. However, I do a fair amount of programming on my macbook which means I don&rsquo;t always have access to the DSA functions.</p>
    223 <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>
    224 
    225 
    226 
    227 
    228 <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>
    229 </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>
    230 </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>
    231 </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>
    232 </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>
    233 </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>
    234 </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>
    235 <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&rsquo;re all done and need to draw.</p>
    236 
    237 
    238 
    239 
    240 <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>
    241 </span></span><span class="line"><span class="ln"> 2</span><span class="cl">
    242 </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">-&gt;</span><span class="n">vbo</span><span class="p">);</span>
    243 </span></span><span class="line"><span class="ln"> 4</span><span class="cl"><span class="n">mesh</span><span class="o">-&gt;</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>
    244 </span></span><span class="line"><span class="ln"> 5</span><span class="cl">
    245 </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">-&gt;</span><span class="n">ebo</span><span class="p">);</span>
    246 </span></span><span class="line"><span class="ln"> 7</span><span class="cl"><span class="n">mesh</span><span class="o">-&gt;</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>
    247 </span></span><span class="line"><span class="ln"> 8</span><span class="cl">
    248 </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
    249 </span></span></span><span class="line"><span class="ln">10</span><span class="cl"><span class="c1">// mesh-&gt;vertices and mesh-&gt;indices.
    250 </span></span></span><span class="line"><span class="ln">11</span><span class="cl"><span class="c1"></span>
    251 </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">-&gt;</span><span class="n">vbo</span><span class="p">);</span>
    252 </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>
    253 </span></span><span class="line"><span class="ln">14</span><span class="cl"><span class="n">mesh</span><span class="o">-&gt;</span><span class="n">vertices</span> <span class="o">=</span> <span class="nb">NULL</span>
    254 </span></span><span class="line"><span class="ln">15</span><span class="cl">
    255 </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">-&gt;</span><span class="n">ebo</span><span class="p">);</span>
    256 </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>
    257 </span></span><span class="line"><span class="ln">18</span><span class="cl"><span class="n">mesh</span><span class="o">-&gt;</span><span class="n">indices</span> <span class="o">=</span> <span class="nb">NULL</span>
    258 </span></span><span class="line"><span class="ln">19</span><span class="cl">
    259 </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.
    260 </span></span></span><span class="line"><span class="ln">21</span><span class="cl"><span class="c1"></span>
    261 </span></span><span class="line"><span class="ln">22</span><span class="cl"><span class="p">...</span></span></span></code></pre></div>
    262 <p>Leaving the buffers bound in the above snippet didn&rsquo;t cause problems and seemed a reasonable thing to do to reduce unnecessary OpenGL calls.
    263 The issues I was running into were actually caused by how I was drawing each mesh:</p>
    264 
    265 
    266 
    267 
    268 <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>
    269 </span></span><span class="line"><span class="ln">2</span><span class="cl">
    270 </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">-&gt;</span><span class="n">vao</span><span class="p">);</span>
    271 </span></span><span class="line"><span class="ln">4</span><span class="cl"><span class="nf">glDrawElements</span><span class="p">(...);</span>
    272 </span></span><span class="line"><span class="ln">5</span><span class="cl">
    273 </span></span><span class="line"><span class="ln">6</span><span class="cl"><span class="p">...</span></span></span></code></pre></div>
    274 <p>Since I wasn&rsquo;t unbinding the mesh&rsquo;s vertex array object when I finished drawing it, when I updated another mesh&rsquo;s vertex or index data and bound that mesh&rsquo;s buffer objects, the vertex array object I forgot to unbind would now be using the incorrect buffers to draw with.</p>
    275 <p>Luckily, there was a simple fix, and now I know to be more careful with unbinding vertex array objects:</p>
    276 
    277 
    278 
    279 
    280 <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>
    281 </span></span><span class="line"><span class="ln">2</span><span class="cl">
    282 </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">-&gt;</span><span class="n">vao</span><span class="p">);</span>
    283 </span></span><span class="line"><span class="ln">4</span><span class="cl"><span class="nf">glDrawElements</span><span class="p">(...);</span>
    284 </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>
    285 </span></span><span class="line"><span class="ln">6</span><span class="cl">
    286 </span></span><span class="line"><span class="ln">7</span><span class="cl"><span class="p">...</span></span></span></code></pre></div>
    287 ]]></content:encoded>
    288     </item>
    289   </channel>
    290 </rss>