c++ - 很高兴未能初始化

标签 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 header 所在的同一位置,但我无法找到和我有类似问题的人。

注释掉 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);
}

最佳答案

您从未通过 glfwMakeContextCurrent() 将 GL 上下文设为最新.与其他 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;
}

关于c++ - 很高兴未能初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48650497/

相关文章:

c++ - 如何在函数内部定义仿函数

java - 如何在 Windows 上提取包含多字节字符和禁止字符的 tar.gz

opengl - OpenGL中的顶点限制

visual-studio-2015 - 使用 VS2015 工具(预览版)安装 .NET Core 1.0 后,我无法创建 .NET Core 类库项目

asp.net-mvc - VS 2015错误列表不跳转到MVC View

c++ - 二分搜索插入C++中的段错误

c++ - Mac OS X 上的高性能代码

c++ - OpenGl 更改坐标系的度量单位

C++、OpenGL 裁剪

c++ - 从 Visual Studio 2013 转换到 Visual Studio 2015 的问题