1
\$\begingroup\$

I have few objects in scene and they exist until I turn off the application. Their calls for glDeleteBuffers are in destructor. Since I don't delete anything in the middle of the game everything is fine. But now I created one helper function for drawing lines with following body:

void DrawLine(glm::vec3 p1, glm::vec3 p2, glm::vec4 color)
{
    GLuint vao, vbo;

    glm::vec3 data[2] = { p1, p2 };

    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, 2 * sizeof(glm::vec3) * 2, data, GL_STATIC_DRAW);

    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glEnableVertexAttribArray(0);

    glBindVertexArray(vao);

    SimpleShader.SetAttribute("World", glm::mat4x4());
    SimpleShader.SetAttribute("UseStaticColor", true);
    SimpleShader.SetAttribute("UseLighting", false);
    SimpleShader.SetAttribute("StaticColor", color);
    glDrawArrays(GL_LINES, 0, 2);
    SimpleShader.SetAttribute("UseStaticColor", false);
    SimpleShader.SetAttribute("UseLighting", true);

    glDeleteBuffers(1, &vbo);
    glDeleteBuffers(1, &vao);
}

But as soon as glDeleteBuffers is called nothing is drawing anymore. If I comment those two lines everything is fine (except a bunch of unreleased object names of course).

Anyone know why is this happening?

\$\endgroup\$
  • 1
    \$\begingroup\$ You are using glDeleteBuffers() on a vertex array object, you should use glDeleteVertexArrays(1, &vao); \$\endgroup\$ – ashleysmithgpu Jul 3 '14 at 12:42
1
\$\begingroup\$

I am just rehashing what @yuumei said, but since he did not post an awnser:

You are creating the vao object with:

 glGenVertexArrays(1, &vao);

As such you need to delete it with:

glDeleteVertexArrays(1, &vao);

Here is the documentation for glGenVertexArrays.

What happens here is that the value in vao is the same value with some other buffer and as such that buffer is being deleted instead. Next time you try to use that buffer, it will not work, since you deleted it.

\$\endgroup\$
  • \$\begingroup\$ Thanks! That was the problem. Didn't realize there was separate function for deleting vao. \$\endgroup\$ – instancedName Jul 3 '14 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.