Read access violation on draw calls even though GLEW is initialized properly

1

I am using GLEW and GLFW and I am repeatedly getting this error :

Exception thrown at 0x0819FF06 in GLTest.exe: 0xC0000005: Access violation reading location 0x00000000.

I pretty sure that I have correctly initialized GLEW (which seems to be whats wrong when most people get this error).

bool Game_Window::CreateWindow()
{
    if (glfwInit() != true)
        return false;
    glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    m_window = glfwCreateWindow(m_width, m_height, m_title, NULL, NULL);
    glfwMakeContextCurrent(m_window);

    glewExperimental = true;
    if (glewInit() != GLEW_OK)
        return false;

    glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
    std::cout << glGetString(GL_VERSION) << std::endl;

     return true;
}

Here is the code I'm using to draw it:

#include "Game_Window.h"
#include "Shader.h"

float verticies[] = 
{
    -0.5f,-0.5f,0.0f,
    0.5f,-0.5f,0.0f,
    0.0f,0.5f,0.0f,
};
GLuint indecies[] =
{
    0,1,2,
};

int main()
{
    Game_Window window("Window", 1600, 900);
    if (window.CreateWindow())
    {
        Shader shader("basic.vert", "basic.frag");
        shader.CreateShader();
        shader.Use();

        GLuint VAO, VBO, EBO;
        glGenVertexArrays(1, &VAO);
        glBindVertexArray(VAO);

        glGenBuffers(1, &VBO);
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferData(VBO, sizeof(verticies), verticies, GL_STATIC_DRAW);

        glGenBuffers(1, &EBO);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indecies), indecies, GL_STATIC_DRAW);

        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 
 (void*)0);
        glEnableVertexAttribArray(0);

        while (window.ShouldStayOpen())
        {           
            window.Update();
            window.clear();
            glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
            window.swapBuffers();
        }
    }
    return 0;
}

I am quite confused by this error as it seems also occurs on random glfw functions (but im not sure as I can't get it to happen consistently). I don't get any error untill I add:

  glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
c++
opengl
runtime-error
glfw
glew
asked on Stack Overflow Jun 22, 2018 by Liiht • edited Jun 22, 2018 by genpfault

1 Answer

2

The 1st parameter of glBufferData has to be the target enumeration constant (e.g. GL_ARRAY_BUFFER), not the named buffer object.

Change your code like this to solve the issue:

glBufferData(
    GL_ARRAY_BUFFER, // GL_ARRAY_BUFFER instead of VBO
    sizeof(verticies), verticies, GL_STATIC_DRAW);

Note, if you would check for OpenGL errors (glGetError), then you would get an GL_INVALID_ENUM error in this case.

answered on Stack Overflow Jun 22, 2018 by Rabbid76 • edited Jun 22, 2018 by Rabbid76

User contributions licensed under CC BY-SA 3.0