2

我编写了以下简单的代码来学习如何使用 vbo,但没有出现任何内容:

GLuint vbo=0;
GLfloat data[] = {175.0, 25.0, 0.0, 1.0, 0.0, 0.0, 100.0, 325.0, 0.0, 0.0,1.0,0.0,
250.0, 25.0, 0.0, 0.0, 0.0, 1.0};

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(data),data, GL_STATIC_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, 0);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_FLOAT, 0, (void*)(3*sizeof(float)) );
glDrawArrays(GL_TRIANGLES, 0,3);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glutSwapBuffers();
glFlush();
}
void reshape(int w, int h){
 glClearColor(0.5,0.2,0.5,1);
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, (GLsizei) w, 0, (GLsizei) h, -1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(350, 350);
glutInitWindowPosition(100, 100);
glutCreateWindow("VBO");
glutDisplayFunc(&display);
glutReshapeFunc(reshape);
glutMainLoop();

}

错误在哪里?

4

1 回答 1

5

您传递的步幅为 0,这意味着数据应该紧密打包(所有顶点在一起,所有颜色在一起等)。但是您的数据数组似乎是交错的。将指针调用更改为:

glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), 0);

glColorPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));

作为旁注,您绝对不应该在每次调用display().

于 2013-10-29T13:08:32.093 回答