什么时候应该调用 glVertexAttribPointer? [英] When should glVertexAttribPointer be called?

查看:33
本文介绍了什么时候应该调用 glVertexAttribPointer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从文档中看不到什么时候应该调用 glVertexAttribPointer.看起来它是 VBO 初始化的一部分,但我注意到在渲染过程中调用它的示例代码.

It's not obvious from the documentation when glVertexAttribPointer should be called. It looks like it's part of VBO initialisation, but I notice example code calling it during rendering.

glVertexAttribPointer(vertexAttributeId, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), reinterpret_cast(offsetof(Vertex2D, m_x)));

应该在 GL_ARRAY_BUFFER 的初始化过程中调用 glVertexAttribPointer 还是应该在渲染过程中调用它(在调用 glBindBuffer 之后)?>

Should glVertexAttribPointer be called during initialisation of a GL_ARRAY_BUFFER or should it be called during rendering (after a call to glBindBuffer)?

推荐答案

glVertexAttribPointer 函数指定了顶点属性的格式和源缓冲区(忽略不推荐使用的客户端数组)渲染一些东西(即下一个 glDraw... 调用).

The function glVertexAttribPointer specifies the format and source buffer (ignoring the deprecated usage of client arrays) of a vertex attribute that is used when rendering something (i.e. the next glDraw... call).

现在有两种情况.您要么使用顶点数组对象 (VAO),要么不使用(尽管不使用 VAO 在现代 OpenGL 中已被弃用和不鼓励/禁止).如果您使用 VAO,那么您通常会在渲染之前调用 glVertexAttribPointer(以及相应的 glEnableVertexAttribArray)以正确设置状态.但是,如果使用 VAO,您实际上是在 VAO 创建代码(通常是某些初始化或对象创建的一部分)中调用它(以及启用函数),因为它的设置存储在 VAO 中,渲染时您需要做的就是绑定VAO并调用draw函数.

Now there are two scenarios. You either use vertex array objects (VAOs) or you don't (though not using VAOs is deprecated and discouraged/prohibited in modern OpenGL). If you're not using VAOs, then you would usually call glVertexAttribPointer (and the corresponding glEnableVertexAttribArray) right before rendering to setup the state properly. If using VAOs though, you actually call it (and the enable function) inside the VAO creation code (which is usually part of some initialization or object creation), since its settings are stored inside the VAO and all you need to do when rendering is bind the VAO and call a draw function.

但是无论何时调用 glVertexAttribPointer,都应该在之前绑定相应的缓冲区(无论实际创建和填充的时间),因为 glVertexAttribPointer 函数设置当前绑定的 GL_ARRAY_BUFFER 作为此属性的源缓冲区(并存储此设置,以便之后您可以自由绑定另一个 VBO).

But no matter when you call glVertexAttribPointer, you should bind the corresponding buffer right before (no matter when that was actually created and filled), since the glVertexAttribPointer function sets the currently bound GL_ARRAY_BUFFER as source buffer for this attribute (and stores this setting, so afterwards you can freely bind another VBO).

因此在使用 VAO(推荐)的现代 OpenGL 中,它通常类似于此工作流程:

So in modern OpenGL using VAOs (which is recommended), it's usually similar to this workflow:

//initialization
glGenVertexArrays
glBindVertexArray

glGenBuffers
glBindBuffer
glBufferData

glVertexAttribPointer
glEnableVertexAttribArray

glBindVertexArray(0)

glDeleteBuffers //you can already delete it after the VAO is unbound, since the
                //VAO still references it, keeping it alive (see comments below).

...

//rendering
glBindVertexArray
glDrawWhatever

<小时>

当不使用 VAO 时,它会是这样的:


When not using VAOs it would be something like that:

//initialization
glGenBuffers
glBindBuffer
glBufferData

...

//rendering
glBindBuffer
glVertexAttribPointer
glEnableVertexAttribArray
glDrawWhatever

这篇关于什么时候应该调用 glVertexAttribPointer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆