何时应调用glVertexAttribPointer?

编程入门 行业动态 更新时间:2024-10-26 12:28:52
本文介绍了何时应调用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<const GLvoid*>(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),也可以不使用(虽然在现代OpenGL中不建议使用VAO,但不鼓励/禁止使用VAO).如果您不使用VAO,则通常会在渲染之前立即调用glVertexAttribPointer(以及相应的glEnableVertexAttribArray)以正确设置状态.如果使用VAO,则实际上是在VAO创建代码(通常是某些初始化或对象创建的一部分)中调用它(以及使能函数),因为它的设置存储在VAO中,并且渲染时您需要做的所有事情就是绑定VAO并调用绘制函数.

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?

本文发布于:2023-11-03 06:28:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554451.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时应   glVertexAttribPointer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!