Background/Issue: I'm pretty new to openGL and I'm trying to create a game engine (for learning purposes) and my program keeps crashing on my glDrawElements() call but only after trying to set glBindBuffer a second time.
Code: Below is some of my code in my engine. Basically, I have two possible objects I can draw with my engine, a triangle and a square. I'm trying to first send initial shape data down to GPU buffers within my RenderSystem's Initialize function like so:
RenderSystem.cpp
bool RenderSystem::Initialize()
{
//Send triangle shape data down to GPU
MyOpenGL::InitializeBuffers(ShapeData::Triangle().vertices.size(),
&ShapeData::Triangle().vertices.front(),
ShapeData::Triangle().indicies.size(),
&ShapeData::Triangle().indicies.front(),
triangleVertexBufferID , triangleIndexBufferID);
//Send square shape data down to GPU
MyOpenGL::InitializeBuffers(ShapeData::Square().vertices.size(),
&ShapeData::Square().vertices.front(),
ShapeData::Square().indicies.size(),
&ShapeData::Square().indicies.front(),
squareVertexBufferID, squareIndexBufferID);
return true;
}
The MyOpenGL::InitializeBuffers() function code is next:
void InitializeBuffers(int64 sizeOfGeometry, const void* GeometryDataFirstElement, int64 sizeOfIndicies, const void* indicieDataFirstElement, uint32 vertexBufferID, uint32 indexBufferID)
{
glGenBuffers(1, &vertexBufferID);
glGenBuffers(1, &indexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ARRAY_BUFFER, (sizeof(Vector2D) * sizeOfGeometry), GeometryDataFirstElement, GL_DYNAMIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (sizeof(uint16) * sizeOfIndicies), indicieDataFirstElement, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, nullptr);
}
Now, I want to call a draw function within my RenderSystems update() function which basically just calls this MyOpenGL::Draw() function:
//Passing in which ever BufferID's I want to draw (square or triangle)
void Draw(uint32 vertexBufferID, uint32 indexBufferID, uint16 numOfIndices)
{
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glDrawElements(GL_TRIANGLES, numOfIndices, GL_UNSIGNED_SHORT, 0);
);
However, after the glDrawElements call my program crashes. If I remove the glBindBuffer functions then it works, calling the last buffer object I bound everything to. Why is my program crashing when trying to rebind to whatever object I want to draw?