3

我无法发现这个 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];
}
4

1 回答 1

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:.
  2. 如果您使用的是更新版本的 osx,您应该考虑使用最新版本的 OpenGL ( OpenGL/gl3.h)。
于 2014-02-18T20:22:30.930 回答