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

时间:2015-03-07 16:33:58

标签: c++ arrays opengl-es

我在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);

正如我在这里所说的那样是问题所在。我究竟做错了什么?感谢。

1 个答案:

答案 0 :(得分:2)

如果传递GL_TRIANGLES,则索引缓冲区长度应为3的倍数(即每3个索引值形成一个三角形)。

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

相关问题