用OpenGL和Vertex Array Objects搞错了

时间:2012-12-20 02:22:43

标签: objective-c opengl vertex-array

我无法通过此OpenGL实现发现错误。当我运行程序时,我唯一得到的是黑屏。我应该看到一个立方体。我没有遇到任何错误。我怀疑罪魁祸首可能与VAO有关:

// draws the view
-(void)drawRect:(NSRect)dirtyRect
{
// clear the viewport
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

shaderProgramID = [self loadShaders];

// get uniform locations
uniformLocations[0] = glGetUniformLocation(shaderProgramID, "rotationX");
uniformLocations[1] = glGetUniformLocation(shaderProgramID, "rotationY");
uniformLocations[2] = glGetUniformLocation(shaderProgramID, "rotationZ");
uniformLocations[3] = glGetUniformLocation(shaderProgramID, "translation");
uniformLocations[4] = glGetUniformLocation(shaderProgramID, "projection");

// use this program
glUseProgram(shaderProgramID);

// create VAO
glGenVertexArraysAPPLE(1, &vao);
glBindVertexArrayAPPLE(vao);
glGenBuffers(3, vbo);

// bind and copy data for vertex position data
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubePositionData), cubePositionData, GL_STATIC_DRAW);
glVertexAttribPointer(TCV_VERTEX_POS_INDEX, TCV_NUM_POS_COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(TCV_VERTEX_POS_INDEX);

// bind and copy data for vertex color data
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeColorData), cubeColorData, GL_STATIC_DRAW);
glVertexAttribPointer(TCV_VERTEX_COLOR_INDEX, TCV_NUM_COLOR_COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(TCV_VERTEX_COLOR_INDEX);

// bind and copy data for vertex indices data
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[2]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cubeIndices), cubeIndices, GL_STATIC_DRAW);
glVertexAttribPointer(TCV_VERTEX_INDICES_INDEX, TCV_NUM_INDEX_ARRAY_COMPONENTS, GL_UNSIGNED_BYTE, GL_FALSE, 0, 0);
glEnableVertexAttribArray(TCV_VERTEX_INDICES_INDEX);

// load uniform data into shader program
glUniformMatrix4fv(uniformLocations[0], 1, GL_FALSE, rotationMatX);
glUniformMatrix4fv(uniformLocations[1], 1, GL_FALSE, rotationMatY);
glUniformMatrix4fv(uniformLocations[2], 1, GL_FALSE, rotationMatZ);
glUniformMatrix4fv(uniformLocations[3], 1, GL_FALSE, translationMat);
glUniformMatrix4fv(uniformLocations[4], 1, GL_FALSE, projectionMat);

// tell OGL to draw the cube, in this order
glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_BYTE, cubeIndices);

// unbind VAO
glBindVertexArrayAPPLE(0);

// no longer using program
glUseProgram(0);

// flush buffer
glFlush();
[[self openGLContext] flushBuffer];
}

1 个答案:

答案 0 :(得分:0)

我的OpenGL实验遇到了类似的问题。我还使用单独的缓冲区对象作为顶点位置和顶点颜色数据。我相信只有一个GL_ARRAY_BUFFER可以绑定到顶点数组对象。因此,将所有数据放入一个组合缓冲区,然后将具有偏移量的属性指针设置到该缓冲区中。

你的VBO / VAO设置应该是这样的:

glGenVertexArrays(1, &vertexArray);
glBindVertexArray(vertexArray);

glGenBuffers(1,&vertexBuffer);
glBindBuffer(vertexBuffer);
glBindBufferData(GL_ARRAY_BUFFER, sizeof(cubePositionAndColorData), cubePositionAndColorData, GL_STATIC_DRAW);

glVertexAttribPointer(kPositionAttribute, NUM_VERTEX_COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0);
// colorStartingIndex is probably just NUM_VERTEX_COMPONENTS*sizeof(GLfloat)
glVertexAttribPointer(kColorAttribute, NUM_COLOR_COMPONENTS, GL_FLOAT, GL_FALSE, 0, (void *)colorStartingIndex);

glEnableVertexAttribArray(kPositionAttribute);
glEnableVertexAttribArray(kColorAttribute);

glGenBuffers(1,&indexBuffer);
glBindBuffer(indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cubeIndices), cubeIndices, GL_STATIC_DRAW);

glBindVertexArray(0);

然后,在-drawRect:功能中,您可以使用该程序,设置制服并致电glDrawElements()

glUseProgram(shaderProgramID);

// do the necessary uniform configuration
glUniformMatrix4fv(...)

glBindVertexArray(vertexArray);
glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_BYTE, 0);

glBindVertexArray(0);

glUseProgram(0);

另外两件值得注意的事项:

  1. 我建议将所有缓冲区初始化移出-drawRect:方法,因为每次重新绘制视图时都会调用它。这意味着您将在每个-drawRect:上将所有数据重新复制到OpenGL服务器。
  2. 如果您使用的是更新版本的osx,则应考虑使用最新版本的OpenGL(OpenGL/gl3.h)。