我需要做的是绘制一个包含超过 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!!!!