微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

与 GLFW 一起很高兴在 Windows 上工作

如何解决与 GLFW 一起很高兴在 Windows 上工作

我想开始一个使用 OpenGL 的小项目。之前用Java做过类似的事情,想把一些代码转成C++。

我开始使用 glfw 安装 msys2

pacman -S mingw-w64-x86_64-glfw

除此之外,我还在线生成一个 glad.cglad.h here,并将其添加到我的项目文件

此外,为了验证我的安装,我下载了一个非常小的示例代码

#include <GLFW/glfw3.h>

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640,480,"Hello World",NULL,NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

这没有编译,所以我手动将导入更改为


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

我使用 Cmake 编译:

cmake_minimum_required(VERSION 3.19)
project(Engine3D)

find_package(glfw3 3.3 required)
find_package(OpenGL required)


set(CMAKE_CXX_STANDARD 17)


add_executable(Engine3D glad.h glad.c main.cpp)


target_link_libraries(Engine3D glfw)
target_link_libraries(Engine3D OpenGL::GL)


问题

所以代码确实使用上面的代码进行了编译,但它立即崩溃了。我将问题追溯到这一行:


        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

注释掉该行后,它不会挂起或崩溃,只会显示黑色背景。

我想知道是什么原因导致我下载的示例代码无法正常工作。

我很高兴得到任何帮助或解释。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。