Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: OpenGL & SFML, crash at exit when using glDrawElements();  (Read 4713 times)

0 Members and 1 Guest are viewing this topic.

bcvz

  • Newbie
  • *
  • Posts: 17
    • View Profile
OpenGL & SFML, crash at exit when using glDrawElements();
« on: May 16, 2014, 09:26:10 pm »
Hello,

I'am learning OpenGL and I am experimenting with glDrawElements, I use SFML for window system.

I've generated a 3d cube VAO using VBO and index buffer object, it renders well and correctly, but when I close the window I have an error message:
"Unhandled exception at 0x69761CB7 (nvoglv32.dll) in Opengl.exe: 0xC0000005: Access violation reading location 0x00000000."

And it shows me this line :
"window.display();"

VAO generation part:
GLuint createCube()
{
        const float vertices[] = {
                0.25f, 0.25f, -1.25f, 1.0f,
                0.25f, -0.25f, -1.25f, 1.0f,
                -0.25f, 0.25f, -1.25f, 1.0f,
                -0.25f, -0.25f, -1.25f, 1.0f,
                0.25f, 0.25f, -2.75f, 1.0f,
                -0.25f, 0.25f, -2.75f, 1.0f,
                0.25f, -0.25f, -2.75f, 1.0f,
                -0.25f, -0.25f, -2.75f, 1.0f,


                0.0f, 0.0f, 1.0f, 1.0f,
                0.8f, 0.8f, 0.8f, 1.0f,
                1.0f, 0.0f, 0.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,
                0.5f, 0.5f, 0.0f, 1.0f,
                0.0f, 1.0f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,
                0.5f, 0.5f, 0.0f, 1.0f,
        };

        const GLshort indexData[]
        {
                        0, 1, 2,
                        1, 3, 2,
                        4, 5, 6,
                        6, 5, 7,
                        2, 3, 7,
                        2, 7, 5,
                        0, 6, 1,
                        0, 4, 6,
                        4, 0, 2,
                        4, 2, 5,
                        6, 3, 1,
                        6, 7, 3,
        };

        GLuint vao;
        glGenVertexArrays(1, &vao);
       
        GLuint vbo;
        glGenBuffers(1, &vbo);

        GLuint indexObjectBuffer;
        glGenBuffers(1, &indexObjectBuffer);

        glBindVertexArray(vao);

        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

       
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexObjectBuffer);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36*4, indexData, GL_STATIC_DRAW);

        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);

        glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0); // vertices
        glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)128); // color

        glBindVertexArray(0);

        return vao;
}

Drawing:
        glUseProgram(program);
        glBindVertexArray(vao);
       
        // (...)
 
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                        {
                                window.close();
                        }
                }
 
               // (...)
 
                glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
                glClear(GL_COLOR_BUFFER_BIT);
 
 
                glUniform3f(offsetUniform, offsetX, offsetY, offsetZ);
 
                glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
 
                window.display();
        }


The entire code:
http://pastebin.com/uhRRbKNr

Thanks,

bcvz

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: OpenGL & SFML, crash at exit when using glDrawElements();
« Reply #1 on: May 16, 2014, 10:42:52 pm »
I have justed tested this code from open.gl:
http://open.gl/content/code/c2_triangle_elements.txt

I had the same issue.
It only happens with glDrawElements not with glDrawArrays.

So the problem does not come from the code but something else :/

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: OpenGL & SFML, crash at exit when using glDrawElements();
« Reply #2 on: May 16, 2014, 11:55:50 pm »
Vertex array objects aren't shared between contexts. You create yours in your window's context, and when you close the window, it's context gets destroyed which means the vertex array object no longer exists. Obviously your driver doesn't like it when you try to draw from a destroyed resource...

Make sure you do not do anything with OpenGL after you close your window and your driver will be happy.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

bcvz

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: OpenGL & SFML, crash at exit when using glDrawElements();
« Reply #3 on: May 17, 2014, 12:06:22 am »
Thanks, it was that.

Now I moved the pollEvent loop at the end of the main loop, so drawing process will always be done BEFORE closing the window.