Search code examples
openglglfwglew

glBindBuffer : Buffer name does not refer to an buffer object generated by OpenGL


After switching from SFML to GLFW for window management, trying to bind my vbo leads to OpenGL error GL_INVALID_OPERATION (1282) with detail "Buffer name does not refer to an buffer object generated by OpenGL".

I manually checked my vbo and it seems to be assign a correct value.

Here is the working example I can produce, using glew-2.1.0 and glfw-3.3.0.


    if (!glfwInit())
    {
        return EXIT_FAILURE;
    }
    std::cout << glfwGetVersionString() << std::endl;
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
    glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
    auto window = glfwCreateWindow(g_width, g_height, "An Other Engine", nullptr, nullptr);
    if (window == nullptr)
    {
        return EXIT_FAILURE;
    }
    glfwMakeContextCurrent(window);
    if (glewInit() != GLEW_OK)
    {
        return EXIT_FAILURE;
    }
    GLint flags;
    glGetIntegerv(GL_CONTEXT_FLAGS, &flags);
    if (flags & GL_CONTEXT_FLAG_DEBUG_BIT)
    {
        glEnable(GL_DEBUG_OUTPUT);
        glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
        glDebugMessageCallback(glDebugOutput, nullptr);
        glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, GL_TRUE);
    }

    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    GLuint vbo;
    glGenVertexArrays(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);

Solution

  • In a core profile OpenGL Context, the buffer object (name) value has to be generated (reserved) by glGenBuffers. This is not necessary in a compatibility profile context.

    You wrongly tried to generate the buffer name by glGenVertexArrays rather than glGenBuffers.

    glGenVertexArrays(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    

    That causes an INVALID_OPERATION error when you try to generate the buffer object by glBindBuffer.

    Use glGenBuffers to solve the issue:

    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    

    Note, you do not specify the profile type (glfwWindowHint(GLFW_OPENGL_PROFILE, ...)), by default the profile type is GLFW_OPENGL_ANY_PROFILE and not specified.