mac openGL环境搭建

Mac下openGL环境搭建

一.准备工作

1.安装glfw3

brew install glfw3

安装后的文件在usr/local/Cellar

2.配置glad库

在线服务
mac openGL环境搭建_第1张图片
按图片配置后点击GENRATE,跳转链接后下载glad.zip

3.解压后得到include与src文件

4.安装cmake

brew install cmake

二.检查验证配置

1.建立project_openGL工程目录

目录结构如下
mac openGL环境搭建_第2张图片

2.引入头文件等配置

1.将glad.zip解压后include文件夹中的gladKHR复制到工程目录下的include中,src中的glad.c复制到工程目录下的src中。
2.将/usr/local/Cellar/glfw/3.3/include/GLFW文件夹复制到工程目录下的include
3.将/usr/local/lib文件夹中的libglfw3.a复制到工程目录下的lib
4.创建并编写CMakeLists.txt至project_openGL目录下

# 最低版本
cmake_minimum_required (VERSION 2.8)

# 项目信息
project(project_openGL)

include_directories(${CMAKE_SOURCE_DIR}/include)

link_directories(${CMAKE_SOURCE_DIR}/lib)
link_libraries(GLFW3)

list(APPEND CMAKE_CXX_FLAGS "-std=c++11")

if(WIN32)
  set(LIBS glfw3 opengl32 assimp)
elseif(UNIX AND NOT APPLE)
  set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
  find_package(OpenGL REQUIRED)
  add_definitions(${OPENGL_DEFINITIONS})
  find_package(X11 REQUIRED)
  # note that the order is important for setting the libs
  # use pkg-config --libs $(pkg-config --print-requires --print-requires-private glfw3) in a terminal to confirm
  set(LIBS ${GLFW3_LIBRARY} X11 Xrandr Xinerama Xi Xxf86vm Xcursor GL dl pthread ${ASSIMP_LIBRARY})
  set (CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE} -ldl")
elseif(APPLE)
  INCLUDE_DIRECTORIES(/System/Library/Frameworks)
  FIND_LIBRARY(COCOA_LIBRARY Cocoa)
  FIND_LIBRARY(OpenGL_LIBRARY OpenGL)
  FIND_LIBRARY(GLUT_LIBRARY GLUT)
  FIND_LIBRARY(IOKit_LIBRARY IOKit)
  FIND_LIBRARY(CoreVideo_LIBRARY CoreVideo)
  MARK_AS_ADVANCED(COCOA_LIBRARY OpenGL_LIBRARY)
  SET(APPLE_LIBS ${COCOA_LIBRARY} ${IOKit_LIBRARY} ${OpenGL_LIBRARY} ${GLUT_LIBRARY} ${CoreVideo_LIBRARY})
  set(LIBS ${LIBS} ${APPLE_LIBS})
else()
  set(LIBS )
endif(WIN32)

set(LIBS ${LIBS} ${GLFW3})

# glad库
add_library(GLAD "src/glad.c")
set(LIBS ${LIBS} GLAD)

# 指定生成目标
add_executable(main "src/study.cpp")

target_link_libraries(main ${LIBS})

4.创建study.cpp至src中

//示例代码,只是用来测试环境
#include 
#include 

int main()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    GLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        // std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    while (!glfwWindowShouldClose(window))
    {
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}

5.打开终端进入project_openGL目录按顺序输入如下指令:

mkdir build //创建build文件夹
cd build
cmake ..
make

以上步骤执行完后在build中会生成一个main的unix可执行文件。点击即可
mac openGL环境搭建_第3张图片

你可能感兴趣的:(openGL学习)