使用GLFW和GLAD时如何修复“访问冲突执行位置”

问题描述 投票:0回答:1

我在运行程序时发现了这个异常:

Exception thrown at 0x0000000000000000 in OpenGL project.exe: 0xC0000005: Access violation executing location 0x0000000000000000.

这是我的代码:

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

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(800, 800, "Win", NULL, NULL) };

    if (window == NULL)
    {
        std::cout << "Doesn't work";
        glfwTerminate();
        return -1;
    }

    gladLoadGL();
    glfwMakeContextCurrent(window);

    glViewport(0, 0, 800, 800);

    glClearColor(50.0f, 1.3f, 1.7f, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glfwSwapBuffers(window);

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

    glfwTerminate();
    return 0;
}

当我尝试添加颜色时代码停止工作

这是我正在使用的教程的链接:https://youtu.be/z03LXhRBLGI?list=PLPaoO-vpZnumdcb4tZc4x5Q-v7CkrQ6M-

我不知道异常指的是什么

我尝试移动一些东西,例如将此块放入 while 语句中

    glViewport(0, 0, 800, 800);

    glClearColor(50.0f, 1.3f, 1.7f, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glfwSwapBuffers(window);
c++ opengl graphics glfw glad
1个回答
0
投票

OpenGL Context 是必需的,并且必须是当前上下文才能加载 OpenGL API。因此

glfwMakeContextCurrent
必须在
gladLoadGL
之前调用:

glfwMakeContextCurrent(window);
gladLoadGL();
© www.soinside.com 2019 - 2024. All rights reserved.