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:


bool RenderSystem::Initialize()
        //Send triangle shape data down to GPU
                   triangleVertexBufferID , triangleIndexBufferID);

        //Send square shape data down to GPU
                   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);

        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?


1 Answer 1


Well I figured it out if anyone else has a similar problem in the future. I was passing in my triangle and square vertex and index bufferID's by value instead of by reference in my initializeBuffers() funciton. So temporary ID's where being used and then destroyed at the end of the function, leaving my global triangleBufferID's and squareBufferID's set to a default value of 0.

  • \$\begingroup\$ I've had this happen before, never seen a question here about it though, you can also check our own answer FYI. \$\endgroup\$
    – Krupip
    Dec 6, 2017 at 17:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .