用于渲染不同对象的VAO和VBO

时间:2014-12-14 20:36:00

标签: c++ c opengl vbo vao

我写了这个"模型"用于加载.obj文件并在VBO中为它们分配数据的类。 它的代码是这样的:(注意它没有使用VAO)

class Model {...}

void Model::LoadOBJ(const char *file)
{
    //load vertices, normals, UVs, and put them all in _vec, which is a private data member of std::vector<glm::vec3>
    ...

    //if an .obj file is loaded for the first time, generate a buffer object and bind it
    if(glIsBuffer(_vbo) == GL_FALSE)
    {
        glGenBuffers(1, &_vbo);//_vbo is a private data member
        glBindBuffer(GL_ARRAY_BUFFER, _vbo);
    }

    //load the data in the array buffer object
    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * _vec.size(), &_vec[0][0], GL_STATIC_DRAW);
}

void Model::Draw()
{
    glBindBuffer(GL_ARRAY_BUFFER, _vbo);
    glDrawArrays(GL_TRIANGLES, 0, _numVertices);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

我曾经认为下一个代码可以很好地渲染两个不同的对象:

void init()
{
    //vao dummy (?)
    GLuint VAO;
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);

    //load 3d models
    Model cube = load("cube.obj");
    Model cow = load("cow.obj");

    //the next two lines should be valid for both objects?
    glVertexAttribPointer(prog.GetAttribLocation("vertex"), 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(prog.GetAttribLocation("vertex"));
}

void render()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //draw cube:
    //some matrix transformations
    ...
    cube.Draw();

    //draw cow:
    //some matrix transformations
    ...
    cow.Draw()

    glutSwapBuffers();
}

但事实证明,OpenGL只会绘制两头牛或两个立方体。 (取决于我在init()中加载的模型) two cows two cubes

顺便说一句,我很确定在第一张图片中,opengl确实试图绘制两头母牛,但是glDrawArrays()函数是用多维数据集所需的顶点量调用的。

那我错过了什么?我是否需要为每个缓冲区对象或类似的东西使用不同的VAO?

2 个答案:

答案 0 :(得分:4)

&#34;电流&#34;国家应该归咎于此,特别是与glVertexAttribPointer相关的。

glVertexAttribPointer (...)的所有调用都建立了与当前绑定到GL_ARRAY_BUFFER的缓冲区对象相关的内存指针。我们倾向于将一个绑定到该位置的缓冲区对象称为 顶点缓冲区对象 ,但实际上单个缓冲区对象可用于多种用途而实际上并不是有类型。

在现代OpenGL中想到它有点像这样:

GLuint GL_ARRAY_BUFFER_BINDING = 0; // Only 1 or 2 commands in GL care about this state
GLuint GL_VERTEX_ARRAY_BINDING = 0; // 0 is an invalid VAO (if this is 0, most vertex commands will generate `GL_INVALID_OPERATION`).

// Generic GPU-side Memory Store
struct GLBufferObject {
  GLsizeiptr* gpu_base_addr;
} *gl_buffer_objects;

// Vertex Array State
struct GLVertexArrayObject {
  GLsizeiptr* attribute_pointers [GL_MAX_VERTEX_ATTRIBUTES];
  GLboolean   attribute_enabled  [GL_MAX_VERTEX_ATTRIBUTES];
  GLuint      GL_ELEMENT_ARRAY_BUFFER_BINDING;
} *gl_array_objects;

void glBindVertexArray (GLuint array)
{
  GL_VERTEX_ARRAY_BINDING = array;
}

void glBindBuffer (GLenum target, GLuint buffer)
{
  if (target == GL_ARRAY_BUFFER)
    GL_ARRAY_BUFFER_BINDING = buffer;
}

void glVertexAttribPointer (GLuint index, ..., const GLvoid* offset)
{
  GLBufferObject*      current_vbo = &gl_buffer_objects [GL_ARRAY_BUFFER_BINDING];
  GLVertexArrayObject* current_vao = &gl_array_objects  [GL_VERTEX_ARRAY_BINDING];

  current_vao->attribute_pointers [index] = current_vbo->gpu_base_addr + offset;
}

这个伪代码的要点是向您展示OpenGL中只有一个命令,您绑定到GL_ARRAY_BUFFER的内容在整个代码中都很重要:glVertexAttribPointer (...)

所有其他命令(例如glDrawArrays (...))实际上使用存储在&#34; current_vao&#34;中的状态。如glVertexAttribPointer (...)的模拟实现中所示。


最后,您不使用VAO实际上是一个问题。您正在覆盖软件当前使用的 VAO的属性指针,以及最后加载的模型。

查看哪些状态GLVetexArrayObject存储在伪代码中,然后考虑重构自己的代码以利用它。否则,每次致电glVertexAttribPointer时,您都必须至少拨打一次void Model::Draw()电话。

有关不太有趣的解释,请参阅此related answer

答案 1 :(得分:2)

绑定缓冲区后,需要将顶点属性代码(设置和启用)移动到绘图函数。这些调用将作用于您当前绑定的缓冲区。

如果对不同的模型使用不同的vaos,attrib数据将存储在vao中,每次绘制新对象时都不需要重新绑定attribs(只需更改vao)。