为什么 glDrawElements 给我 GL_OUT_OF_MEMORY?
我只画了4个顶点。我正在尝试使用 glDrawElements
渲染一个简单的四边形,但似乎没有任何效果。我也用 Java 编写了相同的代码,并且它可以工作。然而,事实并非如此。
这是绘图代码:
glUseProgram(mProgram);
glBindBuffer(GL_ARRAY_BUFFER, mBuffers[0]);
glEnableVertexAttribArray(mPosAttr);
glVertexAttribPointer(mPosAttr, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffers[1]);
if (er == 0){ er = glGetError(); std::cout<<gluErrorString(er)<<std::endl; }
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, (void*)0);
glDisableVertexAttribArray(mPosAttr);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glUseProgram(0);
我省略了诸如 glUniform__
调用之类的无用内容,无论哪种方式我都会得到相同的结果。无论如何,第一次打印“无错误”,第二次打印“内存不足”。这意味着它肯定是由 glDrawElements 引起的。
为什么会发生这种情况?什么可以解决它?我应该提供更多代码吗?
I'm only drawing 4 vertices. I'm trying to render a simple quad using glDrawElements
, but nothing seems to work. I've written identical code in Java as well, and it works. This, however, does not.
Here's the drawing code:
glUseProgram(mProgram);
glBindBuffer(GL_ARRAY_BUFFER, mBuffers[0]);
glEnableVertexAttribArray(mPosAttr);
glVertexAttribPointer(mPosAttr, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBuffers[1]);
if (er == 0){ er = glGetError(); std::cout<<gluErrorString(er)<<std::endl; }
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, (void*)0);
glDisableVertexAttribArray(mPosAttr);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glUseProgram(0);
I've left out useless things like glUniform__
calls, I get the same result either way. Anyway, this prints "no error" the first time and then "out of memory" the second. That means it is most definitely caused by glDrawElements.
Why is this happening? What would fix it? Should I provide more code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OP 在评论中指出:
然而,在给定的源代码中,
错误检查是在glDrawElements之前执行的。而且错误检查代码也是错误的。正确的方法是:
正如已经提到的,错误会累积,必须在这样的循环中穷举查询。
如果不了解更多有关程序的信息,就无法判断为什么会发生此错误:上下文是如何创建的,扩展是否正确初始化,诸如此类的东西。理想情况下,拥有完整的源代码,或者一个最小的(不能)工作的可编译示例。
In a comment the OP stated:
However in the given source code
The error check is performed before glDrawElements. Also the error checking code is wrong. The correct method was:
As already mentioned, error accumulate and must be queried to exhaustion in such a loop.
Why this error happens one can not tell without knowing more about the program: How is the context created, are extensions properly initialized, stuff like the. Ideally one had the full source code, or a minimal (not) working compilable example.