我一直试图建立只有glBegin(GLenum模式)的VBO insteath ... 当我尝试运行它时,我没有遇到编译器错误或崩溃,但我也没有看到对象...... 我试图画一个立方体。所以我初始化它并在调试器中检查我的顶点的值。他们都很好。然后我使用此代码分配它们。 这是我的渲染代码。我看到绿色和红色的四边形,但不是立方体对象。
void Window::render() {
//Render scene
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//glRotatef(rotY, 1, 0, 0);
//glRotatef(rotX, 0, 1, 0);
glBegin(GL_QUADS);
glColor3ub(255, 0, 0);
glVertex3f(0, 0, 0);
glVertex3f(10, 0, 0);
glVertex3f(10, 10, 0);
glVertex3f(0, 10, 0);
glEnd();
cube1->render();
//Render interface
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
glOrtho(0, windowResolution->w, windowResolution->h, 0, 1, -1);
glBegin(GL_QUADS);
glColor3ub(0, 255, 0);
glVertex3f(0, 0, 0);
glVertex3f(10, 0, 0);
glVertex3f(10, 10, 0);
glVertex3f(0, 10, 0);
glEnd();
}
void Cube::render() {
updateVBO();
//Set vertex pointer
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexPointer(3, GL_FLOAT, 0, 0);
//Set color pointer
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glColorPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_QUADS, 0, 4);
//Clear buffers
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, NULL);
}
void Cube::updateVBO() {
//Vertices:
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) *vertices.size(), &vertices.front(), GL_STATIC_DRAW);
//Colors
glGenBuffers(1, &colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) *colors.size(), &colors.front(), GL_STATIC_DRAW);
}