glDrawElements 绘制所有连接的对象

发布于 2024-09-25 18:56:00 字数 1203 浏览 2 评论 0原文

我不知道如何让 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

应该是两只独立的牛,但它们用黑线连接起来。任何建议将不胜感激!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心未许 2024-10-02 18:56:00

我的猜测是您尝试绘制的元素数量是错误的(太大)。因此,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),
};

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),
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文