5
$\begingroup$

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

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

What might the problem be?

$\endgroup$
5
  • $\begingroup$ 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). $\endgroup$
    – Wumpf
    Oct 27, 2015 at 22:21
  • $\begingroup$ 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? $\endgroup$
    – Darkilon
    Oct 28, 2015 at 11:32
  • $\begingroup$ 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? $\endgroup$
    – Wumpf
    Oct 28, 2015 at 19:43
  • $\begingroup$ 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. $\endgroup$
    – Darkilon
    Oct 29, 2015 at 4:38
  • $\begingroup$ Sorry for double posting, however, strangely enough, this is what happens when I turn on the "wireframe mode". The middle quad gets cut. $\endgroup$
    – Darkilon
    Oct 29, 2015 at 5:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.