glBindVertexArray不会在display()中切换VAO,但会在init()中切换

时间:2019-01-31 06:41:04

标签: draw opengl-3 vertex-array-object

我正在尝试学习如何对OpenGL进行编程。现在,我正在编写一个绘制多维数据集的程序。我将每个多维数据集存储在单独的VBO中。这是代码。

void
init()
{
enum { Vertices, Colors, Elements, NumVBOs };
GLuint buffers[NumVBOs];
glGenVertexArrays(NumVAOs,VAO);
{
GLfloat vertices[][3] = {
 { -0.5, -0.5, -0.5 },
 { 0.5, -0.5, -0.5 },
 { 0.5, 0.5, -0.5 },
 { -0.5, 0.5, -0.5 },
 { -0.5, -0.5, 0.5 },
 { 0.5, -0.5, 0.5 },
 { 0.5, 0.5, 0.5 },
 { -0.5, 0.5, 0.5 }
};

GLfloat cubeColors[][3] = {
 { 0.0, 0.0, 0.0 },
 { 0.0, 0.0, 1.0 },
 { 0.0, 1.0, 0.0 },
 { 0.0, 1.0, 1.0 },
 { 1.0, 0.0, 0.0 },
 { 1.0, 0.0, 1.0 },
 { 1.0, 1.0, 0.0 },
 { 1.0, 1.0, 1.0 },
 };

GLubyte indices[][4] = {
 { 0, 1, 2, 3 },
 { 4, 7, 6, 5 },
 { 0, 4, 5, 1 },
 { 3, 2, 6, 7 },
 { 0, 3, 7, 4 },
 { 1, 5, 6, 2 }
};
glBindVertexArray(VAO[Cube1]);
glGenBuffers(NumVBOs, buffers);
glBindBuffer(GL_ARRAY_BUFFER, buffers[Vertices]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
 GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, buffers[Colors]);
glBufferData(GL_ARRAY_BUFFER,sizeof(cubeColors),cubeColors,GL_STATIC_DRAW);
glColorPointer(3,GL_FLOAT,0,BUFFER_OFFSET(0));
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[Elements]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,
 GL_STATIC_DRAW);
}
{
GLfloat vertices2[][3] = {
 { -0.5, -0.5, -1.5 },
 { 0.5, -0.5, -1.5 },
 { 0.5, 0.5, -1.5 },
 { -0.5, 0.5, -1.5 },
 { -0.5, -0.5, -2.5 },
 { 0.5, -0.5, -2.5 }, 
 { 0.5, 0.5, -2.5 },
 { -0.5, 0.5, -2.5 }
}; 

GLfloat cubeColors2[][3] = {
 { 0.0, 0.0, 0.0 },
 { 0.0, 0.0, 1.0 },
 { 0.0, 1.0, 0.0 },
 { 0.0, 1.0, 1.0 },
 { 1.0, 0.0, 0.0 },
 { 1.0, 0.0, 1.0 },
 { 1.0, 1.0, 0.0 },
 { 1.0, 1.0, 1.0 },
 };

GLubyte indices2[][4] = {
 { 0, 1, 2, 3 }, 
 { 4, 7, 6, 5 }, 
 { 0, 4, 5, 1 }, 
 { 3, 2, 6, 7 },
 { 0, 3, 7, 4 },
 { 1, 5, 6, 2 }
};
glBindVertexArray(VAO[Cube2]);
glGenBuffers(NumVBOs, buffers);
glBindBuffer(GL_ARRAY_BUFFER, buffers[Vertices]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2,
 GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, buffers[Colors]);
glBufferData(GL_ARRAY_BUFFER,sizeof(cubeColors2),cubeColors2,GL_STATIC_DRAW);
glColorPointer(3,GL_FLOAT,0,BUFFER_OFFSET(0));
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[Elements]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices2), indices2,
GL_STATIC_DRAW);
}

glEnable(GL_DEPTH_TEST);

}

在display()中调用此方法

   glBindVertexArray[VAO[Cube1]];
   glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));

   glBindVertexArray[VAO[Cube2]];
   glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));

该程序仅绘制Cube2。但是,如果我插入

 glBindVertexArray[VAO[Cube1]];

在init()的末尾,它将只绘制Cube1。

现在我很困惑。我希望能够将VAO用于更复杂的应用程序,但目前仍处于困境。

1 个答案:

答案 0 :(得分:0)

glBindVertexArray[VAO[Cube1]]; 本来应该 glBindVertexArray(VAO[Cube1]);