<p>Previously, to render a bunch of quads, I was simply using a few uniforms (one for a model matrix and another for the texture layer ID). However, I'd rather not have to loop through each quad and set both uniforms each time, each frame.</p>

<p>So I went ahead and looked for better alternatives with which I could render everything with a single call. Now I'm using instanced rendering:</p>

<pre><code>// In VAO definition            
glGenBuffers(1, &amp;instanceVBO);
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(RenderingData), nullptr);
glVertexAttribDivisor(3, 1);
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(RenderingData), (GLvoid*)(sizeof(glm::vec4)));
glVertexAttribDivisor(4, 1);
glEnableVertexAttribArray(5);
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(RenderingData), (GLvoid*)(sizeof(glm::vec4) * 2));
glVertexAttribDivisor(5, 1);
glEnableVertexAttribArray(6);
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(RenderingData), (GLvoid*)(sizeof(glm::vec4) * 3));
glVertexAttribDivisor(6, 1);
glEnableVertexAttribArray(7);
glVertexAttribIPointer(7, 1, GL_UNSIGNED_INT, sizeof(RenderingData), (GLvoid*)(sizeof(glm::mat4)));
glVertexAttribDivisor(7, 1);

// In quad adding function
instanceData.push_back(quad-&gt;data);
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(RenderingData) * quads.size(), &amp;instanceData[0], GL_DYNAMIC_DRAW);

// Rendering
glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0, quads.size());
</code></pre>

<p>I'm using 2 VBOs: one for per-vertex data (position, normal, etc vectors) and another for per-instance data (model matrix and texture layer ID).</p>

<p>My problem is the following:</p>

<p><a href="https://i.sstatic.net/9UAOQ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/9UAOQ.png" alt="middle one is way too stretched, whereas bottom one&#39;s UV are incorrect, since they aren&#39;t filling the whole quad"></a></p>

<p>As you can see, the middle and bottom images aren't rendering correctly (middle one is way too stretched, whereas bottom one's UV are incorrect, since they aren't filling the whole quad), however, I'm 98% sure both my vertex and instance data are correct, since when I was using the previously mentioned uniforms, they were rendering correctly.</p>

<p>Also, I <strong><em>might</em></strong> 've spotted the problem: the indices. Somehow, if I change my <code>glDrawElementsInstanced</code>'s count value to say, 12 indices, the middle one renders correctly, whereas the other 2, do not (see picture below). Same thing happens if I change them to 18 (the last one renders correctly and the other 2, do not).</p>

<p><a href="https://i.sstatic.net/2QSBs.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/2QSBs.png" alt="middle one renders correctly, whereas the other 2, do not"></a></p>

<p>What might the problem be?</p>

CC BY-SA 3.0
2015-10-27T22:21:27.677Z
In your code sample the instance buffer is empty, is this intended? Also there is no vertex buffer binding (either glBindBufferRange​ or the newer glBindVertexBuffer).
Wumpf
CC BY-SA 3.0
2015-10-28T11:32:11.837Z
I'm filling the data into the second paragraph, also, why would I need to call glBindBufferRange?

Isn't that just needed for UBOs / SSBOs?
Darkilon
CC BY-SA 3.0
2015-10-28T19:43:50.567Z
Oh sorry, overlooked the buffer filling. And I also remembered the glBindBufferRange wrong since I was by now too much used to the newer glBindVertexBuffer semantics. But there should be a glBindBuffer for the per-vertex data somewhere between your VertexAttrib calls, right?
Wumpf
CC BY-SA 3.0
2015-10-29T04:38:49.910Z
Yes. As you can see, my per-instance's data's vertex attrib locations start at 3, that's because, previously, from 0 to 2, I've defined my per-vertex data. I just posted the instancing related parts of the code.
Darkilon
CC BY-SA 3.0
2015-10-29T05:01:15.347Z
Sorry for double posting, however, strangely enough, [this](http://imgur.com/KgVN4L1) is what happens when I turn on the "wireframe mode". The middle quad gets cut.
Darkilon
<opengl><rendering><c++>
Incorrect instanced rendering
Darkilon
# Incorrect instanced rendering

(Asked by: username_0 on 2015-10-27)

Previously, to render a bunch of quads, I was simply using a few uniforms (one for a model matrix and another for the texture layer ID). However, I'd rather not have to loop through each quad and set both uniforms each time, each frame.

So I went ahead and looked for better alternatives with which I could render everything with a single call. Now I'm using instanced rendering:

```
// In VAO definition            
glGenBuffers(1, &instanceVBO);
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(RenderingData), nullptr);
glVertexAttribDivisor(3, 1);
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(RenderingData), (GLvoid*)(sizeof(glm::vec4)));
glVertexAttribDivisor(4, 1);
glEnableVertexAttribArray(5);
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(RenderingData), (GLvoid*)(sizeof(glm::vec4) * 2));
glVertexAttribDivisor(5, 1);
glEnableVertexAttribArray(6);
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(RenderingData), (GLvoid*)(sizeof(glm::vec4) * 3));
glVertexAttribDivisor(6, 1);
glEnableVertexAttribArray(7);
glVertexAttribIPointer(7, 1, GL_UNSIGNED_INT, sizeof(RenderingData), (GLvoid*)(sizeof(glm::mat4)));
glVertexAttribDivisor(7, 1);

// In quad adding function
instanceData.push_back(quad->data);
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(RenderingData) * quads.size(), &instanceData[0], GL_DYNAMIC_DRAW);

// Rendering
glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0, quads.size());

```

I'm using 2 VBOs: one for per-vertex data (position, normal, etc vectors) and another for per-instance data (model matrix and texture layer ID).

My problem is the following:

[![middle one is way too stretched, whereas bottom one's UV are incorrect, since they aren't filling the whole quad](https://i.sstatic.net/9UAOQ.png)](https://i.sstatic.net/9UAOQ.png)

As you can see, the middle and bottom images aren't rendering correctly (middle one is way too stretched, whereas bottom one's UV are incorrect, since they aren't filling the whole quad), however, I'm 98% sure both my vertex and instance data are correct, since when I was using the previously mentioned uniforms, they were rendering correctly.

Also, I ***might*** 've spotted the problem: the indices. Somehow, if I change my `glDrawElementsInstanced`'s count value to say, 12 indices, the middle one renders correctly, whereas the other 2, do not (see picture below). Same thing happens if I change them to 18 (the last one renders correctly and the other 2, do not).

[![middle one renders correctly, whereas the other 2, do not](https://i.sstatic.net/2QSBs.png)](https://i.sstatic.net/2QSBs.png)

What might the problem be?

**Tags:** opengl, rendering, c++

> **Comments:**
> **username_1**: In your code sample the instance buffer is empty, is this intended? Also there is no vertex buffer binding (either glBindBufferRange​ or the newer glBindVertexBuffer).  
> **username_0**: I'm filling the data into the second paragraph, also, why would I need to call glBindBufferRange?  Isn't that just needed for UBOs / SSBOs?  
> **username_1**: Oh sorry, overlooked the buffer filling. And I also remembered the glBindBufferRange wrong since I was by now too much used to the newer glBindVertexBuffer semantics. But there should be a glBindBuffer for the per-vertex data somewhere between your VertexAttrib calls, right?  
> **username_0**: Yes. As you can see, my per-instance's data's vertex attrib locations start at 3, that's because, previously, from 0 to 2, I've defined my per-vertex data. I just posted the instancing related parts of the code.  
> **username_0**: Sorry for double posting, however, strangely enough, [this](http://imgur.com/KgVN4L1) is what happens when I turn on the "wireframe mode". The middle quad gets cut.  

---

## 0 answers

