我试图使用OpenGL 4.1绘制两个不同的网格。在任何给定的时间,只会绘制一个网格,我的目标是在它们之间切换。我在一个对象中存储网格的顶点和索引数据,下面称为g_obj。因此,第一个网格的数据由g_obj-> vertices_和g_obj->索引得到,第二个网格的数据来自g_obj-> vertices_linear_和g_obj-> indices_linear _。
我可怕的想法是为每个网格设置不同的VBO和VAO。然后在绘制调用中,我将简单地绑定适当的VAO,然后执行glDraw *。但是,我的代码在第一次绘制调用时会出现段错误(见下文)。
enum VAO_ID
{
VAO,
VAO_LINEAR,
NUM_VAOS
};
enum BUFFER_ID
{
VERTEX_BUFFER,
VERTEX_BUFFER_LINEAR,
INDEX_BUFFER,
INDEX_BUFFER_LINEAR,
NUM_BUFFERS
};
enum ATTRIBUTE_ID
{
VERTEX_POSITION,
VERTEX_COLOR
};
GLuint g_vaos[NUM_VAOS];
GLuint g_buffers[NUM_BUFFERS];
再向下,我创建了顶点/索引缓冲区和顶点数组对象:
void bufferGeometry()
{
// create vertex buffers and vertex array object
glGenVertexArrays(NUM_VAOS, g_vaos);
glGenBuffers(NUM_BUFFERS, g_buffers);
// rotational grating
glBindBuffer(GL_ARRAY_BUFFER, g_buffers[VERTEX_BUFFER]);
glBufferData(GL_ARRAY_BUFFER,
g_obj->num_vertices_ * sizeof(vertex2D),
g_obj->vertices_,
GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_buffers[INDEX_BUFFER]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
g_obj->num_indices_ * sizeof(GLushort),
g_obj->indices_,
GL_STATIC_DRAW);
glBindVertexArray(g_vaos[VAO]);
glEnableVertexAttribArray(VERTEX_POSITION);
glVertexAttribPointer(VERTEX_POSITION, 2, GL_FLOAT, GL_FALSE,
sizeof(vertex2D),
(const GLvoid *)offsetof(vertex2D, position));
glEnableVertexAttribArray(VERTEX_COLOR);
glVertexAttribPointer(VERTEX_COLOR, 4, GL_UNSIGNED_BYTE, GL_FALSE,
sizeof(vertex2D),
(const GLvoid *)offsetof(vertex2D, color));
glBindVertexArray(0);
// linear grating
glBindBuffer(GL_ARRAY_BUFFER, g_buffers[VERTEX_BUFFER_LINEAR]);
glBufferData(GL_ARRAY_BUFFER,
g_obj->num_vertices_linear_ * sizeof(vertex2D),
g_obj->vertices_linear_,
GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_buffers[INDEX_BUFFER_LINEAR]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
g_obj->num_indices_linear_ * sizeof(GLushort),
g_obj->indices_linear_,
GL_STATIC_DRAW);
glBindVertexArray(g_vaos[VAO_LINEAR]);
glEnableVertexAttribArray(VERTEX_POSITION);
glVertexAttribPointer(VERTEX_POSITION, 2, GL_FLOAT, GL_FALSE,
sizeof(vertex2D),
(const GLvoid *)offsetof(vertex2D, position));
glEnableVertexAttribArray(VERTEX_COLOR);
glVertexAttribPointer(VERTEX_COLOR, 4, GL_UNSIGNED_BYTE, GL_FALSE,
sizeof(vertex2D),
(const GLvoid *)offsetof(vertex2D, color));
glBindVertexArray(0);
}
然后,这是绘图调用:
glBindVertexArray(g_vaos[VAO]);
g_obj = &g_grating_uno;
glUseProgram(g_program);
glUniform3fv(g_trans_scale_location, 1, g_obj->trans_scale_);
glDrawElements(GL_TRIANGLES, g_obj->num_indices_,
GL_UNSIGNED_SHORT, (const GLvoid *)0);
glDrawElements行中的哪些段错误。
我知道一般的“高性能”策略是在单个VBO中打包来自两个网格的数据,并且因为两个网格之间的数据格式相同,所以我只需要一个VAO。但是,由于我很懒,我想知道是否有人能够理解为什么我现有的策略不起作用或不起作用,或者你认为问题出在其他地方。我的猜测是它与我的bufferGeometry()函数有关。我对VAO如何与特定VBO相关联的理解有点弱,如果它们完全相同的话。谢谢,抱歉愚蠢!
答案 0 :(得分:0)
啊,通过在绑定VBO之前绑定VAO来修复它(我认为):
void bufferGeometry()
{
// create vertex buffers and vertex array object
glGenVertexArrays(NUM_VAOS, g_vaos);
glGenBuffers(NUM_BUFFERS, g_buffers);
// rotational grating
glBindVertexArray(g_vaos[VAO]);
glBindBuffer(GL_ARRAY_BUFFER, g_buffers[VERTEX_BUFFER]);
glBufferData(GL_ARRAY_BUFFER,
g_obj->num_vertices_ * sizeof(vertex2D),
g_obj->vertices_,
GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_buffers[INDEX_BUFFER]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
g_obj->num_indices_ * sizeof(GLushort),
g_obj->indices_,
GL_STATIC_DRAW);
glEnableVertexAttribArray(VERTEX_POSITION);
glVertexAttribPointer(VERTEX_POSITION, 2, GL_FLOAT, GL_FALSE,
sizeof(vertex2D),
(const GLvoid *)offsetof(vertex2D, position));
glEnableVertexAttribArray(VERTEX_COLOR);
glVertexAttribPointer(VERTEX_COLOR, 4, GL_UNSIGNED_BYTE, GL_FALSE,
sizeof(vertex2D),
(const GLvoid *)offsetof(vertex2D, color));
glBindVertexArray(0);
// linear grating
glBindVertexArray(g_vaos[VAO_LINEAR]);
glBindBuffer(GL_ARRAY_BUFFER, g_buffers[VERTEX_BUFFER_LINEAR]);
glBufferData(GL_ARRAY_BUFFER,
g_obj->num_vertices_linear_ * sizeof(vertex2D),
g_obj->vertices_linear_,
GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_buffers[INDEX_BUFFER_LINEAR]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
g_obj->num_indices_linear_ * sizeof(GLushort),
g_obj->indices_linear_,
GL_STATIC_DRAW);
glEnableVertexAttribArray(VERTEX_POSITION);
glVertexAttribPointer(VERTEX_POSITION, 2, GL_FLOAT, GL_FALSE,
sizeof(vertex2D),
(const GLvoid *)offsetof(vertex2D, position));
glEnableVertexAttribArray(VERTEX_COLOR);
glVertexAttribPointer(VERTEX_COLOR, 4, GL_UNSIGNED_BYTE, GL_FALSE,
sizeof(vertex2D),
(const GLvoid *)offsetof(vertex2D, color));
glBindVertexArray(0);
}
答案 1 :(得分:0)
你应该重复调用以绑定glBindVertexArray之间的元素缓冲区:
glBindVertexArray(g_vaos[VAO]);
glEnableVertexAttribArray(VERTEX_POSITION);
glVertexAttribPointer(VERTEX_POSITION, 2, GL_FLOAT, GL_FALSE,
sizeof(vertex2D),
(const GLvoid *)offsetof(vertex2D, position));
glEnableVertexAttribArray(VERTEX_COLOR);
glVertexAttribPointer(VERTEX_COLOR, 4, GL_UNSIGNED_BYTE, GL_FALSE,
sizeof(vertex2D),
(const GLvoid *)offsetof(vertex2D, color));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_buffers[INDEX_BUFFER]);
glBindVertexArray(0);
元素缓冲区绑定是VAO状态的一部分,并在新VAO绑定时重置。