前言

最近想关注一些图形编程方面的知识(主要是想要自己能做点动画甚至小游戏),而且之前也研究过一段时间的白鹭引擎,在了解了一下html的webgl之后,发现还是有必要回归本质,直接去了解一下opengl这个老牌图形标准库,但是本人不是C++工程师,并且接触了一篇教程后,发现想学习opengl还绕不开C++,并且本人是mac用户,导致想搞一个hello world搞了半天,在此写一篇博客记录一下

一. 确认必要软件

首先确保以下几个软件在你的mac上

  1. homebrew
  2. xcode
  3. cmake

关于这几个软件的安装暂时就不细说了, cmake可以用brew装

二. 安装opengl的接口库,glfw, glew

最简单的方法就是用homebrew去安装,没搞过C++的最好都这么做,而不用自己下载源码,再按照合适的系统参数编译,这样反而会出更多问题

$ brew install glew
$ brew install glfw3

安装完毕后,你可以在/usr/local/Cellar下面找到这两个软件

三. 安装GLTools

先到下面的地址把项目下载到本地
https://github.com/HazimGazov/GLTools

$ cd build
$ cmake ..
$ make
$ sudo make install
$ sudo ldconfig

最后一步如果出问题,可以用下面的命令代替

$ sudo update_dyld_shared_cache

四. 配置Custom paths

打开xcode


点开偏好设置

按照图中的配置一一填入

五. 新开一个C++的项目

点开xcode面板,选择Create a new Xcode project

六. 配置header search path和Library search path

找到这两个title,找不到的可以搜索一下,然后填入$(glew_header)等三项,下面的Library search path也是同样,写入$(glew_lib)等三项

七. 安装glad

Glad有一个在线服务

导出后把两个头文件目录(glad和KHR)复制到你的Include文件夹中(即/usr/local/include),并把glad.c文件拖到你的项目中

八. 添加库链接(Link Framworks and Librarys)

找不到的直接command + shift + G 搜索/usr/local/lib/ 下找

九. 运行代码,大功告成

#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); // uncomment this statement to fix compilation on OS X
#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);
}

编译成功后应该能看到一个绿色的框