为什么在适用于 iOS 的 OpenGL ES 中使用 GL_UNSIGNED_SHORT 时会收到 EXC_BAD_ACCESS?
我需要做的是绘制一个包含超过 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过定义:
as
?
原因是,如果它代表顶点的索引,它应该接受 GLushort 的所有可能值,否则本地索引将始终是 GLubyte。
Have you tried defining:
as
?
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.
每次通过循环记录您的索引值。听起来您正在将变量增加到超出其最大值,并且它正在变成负数。
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.