我正在尝试将一些 OpenGL 3.2 代码从 Windows 移植到 OS/X 10.8(使用 GLFW 核心配置文件),但是当我调用 glDrawElements 时得到一个 INVALID_OPERATION (glError())。glDrawArrays 函数工作正常,所以我的着色器初始化好了。
以下片段很好地解释了我在做什么。知道我做错了什么吗?
struct Vertex {
vec2 position;
vec3 color;
};
void display() {
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
mat4 projection = Ortho2D(-15.0f, 15.0f, -15.0f, 15.0f);
glUniformMatrix4fv(projectionUniform, 1, GL_TRUE, projection);
glBindVertexArray(shapeVertexArrayBuffer);
mat4 modelView;
// upper left
modelView = Translate(-7,+7,0);
glUniformMatrix4fv(modelViewUniform, 1, GL_TRUE, modelView);
glDrawArrays(GL_TRIANGLE_FAN, 0, rectangleSize); // renders correctly
// upper right
modelView = Translate(+7,+7,0);
glUniformMatrix4fv(modelViewUniform, 1, GL_TRUE, modelView);
GLuint indices[6] = {0,1,2,3,4,0};
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, indices); //INVALID_OPERATION
glfwSwapBuffers();
}
void loadGeometry() {
vec3 color(1.0f, 1.0f, 0.0f);
Vertex rectangleData[rectangleSize] = {
{ vec2( 0.0, 0.0 ), color },
{ vec2( 5.0, -5.0 ), color },
{ vec2( 5.0, 0.0 ), color },
{ vec2( 0.0, 5.0 ), color },
{ vec2(-5.0, 0.0 ), color },
{ vec2(-5.0, -5.0 ), color }
};
shapeVertexArrayBuffer = loadBufferData(rectangleData, rectangleSize);
}
GLuint loadBufferData(Vertex* vertices, int vertexCount) {
GLuint vertexArrayObject;
glGenVertexArrays(1, &vertexArrayObject);
glBindVertexArray(vertexArrayObject);
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertexCount * sizeof(Vertex), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(positionAttribute);
glEnableVertexAttribArray(colorAttribute);
glVertexAttribPointer(positionAttribute, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *)0);
glVertexAttribPointer(colorAttribute , 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *)sizeof(vec2));
return vertexArrayObject;
}