了解VAO和VBO

发布于 2025-02-12 09:34:41 字数 898 浏览 3 评论 0原文

我正在使用两个着色器来渲染场景中的所有项目。每个项目仅由一个着色器绘制。

每个着色器都有不同的属性。就我而言,一个着色器只有顶点坐标和颜色,而第二个着色器的坐标,纹理坐标和颜色。 每个着色器都有一个与这些属性相对应的VAO。

我为渲染场景做的事情是循环通过所有着色器。对于每个着色器,我都称为GluSeprogram,我将与着色器关联的VAO称为Glbindvertexarray,并初始化属性。然后,一旦着色器处于活动状态,我就会循环遍历场景中的所有项目,以使用当前的着色器渲染它们。

现在,要简化每个项目的绘制方法,其中包含以下内容:

glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
//s->setupVertices();
glDrawElements(m_primitive, m_elementSize, GL_UNSIGNED_INT, (GLvoid*)(sizeof(GLuint) * 0));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

如果我这样运行程序,则该项目未正确渲染,但是我注意到,如果我取消注释setupvertices()行(这是GlverTexAttribPointer的当前的GLVERTEXATTRIBPOINTER的位置着色器被称为),然后一切都很好。

现在这是我不明白的。据我在VAO上的了解,VAO商店有关顶点属性的信息,所以我希望一旦使用当前的VAO致电Glbindvertexarray,就可以正确记住顶点布局的数据,并且无需重新指定的角度布局。每个缓冲对象;但是看来这是必要的,否则所有项目都是错误的。

我在做错什么/缺少吗?还是我对VAO/VBO工作不了解?

I am using two shaders to render all the items in my scene. Each item is drawn by one shader only.

Every shader has different attributes. In my case, one shader has only vertex coordinates and color, while the second shader has vertex coordinates, texture coordinates and color.
Each shader has a VAO corresponding with these attributes.

What I do to render the scene is loop through all shaders. For each shader, I call glUseProgram, I call glBindVertexArray with the VAO associated with the shader and also initialize the attributes. Then once the shader is active I loop through all the items in my scene to render them with the current shader.

Now to simplify each item has a draw method which contains the following:

glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
//s->setupVertices();
glDrawElements(m_primitive, m_elementSize, GL_UNSIGNED_INT, (GLvoid*)(sizeof(GLuint) * 0));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

If I run the program like this, the items are not rendered correctly, but I noticed that if I uncomment the setupVertices() line (this is where the glVertexAttribPointer for the current shader are called), then everything is rendered fine.

Now this is something I don't understand. From what I understood on VAO, VAO store information on the vertex attributes, so I was expecting that once I call glBindVertexArray with the current VAO, the data for the vertex layout are remembered correctly and there's no need to re-specify the vertices layout for each buffer object; but it looks like this is necessary otherwise all items are rendered wrongly.

Am I doing something wrong/missing? Or I didn't understand a bit about VAO/VBO work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文