1. 程式人生 > >GLFW+GLAD OpenGL Mac開發環境搭建

GLFW+GLAD OpenGL Mac開發環境搭建

前言

  • OpenGL 是什麼?The Industry Standard for High Performance Graphics 這是官方解釋。說白了他就是一套標準介面。對,是介面,並沒有實現具體的程式碼。
  • GLFW 是什麼?基於上面的原因,也就清楚了,GLFW就是一種OpenGL的實現。所以開發OpenGL,就可以使用GLFW
  • GLAD 是什麼?也是由於OpenGL是由各個公司自己實現的方案,所以各個實現的細節不一致。因為OpenGL只是一個標準/規範,具體的實現是由驅動開發商針對特定顯示卡實現的。由於OpenGL驅動版本眾多,它大多數函式的位置都無法在編譯時確定下來,需要在執行時查詢。所以任務就落在了開發者身上,開發者需要在執行時獲取函式地址並將其儲存在一個函式指標中供以後使用。GLAD可以遮蔽平臺之間API的差異。我感覺類似Java的虛擬機器的角色。

GLFW安裝

編譯安裝

  • 從官網下載原始碼包:http://www.glfw.org/download.html 我是直接從github下載的(https://github.com/glfw/glfw)
  • 編譯  
cd glfw-master
cmake . 
# 預設是編譯靜態庫,如果要編譯動態庫則 cmake -DBUILD_SHARED_LIBS=ON .
make
make install

  最後會看到,說明編譯安裝成功。

Install the project...
-- Install configuration: ""
-- Installing: /usr/local/include/GLFW
-- Installing: /usr/local/include/GLFW/glfw3.h
-- Installing: /usr/local/include/GLFW/glfw3native.h
-- Installing: /usr/local/lib/cmake/glfw3/glfw3Config.cmake
-- Installing: /usr/local/lib/cmake/glfw3/glfw3ConfigVersion.cmake
-- Installing: /usr/local/lib/cmake/glfw3/glfw3Targets.cmake
-- Installing: /usr/local/lib/cmake/glfw3/glfw3Targets-noconfig.cmake
-- Installing: /usr/local/lib/pkgconfig/glfw3.pc
-- Installing: /usr/local/lib/libglfw3.a

配置Xcode

在mac上預設開發工具是xcdoe,所以下面展示下xcode的配置。理論上,GLFW的使用跟IDE是無關的,所以其他IDE應該也是可以配置的。

  1. 配置專案的Build Settings:

    • Other Linker Flags 為 -lgfw3

    • Library Search Paths 的 Debug 和 Release 分別加上/usr/local/lib/

    • Header Search Paths 加上 /usr/local/include/

  2. 新增庫,開啟專案的Build Phases

    在 Link Binary With Libraries 中新增:

    • Cocoa

    • OpenGL

    • IOKit

    • CoreVideo

到此GLFW的開發環境已經配置完畢,可以開始OpenGL的開發了。

 

GLAD 配置

線上配置

開啟地址:https://glad.dav1d.de/

選3.3版本,generate就可以。

如上圖,把glad解壓以後,把include裡面的2個資料夾放入/usr/local/include 下面。

//
//  main.m
//  GlfwDemo
//
//  Created by banma-087 on 2019/8/21.
//  Copyright © 2019 Deman. All rights reserved.
//

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

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

int main(int argc, const char * argv[]) {
    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
    
    GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
    if(window == NULL){
        std::cout << "fail to create window" <<std::endl;
        glfwTerminate();
        return -1;
    }
    
    glfwMakeContextCurrent(window);
    
    if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }
    
    glViewport(0,0,800,600);
    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;
}

void framebuffer_size_callback(GLFWwindow* window,int width,int height){
    glViewport(0,0,width,height);
}

void processInput(GLFWwindow* window){
    if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS){
        glfwSetWindowShouldClose(window, true);
    }
}

這個時候,我們通過上述程式碼,生成一個頁面。

效果如下:

至此,整個OpenGL的環境配置完成了。

 

參考:

https://www.glfw.org/docs/latest/quick.html

https://github.com/glfw/glfw

&n