我在理解如何使用 VBO 时遇到了一些麻烦。我一直在尝试获得相当于以下内容的图像:
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (70, 4.0f / 3, 1, 1000);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
gluLookAt (0, 0, -5,
0, 0, 0,
0, 1, 0);
glBegin (GL_TRIANGLES);
glVertex3f (-1, -1, -1);
glVertex3f (1, -1, -1);
glVertex3f (1, 1, -1);
glVertex3f (-1, -1, -1);
glVertex3f (1, 1, -1);
glVertex3f (-1, 1, -1);
glEnd ();
这导致一个白色的正方形。根据我从各种教程中收集到的信息,使用 VBO 应该将代码更改为:
float vertexes[][3] = {
{ 1, 1, -1},
{ 1, -1, -1},
{-1, 1, -1},
{-1, -1, -1},
};
unsigned int indexes[] = {
3, 1, 0,
3, 0, 2,
};
GLuint vbo, ibo;
glGenBuffers (1, &vbo);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glEnableClientState (GL_VERTEX_ARRAY);
glVertexPointer (3, GL_FLOAT, 0, 0);
glGenBuffers (1, &ibo);
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, ibo);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (70, 4.0f / 3, 1, 1000);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
gluLookAt (0, 0, -5,
0, 0, 0,
0, 1, 0);
glBufferData (GL_ARRAY_BUFFER, sizeof vertexes, vertexes, GL_STATIC_DRAW);
glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof indexes, indexes, GL_STATIC_DRAW);
glDrawElements (GL_TRIANGLES, 2, GL_UNSIGNED_INT, 0);
但是,这不会渲染任何东西。