【问题标题】:glBindVertexArray does not switch VAO in display() but does in init()glBindVertexArray 不会在 display() 中切换 VAO,但会在 init() 中切换
【发布时间】:2019-06-24 13:40:33
【问题描述】:

我正在尝试学习如何编写 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 用于更复杂的应用程序,但在这一点上被卡住了。

【问题讨论】:

  • 为什么glBindVertexArray[...],不应该是glBindVertexArray(...)
  • 绘制相同立方体不同位置的常用方法是创建一个网格并通过设置不同的模型矩阵绘制两次,而不是生成具有不同顶点坐标的 2 个网格。
  • 是的,你对括号是正确的。这解决了我的问题。奇怪的是没有编译器错误。我应该在发布之前仔细检查我的代码。
  • glBindVertexArray 是一个函数指针。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多