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

最佳答案

您是否尝试定义:

GLubyte localIndices[glIndLim];


GLushort localIndices[glIndLim];



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

关于objective-c - 在iOS的OpenGL ES中使用GL_UNSIGNED_SHORT时,为什么会得到EXC_BAD_ACCESS?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9242637/

10-11 21:39