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

一、下載和安裝HomeBrewgit

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

 

 

 

/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)"
 
 

可能在下載的過程當中遇到以下問題,不要慌,多是網絡問題致使的,從新執行一遍安裝命令便可macos

 

而後就是一路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
 
 
二、安裝GLEW和GLFW

很過教程都是基於GLUT的,但Xcode上會顯示deprecate的warning,主要由於GLUT從1998年再也不更新了,使用也有必定隱患。如今使用的通常爲GLEW,GLFW,FreeGLUT(兼容GLUT),在terminal中輸入ruby

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安裝完畢
 
三、Xcode配置
新建一個command line tool工程
    
在Build Setting中設置好Header Search Paths和Library Search Paths以下:
而後 在Build Phases裏面添加庫文件
四、編寫代碼測試
從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;">網絡



相關文章
相關標籤/搜索