很高兴未能初始化 [英] Glad failing to initialize

查看:56
本文介绍了很高兴未能初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having a problem where the following lines of code always print "Failed to initialize glad" and then exits the program:

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

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

I have been using https://learnopengl.com/ as a guide and have been following the steps in the getting started section. I am writing this using Visual Studio, I have moved the glad.c source file into the build to get this working and added the header files to the same location where I specified the glfw header would be, but I haven't been able to find anyone with a problem similar to mine.

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

Commenting out return -1; line results in an access violation exception, so it is definitely here that the program is having trouble.

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

Here is the entire program in case there is something else I am missing:

#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 上下文保持当前状态.

You never made your GL context current via glfwMakeContextCurrent(). Unlike other GL windowing frameworks GLFW doesn't leave the GL context current when glfwCreateWindow() succeeds.

glfwCreateWindow 之后调用 glfwMakeContextCurrent()() 成功:

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

这篇关于很高兴未能初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆