【问题标题】:Glad failing to initialize很高兴初始化失败
【发布时间】:2021-08-18 22:07:26
【问题描述】:

我遇到了以下代码行总是打印“无法初始化高兴”然后退出程序的问题:

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

我一直使用https://learnopengl.com/ 作为指导,并一直按照入门部分中的步骤进行操作。我正在使用 Visual Studio 编写此代码,我已将glad.c 源文件移动到构建中以使其正常工作,并将头文件添加到我指定glfw 头文件所在的位置,但我无法找到和我有类似问题的人。

注释掉 return -1;行导致访问冲突异常,所以肯定是程序有问题。

这是整个程序,以防我遗漏了其他东西:

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

using namespace std;

void init_glfw();

void framebuffer_size_callback(GLFWwindow*, int, int);

int main(int argc, char **argv)
{
    init_glfw();

    GLFWwindow* window = glfwCreateWindow(800, 600, "Lab3", NULL, NULL);

    if (window == NULL)
    {
        cout << "Failed to create GLFW window" << endl;
        glfwTerminate();
        return -1;
    }


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

    glViewport(0, 0, 800, 600);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);


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

    glfwTerminate();
    return 0;
}

void init_glfw()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}

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

【问题讨论】:

    标签: c++ opengl visual-studio-2015 glfw


    【解决方案1】:

    您从未通过glfwMakeContextCurrent() 将您的总帐上下文设为最新。与其他 GL 窗口框架不同,当 glfwCreateWindow() 成功时,GLFW 不会使 GL 上下文保持当前状态。

    Call glfwMakeContextCurrent() after glfwCreateWindow() succeeds:

    GLFWwindow* window = glfwCreateWindow(800, 600, "Lab3", NULL, NULL);
    if (window == NULL)
    {
        cout << "Failed to create GLFW window" << endl;
        glfwTerminate();
        return -1;
    }
    
    glfwMakeContextCurrent( window );
    
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }
    

    【讨论】:

    • 谢谢,只是在教程中漏掉了一行。
    猜你喜欢
    • 1970-01-01
    • 2016-03-25
    • 2012-11-17
    • 2017-12-10
    • 2018-09-17
    • 1970-01-01
    • 2018-10-13
    • 2014-03-13
    相关资源
    最近更新 更多