为什么在适用于 iOS 的 OpenGL ES 中使用 GL_UNSIGNED_SHORT 时会收到 EXC_BAD_ACCESS?

发布于 2025-01-04 06:30:06 字数 1631 浏览 5 评论 0原文

我需要做的是绘制一个包含超过 256 个元素的顶点数组。当我的数量少于那么多,并且我在调用 glDrawElements 时使用 GL_UNSIGNED_BYTE 时,一切正常。当我有超过 256 个元素时,它再次开始在第一个顶点处绘制(即,最后一个元素 [256 - 255,无论什么] 与第一个 [1 或 0] 连接,并且不会绘制更多元素)。如果我使用 GL_UNSIGNED_SHORT 代替,我会得到 EXC_BAD_ACCESS。什么给?

int indexLim = self.animIndex;

GLushort glIndLim = (GLushort)indexLim;

Vertex localVertices[glIndLim];
GLubyte localIndices[glIndLim];

for(GLushort i=0; i < glIndLim; i++)
{
    x = (float)i;
    y = [[data objectAtIndex:i ] floatValue];

    x = x*xScale + xOffset;
    y = y*yScale + yOffset;

    localVertices[i].Position[0] = x;
    localVertices[i].Position[1] = y;
    localVertices[i].Position[2] = z;
    localVertices[i].Color[0]    = r;
    localVertices[i].Color[1]    = g;
    localVertices[i].Color[2]    = b;
    localVertices[i].Color[3]    = a;
    localIndices[i] = i;
}    

// setupVBOs
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(localVertices), localVertices, GL_STATIC_DRAW);

GLuint indexBuffer;
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(localIndices), localIndices, GL_STATIC_DRAW);

glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));


//glDrawElements(GL_LINE_STRIP, glIndLim, GL_UNSIGNED_BYTE, 0); // Works, but only draws 256 elements
glDrawElements(GL_LINE_STRIP, glIndLim, GL_UNSIGNED_SHORT, 0); // EXC_BAD_ACCESS!!!!

What I need to do is draw a vertex array that has more than 256 elements. When I have less than that many, and I use GL_UNSIGNED_BYTE in my call to glDrawElements, everything works fine. When I have more than 256 elements, it starts drawing back at the first vertex again (i.e., last element [256 - 255, whatever] connects with first [1, or 0], and further elements don't get drawn). If I use GL_UNSIGNED_SHORT instead, I get EXC_BAD_ACCESS. What gives?

int indexLim = self.animIndex;

GLushort glIndLim = (GLushort)indexLim;

Vertex localVertices[glIndLim];
GLubyte localIndices[glIndLim];

for(GLushort i=0; i < glIndLim; i++)
{
    x = (float)i;
    y = [[data objectAtIndex:i ] floatValue];

    x = x*xScale + xOffset;
    y = y*yScale + yOffset;

    localVertices[i].Position[0] = x;
    localVertices[i].Position[1] = y;
    localVertices[i].Position[2] = z;
    localVertices[i].Color[0]    = r;
    localVertices[i].Color[1]    = g;
    localVertices[i].Color[2]    = b;
    localVertices[i].Color[3]    = a;
    localIndices[i] = i;
}    

// setupVBOs
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(localVertices), localVertices, GL_STATIC_DRAW);

GLuint indexBuffer;
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(localIndices), localIndices, GL_STATIC_DRAW);

glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));


//glDrawElements(GL_LINE_STRIP, glIndLim, GL_UNSIGNED_BYTE, 0); // Works, but only draws 256 elements
glDrawElements(GL_LINE_STRIP, glIndLim, GL_UNSIGNED_SHORT, 0); // EXC_BAD_ACCESS!!!!

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

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

发布评论

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

评论(2

旧伤还要旧人安 2025-01-11 06:30:06

您是否尝试过定义:

GLubyte localIndices[glIndLim];

as

GLushort localIndices[glIndLim];

原因是,如果它代表顶点的索引,它应该接受 GLushort 的所有可能值,否则本地索引将始终是 GLubyte。

Have you tried defining:

GLubyte localIndices[glIndLim];

as

GLushort localIndices[glIndLim];

?

The reasoning is that if that should represent the index for your vertex, it should admit all possible values for GLushort, otherwise the local index will always be a GLubyte.

救赎№ 2025-01-11 06:30:06

每次通过循环记录您的索引值。听起来您正在将变量增加到超出其最大值,并且它正在变成负数。

Log your index value each time through the loop. It sounds like you're incrementing a variable beyond its maximum value and it is becoming a negative number.

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