如何在没有VAO的情况下使用OpenGL Core配置文件

How to use VBOs without VAOs with OpenGL core profile?

本文关键字:OpenGL Core 配置文件 情况下 VAO      更新时间:2023-10-16

我使用顶点缓冲对象使用顶点阵列对象遇到了麻烦。

我的理解是,VAO只是在VBO周围封装了状态。但是,如果没有VAO,VBO不应该使用吗?

这是一个迷你示例。使用use_vao=true,这可以正确起作用(呈橙色RECT)。使用use_vao=false,这一切都没有呈现,并在glDrawElements上生成GL_INVALID_OPERATION错误。

// make sure the modern opengl headers are included before any others
#include <OpenGL/gl3.h>
#define __gl_h_
#include <GLUT/glut.h>
#include <string>
#include <cassert>
// For rendering a full-viewport quad, set tex-coord from position
std::string tex_v_shader = R"(
#version 330 core
in vec3 position;
void main()
{
  gl_Position = vec4(position,1.);
}
)";
// Render directly from color or depth texture
std::string tex_f_shader = R"(
#version 330 core
out vec4 color;
void main()
{
  color = vec4(0.8,0.4,0.0,0.75);
}
)";
// shader id, vertex array object
GLuint tex_p_id;
int w=1440,h=480;
const GLfloat V[] = {-0.5,-0.5,0,0.5,-0.5,0,0.5,0.5,0,-0.5,0.5,0};
const GLuint F[] ={0,1,2, 0,2,3};
int main(int argc, char * argv[])
{
  // Init glut and create window + OpenGL context
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_3_2_CORE_PROFILE|GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH); 
  glutInitWindowSize(w,h);
  glutCreateWindow("test");
  // Compile shaders
  const auto & compile = [](const char * src,const GLenum type)->GLuint
  {
    GLuint s = glCreateShader(type);
    glShaderSource(s, 1, &src, NULL);
    glCompileShader(s);
    return s;
  };
  tex_p_id = glCreateProgram();
  glAttachShader(tex_p_id,compile(tex_v_shader.c_str(), GL_VERTEX_SHADER));
  glAttachShader(tex_p_id,compile(tex_f_shader.c_str(), GL_FRAGMENT_SHADER));
  glLinkProgram(tex_p_id);
  glutDisplayFunc(
    []()
    {
      glViewport(0,0,w,h);
      glUseProgram(tex_p_id);
      glClearColor(0.0,0.4,0.7,0.);
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      const bool use_vao = true;
      GLuint VAO;
      if(use_vao)
      {
        glGenVertexArrays(1, &VAO);
        glBindVertexArray(VAO);
      }
      GLuint VBO, EBO;
      glGenBuffers(1, &VBO);
      glGenBuffers(1, &EBO);
      glBindBuffer(GL_ARRAY_BUFFER, VBO);
      glBufferData(GL_ARRAY_BUFFER, sizeof(V), V, GL_STATIC_DRAW);
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
      glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(F), F, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0); 
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 
      glBindBuffer(GL_ARRAY_BUFFER, VBO); 
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); 
      if(use_vao)
      {
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, 0); 
      }
      assert(glGetError() == GL_NO_ERROR);
      glDrawElements(GL_TRIANGLES,sizeof(F)/sizeof(GLuint),GL_UNSIGNED_INT, 0);
      assert(glGetError() == GL_NO_ERROR);
      glutSwapBuffers();
    }
    );
  glutReshapeFunc( [](int w,int h){::h=h, ::w=w;});
  glutMainLoop();
}

在我的机器上glGetString(GL_VERSION)产生4.1 INTEL-10.6.20

在核心配置文件中需要使用VAO。摘自OpenGL 3.3规格,第342页,在E.2.2节"删除功能"部分中:

默认顶点数组对象(名称零)也被弃用。

这意味着您无法在不创建和绑定自己的VAO的情况下设置顶点属性。

no with core 3.3 个人资料您需要 VAO渲染。

但是,您可以创建并绑定VAO并忘记它(保持绑定)。

即使使用兼容性配置文件并且不使用vao,

也必须调用glEnableVertexAttribArray(0);

其他一些评论是,您将每个框架都重新生成所有缓冲区和VAO,但不要清理它。您应该在初始化期间一次这样做,然后在绘画时重新启动:

if(!use_vao){
      glBindBuffer(GL_ARRAY_BUFFER, VBO);
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
}
else
{
      glBindVertexArray(VAO);
}