tags:

views:

176

answers:

1

I can't figure out how to get glDrawElements to not connect everything it draws...

 //Draw Reds
 glEnableVertexAttribArray(vLoc);
 glEnableVertexAttribArray(cLoc);

 glBindBuffer(GL_ARRAY_BUFFER,positionBufferRed);
 glVertexAttribPointer(vLoc,3,GL_FLOAT,GL_FALSE,0,0);

 glBindBuffer(GL_ARRAY_BUFFER,redBuffer);
 glVertexAttribPointer(cLoc,3,GL_FLOAT,GL_FALSE,0,0);

 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,elementBufferRed);
 glDrawElements(GL_TRIANGLES,nElements*3,GL_UNSIGNED_INT,0);

 glDisableVertexAttribArray(vLoc);
 glDisableVertexAttribArray(cLoc);


 //Draw Blues
 glEnableVertexAttribArray(vLoc);
 glEnableVertexAttribArray(cLoc);

 glBindBuffer(GL_ARRAY_BUFFER,positionBufferBlue);
 glVertexAttribPointer(vLoc,3,GL_FLOAT,GL_FALSE,0,0);

 glBindBuffer(GL_ARRAY_BUFFER,blueBuffer);
 glVertexAttribPointer(cLoc,3,GL_FLOAT,GL_FALSE,0,0);

 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,elementBufferBlue);
 glDrawElements(GL_TRIANGLES,nElements*3,GL_UNSIGNED_INT,0);

 glDisableVertexAttribArray(vLoc);
 glDisableVertexAttribArray(cLoc);

This is what the result looks like: http://img338.imageshack.us/img338/2440/cows.png

Should be two separate cows but instead they're connected with black lines. Any advice will be appreciated!

A: 

My guess is that the number of elements you're trying to draw is wrong (too big). So the GPU tries to get triangles that don't exist in the buffer, and accidentally access the vertices of the next mesh, but not the color (black).

Try with glDrawElements(GL_TRIANGLES,nElements,GL_UNSIGNED_INT,0);

If it doesn't work, try with a handcoded single triangle.

Here's an example :

GLsizei const TonemapperElementCount = 3;
GLsizeiptr const TonemapperElementSize = TonemapperElementCount * sizeof(glm::uint32);
glm::uint32 const TonemapperElementData[TonemapperElementCount] =
{
    0, 1, 2,
};

GLsizei const TonemapperVertexCount = 3;
GLsizeiptr const TonemapperPositionSize = TonemapperVertexCount * sizeof(glm::vec4);
glm::vec4 const TonemapperPositionData[TonemapperVertexCount] =
{ // A full-screen triangle in normalized screen space.
    glm::vec4( -1.0f, -1.0f,0,1),
    glm::vec4( 3.0f, -1.0f ,0,1),
    glm::vec4( -1.0f, 3.0f ,0,1),
};
Calvin1602