Mac配置OpenGL开发环境(CMake)



  • 一、安装homebrew

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

    二、安装cmake

    brew install cmake

    三、安装glew和glfw3

    brew install glew
    brew install glfw3
    

    四、下载glad

    https://glad.dav1d.de/
    按照下图选择,其他全部默认,然后点击下面的GENERATE
    0_1534852011197_WX20180821-194552@2x.png
    然后解压glad.zip,将glad目录放到当前工程目录下,修改一下文件目录格式,并在glad目录中添加CMakeLists.txt:

    glad
      |--include
      |    |--glad.h
      |    |--KHR
      |        |--khrplatform.h
      |--glad.c
      |--CMakeLists.txt
    
    

    glad->CMakeLists.txt内容如下:

    set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
    add_library(glad STATIC glad.c)
    target_link_libraries(glad)
    

    五、下载编译gltools

    下载链接:https://github.com/HazimGazov/GLTools

    cd build
    cmake ..
    make
    make install
    

    六、编写CMakeLists.txt

    # 项目名称
    project(demo)
    # 设置CMake最小版本
    cmake_minimum_required(VERSION 2.8)
    set(CMAKE_MACOSX_RPATH 0)
    # 代码路径
    aux_source_directory(. demo_src)
    
    #设置编译器
    set(CMAKE_CXX_COMPILER "g++")
    #设置构建类型,及相应的编译选项
    set(CMAKE_BUILD_TYPE "Debug")
    set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb -m64")
    set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall -m64")
    
    #设置执行文件输出目录
    set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build)
    #设置库输出路径
    set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
    
    #设置glad头文件和子目录,用来生成libglad.a
    set(glad_dir ./glad)
    add_subdirectory(${glad_dir})
    include_directories(${glad_dir})
    
    if(APPLE AND NOT IPHONE)
    set(MACOSX 1)
    endif(APPLE AND NOT IPHONE)
    
    if(MACOSX)
    find_library(carbon_lib Carbon)
    find_library(iokit_lib IOKit)
    find_library(forcefeedback_lib ForceFeedback)
    find_library(cocoa_lib Cocoa)
    find_library(audiounit_lib AudioUnit)
    find_library(corevideo_lib CoreVideo)
    find_library(coreaudio_lib CoreAudio)
    find_library(opengl_lib OpenGL)
    find_library(corefoundation_lib CoreFoundation)
    
    set(frameworks
        ${carbon_lib}
        ${iokit_lib}
        ${forcefeedback_lib}
        ${cocoa_lib}
        ${audiounit_lib}
        ${coreaudio_lib}
        ${corevideo_lib}
        ${opengl_lib}
        ${corefoundation_lib})
    endif(MACOSX)
    
    # 生成可执行的文件
    add_executable(demo ${demo_src})
    target_link_libraries(demo glfw3 gltools glew glad ${frameworks})
    

    六、编写测试代码

    #include "glad/include/glad.h"
    #include <GLFW/glfw3.h>
    #include <iostream>
    
    using namespace std;
    
    void processInput(GLFWwindow *window)
    {
        // 判断按键状态,如果ESC按下则设置WindowShouldClose状态为True
        // 当下次渲染循环执行的时候将会退出程序
        if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
            glfwSetWindowShouldClose(window, true);
    }
    
    void framebuffer_size_callback(GLFWwindow *window, int width, int height)
    {
        // 当glfw窗口大小发现变化的时候需要同时更改GL视图的大小
        glViewport(0, 0, width, height);
    }
    
    int main(int argc, char **argv)
    {
        // 初始化GLFW
        glfwInit(); 
        // 设置GLFW兼容最低的OpenGL版本
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
        // 设置GLFW使用OpenGL的核心模式
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        // Mac OS X需要添加以下这行配置才能生效
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
        // 创建GLFW窗口对象
        GLFWwindow *window = glfwCreateWindow(800, 600, "GhostAsm", NULL, NULL);
        if (window == NULL)
        {
            std::cout << "Failed to create GLFW window" << std::endl;
            glfwTerminate();
            return -1;
        }
        // 通知GLFW将我们窗口的上下文设置为当前线程的主上下文
        glfwMakeContextCurrent(window);
        // 初始化GLAD 并获取OpenGL函数指针
        // 将获取函数指针地址的函数告诉GLAD,然后由GLAD去负责获取OpenGL相关的函数地址
        if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
        {
            std::cout << "Failed to initialize GLAD" << std::endl;
            return -1;
        }
        glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
        while (!glfwWindowShouldClose(window))
        {
            // 输入
            processInput(window);
            // 渲染指令
            glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
            glClear(GL_COLOR_BUFFER_BIT);
            // 检查并调用事件,交换缓冲
            glfwSwapBuffers(window);
            glfwPollEvents();
        }
        // 回收资源
        glfwTerminate();
        return 0;
    }
    

    编译运行结果:
    0_1534858171378_WX20180821-212859@2x.png


    异常排查:

    GLFW/glfw3.h’ file not found(Mac)
    在CMakeLists.txt中添加:

    include_directories(/usr/local/include)
    LINK_DIRECTORIES(/usr/local/lib)
    

    去除Vscode中include波浪线
    进入settings,搜索C_Cpp:Error Squiggles,设置成Disabled


Log in to reply