0

我想用 OpenGL 开始一个小项目。我以前在 Java 中做过类似的事情,想将一些代码转移到 C++ 中。

我开始使用glfw安装msys2

pacman -S mingw-w64-x86_64-glfw

除此之外,我在这里在线生成了一个glad.c并添加到我的项目文件中glad.h

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

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

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

我想知道为什么我下载的示例代码没有按应有的方式工作。

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

4

0 回答 0