2
\$\begingroup\$

Searching about VAOs and VBOs I read here and here that I can optionally unbind them to avoid accidental modification so I gave it a try. The problem is that unbinding the VBO causes an access violation when calling glDrawElements and I don't understand why am I getting this behavior.

int main(int argc, char *argv[]) {
    if (!glfwInit()) {
        return -1;
    }

    GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", nullptr, nullptr);
    glfwMakeContextCurrent(window);
    glewExperimental = GL_TRUE;

    if (glewInit() != GLEW_OK) {
        return -1;
    }

    GLfloat vertices[] = {
        -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // Top-left
        0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // Top-right
        0.5f, -0.5f, 0.0f, 0.0f, 1.0f, // Bottom-right
        -0.5f, -0.5f, 1.0f, 1.0f, 1.0f  // Bottom-left
    };

    // Create Vertex Array Object
    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    //Vertex Buffer Object
    GLuint vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    // Index Buffer Object
    GLuint elements[] = {
        0, 1, 2,
        2, 3, 0
    };

    GLuint ibo;
    glGenBuffers(1, &ibo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);

    (...) // Shader-related stuff

    // Specify the layout of the vertex data
    GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
    glEnableVertexAttribArray(posAttrib);
    glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 0);

    GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
    glEnableVertexAttribArray(colAttrib);
    glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));

    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    glBindVertexArray(vao);
    while (!glfwWindowShouldClose(window)) {
        glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);    

        if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
            glfwSetWindowShouldClose(window, GL_TRUE);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // Prepare to close the application
    glDeleteProgram(shaderProgram);
    glDeleteShader(fragmentShader);
    glDeleteShader(vertexShader);
    glDeleteBuffers(1, &vbo);
    glDeleteBuffers(1, &vao);
    glDeleteBuffers(1, &eab);

    return 0;
}

I'm getting the following error and it happens only when I unbind the VBO. Unbinding the VAO and the IBO doesn't throw any exception.

Unhandled exception at 0x0282D434 (ig4icd32.dll) in main.exe: 0xC0000005: Access violation reading location 0x00000000.

Is there a problem with the way I did the unbinding?

\$\endgroup\$
  • \$\begingroup\$ How do you create GL context? Which version of OpenGL and which profile (core or compatibility) this context supports? When OpenGL context is being created? \$\endgroup\$ – Shot Jun 22 '16 at 8:42
1
\$\begingroup\$

Before glfwCreateWindow() call these functions:

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

And after glfwMakeContextCurrent(window) call:

printf("OpenGL version: %s\n", glGetString(GL_VERSION)); 
\$\endgroup\$
  • \$\begingroup\$ I tried calling both the glfwWindowHint() statements and the printf() but glGetString(GL_VERSION) printed <null>. Without the glfw hints, it printed "OpenGL version: 2.1.0 - Build 8.15.10.2302" which probably means I don't have support for OpenGL's 3.3 version, right? Could this be the problem here? \$\endgroup\$ – Jean Catanho Jun 22 '16 at 9:43
  • \$\begingroup\$ Yup, since VAOs are core functionality since OpenGL 3.0. Up to this version of OpenGL you have to use ARB_vertex_array_object extension (if your GPU supports it). What GPU you have installed in your PC/laptop? \$\endgroup\$ – Shot Jun 22 '16 at 9:54
  • \$\begingroup\$ It's a Mobile Intel(R) 4 Series Express Chipset Family and, if I searched the right place, it does not support the ARB extension. I'll test it out later on a newer GPU to see if it works. \$\endgroup\$ – Jean Catanho Jun 22 '16 at 10:05
  • \$\begingroup\$ @JeanCatanho Someone comments the linked article stating it is outdated and the driver supports more extensions. Can't verify the claim myself. You should query the extensions list and check what is supported in runtime. Good luck \$\endgroup\$ – Andreas Jun 22 '16 at 16:32
  • \$\begingroup\$ @Andreas thanks for the suggestion, I queried it here and it looks like it's supported and I tested the same code on a GeForce GTX 750 Ti and it worked like a charm, the problem really was due to OpenGL's version. Thank you both. \$\endgroup\$ – Jean Catanho Jun 23 '16 at 5:45

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.