OpenGL:在 glEnableVertexAttribArray 之后的 INVALID_OPERATION [英] OpenGL: INVALID_OPERATION following glEnableVertexAttribArray

查看:24
本文介绍了OpenGL:在 glEnableVertexAttribArray 之后的 INVALID_OPERATION的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个正常运行的 OpenGL 应用程序从 Windows 移植到 OSX,并且在调用 glEnableVertexAttribArray() 后不断收到无效操作"(代码 1282)错误.这是渲染方法:

I'm porting a functioning OpenGL app from Windows to OSX, and keep getting an "invalid operation" (code 1282) error after calling glEnableVertexAttribArray(). Here's the render method:

gl::Disable(gl::DEPTH_TEST);    
gl::Disable(gl::CULL_FACE);
gl::PolygonMode(gl::FRONT_AND_BACK,gl::FILL);

/// render full-screen quad
gl::UseProgram(m_program);

check_gl_error();

gl::BindBuffer(gl::ARRAY_BUFFER, m_vertexBuffer);
gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, m_indexBuffer);

check_gl_error();
GLint positionLocation = -1;

positionLocation = gl::GetAttribLocation(m_program,"Position");
check_gl_error();

/// positionLocation now == 0

gl::EnableVertexAttribArray(positionLocation);
//// ************ ERROR RETURNED HERE **********************
//// ************ ERROR RETURNED HERE **********************
check_gl_error();

gl::VertexAttribPointer(positionLocation,3,gl::FLOAT,false,3 * sizeof(GLfloat),(const GLvoid*)0);
check_gl_error();

gl::DrawElements(gl::TRIANGLES,m_indexCount,gl::UNSIGNED_SHORT,0);

check_gl_error();

gl::BindBuffer(gl::ARRAY_BUFFER,0);
check_gl_error();

gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER,0);
check_gl_error();

check_gl_error() 只是获取最后一个 GL 错误并返回一个稍微可读的描述.

check_gl_error() just gets the last GL error and returns a somewhat-readable description thereof.

此代码在 Windows 下运行良好.但是,正如我正在快速学习的那样,这并不一定意味着它是正确的.我已经验证所有先前绑定的对象(程序、顶点缓冲区、索引缓冲区)都是有效的句柄.glGetAttribLocation() 返回 Position 属性的有效位置(在本例中为 0).

This code works fine under Windows. But, as I'm rapidly learning, that doesn't necessarily mean that it is correct. I've verified that all of the previously-bound objects (program, vertex buffer, index buffer) are valid handles. glGetAttribLocation() returns a valid location (0 in this case) for the Position attribute.

glEnableVertexAttribArray() 的失败案例有哪些?在此之前是否有一些我没有设置的状态?

What are the failure cases for glEnableVertexAttribArray()? Is there some state that I've not set before this?

如果我注释掉绘制代码,则窗口在每一帧上都被清除为我的测试颜色(红色)(从代码片段中未显示的方法调用),其他一切正常,这暗示 其他一切都是正确的.

If I comment out the draw code, the window is cleared to my test color (red) (called from a method not shown in the code snippet) on every frame and everything else works fine, which implies that everything else is correct.

建议?

哦,对于 GL 状态机模拟器,它会告诉我为什么这是一个无效操作".(或者对一些神秘的、神奇的文档的引用,这些文档描述了每个 gl* 调用所需的输入状态.)

Oh, for a GL state machine simulator that would tell me why it is an "invalid operation." (Or a reference to some mystical, magical documentation that describes required input state for each gl* call.)

推荐答案

您会在 OS X 上看到此错误,因为它仅在您使用 OpenGL 3.x 或更高版本时才支持 OpenGL 核心配置文件.您的代码不符合核心配置文件.您很可能在 Windows 上使用了兼容性配置文件.

You're seeing this error on OS X because it only supports the OpenGL Core Profile if you're using OpenGL 3.x or higher. Your code is not Core Profile compliant. You were most likely using the Compatibility Profile on Windows.

具体来说,Core Profile 需要为所有与顶点相关的调用绑定一个顶点数组对象 (VAO).因此,在调用 glEnableVertexAttribArray() 或其他类似函数之前,您需要创建并绑定一个 VAO:

Specifically, the Core Profile requires a Vertex Array Object (VAO) to be bound for all vertex related calls. So before calling glEnableVertexAttribArray(), or other similar functions, you will need to create and bind a VAO:

GLuint vaoId = 0;
glGenVertexArrays(1, &vaoId);
glBindVertexArray(vaoId);

关于如何找出错误条件:在这种情况下,它并不像应该的那么容易.假设您使用 GL3 级别的功能集.在理想的世界中,您将转到 www.opengl.org,拉下靠近左上角的文档"菜单,选择OpenGL 3.3 Reference Pages",单击左侧窗格中的glEnableVertexAttribArray,然后查看页面上的错误"部分.然后您会看到... GL_INVALID_OPERATION 未列为可能的错误.

On how to find out the error conditions: In this case, it's not nearly as easy as it should be. Let's say you work with a GL3 level feature set. In an ideal world, you would go to www.opengl.org, pull down the "Documentation" menu close to the top-left corner, choose "OpenGL 3.3 Reference Pages", click on glEnableVertexAttribArray in the left pane, and look at the "Errors" section on the page. Then you see that... GL_INVALID_OPERATION is not listed as a possible error.

接下来,您可能想检查最新版本中是否有更好的东西.你也这样做,但选择OpenGL 4 Reference Pages".错误情况仍未列出.

Next, you might want to check if there's anything better in the latest version. You do the same, but choose "OpenGL 4 Reference Pages" instead. The error condition is still not listed.

现在您已经意识到,和您之前的许多人一样,这些手册页经常有问题.所以你去最终来源:规格.这次您在文档"菜单中选择OpenGL 注册表".这为您提供了指向 PDF 格式的所有规范文档的链接.同样,让我们​​先尝试 3.3.在文档中搜索EnableVertexAttribArray",但仍然没有将 GL_INVALID_OPERATION 记录为可能的错误.

By now you realize, like many before you, that these man pages are often faulty. So you go to the ultimate source: the specs. This time you choose "OpenGL Registry" in the Documentation menu. This gives you links to all the spec documents in PDF format. Again, let's try 3.3 first. Search for "EnableVertexAttribArray" in the document and there is... still no GL_INVALID_OPERATION documented as a possible error.

最后的手段,检查最新的规范文档,即 4.4.再次寻找EnableVertexAttribArray",是时候heureka了:

Last resort, checking the very latest spec document, which is 4.4. Again looking for "EnableVertexAttribArray", it's time for a heureka:

如果没有绑定顶点数组对象,则会生成 INVALID_OPERATION 错误.

An INVALID_OPERATION error is generated if no vertex array object is bound.

我很确定该错误也适用于 GL3.虽然手册页不完整是很常见的,但规范文档丢失东西的情况要少得多.非常密切相关的 glVertexAttribPointer() 调用已经在 GL3 中记录了此错误情况.

I'm quite certain that the error also applies to GL3. While it's reasonably common for the man pages to be incomplete, it's much rarer for the spec documents to be missing things. The very closely related glVertexAttribPointer() call has this error condition documented in GL3 already.

这篇关于OpenGL:在 glEnableVertexAttribArray 之后的 INVALID_OPERATION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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