I have a class which in its constructor creates and binds a VAO, a VBO and then sets the vertex attributes:
ShapeBatcher2D::ShapeBatcher2D()
{
vao = std::make_unique <VAO>();//literally only glGenVertexArrays
vao->Bind();
vbo = std::make_unique<VBO>((void*)0,GL_ARRAY_BUFFER,MAXNUMBEROFVERTICES*VertexPositionColor::size,GL_STREAM_DRAW);
VertexPositionColor::declaration.SetDeclaration();
}
Where:
VBO::VBO(void* data, GLenum targt, unsigned int size, GLenum hint)
{
glGenBuffers(1, &handle);
target = targt;
Bind();
SetData(data, size, hint);
}
void VBO::SetData(void* data, unsigned int size, GLenum hint)
{
glBufferData(target, size, data, hint);
}
Objects of that class have a std::vector of vertices, that can be cleared or added to (no OpenGL calls). Then they have a draw method, which binds the VAO, sets the VBO data with glBufferData(), and calls glDrawArrays():
void ShapeBatcher2D::DrawUpdateData(GLenum primitiveType)
{
vao->Bind();
vbo->SetSubdata((void*)&info[0], info.size() * VertexPositionColor::size, 0);
glDrawArrays(primitiveType, 0, info.size());
}
or, for when data isn't updated:
void ShapeBatcher2D::Draw(GLenum primitiveType)
{
vao->Bind();
glDrawArrays(primitiveType,0,info.size());
}
When I have a single one of these objects, it works as I expected it to(all vertices are drawn). But when I have 2, only the last one to call its constructor is drawn.
Here is the main drawing loop:
void StreetWindow::Draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader->Bind();
shader->SetUniform("ortho", cam.GetOrthoProjection());
if (batcherRoads->CanDraw())
{
batcherRoads->Draw();// one of such objects
}
//start of not relevant
batcherCars->Clear();
for (int i = 0; i < cars.size(); i++)
{
batcherCars->AddPolygon(vec2(-roadLength / 2 + roadLength * cars[i].progress/lengthTime, 0), vec2(StreetExit::width, StreetExit::width), 0, 4, RED, 0.1f, pi<float>() / 4);
}
for (int i = 0; i < exits.size(); i++)
{
float exitX = -roadLength / 2 + roadLength * exits[i].position;
for (int v = 0; v < exits[i].carProgress.size(); v++)
{
batcherCars->AddPolygon(vec2(exitX, exits[i].up*(roadWidth / 2 + StreetExit::length * exits[i].carProgress[v] / exits[i].lengthTime)), vec2(StreetExit::width, StreetExit::width), 0, 4, RED, 0.1f, pi<float>() / 4);
}
}
//end of not relevant
if (batcherCars->CanDraw())
{
batcherCars->DrawUpdateData(); // another of such objects
}
}
I think this has something to do with having multiple VBOs, because calling glBindBuffer(GL_ARRAY_BUFFER,0) changes which one gets drawn.
Can someone help me here?