Search code examples
c++graphicswindowresponseglfw

Why does the function glViewport () give me errors?


I tried doing OpenGL using GLFW. I was beginning to make a window

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>

int main() {
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window = glfwCreateWindow(500, 500, "Test", NULL, NULL);

    if (window == NULL) {
        std::cout << "GLAD failed.";
    }

    glfwMakeContextCurrent(window);
    void frameBufferSizeCallback(GLFWwindow* window,int width,int height);
    glfwSetFramebufferSizeCallback(window, frameBufferSizeCallback);
    glViewport(0, 0, 500, 500);//Error here

    while (!glfwWindowShouldClose(window)) {
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

void frameBufferSizeCallback(GLFWwindow* window,int width,int height) {
    glViewport(0,0,width,height);
}

I get some error saying Exception thrown at 0x00000000 in OpenGLTest0.exe: 0xC0000005: Access violation executing location 0x00000000. And the window I created doesn't even respond to anything including Task Manager "end task. I was using Visual Studio 2017 and had to "end task" the whole aplication in order to remove the window. Also I am on Windows 10.

Could somebody tell me where I am wrong? I thank you in advance!


Solution

  • You need to initalize the glad library, without that it can't load in the function pointers.

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }