GLFW 窗口未在 macOS 上显示?

问题描述 投票:0回答:0

我正在尝试建立一个简单的窗口。

这是我的文件树:

├── CMakeLists.txt
├── build.sh
├── configure.sh
├── external
│   ├── glad
│   │   ├── CMakeLists.txt
│   │   ├── include
│   │   └── src
│   └── glfw
│       ├── CMake
│       ├── CMakeLists.txt
│       ├── CONTRIBUTORS.md
│       ├── LICENSE.md
│       ├── README.md
│       ├── deps
│       ├── docs
│       ├── examples
│       ├── include
│       ├── src
│       └── tests
├── main.cpp
├── out
│   └── build
│       ├── CMakeCache.txt
│       ├── CMakeFiles
│       ├── Makefile
│       ├── OLAS
│       ├── adder
│       ├── cmake_install.cmake
│       └── external
└── run.sh

这是我的顶级 CMakeLists.txt 文件:

cmake_minimum_required(VERSION 3.13.4)

project(OLAS)

add_executable(${PROJECT_NAME} main.cpp)

add_subdirectory(external/glfw)

add_subdirectory(external/glad)

target_include_directories(${PROJECT_NAME} 
    PUBLIC external/glfw/include
    PUBLIC external/glad/include/glad
    PUBLIC external/glad/include/KHR)

target_link_directories(${PROJECT_NAME} 
    PRIVATE external/glfw/src
    PRIVATE external/glad/src)

target_link_libraries(${PROJECT_NAME} 
    glfw
    glad
    )

这是我的 main.cpp:

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <iostream>

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

int main()
{
    // glfw: initialize and configure
    // ------------------------------
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

    // glfw window creation
    // --------------------
    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // glad: load all OpenGL function pointers
    // ---------------------------------------
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }    

    // render loop
    // -----------
    while (!glfwWindowShouldClose(window))
    {
        // input
        // -----
        processInput(window);

        // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
        // -------------------------------------------------------------------------------
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // glfw: terminate, clearing all previously allocated GLFW resources.
    // ------------------------------------------------------------------
    glfwTerminate();
    return 0;
}

// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
    if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    // make sure the viewport matches the new window dimensions; note that width and 
    // height will be significantly larger than specified on retina displays.
    glViewport(0, 0, width, height);
}

我的问题是一切都配置、构建和运行,但是什么也没有发生,也没有弹出窗口。

c++ macos opengl cmake glfw
© www.soinside.com 2019 - 2024. All rights reserved.