gpt4 book ai didi

c++ - VBO必须绑定(bind)在VAO之后

转载 作者:行者123 更新时间:2023-11-30 04:56:58 40 4
gpt4 key购买 nike

代码:

void render()
{
glClear(GL_COLOR_BUFFER_BIT);

GLfloat vertices[6][4] = {
{ 1.0f, 1.0f, 1.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f },

{ 1.0f, 1.0f, 1.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f }
};

glUseProgram(shadptr->progGraphics);
glBindVertexArray(vao_g);
glBindBuffer(GL_ARRAY_BUFFER, vbo_g);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, vertices, GL_DYNAMIC_DRAW);
glDrawArrays(GL_TRIANGLES, 0, 6);

glUseProgram(shadptr->progText);
glBindVertexArray(vao_t);
glBindBuffer(GL_ARRAY_BUFFER, vbo_t);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, vertices, GL_DYNAMIC_DRAW);
glDrawArrays(GL_TRIANGLES, 0, 6);

SDL_GL_SwapWindow(winptr->window);
}

void set_buffers()
{
glGenVertexArrays(1, &vao_g);
glBindVertexArray(vao_g);

glGenBuffers(1, &vbo_g);
glBindBuffer(GL_ARRAY_BUFFER, vbo_g);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), nullptr);

glGenVertexArrays(1, &vao_t);
glBindVertexArray(vao_t);

glGenBuffers(1, &vbo_t);
glBindBuffer(GL_ARRAY_BUFFER, vbo_t);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), nullptr);
}

我有一个问题。不知道为什么在render函数中绑定(bind)vao之后还要绑定(bind)vbo。当我不绑定(bind) vbo 时,在 RenderDoc 中出现错误“glDrawArrays 已生成错误 (GL_OUT_OF_MEMORY)”,在网格输出中,在 VS 输入中我看到错误的值或 --- 值。

最佳答案

I don't know why I have to bind vbo after binding vao in render function"

因为您尝试通过 glBufferData 创建和初始化缓冲区对象的数据存储为此,必须绑定(bind)缓冲区。

我建议将创建和初始化缓冲区的代码移动到 set_buffers。然后在函数 render 中仅绑定(bind)顶点数组对象就足够了:

void render()
{
glClear(GL_COLOR_BUFFER_BIT);

glUseProgram(shadptr->progGraphics);
glBindVertexArray(vao_g);
glDrawArrays(GL_TRIANGLES, 0, 6);

glUseProgram(shadptr->progText);
glBindVertexArray(vao_t);
glDrawArrays(GL_TRIANGLES, 0, 6);

SDL_GL_SwapWindow(winptr->window);
}

void set_buffers()
{
....

GLfloat vertices[6][4] = {
{ 1.0f, 1.0f, 1.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f },

{ 1.0f, 1.0f, 1.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f }
};

glBindBuffer(GL_ARRAY_BUFFER, vbo_g);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, vertices, GL_DYNAMIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, vbo_t);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, vertices, GL_DYNAMIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, 0);
}

我建议使用 glBufferSubData , 改变缓冲区的内容。这将更新缓冲区,但不会创建和初始化一个全新的数据存储,因此效率会更高。

关于c++ - VBO必须绑定(bind)在VAO之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52245193/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com