C++ - Opengl VAO and VBO Objects - Stack Overflow

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Opengl VAO and VBO objects

Asked 7 years, 2 months ago Modified 1 year, 3 months ago Viewed 214 times

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

-1 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?

c++ opengl constructor

Share Improve this question Follow edited Jan 11, 2023 at 4:02 asked Jan 15, 2017 at 6:36
genpfault Pavel Angel Mendoza
51.6k 11 88 142 Villafane
417 1 5 21

1 You should show your drawing code too. – Yakov Galka Jan 15, 2017 at 7:46

1 Answer Sorted by: Highest score (default)

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* .
1
See also this answer to see what state VAOs contain.
Share Improve this answer Follow edited Jan 11, 2023 at 3:43 answered Jan 15, 2017 at 7:43
Yakov Galka
71.6k 16 143 220

You might also like