CodeXProd
CodeXProd

Reputation: 29

0xC0000005: Access violation reading location 0x00000000

I have tried multiple times restarting visual studio compiling it and everything but the error doesn't seem to be going away. But it started when i created the temporarily shaders and the program, if that helps.

Here is the code

#include <iostream>
#include <string>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

std::string fatalError(std::string error)
{
    std::cout << error << std::endl;
    return error;
}

int main()
{
    int screenWidth = 1024;
    int screenHeight = 726;

    if (!glfwInit())
    {
        fatalError("GLFW failed to be initialized");
        glfwTerminate();
        return -1;
    }

    GLFWwindow *window;
    window = glfwCreateWindow(screenWidth, screenHeight, "Graphics", NULL, NULL);

    if (window == nullptr)
    {
        fatalError("Window failed to be created");
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);
    glewExperimental = GL_TRUE;

    if (glewInit() != GLEW_OK)
    {
        fatalError("GLEW failed to be initialized");
        glfwTerminate();
        return -1;
    }

    GLfloat vertexData[] =
    {
        0.0f, 1.0f, 0.0f,
        1.0f, -1.0f, 0.0f,
        -1.0f, -1.0f, 0.0f
    };

    GLuint vboID = 0;
    glGenBuffers(1, &vboID);
    glBindBuffer(GL_ARRAY_BUFFER, vboID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);

    GLuint vaoID = 0;
    glGenVertexArrays(1, &vaoID);
    glBindVertexArray(vaoID);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_ARRAY_BUFFER, GL_FALSE, 0, NULL);

    const GLchar* vertexShader[] =
    {
        "#version 410 \n"

        "in vec3 vertexPos \n"

        "void main() \n"
        "{ \n"
        "gl_Position = vec4(vertexPos, 1); \n"
        "} \n"
    };

    const GLchar* fragmentShader[] =
    {
        "#version 410 \n"

        "out vec4 vertexColor \n"

        "void main() \n"
        "{ \n"
        "vertexColor = vec4(0.0, 1.0, 0.0, 1.0); \n"
        "} \n"
    };

    GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertShader, 1, vertexShader, NULL);
    glCompileShader(vertShader);

    GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragShader, 1, fragmentShader, NULL);
    glCompileShader(fragShader);

    GLuint program = glCreateProgram();
    glAttachShader(program, vertShader);
    glAttachShader(program, fragShader);
    glLinkProgram(program);

    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glUseProgram(program);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();

    return 0;
}

Upvotes: 1

Views: 3145

Answers (1)

Jennifer Wilcox
Jennifer Wilcox

Reputation: 343

Your program has 3 issues that I could find (before I got a green triangle to show up for me anyways):

  • glVertexAttribPointer expects a type for the data, not what type of buffer it should be in. Replacing GL_ARRAY_BUFFER with GL_FLOAT gets you further into the program.
  • Both of the shaders have a typo. The in and out paramters should have semicolons after them.

In the future when dealing with GL you should try doing some error checking with glGetError and for shaders glGetShaderInfoLog (there's a tutorial for that here). In the case of your code, glGetError returned GL_INVALID_ENUM after the glVertexAttribePointer, which is a good hint that your arugments are incorrect. glGetShaderInfoLog (plus the other support code) printed out where the shaders failed to compile.

As someone else mentioned, you'll also want to become familiar with a debugger (though it wouldn't have helped much in this case). There are also some good debuggers for GL specific issues like these (I've had good experiences with GPU PerfStudio on AMD cards).

Upvotes: 3

Related Questions