首页 > 代码库 > Xcode环境下OpenGL C++ GLFW开发环境搭建
Xcode环境下OpenGL C++ GLFW开发环境搭建
- 操作系统版本:MacOS Mavericks 10.9.2
- Xcode版本:5.0.1
- OpenGL版本:4.10
- GLFW版本:3.0.4
一、GLFW编译
1、下载并安装cmake(当前最新版本为2.8.12.2)
http://www.cmake.org/cmake/resources/software.html
下载cmake-2.8.12.2-Darwin64-universal.dmg文件安装即可
2、下载GLFW源代码(当前最新版本为3.0.4)
http://www.glfw.org/download.html
3、将GLFW解压到任意目录,例如~/Library/glfw-3.0.4
4、用文本编辑器打开glfw-3.0.4目录下glfw_config.h.in文件,可根据需要修改配置信息
(1)将#cmakedefine _GLFW_COCOA
修改为#cmakedefine _GLFW_COCOA 1
(2)将#cmakedefine _GLFW_NSGL
修改为#cmakedefine _GLFW_NSGL 1
(3)将#cmakedefine _GLFW_NO_DLOAD_WINMM
修改为#cmakedefine _GLFW_NO_DLOAD_WINMM 1
(4)将#cmakedefine _GLFW_USE_OPENGL
修改为#cmakedefine _GLFW_USE_OPENGL 1
5、打开terminal,进入到glfw-3.0.4目录,依次键入以下命令:
cmake .
sudo make install
执行成功后,会打印出以下内容:
-- 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/glfw/glfwConfig.cmake -- Installing: /usr/local/lib/cmake/glfw/glfwConfigVersion.cmake -- Installing: /usr/local/lib/cmake/glfw/glfwTargets.cmake -- Installing: /usr/local/lib/cmake/glfw/glfwTargets-noconfig.cmake -- Installing: /usr/local/lib/pkgconfig/glfw3.pc -- Installing: /usr/local/lib/libglfw3.a
二、建立Xcode工程
1、打开Xcode,新建OSX Command Line Tool工程
2、左侧选中工程
(1)在Build Settings里找到Header Search Paths
添加terminal中输出的头文件路径:/usr/local/include/
(2)在Build Settings里找到Library Search Paths
添加terminal中输出的lib文件路径:/usr/local/lib/
3、在Build Phases中的Link Binary With Libraries中,添加:
(1)IOKit.framework
(2)Cocoa.framework
(3)OpenGL.framework
(4)CoreVideo.framework
(5)libglfw3.a
若不能找到libglfw3.a,可通过Add Other...按钮手动定位该文件,文件在/usr/local/lib/中
三、main文件编写
1、头文件
#define GLFW_INCLUDE_GLCOREARB #include <GLFW/glfw3.h>
#define GLFW_INCLUDE_GLCOREARB可以使得GLFW包含gl3.h头文件,否则为gl.h
2、main函数
1 int main() { 2 if (!glfwInit()) { 3 return -1; 4 } 5 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 6 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 7 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 8 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); 9 GLFWwindow* window = glfwCreateWindow(640, 480, "Hello OpenGL", NULL, NULL); 10 if (!window) { 11 glfwTerminate(); 12 return -1; 13 } 14 15 glfwMakeContextCurrent(window); 16 17 cout << "OpenGL Vendor:" << glGetString(GL_VENDOR) << endl; 18 cout << "OpenGL Renderer: " << glGetString(GL_RENDERER) << endl; 19 cout << "OpenGL Version: " << glGetString(GL_VERSION) << endl; 20 cout << "GLSL Version:" << glGetString(GL_SHADING_LANGUAGE_VERSION) << endl; 21 22 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 23 while (!glfwWindowShouldClose(window)) { 24 glClear(GL_COLOR_BUFFER_BIT); 25 glfwSwapBuffers(window); 26 glfwPollEvents(); 27 } 28 29 glfwTerminate(); 30 return 0; 31 }
一系列glfwWindowHint的调用可以使得GLFW使用系统中高版本的OpenGL,打印出的版本为4.1,如不使用则为2.1
另附控制台输出结果:
OpenGL Vendor:Intel Inc. OpenGL Renderer: Intel Iris OpenGL Engine OpenGL Version: 4.1 INTEL-8.24.11 GLSL Version:4.10