LearnOpenGL

Translation in progress of learnopengl.com.
git clone https://git.mtkn.jp/LearnOpenGL
Log | Files | Refs

Instancing.html (25682B)


      1     <div id="content">
      2     <h1 id="content-title">Instancing</h1>
      3 <h1 id="content-url" style='display:none;'>Advanced-OpenGL/Instancing</h1>
      4 <p>
      5   Say you have a scene where you're drawing a lot of models where most of these models contain the same set of vertex data, but with different world transformations. Think of a scene filled with grass leaves: each grass leave is a small model that consists of only a few triangles. You'll probably want to draw quite a few of them and your scene may end up with thousands or maybe tens of thousands of grass leaves that you need to render each frame. Because each leaf is only a few triangles, the leaf is rendered almost instantly. However, the thousands of render calls you'll have to make will drastically reduce performance.
      6 </p>
      7 
      8 <p>
      9   If we were to actually render such a large amount of objects it will look a bit like this in code:
     10 </p>
     11 
     12 <pre><code>
     13 for(unsigned int i = 0; i &lt; amount_of_models_to_draw; i++)
     14 {
     15     DoSomePreparations(); // bind VAO, bind textures, set uniforms etc.
     16     <function id='1'>glDrawArrays</function>(GL_TRIANGLES, 0, amount_of_vertices);
     17 }
     18 </code></pre>
     19 
     20 <p>
     21   When drawing many <def>instances</def> of your model like this you'll quickly reach a performance bottleneck because of the many draw calls. Compared to rendering the actual vertices, telling the GPU to render your vertex data with functions like <fun><function id='1'>glDrawArrays</function></fun> or <fun><function id='2'>glDrawElements</function></fun> eats up quite some performance since OpenGL must make necessary preparations before it can draw your vertex data (like telling the GPU which buffer to read data from, where to find vertex attributes and all this over the relatively slow CPU to GPU bus). So even though rendering your vertices is super fast, giving your GPU the commands to render them isn't.
     22 </p>
     23 
     24 <p>
     25   It would be much more convenient if we could send data over to the GPU once, and then tell OpenGL to draw multiple objects using this data with a single drawing call. Enter <def>instancing</def>.
     26 </p>
     27 
     28 <p>
     29   Instancing is a technique where we draw many (equal mesh data) objects at once with a single render call, saving us all the CPU -> GPU communications each time we need to render an object. To render using instancing all we need to do is change the render calls <fun><function id='1'>glDrawArrays</function></fun> and <fun><function id='2'>glDrawElements</function></fun> to <fun><function id='98'><function id='1'>glDrawArrays</function>Instanced</function></fun> and <fun><function id='99'><function id='2'>glDrawElements</function>Instanced</function></fun> respectively. These <em>instanced</em> versions of the classic rendering functions take an extra parameter called the <def>instance count</def> that sets the number of instances we want to render. We sent all the required data to the GPU once, and then tell the GPU how it should draw all these instances with a single call. The GPU then renders all these instances without having to continually communicate with the CPU.
     30 </p>
     31 
     32 <p>
     33   By itself this function is a bit useless. Rendering the same object a thousand times is of no use to us since each of the rendered objects is rendered exactly the same and thus also at the same location; we would only see one object! For this reason GLSL added another built-in variable in the vertex shader called <var>gl_InstanceID</var>. 
     34 </p>
     35 
     36 <p>
     37   When drawing with one of the instanced rendering calls, <var>gl_InstanceID</var> is incremented for each instance being rendered starting from <code>0</code>. If we were to render the 43th instance for example, <var>gl_InstanceID</var> would have the value <code>42</code> in the vertex shader. Having a unique value per instance means we could now for example index into a large array of position values to position each instance at a different location in the world.
     38 </p>
     39 
     40 <p>
     41   To get a feel for instanced drawing we're going to demonstrate a simple example that renders a hundred 2D quads in normalized device coordinates with just one render call. We accomplish this by uniquely positioning each instanced quad by indexing a uniform array of <code>100</code> offset vectors. The result is a neatly organized grid of quads that fill the entire window:
     42 </p>
     43 
     44 <img src="/img/advanced/instancing_quads.png" class="clean" alt="100 Quads drawn via OpenGL instancing."/>
     45 
     46 <p>
     47   Each quad consists of 2 triangles with a total of 6 vertices. Each vertex contains a 2D NDC position vector and a color vector. Below is the vertex data used for this example - the triangles are small enough to properly fit the screen when there's a 100 of them:
     48 </p>
     49 
     50 <pre><code>
     51 float quadVertices[] = {
     52     // positions     // colors
     53     -0.05f,  0.05f,  1.0f, 0.0f, 0.0f,
     54      0.05f, -0.05f,  0.0f, 1.0f, 0.0f,
     55     -0.05f, -0.05f,  0.0f, 0.0f, 1.0f,
     56 
     57     -0.05f,  0.05f,  1.0f, 0.0f, 0.0f,
     58      0.05f, -0.05f,  0.0f, 1.0f, 0.0f,   
     59      0.05f,  0.05f,  0.0f, 1.0f, 1.0f		    		
     60 };  
     61 </code></pre>
     62 
     63 <p>
     64   The quads are colored in the fragment shader that receives a color vector from the vertex shader and sets it as its output:
     65 </p>
     66 
     67 <pre><code>
     68 #version 330 core
     69 out vec4 FragColor;
     70   
     71 in vec3 fColor;
     72 
     73 void main()
     74 {
     75     FragColor = vec4(fColor, 1.0);
     76 }
     77 </code></pre>
     78 
     79 <p>
     80   Nothing new so far, but at the vertex shader it's starting to get interesting:
     81 </p>
     82 
     83 <pre><code>
     84 #version 330 core
     85 layout (location = 0) in vec2 aPos;
     86 layout (location = 1) in vec3 aColor;
     87 
     88 out vec3 fColor;
     89 
     90 uniform vec2 offsets[100];
     91 
     92 void main()
     93 {
     94     vec2 offset = offsets[gl_InstanceID];
     95     gl_Position = vec4(aPos + offset, 0.0, 1.0);
     96     fColor = aColor;
     97 }  
     98 </code></pre>
     99 
    100 <p>
    101   Here we defined a uniform array called <var>offsets</var> that contain a total of <code>100</code> offset vectors. Within the vertex shader we retrieve an offset vector for each instance by indexing the <var>offsets</var> array using <var>gl_InstanceID</var>. If we now were to draw <code>100</code> quads with instanced drawing we'd get <code>100</code> quads located at different positions.
    102 </p>
    103 
    104 <p>
    105   We do need to actually set the offset positions that we calculate in a nested for-loop before we enter the render loop:
    106 </p>
    107 
    108 <pre><code>
    109 glm::vec2 translations[100];
    110 int index = 0;
    111 float offset = 0.1f;
    112 for(int y = -10; y &lt; 10; y += 2)
    113 {
    114     for(int x = -10; x &lt; 10; x += 2)
    115     {
    116         glm::vec2 translation;
    117         translation.x = (float)x / 10.0f + offset;
    118         translation.y = (float)y / 10.0f + offset;
    119         translations[index++] = translation;
    120     }
    121 }  
    122 </code></pre>
    123 
    124 <p>
    125   Here we create a set of <code>100</code> translation vectors that contains an offset vector for all positions in a 10x10 grid. In addition to generating the <var>translations</var> array, we'd also need to transfer the data to the vertex shader's uniform array:
    126 </p>
    127 
    128 <pre><code>
    129 shader.use();
    130 for(unsigned int i = 0; i &lt; 100; i++)
    131 {
    132     shader.setVec2(("offsets[" + std::to_string(i) + "]")), translations[i]);
    133 }  
    134 </code></pre>
    135 
    136 <p>
    137   Within this snippet of code we transform the for-loop counter <var>i</var> to a <fun>string</fun> to dynamically create a location string for querying the uniform location. For each item in the <var>offsets</var> uniform array we then set the corresponding translation vector.
    138 </p>
    139 
    140 <p>
    141   Now that all the preparations are finished we can start rendering the quads. To draw via instanced rendering we call <fun><function id='98'><function id='1'>glDrawArrays</function>Instanced</function></fun> or <fun><function id='99'><function id='2'>glDrawElements</function>Instanced</function></fun>. Since we're not using an element index buffer we're going to call the <fun><function id='1'>glDrawArrays</function></fun> version:
    142 </p>
    143 
    144 <pre class="cpp"><code>
    145 <function id='27'>glBindVertexArray</function>(quadVAO);
    146 <function id='98'><function id='1'>glDrawArrays</function>Instanced</function>(GL_TRIANGLES, 0, 6, 100);  
    147 </code></pre>
    148 
    149 <p>
    150   The parameters of <fun><function id='98'><function id='1'>glDrawArrays</function>Instanced</function></fun> are exactly the same as <fun><function id='1'>glDrawArrays</function></fun> except the last parameter that sets the number of instances we want to draw. Since we want to display <code>100</code> quads in a 10x10 grid we set it equal to <code>100</code>. Running the code should now give you the familiar image of <code>100</code> colorful quads.
    151 </p>
    152 
    153 <h2>Instanced arrays</h2>
    154 <p>
    155   While the previous implementation works fine for this specific use case, whenever we are rendering a lot more than <code>100</code> instances (which is quite common) we will eventually hit a <a href="https://www.khronos.org/opengl/wiki/GLSL_Uniform#Implementation_limits" target="_blank">limit</a> on the amount of uniform data we can send to the shaders. One alternative option is known as <def>instanced arrays</def>. Instanced arrays are defined as a vertex attribute (allowing us to store much more data) that are updated per instance instead of per vertex.
    156 </p>
    157 
    158 <p>
    159   With vertex attributes, at the start of each run of the vertex shader, the GPU will retrieve the next set of vertex attributes that belong to the current vertex. When defining a vertex attribute as an instanced array however, the vertex shader only updates the content of the vertex attribute per instance. This allows us to use the standard vertex attributes for data per vertex and use the instanced array for storing data that is unique per instance.
    160 </p>
    161 
    162 <p>
    163   To give you an example of an instanced array we're going to take the previous example and convert the offset uniform array to an instanced array. We'll have to update the vertex shader by adding another vertex attribute:
    164 </p>
    165 
    166 <pre><code>
    167 #version 330 core
    168 layout (location = 0) in vec2 aPos;
    169 layout (location = 1) in vec3 aColor;
    170 layout (location = 2) in vec2 aOffset;
    171 
    172 out vec3 fColor;
    173 
    174 void main()
    175 {
    176     gl_Position = vec4(aPos + aOffset, 0.0, 1.0);
    177     fColor = aColor;
    178 }  
    179 </code></pre>
    180 
    181 <p>
    182   We no longer use <var>gl_InstanceID</var> and can directly use the <var>offset</var> attribute  without first indexing into a large uniform array.
    183 </p>
    184 
    185 <p>
    186   Because an instanced array is a vertex attribute, just like the <var>position</var> and <var>color</var> variables, we need to store its content in a vertex buffer object and configure its attribute pointer. We're first going to store the <var>translations</var> array (from the previous section) in a new buffer object:
    187 </p>
    188 
    189 <pre><code>
    190 unsigned int instanceVBO;
    191 <function id='12'>glGenBuffers</function>(1, &instanceVBO);
    192 <function id='32'>glBindBuffer</function>(GL_ARRAY_BUFFER, instanceVBO);
    193 <function id='31'>glBufferData</function>(GL_ARRAY_BUFFER, sizeof(glm::vec2) * 100, &translations[0], GL_STATIC_DRAW);
    194 <function id='32'>glBindBuffer</function>(GL_ARRAY_BUFFER, 0); 
    195 </code></pre>
    196 
    197 <p>
    198   Then we also need to set its vertex attribute pointer and enable the vertex attribute:
    199 </p>
    200 
    201 <pre><code>
    202 <function id='29'><function id='60'>glEnable</function>VertexAttribArray</function>(2);
    203 <function id='32'>glBindBuffer</function>(GL_ARRAY_BUFFER, instanceVBO);
    204 <function id='30'>glVertexAttribPointer</function>(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
    205 <function id='32'>glBindBuffer</function>(GL_ARRAY_BUFFER, 0);	
    206 <function id='100'>glVertexAttribDivisor</function>(2, 1);  
    207 </code></pre>
    208 
    209 <p>
    210   What makes this code interesting is the last line where we call <fun><function id='100'>glVertexAttribDivisor</function></fun>. This function tells OpenGL <strong>when</strong> to update the content of a vertex attribute to the next element. Its first parameter is the vertex attribute in question and the second parameter the <def>attribute divisor</def>. By default, the attribute divisor is <code>0</code> which tells OpenGL to update the content of the vertex attribute each iteration of the vertex shader. By setting this attribute to <code>1</code> we're telling OpenGL that we want to update the content of the vertex attribute when we start to render a new instance. By setting it to <code>2</code> we'd update the content every 2 instances and so on. By setting the attribute divisor to <code>1</code> we're effectively telling OpenGL that the vertex attribute at attribute location <code>2</code> is an instanced array.
    211 </p>
    212 
    213 <p>
    214   If we now were to render the quads again with <fun><function id='98'><function id='1'>glDrawArrays</function>Instanced</function></fun> we'd get the following output:
    215 </p>
    216 
    217 <img src="/img/advanced/instancing_quads.png" class="clean" alt="Same image of OpenGL instanced quads, but this time using instanced arrays."/>
    218 
    219 <p>
    220   This is exactly the same as the previous example, but now with instanced arrays, which allows us to pass a lot more data (as much as memory allows us) to the vertex shader for instanced drawing.
    221 </p>
    222 
    223 <p>
    224   For fun we could slowly downscale each quad from top-right to bottom-left using <var>gl_InstanceID</var> again, because why not?
    225 </p>
    226 
    227 <pre><code>
    228 void main()
    229 {
    230     vec2 pos = aPos * (gl_InstanceID / 100.0);
    231     gl_Position = vec4(pos + aOffset, 0.0, 1.0);
    232     fColor = aColor;
    233 } 
    234 </code></pre>
    235 
    236 <p>
    237   The result is that the first instances of the quads are drawn extremely small and the further we're in the process of drawing the instances, the closer <var>gl_InstanceID</var> gets to <code>100</code> and thus the more the quads regain their original size. It's perfectly legal to use instanced arrays together with <var>gl_InstanceID</var> like this.
    238 </p>
    239 
    240 <img src="/img/advanced/instancing_quads_arrays.png" class="clean" alt="Image of instanced quads drawn in OpenGL using instanced arrays"/>
    241 
    242 <p>
    243   If you're still a bit unsure about how instanced rendering works or want to see how everything fits together you can find the full source code of the application <a href="/code_viewer_gh.php?code=src/4.advanced_opengl/10.1.instancing_quads/instancing_quads.cpp" target="_blank">here</a>.
    244 </p>
    245 
    246 <p>
    247   While fun and all, these examples aren't really good examples of instancing. Yes, they do give you an easy overview of how instancing works, but instancing gets most of its power when drawing an enormous amount of similar objects. For that reason we're going to venture into space.
    248 </p>
    249 
    250 <h1>An asteroid field</h1>
    251 <p>
    252   Imagine a scene where we have one large planet that's at the center of a large asteroid ring. Such an asteroid ring could contain thousands or tens of thousands of rock formations and quickly becomes un-renderable on any decent graphics card. This scenario proves itself particularly useful for instanced rendering, since all the asteroids can be represented with a single model. Each single asteroid then gets its variation from a transformation matrix unique to each asteroid.
    253 </p>
    254 
    255 <p>
    256   To demonstrate the impact of instanced rendering we're first going to render a scene of asteroids hovering around a planet <em>without</em> instanced rendering. The scene will contain a large planet model that can be downloaded from <a href="/data/models/planet.zip" target="_blank">here</a> and a large set of asteroid rocks that we properly position around the planet. The asteroid rock model can be downloaded <a href="/data/models/rock.zip" target="_blank">here</a>.
    257 </p>
    258 
    259 <p>
    260   Within the code samples we load the models using the model loader we've previously defined in the <a href="https://learnopengl.com/Model-Loading/Assimp" target="_blank">model loading</a> chapters. 
    261 </p>
    262 
    263 <p>
    264   To achieve the effect we're looking for we'll be generating a model transformation matrix for each asteroid. The transformation matrix first translates the rock somewhere in the asteroid ring - then we'll add a small random displacement value to the offset to make the ring look more natural. From there we also apply a random scale and a random rotation. The result is a transformation matrix that translates each asteroid somewhere around the planet while also giving it a more natural and unique look compared to the other asteroids. 
    265 </p>
    266 
    267 <pre><code>
    268 unsigned int amount = 1000;
    269 glm::mat4 *modelMatrices;
    270 modelMatrices = new glm::mat4[amount];
    271 srand(<function id='47'>glfwGetTime</function>()); // initialize random seed	
    272 float radius = 50.0;
    273 float offset = 2.5f;
    274 for(unsigned int i = 0; i &lt; amount; i++)
    275 {
    276     glm::mat4 model = glm::mat4(1.0f);
    277     // 1. translation: displace along circle with 'radius' in range [-offset, offset]
    278     float angle = (float)i / (float)amount * 360.0f;
    279     float displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset;
    280     float x = sin(angle) * radius + displacement;
    281     displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset;
    282     float y = displacement * 0.4f; // keep height of field smaller compared to width of x and z
    283     displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset;
    284     float z = cos(angle) * radius + displacement;
    285     model = <function id='55'>glm::translate</function>(model, glm::vec3(x, y, z));
    286 
    287     // 2. scale: scale between 0.05 and 0.25f
    288     float scale = (rand() % 20) / 100.0f + 0.05;
    289     model = <function id='56'>glm::scale</function>(model, glm::vec3(scale));
    290 
    291     // 3. rotation: add random rotation around a (semi)randomly picked rotation axis vector
    292     float rotAngle = (rand() % 360);
    293     model = <function id='57'>glm::rotate</function>(model, rotAngle, glm::vec3(0.4f, 0.6f, 0.8f));
    294 
    295     // 4. now add to list of matrices
    296     modelMatrices[i] = model;
    297 }  
    298 </code></pre>
    299 
    300 <p>
    301   This piece of code may look a little daunting, but we basically transform the x and z position of the asteroid along a circle with a radius defined by <var>radius</var> and randomly displace each asteroid a little around the circle by <var>-offset</var> and <var>offset</var>. We give the <code>y</code> displacement less of an impact to create a more flat asteroid ring. Then we apply scale and rotation transformations and store the resulting transformation matrix in <var>modelMatrices</var> that is of size <var>amount</var>. Here we generate <code>1000</code> model matrices, one per asteroid.
    302 </p>
    303 
    304 <p>
    305   After loading the planet and rock models and compiling a set of shaders, the rendering code then looks a bit like this:
    306 </p>
    307 
    308 <pre><code>
    309 // draw planet
    310 shader.use();
    311 glm::mat4 model = glm::mat4(1.0f);
    312 model = <function id='55'>glm::translate</function>(model, glm::vec3(0.0f, -3.0f, 0.0f));
    313 model = <function id='56'>glm::scale</function>(model, glm::vec3(4.0f, 4.0f, 4.0f));
    314 shader.setMat4("model", model);
    315 planet.Draw(shader);
    316   
    317 // draw meteorites
    318 for(unsigned int i = 0; i &lt; amount; i++)
    319 {
    320     shader.setMat4("model", modelMatrices[i]);
    321     rock.Draw(shader);
    322 }  
    323 </code></pre>
    324 
    325 <p>
    326   First we draw the planet model, that we translate and scale a bit to accommodate the scene, and then we draw a number of rock models equal to the <var>amount</var> of transformations we generated previously.  Before we draw each rock however, we first set the corresponding model transformation matrix within the shader.
    327 </p>
    328 
    329 <p>
    330   The result is then a space-like scene where we can see a natural-looking asteroid ring around a planet:
    331 </p>
    332 
    333 <img src="/img/advanced/instancing_asteroids.png" class="clean" alt="Image of asteroid field drawn in OpenGL"/>
    334 
    335 <p>
    336   This scene contains a total of <code>1001</code> rendering calls per frame of which <code>1000</code> are of the rock model. You can find the source code for this scene <a href="/code_viewer_gh.php?code=src/4.advanced_opengl/10.2.asteroids/asteroids.cpp" target="_blank">here</a>.
    337 </p>
    338 
    339 <p>
    340   As soon as we start to increase this number we will quickly notice that the scene stops running smoothly and the number of frames we're able to render per second reduces drastically. As soon as we set <var>amount</var> to something close to <code>2000</code> the scene already becomes so slow on our GPU that it becomes difficult to move around. 
    341 </p>
    342 
    343 <p>
    344   Let's now try to render the same scene, but this time with instanced rendering. We first need to adjust the vertex shader a little:
    345 </p>
    346 
    347 <pre><code>
    348 #version 330 core
    349 layout (location = 0) in vec3 aPos;
    350 layout (location = 2) in vec2 aTexCoords;
    351 layout (location = 3) in mat4 instanceMatrix;
    352 
    353 out vec2 TexCoords;
    354 
    355 uniform mat4 projection;
    356 uniform mat4 view;
    357 
    358 void main()
    359 {
    360     gl_Position = projection * view * instanceMatrix * vec4(aPos, 1.0); 
    361     TexCoords = aTexCoords;
    362 }
    363 </code></pre>
    364 
    365 <p>
    366   We're no longer using a model uniform variable, but instead declare a <fun>mat4</fun> as a vertex attribute so we can store an instanced array of transformation matrices. However, when we declare a datatype as a vertex attribute that is greater than a <fun>vec4</fun> things work a bit differently. The maximum amount of data allowed for a vertex attribute is equal to a <fun>vec4</fun>. Because a <fun>mat4</fun> is basically 4 <fun>vec4</fun>s, we have to reserve 4 vertex attributes for this specific matrix. Because we assigned it a location of <code>3</code>, the columns of the matrix will have vertex attribute locations of <code>3</code>, <code>4</code>, <code>5</code>, and <code>6</code>.
    367 </p>
    368 
    369 <p>
    370   We then have to set each of the attribute pointers of those <code>4</code> vertex attributes and configure them as instanced arrays:
    371 </p>
    372 
    373 <pre><code>
    374 // vertex buffer object
    375 unsigned int buffer;
    376 <function id='12'>glGenBuffers</function>(1, &buffer);
    377 <function id='32'>glBindBuffer</function>(GL_ARRAY_BUFFER, buffer);
    378 <function id='31'>glBufferData</function>(GL_ARRAY_BUFFER, amount * sizeof(glm::mat4), &modelMatrices[0], GL_STATIC_DRAW);
    379   
    380 for(unsigned int i = 0; i &lt; rock.meshes.size(); i++)
    381 {
    382     unsigned int VAO = rock.meshes[i].VAO;
    383     <function id='27'>glBindVertexArray</function>(VAO);
    384     // vertex attributes
    385     std::size_t vec4Size = sizeof(glm::vec4);
    386     <function id='29'><function id='60'>glEnable</function>VertexAttribArray</function>(3); 
    387     <function id='30'>glVertexAttribPointer</function>(3, 4, GL_FLOAT, GL_FALSE, 4 * vec4Size, (void*)0);
    388     <function id='29'><function id='60'>glEnable</function>VertexAttribArray</function>(4); 
    389     <function id='30'>glVertexAttribPointer</function>(4, 4, GL_FLOAT, GL_FALSE, 4 * vec4Size, (void*)(1 * vec4Size));
    390     <function id='29'><function id='60'>glEnable</function>VertexAttribArray</function>(5); 
    391     <function id='30'>glVertexAttribPointer</function>(5, 4, GL_FLOAT, GL_FALSE, 4 * vec4Size, (void*)(2 * vec4Size));
    392     <function id='29'><function id='60'>glEnable</function>VertexAttribArray</function>(6); 
    393     <function id='30'>glVertexAttribPointer</function>(6, 4, GL_FLOAT, GL_FALSE, 4 * vec4Size, (void*)(3 * vec4Size));
    394 
    395     <function id='100'>glVertexAttribDivisor</function>(3, 1);
    396     <function id='100'>glVertexAttribDivisor</function>(4, 1);
    397     <function id='100'>glVertexAttribDivisor</function>(5, 1);
    398     <function id='100'>glVertexAttribDivisor</function>(6, 1);
    399 
    400     <function id='27'>glBindVertexArray</function>(0);
    401 }  
    402 </code></pre>
    403 
    404 <p>
    405   Note that we cheated a little by declaring the <var>VAO</var> variable of the <fun>Mesh</fun> as a public variable instead of a private variable so we could access its vertex array object. This is not the cleanest solution, but just a simple modification to suit this example. Aside from the little hack, this code should be clear. We're basically declaring how OpenGL should interpret the buffer for each of the matrix's vertex attributes and that each of those vertex attributes is an instanced array.
    406 </p>
    407 
    408 <p>
    409   Next we take the <var>VAO</var> of the mesh(es) again and this time draw using <fun><function id='99'><function id='2'>glDrawElements</function>Instanced</function></fun>:
    410 </p>
    411 
    412 <pre><code>
    413 // draw meteorites
    414 instanceShader.use();
    415 for(unsigned int i = 0; i &lt; rock.meshes.size(); i++)
    416 {
    417     <function id='27'>glBindVertexArray</function>(rock.meshes[i].VAO);
    418     <function id='99'><function id='2'>glDrawElements</function>Instanced</function>(
    419         GL_TRIANGLES, rock.meshes[i].indices.size(), GL_UNSIGNED_INT, 0, amount
    420     );
    421 }  
    422 </code></pre>
    423 
    424 <p>
    425   Here we draw the same <var>amount</var> of asteroids as the previous example, but this time with instanced rendering. The results should be exactly the same, but once we increase the <var>amount</var> you'll really start to see the power of instanced rendering. Without instanced rendering we were able to smoothly render around <code>1000</code> to <code>1500</code> asteroids. With instanced rendering we can now set this value to <code>100000</code>. This, with the rock model having <code>576</code> vertices, would equal around <code>57</code> million vertices drawn each frame without significant performance drops; and only 2 draw calls!
    426 </p>
    427 
    428 <img src="/img/advanced/instancing_asteroids_quantity.png" class="clean" alt="Image of asteroid field in OpenGL drawn using instanced rendering"/>
    429 
    430 <p>
    431   This image was rendered with <code>100000</code> asteroids with a radius of <code>150.0f</code> and an offset equal to <code>25.0f</code>. You can find the source code of the instanced rendering demo <a href="/code_viewer_gh.php?code=src/4.advanced_opengl/10.3.asteroids_instanced/asteroids_instanced.cpp" target="_blank">here</a>.
    432 </p>
    433 
    434 <note>
    435   On different machines an asteroid count of <code>100000</code> may be a bit too high, so try tweaking the values till you reach an acceptable framerate.
    436 </note>
    437 
    438 <p>
    439   As you can see, with the right type of environments, instanced rendering can make an enormous difference to the rendering capabilities of your application. For this reason, instanced rendering is commonly used for grass, flora, particles, and scenes like this - basically any scene with many repeating shapes can benefit from instanced rendering.
    440 </p>       
    441 
    442     </div>
    443