PDA

View Full Version : How do I draw separate triangles (not attached) in one draw call?



Spankenstein
09-04-2012, 03:14 AM
If I instantiate and position two separate quadrangles



Quadrangle q0;
q0.setPosition(glm::vec3(0, 0, 0));

Quadrangle q1;
q1.setPosition(glm::vec3(1, 1, 1));


I can draw them separately in separate draw calls with no problems.

I encounter problems if I try and draw them in the same draw call



glDrawElements(GL_TRIANGLES, m_numQuadrangles * 6, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));


A connecting triangle between the first and second quadrangle is drawn and the second quadrangle triangles are ignored completely.

http://img268.imageshack.us/img268/3346/separatequadproblem.png

My indices for each quadrangle are correct



int numIndices = 0;

for (int i = 0; i < maxQuadrangles; i++)
{
// CW Winding [0, 1, 2] [1, 3, 2]
m_indices[numIndices++] = 0 + i;
m_indices[numIndices++] = 1 + i;
m_indices[numIndices++] = 2 + i;

m_indices[numIndices++] = 1 + i;
m_indices[numIndices++] = 3 + i;
m_indices[numIndices++] = 2 + i;
}


m_numQuadrangles * 6 is the correct index count depending on the number of quadrangles.

The vertices are set correctly



std::array<VertexPositionTexture, 4> v = quadrangle.getVertices();
std::copy(v.begin(), v.end(), m_vertices.begin() + (m_numQuadrangles * 4));

...

glBindBuffer(GL_ARRAY_BUFFER, vbuffer);
glBufferData(GL_ARRAY_BUFFER, maxVertices * sizeof(VertexPositionTexture), &m_vertices[0], GL_DYNAMIC_DRAW);


How can I make sure the triangles making up each quadrangle are completely separate but only using one draw call?

Dan Bartlett
09-04-2012, 04:32 AM
You probably want something like:


m_indices[numIndices++] = 0 + i * 6;
m_indices[numIndices++] = 1 + i * 6;
m_indices[numIndices++] = 2 + i * 6;

m_indices[numIndices++] = 1 + i * 6;
m_indices[numIndices++] = 3 + i * 6;
m_indices[numIndices++] = 2 + i * 6;

Spankenstein
09-04-2012, 04:48 AM
Thanks Dan. I've solved it. You highlighted the problem area but the solution is:



for (int i = 0; i < maxQuadrangles; i += 4)
{
// CW Winding [0, 1, 2] [1, 3, 2]
m_indices[numIndices++] = 0 + i;
m_indices[numIndices++] = 1 + i;
m_indices[numIndices++] = 2 + i;

m_indices[numIndices++] = 1 + i;
m_indices[numIndices++] = 3 + i;
m_indices[numIndices++] = 2 + i;
}