openGL 需要安装glut, glee, glew这三个库,以及一些OpenGL扩展支持。
glut : 提供对窗口的封装,这是跨平台窗口的,我们就不必自己去编写烦琐的窗口代码。
glee : 方便用来判断当前系统是不是支持某项OpenGL特性,我们就不用自己去写烦琐的先取函数地址然后再判断的代码了。
glew : 因为windows默认只支持OpenGL 1.1,你要想用更高版本的OpenGL,你就需要安装它,它能自动识别你的平台所支持的全部OpenGL高级扩展函数。
1.glut下载地址:http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip
或者去https://www.opengl.org/resources/libraries/glut/glut_downloads.php#2自己找,反正我是没找到。
解压,将 glut32.dll 和 glut.dll 拷贝到 c:\windows\system32 下面(64bit系统,应该放在SysWOW64中),将 glut32.lib 和 glut.lib 拷贝到 VC 安装目录下的 lib 目录下(如:\Microsoft Visual Studio 12.0\VC\lib\下),将 glut.h 拷贝到VC安装目录下的 \include\(vs2013没有gl文件夹) 目录下(如:\Microsoft Visual Studio 12.0\VC\include下)。在程序中我们只需要把
#include <GL/gl.h>
#include <GL/glu.h>
用
#include <GL/glut.h>
替换就可以了。因为在头文件 glut.h 中已经包含这些头文件,并导入了必要的库:
#pragma comment (lib, "winmm.lib") /* link with Windows MultiMedia lib */
#pragma comment (lib, "opengl32.lib") /* link with Microsoft OpenGL lib */
#pragma comment (lib, "glu32.lib") /* link with OpenGL Utility lib */
#pragma comment (lib, "glut32.lib") /* link with Win32 GLUT lib */
2.安装 glew
从:http://glew.sourceforge.net/
下载最新的GLEW(支持OpenGL 3.0),解压,将 \bin\glew32.dll 拷贝到 c:\windows\system32 下面(要不要同上面一样目前还不知道),将 \lib\glew32.lib 拷贝到VC安装目录下的 lib 目录下(如:\Microsoft Visual Studio 12.0\VC\lib\下),将 \include\glew.h 和 \include\wglew.h 拷贝到 VC 安装目录下的 \include 目录下(如:\Microsoft Visual Studio 12.0\VC\include下)。在程序中我们只需要在包含gl,glu 或 glut.h 之前包含 glew.h就可以了(注意:一定要先包含 glew.h),在在代码中加上这么一句:
#pragma comment (lib, "glew32.lib")
示例:
#include <GL/glew.h>
#include <GL/glut.h>
#progrma comment(lib, "glew32.lib")
在创建OpenGL渲染context之后,调用 glewInit(); 初始化glew就可以了。
3.安装一些扩展支持
下载这三个文件 glext.h, glxext.h, 和 wglext.h ,放置到 VC 安装目录下的 \include目录下(如:\Microsoft Visual Studio 12.0\VC\include下)。使用的时候直接包含它们就可以了。
附:csdn上传了一份我收集的openGL配置dll,.h和lib文件。
欢迎大家下载:http://download.csdn.net/detail/u011432871/9456029。
测试代码:
1 // gl.cpp : Defines the entry point for the console application. 2 // 3 4 #include <glew.h> 5 #pragma comment (lib, "glew32.lib") 6 7 8 #include<glut.h> 9 #pragma comment (lib, "winmm.lib") 10 #pragma comment (lib, "opengl32.lib") 11 #pragma comment (lib, "glu32.lib") 12 #pragma comment (lib, "glut32.lib") 13 14 //#include<glaux.h> 15 //#pragma comment (lib, "glaux.lib") 16 17 18 int iPointNum = 0; 19 20 int x1 = 0, x2 = 0, y1 = 0, y2 = 0; 21 22 int winWidth = 400, winHeight = 300; 23 24 25 26 void Initial(void) 27 { 28 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); 29 } 30 31 32 33 void ChangeSize(int w, int h) 34 35 { 36 37 winWidth = w; 38 winHeight = h; 39 40 glViewport(0, 0, w, h); 41 glMatrixMode(GL_PROJECTION); 42 glLoadIdentity(); 43 44 gluOrtho2D(0.0, winWidth, 0.0, winHeight); 45 46 } 47 48 49 50 void Display(void) 51 52 { 53 54 glClear(GL_COLOR_BUFFER_BIT); 55 56 glColor3f(1.0f, 0.0f, 0.0f); 57 58 if (iPointNum >= 1) 59 { 60 61 glBegin(GL_LINES); 62 63 glVertex2i(x1, y1); 64 65 glVertex2i(x2, y2); 66 67 glEnd(); 68 69 } 70 71 glutSwapBuffers(); 72 73 } 74 75 76 77 void MousePlot(GLint button, GLint action, GLint xMouse, GLint yMouse) 78 79 { 80 81 if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN) 82 { 83 84 if (iPointNum == 0 || iPointNum == 2){ 85 86 iPointNum = 1; 87 88 x1 = xMouse; 89 y1 = winHeight - yMouse; 90 91 } 92 93 else { 94 95 iPointNum = 2; 96 97 x2 = xMouse; 98 y2 = winHeight - yMouse; 99 100 glutPostRedisplay(); 101 102 } 103 } 104 105 if (button == GLUT_RIGHT_BUTTON && action == GLUT_DOWN){ 106 107 iPointNum = 0; 108 109 glutPostRedisplay(); 110 111 } 112 113 } 114 115 116 117 void PassiveMouseMove(GLint xMouse, GLint yMouse) 118 119 { 120 121 if (iPointNum == 1) 122 { 123 124 x2 = xMouse; 125 126 y2 = winHeight - yMouse; 127 128 glutPostRedisplay(); 129 130 } 131 132 } 133 134 135 136 int main(int argc, char* argv[]) 137 138 { 139 140 glutInit(&argc, argv); 141 142 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 143 glutInitWindowSize(400, 300); 144 145 glutInitWindowPosition(100, 100); 146 147 glutCreateWindow("橡皮筋技术"); 148 149 glutDisplayFunc(Display); 150 151 glutReshapeFunc(ChangeSize); 152 153 glutMouseFunc(MousePlot); 154 155 glutPassiveMotionFunc(PassiveMouseMove); 156 157 Initial(); 158 159 glutMainLoop(); 160 161 return 0; 162 163 } 164 165 void Key(unsigned char key, int x, int y) 166 { 167 switch (key){ 168 case \'p\': 169 if (iPointNum == 0 || iPointNum == 2) { 170 iPointNum = 1; 171 x1 = x; y1 = winHeight - y; 172 } 173 else { 174 iPointNum = 2; 175 x2 = x; y2 = winHeight - y; 176 glutPostRedisplay(); 177 } 178 break; 179 default: break; 180 } 181 }
参考:http://www.cnblogs.com/lzihua/archive/2012/05/11/2495714.html