为什么函数 glViewport () 会给我错误?

Why does the function glViewport () give me errors?

我尝试使用 GLFW 进行 OpenGL。我开始做 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);
}

我收到一些错误提示 Exception thrown at 0x00000000 in OpenGLTest0.exe: 0xC0000005: Access violation executing location 0x00000000. 而我创建的 window 甚至没有响应任何内容,包括任务管理器 "end task. I was using Visual Studio 2017 and had to "结束任务”整个应用程序以删除window。我也在 Windows 10.

有人能告诉我哪里错了吗?我先谢谢你了!

您需要初始化 glad 库,否则它无法加载函数指针。

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