user2547078
user2547078

Reputation:

If my VBO class references vertex arrays, is data still stored on the GPU?

Quite simply, my question is this. My code is below for a C++ header file that keeps track of and handles a vertex buffer object in OpenGL. In order to comply with core standards, it must use vertex arrays (VAO). Since glGenVertexArrays() and other related methods are called, is this rendering method still have information stored on the GPU or in RAM?

class VertexBuffer
{
private:
    int vertCount;

    GLuint vertArray;
    GLuint vertBuffer;

public:
    VertexBuffer();
    void renderInterleaved();
    void createInterleaved(GLfloat*, int);
    void destroy();

    GLuint* getVboId();
};

VertexBuffer::VertexBuffer()
{
    glGenVertexArrays(1, &vertArray);
    glGenBuffers(1, &vertBuffer);
}

void VertexBuffer::renderInterleaved()
{
    glBindVertexArray(vertArray);
    glBindBuffer(GL_ARRAY_BUFFER, vertBuffer);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    int stride = 7 * sizeof(GLfloat);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*) 0);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, stride, (void*)(3 * sizeof(GLfloat)));
    glDrawArrays(GL_TRIANGLES, 0, vertCount);

    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}

void VertexBuffer::createInterleaved(GLfloat* data, int vertices)
{
    vertCount = vertices;
    glBindBuffer(GL_ARRAY_BUFFER, vertBuffer);
    glBufferData(GL_ARRAY_BUFFER, vertices * 21 * sizeof(GLfloat), data, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void VertexBuffer::destroy()
{
    glDeleteBuffers(1, &vertBuffer);
    glDeleteVertexArrays(1, &vertArray);
}

GLuint* VertexBuffer::getVboId()
{
    return &vertBuffer;
}

Upvotes: 0

Views: 792

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 474276

The term "vertex buffer object" is, and has always been, a misnomer, created by the unfortunate naming of the extension that introduced buffer objects.

There is no "vertex buffer object." There is only a buffer object, which represents a linear array of OpenGL-managed memory. Buffer objects can be used to store vertex data for rendering operations, but there is nothing intrinsic about any particular buffer object that lets it do that. It is only a question of how you use it.

If your object encapsulates the parameters you would use for glVertexAttribPointer, then it is not encapsulating just a buffer object. A buffer object does not know how to render itself or what data it contains; it's just a buffer. The closest OpenGL concept it represents is a vertex array object.

But your code isn't really using VAOs either. Every time you call renderInterleaved, you're setting state that's already stored as part of the VAO. All of those glVertexAttribPointer and glEnableVertexAttribArray calls are recorded into the VAO. That's something you ought to set up once, then just bind the VAO later and render with it. So yes, it is a less-efficient VAO.

But your class encapsulates more than a VAO. It knows how to render itself. So the best name for your class would be "mesh" or something of that effect.

Upvotes: 4

Related Questions