duannei0044 2012-10-25 05:29
浏览 54
已采纳

如何使这个简单的OpenGL代码(在“宽松的” 3.3和4.2配置文件中工作)在严格的3.2和4.2核心配置文件中工作?

I had some 3D code that I noticed wouldn't render in a strict core profile but fine in a "normal" (not explicitly requested-as-core-only) profile context. To isolate the issue, I have written the smallest simplest possible OpenGL program drawing just a triangle and a rectangle:

enter image description here

I have posted that OpenGL program as a Gist here.

With the useStrictCoreProfile variable set to false, the program outputs no error messages to the console and draws a quad and a triangle as per the above screenshot, both on an Intel HD OpenGL 3.3 and on a GeForce with OpenGL 4.2.

However, with useStrictCoreProfile set to true, it clears the background color but does not draw the tri & quad, console output is this:

GLCONN: OpenGL 3.2.0 @ NVIDIA Corporation GeForce GT 640M LE/PCIe/SSE2 (GLSL: 1.50 NVIDIA via Cg compiler)
LASTERR: OpenGL error at step 'render.VertexAttribPointer()': GL_INVALID_OPERATION
LASTERR: OpenGL error at step 'render.DrawArrays()': GL_INVALID_OPERATION
LASTERR: OpenGL error at step 'render.VertexAttribPointer()': GL_INVALID_OPERATION
LASTERR: OpenGL error at step 'render.DrawArrays()': GL_INVALID_OPERATION
LASTERR: OpenGL error at step '(post loop)': GL_INVALID_OPERATION
EXIT

... if a 4.2 strict core profile is requested instead of 3.2, same issue. Applies to 3 different nvidia GPUs so I assume I'm not conforming to the strict core profile properly. What was I doing wrong, and how can I fix this?

Note, you won't find a glEnableVertexAttribArray call in the above Gist, as it's inside the glutil package I'm importing -- but this does get called as the last step in the gist's compileShaders() func.

  • 写回答

1条回答 默认 最新

  • dougua3706 2012-10-25 06:38
    关注

    You're not creating/binding a Vertex Array Object with glGenVertexArrays() and glBindVertexArray(). VAOs encapsulate a bunch of vertex attribute state, including which attributes are enabled, detailed per-attribute information, etc. They were optional when the feature was originally introduced, but they're now required in strict/core contexts according to section 10.4 of the OpenGL core specification:

    An INVALID_OPERATION error is generated by any commands which modify, draw from, or query vertex array state when no vertex array is bound. This occurs in the initial GL state, and may occur as a result of BindVertexArray or a side effect of DeleteVertexArrays.

    Here's a very rough example of how VAOs are used:

    // At initialization time:
    GLuint vao = 0;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    // Set up your vertex attribute state:
    //  - glBindBuffer(GL_ARRAY_BUFFER,...);
    //  - glEnableVertexAttribArray(...);
    //  - glVertexAttribPointer(...);
    //  - etc. -- Refer to OpenGL docs to see what is/isn't included in the VAO!
    glBindVertexArray(0); // unbinds vao
    
    // At draw time:
    glBindVertexArray(vao); // automatically sets up previously-bound vertex attribute state
    glDrawArrays(...);
    glBindVertexArray(0); // unbinds vao
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮