opengl环境配置
工欲善其事,必先利其器。学习opengl必须要先搭建好开发环境,首先就从搭建环境开始。
查阅了不少资料,发现glfw是一个不错的选择,跨平台,方便后续扩展,而且也支持原生的api。所以我们选用glfw库来配置opengl开发环境。
下载地址:http://www.glfw.org/download.html
我下载的是 glfw-3.1.1。系统为mac osx 10.10. 如果在win下可以直接下载lib库,vs中配置一下就可以了,在mac下需要自己下载源码编译安装。
1.在glfw-3.1.1/src下找到glfw_config.h.in文件。打开后修改编译参数:
修改为:
1 // Define this to 1 if building GLFW for X11 2 #cmakedefine _GLFW_X11 3 // Define this to 1 if building GLFW for Win32 4 #cmakedefine _GLFW_WIN32 5 // Define this to 1 if building GLFW for Cocoa 6 #cmakedefine _GLFW_COCOA 1 7 // Define this to 1 if building GLFW for Wayland 8 #cmakedefine _GLFW_WAYLAND 9 // Define this to 1 if building GLFW for Mir 10 #cmakedefine _GLFW_MIR 11 12 // Define this to 1 if building GLFW for EGL 13 #cmakedefine _GLFW_EGL 1 14 // Define this to 1 if building GLFW for GLX 15 #cmakedefine _GLFW_GLX 16 // Define this to 1 if building GLFW for WGL 17 #cmakedefine _GLFW_WGL 18 // Define this to 1 if building GLFW for NSGL 19 #cmakedefine _GLFW_NSGL 20 21 // Define this to 1 if building as a shared library / dynamic library / DLL 22 #cmakedefine _GLFW_BUILD_DLL 23 24 // Define this to 1 if glfwSwapInterval should ignore DWM compositing status 25 #cmakedefine _GLFW_USE_DWM_SWAP_INTERVAL 26 // Define this to 1 to force use of high-performance GPU on Optimus systems 27 #cmakedefine _GLFW_USE_OPTIMUS_HPG 28 29 // Define this to 1 if the XInput X11 extension is available 30 #cmakedefine _GLFW_HAS_XINPUT 31 // Define this to 1 if the Xxf86vm X11 extension is available 32 #cmakedefine _GLFW_HAS_XF86VM 33 // Define this to 1 if glXGetProcAddress is available 34 #cmakedefine _GLFW_HAS_GLXGETPROCADDRESS 35 // Define this to 1 if glXGetProcAddressARB is available 36 #cmakedefine _GLFW_HAS_GLXGETPROCADDRESSARB 37 // Define this to 1 if glXGetProcAddressEXT is available 38 #cmakedefine _GLFW_HAS_GLXGETPROCADDRESSEXT 39 // Define this to 1 if dlopen is available 40 #cmakedefine _GLFW_HAS_DLOPEN 41 42 // Define this to 1 if glfwInit should change the current directory 43 #cmakedefine _GLFW_USE_CHDIR 44 // Define this to 1 if glfwCreateWindow should populate the menu bar 45 #cmakedefine _GLFW_USE_MENUBAR 46 // Define this to 1 if windows should use full resolution on Retina displays 47 #cmakedefine _GLFW_USE_RETINA 48 49 // Define this to 1 if using OpenGL as the client library 50 #cmakedefine _GLFW_USE_OPENGL 51 // Define this to 1 if using OpenGL ES 1.1 as the client library 52 #cmakedefine _GLFW_USE_GLESV1 53 // Define this to 1 if using OpenGL ES 2.0 as the client library 54 #cmakedefine _GLFW_USE_GLESV2 1
2.打开终端 cd 到 glfw-3.1.1 到目录,依次输入:
cmake .
sudo make install
安装过程中可能需要输入密码,安装完毕后显示:
-- 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
3.打开xcode创建一个osx命令行项目,在Build Settings中设置好刚编译好的libglfw的安装目录/usr/local/lib/;然后在Build Phases中添加
IOKit.framework,Cocoa.framework,OpenGL.framework,libglfw3.a
4.修改main.cpp为:
// // main.cpp // hello_glfw // // Created by lukey.liu on 15/3/21. // Copyright (c) 2015年 cn.wing. All rights reserved. // #include <GLFW/glfw3.h> int main(int argc, const char * argv[]) { if (!glfwInit()) { return -1; } GLFWwindow* window = glfwCreateWindow(640, 480, "Hello opengl for GLFW", NULL, NULL); if (!window) { glfwTerminate(); return -1; } glfwMakeContextCurrent(window); while (!glfwWindowShouldClose(window)) { 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(); glfwSwapBuffers(window); glfwPollEvents(); } glfwTerminate(); return 0; }
运行就能看到一个名为Hello opengl for GLFW的窗口,显示一个三角形