很高兴无法初始化

时间:2018-02-06 19:25:37

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

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

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

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

注释掉-1; line会导致访问冲突异常,因此程序在这里遇到麻烦。

以下是整个程序,以防我缺少其他内容:

#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);
}

1 个答案:

答案 0 :(得分:5)

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

Call glfwMakeContextCurrent() after glfwCreateWindow() succeeds

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;
}
相关问题