很高兴无法初始化

foo*_*oop 5 c++ opengl glfw visual-studio-2015

我遇到一个问题,其中以下几行代码始终显示“ Failed to initialize初始化glad”,然后退出程序:

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

我一直在使用https://learnopengl.com/作为指南,并一直遵循入门部分中的步骤。我正在使用Visual Studio编写此文件,已将glad.c源文件移至内部版本以使其正常工作,并将头文件添加到我指定的glfw头文件所在的位置,但我一直无法找到任何与我有类似问题的人。

评论返回-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);
}
Run Code Online (Sandbox Code Playgroud)

gen*_*ult 10

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

成功glfwMakeContextCurrent()后调用glfwCreateWindow()

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

glfwMakeContextCurrent( window );

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;
}
Run Code Online (Sandbox Code Playgroud)