C++ glDrawElements 数组作为参数 EXC_BAD_ACCESS 错误

C++ glDrawElements Array as Parameter EXC_BAD_ACCESS Error

我在调用 glDrawElements 时遇到 EXC_BAD_ACCESS 错误。我认为传递给 Shape 构造函数的数组有问题。这可能是什么问题。

main.cpp

static const GLfloat cube_vertices[] = {
   -1.0, -1.0,  1.0,
   1.0, -1.0,  1.0,
   -1.0,  1.0,  1.0,
   1.0,  1.0,  1.0,
   -1.0, -1.0, -1.0,
   1.0, -1.0, -1.0,
   -1.0,  1.0, -1.0,
   1.0,  1.0, -1.0,
};

static const GLushort indices[] = {
    0, 1, 2, 3, 7, 1, 5, 4, 7, 6, 2, 4, 0, 1
};

cube = new Shape(shader, cube_vertices, 3 * 8, indices, 14);

Shape.h

const GLushort *indices;

Shape.cpp

Shape::Shape(Shader* cshader, const GLfloat *vertices, int size, const GLushort *cindices, int indSize) :  {
indices = cindices;
}

渲染方法

glDrawElements(GL_TRIANGLES, 14, GL_UNSIGNED_SHORT, indices);

正如我所说,这就是问题所在。我究竟做错了什么?谢谢。

如果你传递 GL_TRIANGLES 那么你的索引缓冲区长度应该是 3 的倍数(即每 3 个索引值形成一个三角形)。

除此之外,您发布的内容看起来还不错 - 所以我们需要一个更完整的测试用例才能提供帮助。