glDrawElements绘制所有连接的对象

时间:2010-10-02 07:17:52

标签: opengl 3d

我无法弄清楚如何让glDrawElements不能连接它绘制的所有东西......

 //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);

结果如下: http://img338.imageshack.us/img338/2440/cows.png

应该是两头独立的奶牛,而是用黑线连接。任何建议将不胜感激!

1 个答案:

答案 0 :(得分:0)

我的猜测是你想要绘制的元素数量是错误的(太大)。因此,GPU尝试获取缓冲区中不存在的三角形,并意外访问下一个网格的顶点,但不是颜色(黑色)。

尝试使用glDrawElements(GL_TRIANGLES,nElements,GL_UNSIGNED_INT,0);

如果不起作用,请尝试使用手动编码的单个三角形。

以下是一个例子:

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),
};