title: OpenGL Merging Vertex Data to Minimize Draw Calls
body: <p><strong>Background</strong></p>
  <ul>
  <li>2D &quot;Infinite&quot; World separated into chunks</li>
  <li>One VAO (&amp; VBO/EBO) per chunk</li>
  <li>Nested for loop in chunk render; one draw call per block.</li>
  </ul>
  <p><strong>Code</strong></p>
  
  <pre class="lang-cpp prettyprint-override"><code>void Chunk::Render(/* ... */) {
      glBindVertexArray(vao);
  
      for (int x = 0; x &lt; 64; x++) {
          for (int y = 0; y &lt; 64; y++) {
              if (blocks[x][y] == 1) {
                  /* ... Uniforms ... */
                  glDrawElements(GL_TRIANGLE_STRIP, 6, GL_UNSIGNED_INT, (void*)0);
              }
          }
      }
      glBindVertexArray(0);
  }
  </code></pre>
  <p>There is a generation algorithm in the constructor. This could be anything: noise, random, etc. The algorithm goes through and sets an element in the <code>blocks</code> array to 1 (meaning: render block) or 0 (meaning: do not render)</p>
  <p><strong>Problem</strong></p>
  <p>How would I go about combining these triangle strips together in order to minimize draw calls? I can think of a few algorithms to find the triangles that should be merged together in a draw call, but I am confused as how to merge them together. Do I need to add it to the vertices array and call <code>glBufferData</code> again? Would it be bad to call glBufferData so many times per-frame?</p>
  <p>I'm not really rendering <em>that</em> many triangles, am I? I think I've heard of people who can easily draw ten-thousand triangles with minimal CPU usage (or.. millions even). So what is wrong with how I am drawing currently?</p>
  <h1>EDIT</h1>
  _[Andon M. Coleman][1]_ has given me a lot of information in the [chat][2]. I have now switched over to using instanced arrays; I cannot believe how much of a difference it makes in performance, for a minute I thought Linux's `top` command was malfunctioning. It's _very_ significant. Instead of only being able to render say.. 60 triangles, I can render over a million with barely any change in CPU usage.