0

I am trying to write a procedure called CreateShape which has a parameter shapeID as input and then pushes VAO ID (Vertex Array Object ID) into a vector so I can bind and draw the specified shape at any time using the VAO ID. However, when I try to create and draw a shape using nothing CreateShape nothing gets drawn.

However if I write the same code in the core engine class it works.

Note: GLCall() is just for debugging purposes.

Core Engine Main Procedure Using Render Class Procedures: https://gyazo.com/9f9dfce8bfd8fbb68e06d7e2a3c67066

void Engine::Start()
    {
    graphics::Renderer renderer;
    renderer.CreateShape(graphics::Triangle::id);   
    renderer.SetShader("source/graphics/renderer/resource/Default.shader");


    while(!window.IsWindowClosed())
    {
        renderer.DrawTriangle();
        window.Update();
    }
}

Core Engine Main Procedure creating and drawing shape itself:https://gyazo.com/a7b701d16fae76475456192b31190812

    void Engine::Start()
    {
    graphics::Renderer renderer;

    VertexBuffer VBO(Triangle::vertices, sizeof(Triangle::vertices));
    VBO.PushLayout(3, GL_FLOAT);

    VertexArray VAO;
    VAO.Set(VBO);
    VAO.Bind();

    renderer.SetShader("source/graphics/renderer/resources/Default.shader");

    while(!window.IsWindowClosed())
    {
        GLCall(glDrawArrays(GL_TRIANGLES, 0, 3));
        window.Update();
    }
}

Create Shape Procedure:

void Renderer::CreateShape(unsigned int shapeID){ //During this test shapeID is set to be a Triangle

switch (shapeID)
{
case graphics::Triangle::id:
{
    VertexBuffer VBO(Triangle::vertices, sizeof(Triangle::vertices));
    VBO.PushLayout(3, GL_FLOAT);

    VertexArray VAO;
    VAO.Set(VBO);
    VAO.Bind();

    renderObjects.push_back(VAO.GetId());
    break;
}
// More cases included here...
}

Draw Triangle Procedure:

void graphics::Renderer::DrawTriangle()
{
    GLCall(glBindVertexArray(renderObjects.front()));
    GLCall(glDrawArrays(GL_TRIANGLES, 0, 3));
}
Fabmaur
  • 123
  • 2
  • 12
  • Is stores the triangles shapeID, editted it now so it makes more sense – Fabmaur Oct 25 '18 at 11:38
  • Probably the issue is `VertexBuffer VBO(Triangle::vertices, sizeof(Triangle::vertices));` respectively `VertexArray VAO;`. Does the destructor of `VertexArray` destroy the vertex array object (`glDeleteVertexArrays`) and/or does the destructor of `VertexBuffer` destroy the vertex buffer object (`glDeleteBuffers`)? – Rabbid76 Oct 25 '18 at 11:39
  • Yes VertexArray has destructors which calls glDeleteVertexArrays and VertexBuffer has a destructor which calls glDeleteBuffers. – Fabmaur Oct 25 '18 at 11:52
  • I removed the desctructor for both the vertex and buffer array and the program worked! I thought you could delete a vertex buffer once it had been bound to a vertex array but I guess not. – Fabmaur Oct 25 '18 at 12:01

1 Answers1

0

The problem pointed out by Rabbid76, was that once the procedure got out of scope the destructors for the verex buffer and vertex buffer array got called which deleted the buffers causing the screen to be blank as the id's of the vertex buffer and vertex array were now invalid.

Fabmaur
  • 123
  • 2
  • 12