c++ - 如果我们有 glBindBuffer, "glGenBuffers"真的很重要吗?

标签 c++ opengl

好吧,我从 OpenGL 开始,通过阅读有关 glBindBuffer 的文档,我有点困惑。

"glBindBuffer binds a buffer object to the specified buffer binding point. Calling glBindBuffer with target set to one of the accepted symbolic constants and buffer set to the name of a buffer object binds that buffer object name to the target. If no buffer object with name buffer exists, one is created with that name. When a buffer object is bound to a target, the previous binding for that target is automatically broken." Source: http://docs.gl/gl4/glBindBuffer



这是否意味着如果我不创建具有“foo”名称的缓冲区对象,但我调用 glBindBuffer,它会为我创建一个具有该名称(“foo”)的缓冲区对象?

如果是这样,以下代码应该可以正常工作:
GLuint bar = 70;
glBindBuffer(GL_ARRAY_BUFFER, bar);

-> 创建缓冲区对象,将其与 bar (70) “连接”并将其绑定(bind)到 GL_ARRAY_BUFFER。

最佳答案

不,此代码仅适用于兼容性配置文件上下文(或 OpenGL ES)。

OpenGL 4.6 API Core Profile Specification - 2.6.1 Object Management- page 28

[...] the command GenBuffers returns one or more previously unused buffer object names.
Generated names are marked by the GL as used, for the purpose of name generation only. Object names marked in this fashion will not be returned by additional calls to generate names of the same type until the names are marked unused again by deleting them [...]



这意味着 glGenBuffers除了保留名称(值)之外什么都不做。进一步调用 glGenBuffers不会返回相同的值。
如果 glGenBuffers总是用于获取缓冲区对象的名称值,那么您可以确定该值尚未用于其他缓冲区对象。

但是在桌面 OpenGL 核心配置文件规范中,不允许使用 glBindBuffer 的名称。 ,未由 glGenBuffers 保留(返回) .

OpenGL 4.6 API Core Profile Specification - 6.1 Creating and Binding Buffer Objects - page 62

An INVALID_OPERATION error is generated if buffer is not zero or a name returned from a previous call to GenBuffers, or if such a name has since been deleted with DeleteBuffers.



OpenGL 4.6 API Compatibility Profile Specification - 6.1 Creating and Binding Buffer Objects - page 62 中缺少规范的这一部分

这有点令人困惑,但这就是规范。

此行为可以通过您问题的代码进行验证。以下代码使用兼容性配置文件上下文不返回错误,但返回 GL_INVALID_OPERATION使用核心配置文件上下文:

GLuint bar = 70;
glBindBuffer(GL_ARRAY_BUFFER, bar); 
GLenum error = glGetError();

关于c++ - 如果我们有 glBindBuffer, "glGenBuffers"真的很重要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55552932/

相关文章:

c++ - 如何为 std::list 重载 std::remove ?

c++ - 前向声明导致VC++错误,不知道如何解决

c++ - glReadPixels 无效使用成员(你忘记了 '&' 了吗?)

cocoa - OpenGL 纹理绑定(bind)失败

c++ - Opengl 数组纹理,适用于原始数据但不适用于图像数据

c++ - OpenGL 多边形卡在左上角?

c++ - glUnmapBuffer 同时保持 glMapBuffer 内存有效为只读

c++ - 主窗口打开后显示一个按钮

c++ - 查找返回 6 的字符串长度太高的递归函数?

opengl - 绘制像素的顺序是否取决于 glDrawElements 中的索引?