1. 程式人生 > >OpenGL學習之旅01—Xcode+OpenGL環境配置

OpenGL學習之旅01—Xcode+OpenGL環境配置

2.7 ica front git settings set 卸載 row bsp

1、下載和安裝HomeBrew

打開terminal,輸入下面的命令:

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

可能在下載的過程中遇到如下問題,不要慌,可能是網絡問題導致的,重新執行一遍安裝命令即可

技術分享圖片 然後就是一路enter即可,最後看到successful就搞定了 技術分享圖片 這裏解釋下為何要安裝HomeBrew,我們都知道macOS是基於unix的,但是這套從unix衍生出來的計算機系統很多unix底層的工具都是缺失的,所以這就有了homebrew,官方解釋可以以最簡單,最靈活的方式來安裝macOS中不包含的UNIX工具,安裝和卸載的方式也很簡單,只需要在terminal中輸入下面的兩條代碼即可:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

//常用命令:
//安裝軟件,如:brew install oclint
//卸載軟件,如:brew uninstall oclint
//搜索軟件,如:brew search oclint
//更新軟件,如:brew upgrade oclint
//查看安裝列表, 如:brew list
//更新Homebrew,如:brew update
10 1
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
3




4

//常用命令:
5
//安裝軟件,如:brew install oclint
6
//卸載軟件,如:brew uninstall oclint
7
//搜索軟件,如:brew search oclint
8
//更新軟件,如:brew upgrade oclint
9
//查看安裝列表, 如:brew list
10
//更新Homebrew,如:brew update
2、安裝GLEW和GLFW

很過教程都是基於GLUT的,但Xcode上會顯示deprecate的warning,主要因為GLUT從1998年不再更新了,使用也有一定隱患。現在使用的一般為GLEW,GLFW,FreeGLUT(兼容GLUT),在terminal中輸入

brew install glew
brew install glfw
brew install freeglut
3 1
brew install glew
2
brew install glfw
3
brew install freeglut
技術分享圖片 筆者在安裝最後一個的時候遇到了一個小問題
freeglut: XQuartz 2.7.11 (or newer) is required to install this formula. X11Requirement unsatisfied!
You can install with Homebrew-Cask:
 brew cask install xquartz
You can download from:
 https://xquartz.macosforge.org
Error: An unsatisfied requirement failed this build.
6 1
freeglut: XQuartz 2.7.11 (or newer) is required to install this formula. X11Requirement unsatisfied!
2
You can install with Homebrew-Cask:
3
 brew cask install xquartz
4
You can download from:
5
 https://xquartz.macosforge.org
6
Error: An unsatisfied requirement failed this build.
提示需要安裝XQuartz,這個很簡單,直接去XQuartz的官網下載安裝一下就好了,安裝完XQuartz後繼續執行最後freeglut安裝完畢 3、Xcode配置 新建一個command line tool工程 技術分享圖片 在Build Setting中設置好Header Search Paths和Library Search Paths如下: 技術分享圖片 技術分享圖片 技術分享圖片 然後在Build Phases裏面添加庫文件 技術分享圖片 4、編寫代碼測試 從glfw的官網找了一個基本的代碼框架,然後添加了一段渲染三角形的代碼,主要功能就是創建一個glwindow,然後在裏面繪制一個三角形,代碼如下:
#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

int main(int argc, const char * argv[]) {
    GLFWwindow *window;
    /* Initialize the library */
    if (!glfwInit())
        return -1;
    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello OpenGL", NULL, NULL);
    if (!window) {
        glfwTerminate();
        return -1;
    }
    /* Make the window‘s context current */
    glfwMakeContextCurrent(window);
    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window)) {
        /* Render here */
        glBegin(GL_TRIANGLES);
        
        glColor3f(1.0, 0.0, 0.0);    // Red
        glVertex3f(0.0, 1.0, 0.0);
        
        glColor3f(0.0, 1.0, 0.0);    // Green
        glVertex3f(-1.0, -1.0, 0.0);
        
        glColor3f(0.0, 0.0, 1.0);    // Blue
        glVertex3f(1.0, -1.0, 0.0);
        
        glEnd();
        /* Swap front and back buffers */
        glfwSwapBuffers(window);
        /* Poll for and process events */
        glfwPollEvents();
    }
    glfwTerminate();
  
    return 0;
}
x
1
#include <stdio.h>
2
#include <GL/glew.h>
3
#include <GLFW/glfw3.h>
4




5

int main(int argc, const char * argv[]) {
6
    GLFWwindow *window;
7
    /* Initialize the library */
8
    if (!glfwInit())
9
        return -1;
10
    /* Create a windowed mode window and its OpenGL context */
11
    window = glfwCreateWindow(640, 480, "Hello OpenGL", NULL, NULL);
12
    if (!window) {
13
        glfwTerminate();
14
        return -1;
15
    }
16
    /* Make the window‘s context current */
17
    glfwMakeContextCurrent(window);
18
    /* Loop until the user closes the window */
19
    while (!glfwWindowShouldClose(window)) {
20
        /* Render here */
21
        glBegin(GL_TRIANGLES);
22
        
23
        glColor3f(1.0, 0.0, 0.0);    // Red
24
        glVertex3f(0.0, 1.0, 0.0);
25
        
26
        glColor3f(0.0, 1.0, 0.0);    // Green
27
        glVertex3f(-1.0, -1.0, 0.0);
28
        
29
        glColor3f(0.0, 0.0, 1.0);    // Blue
30
        glVertex3f(1.0, -1.0, 0.0);
31
        
32
        glEnd();
33
        /* Swap front and back buffers */
34
        glfwSwapBuffers(window);
35
        /* Poll for and process events */
36
        glfwPollEvents();
37
    }
38
    glfwTerminate();
39
  
40
    return 0;
41
}
42







 
 




然後build,然後就傻眼了有20個編譯錯誤,仔細看error貌似是庫的鏈接問題,提示找不到定義
/usr/local/Cellar/glew/2.1.0/include/GL/glew.h:19860:17: Missing ‘#include "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/OpenGL.framework/Headers/gl3.h"‘; declaration of ‘PFNGLCOPYTEXSUBIMAGE3DPROC‘ must be imported from module ‘OpenGL.GL3‘ before it is required
技術分享圖片
然後就百度找相關的資料,果然找到了類似的問題,解決辦法就是需要將Build Settings裏的Enable Modules(C and Objective-C)設為No即可,具體原因不詳,筆者猜測可能與OpenGL對C的支持有關系,嘗試設置後,果然可以編譯通過了
技術分享圖片
至此,在Xcode下的OpenGL開發環境就配置好了,接下來就可以愉快的學習OpenGL了
 
 
 
 
 
 
 
 

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">



來自為知筆記(Wiz)



OpenGL學習之旅01—Xcode+OpenGL環境配置