zoukankan      html  css  js  c++  java
  • [环境配置]macOS配置GLAD+GLFW的OpenGL环境(CMake / CLion)

    系统信息

    • macOS Catalina 10.15.7

    • CMake 3.19.0(通过Homebrew安装)

    部署

    • 将依赖部署到系统目录的操作通常需要更高的权限,如果你不想在命令前加sudo,可以选择关闭SIP保护来取得完全的系统目录读写权限

    • 前往 GLFW官网 下载Source Package,手动编译源码的理由是可以用到make install命令部署依赖,方便CMakeLists.txt编写。

    • 前往 GLAD官网,按照下图勾选(gl版本可以选3.3以上),点击Generate按钮

    之后下载生成的压缩包

    • 我个人用一个单独的文件夹存放第三方库,我们将GLFW和GLAD的源码解压到这里:

    • 打开终端,依次输入以下指令进行编译
    1. cd glfw目录

    2. mkdir build

    3. cd build

    4. cmake ..

    5. make -j4

    • 结束后,继续输入sudo make install部署依赖至系统目录。

    使用

    • 在一个空目录新建一个CMakeLists.txt,复制并保存以下内容:
    cmake_minimum_required(VERSION 3.10)
    project(OpenGL)
    
    set(CMAKE_CXX_STANDARD 14)
    #这里GLAD_INCLUDE目录填你自己存放的路径
    set(GLAD_INCLUDE /Users/fijiisland/Documents/Dependencies/glad/include)
    
    find_package(glfw3 REQUIRED)
    
    include_directories(${GLAD_INCLUDE})
    
    add_executable(OpenGL main.cpp glad.c)
    
    target_link_libraries(OpenGL glfw)
    
    • 将glad/src下的glad.c复制到此目录中

    • 新建一个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);
    
            // render
            // ------
            glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
            glClear(GL_COLOR_BUFFER_BIT);
    
            // 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);
    }
    
    • 终端cd进此项目目录,并依次执行:
    1. mkdir build

    2. cd build

    3. cmake ..

    4. make

    5. ./OpenGL

    成功的话,你会看到一个绿色的窗口:

    CLion可以直接打开此目录并运行:

  • 相关阅读:
    gbk学习笔记
    在freebsd下编译nodejs,出现无法找到execinfo.h头文件的错误
    php 截取GBK文档某个位置开始的n个字符
    linux下,phpstorm配置oracle jdk
    gb2312学习笔记
    freebsd下vim默认的vi操作方式太难用,可通过启用vim自带配置文件解决
    freebsd通过ssh远程登陆慢,用户认证时间长解决办法
    php输出全部gb2312编码内的汉字
    visibility:hidden 与 display:none 的区别
    java 实现文件/文件夹复制、删除、移动(二)
  • 原文地址:https://www.cnblogs.com/1Kasshole/p/14057950.html
Copyright © 2011-2022 走看看