Mac使用Xcode配置openGL
博主這學期有圖形學課要用到OpenGL,於是首先就開始配置開發環境了。應該說網上Windows上配置OpenGL教程比較多,Mac版的比較少。博主特來分享配置過程。
介紹
OpenGL(Open Graphics Library)是指定義了一個跨編程語言、跨平台的編程接口規格的專業的圖形程序接口。它用於三維圖像(二維的亦可),是一個功能強大,與硬件無關,調用方便的底層圖形庫。
在編程的時候,一般會采用基於OpenGL封裝的幾個庫,它們提供了OpenGL本身沒有的功能。
很過教程都是基於GLUT的,但Xcode上會顯示deprecate的warning,主要因為GLUT從1998年不再更新了,使用也有一定隱患。
現在使用的一般為GLEW,GLFW,FreeGLUT(兼容GLUT)。
本文介紹GLEW和GLFW(二者可以組合使用)的配置過程,要配置FreeGLUT也完全類似。
配置過程
安裝homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安裝GLEW和GLFW
brew install glew
brew install glfw
brew安裝的目錄在/usr/local/Cellar下,后面會使用到路徑。
新建一個Xcode Command Line C++項目
- 修改Build Settings的Header Search Path和Library Search Path,分別添加兩個庫的頭文件和庫文件路徑(請自行忽略里面的freeglut,因為博主也配好了,與步驟無關)


- 在Build Phases里面添加庫文件

- 在使用時只要把頭文件包含進來就可以了
#include <GL/glew.h>
#include <GLFW/glfw3.h>
測試
由於博主也剛開始學,於是只能從網上找了一段代碼來測試環境。
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", 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 */
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}

顯示了一個標題為hello world的黑框,0 warnings, 0 errors。
配置成功!!!
