-1

I want to create multiple vertex buffers in objects (one by object). the constructor is:

VertexBuffer::VertexBuffer(const GLvoid *data,
                           GLsizeiptr size,
                           GLenum mode, 
                           GLsizei count, 
                           GLsizei stride,
                           ShaderInterface *shaderInterface,
                           GLvoid *positionOffset,
                           GLvoid *normalOffset)
    : _mode(mode),
    _count(count),
    _stride(stride),
    _shaderInterface(shaderInterface),
    _positionOffset(positionOffset),
    _normalOffset(normalOffset)

{
    glGenBuffers(1, &_vertexBufferID);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferID);
    glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);

    //
    GLuint vao = 0;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    glEnableVertexAttribArray(0); //??????
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferID);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    //
}

If I create 1 object VertexBuffer, this works ok. But if I create another object (I won't use it) and I draw the first object again the result is not correct (for example, the object showed is in other place). Why?

genpfault
  • 51,148
  • 11
  • 85
  • 139

1 Answers1

1

You don't seem to store the vao anywhere, but you should. Before drawing each object you shall glBindVertexArray its vao (but not _vertexBufferID) before you call any of glDraw*.

See also this answer to see what state VAOs contain.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220