下载Freeglut源码: http://freeglut.sourceforge.net/
vs2015编译源码工程,生成.lib和.dll文件(.h文件就在源码中)
32位版本,文件目录
1、\include\GL中的所有.h文件(包括freeglut.h,glut.h,freeglut_ext.h,freelut_std.h)拷贝到系统相应目录下,如:C:\Program Files(x86)\Microsoft Visual Studio 14.0\VC\include\GL
2、\lib\x86中的freeglut.lib拷贝到:C:\Program Files(x86)\Microsoft Visual Studio 14.0\VC\lib
3、\lib\x86中的freeglut.dll拷贝到:C:\Windows\SysWOW64\(win7无SysWOW64目录,C:\Windows\System32\)目录下
64位版本,文件目录
1、.h文件同32版本目录
2、.lib文件:C:\Program Files(x86)\Microsoft Visual Studio 14.0\VC\lib\amd64
3、.dll文件:C:\Windows\System32\
VS2015,测试代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// OpenGLStarter.cpp : Defines the entry point for the console application. // #include "stdafx.h" //需要包含的头文件 #include <windows.h> //#include <GL/gl.h> //#include <GL/glu.h> #include <GL/freeglut.h> //定义输出窗口的大小 #define WINDOW_HEIGHT 300 #define WINDOW_WIDTH 500 //摄像机离物体的距离 GLfloat G_fDistance = 3.6f; //物体的旋转角度 GLfloat G_fAngle_horizon = 0.0f; GLfloat G_fAngle_vertical = 0.0f; //////////////////////////////////////////////// void myinit(void); void myReshape(GLsizei w, GLsizei h); void display(void); //响应键盘输入, 从而设定物体移近移远以及旋转的回调函数 void processSpecialKeys(int key, int x, int y); void processNormalKeys(unsigned char key,int x,int y); //////////////////////////////////////////////// //主函数 int main(int argc, char* argv[]) { glutInit(&argc, argv); //初始化OPENGL显示方式 glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA); // glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA); //设定OPENGL窗口位置和大小 glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); //打开窗口 glutCreateWindow ("OpenGL"); //调用初始化函数 myinit(); //设定窗口大小变化的回调函数 glutReshapeFunc(myReshape); //设定键盘控制的回调函数 glutSpecialFunc(processSpecialKeys); glutKeyboardFunc(processNormalKeys); //开始OPENGL的循环 glutDisplayFunc(display); glutIdleFunc(display); glutMainLoop(); return 0; } //////////////////////////////////////////////// //用户初始化函数 void myinit(void) { //your initialization code glEnable(GL_DEPTH_TEST); // glEnable(GL_ALPHA_TEST); // glAlphaFunc( GL_GREATER, 0.5f ); // GLfloat a = 0.0f; } //窗口大小变化时的回调函数 void myReshape(GLsizei w, GLsizei h) { //设定视区 glViewport(0, 0, w, h); //设定透视方式 glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30.0); // gluPerspective(60.0, 1.0, 1.0, 30.0); //调整窗口比例时物体会变形 // glFrustum (-1.0, 1.0, -1.0, 1.0, 1.0, 30.0); } //每桢OpenGL都会调用这个函数,用户应该把显示代码放在这个函数中 void display(void) { //设置清除屏幕的颜色,并清除屏幕和深度缓冲 glClearColor(0.0f,0.0f,0.0f,0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // glEnable(GL_ALPHA_TEST); // glAlphaFunc(GL_LESS, 0.5f); //设置成模型矩阵模式 glMatrixMode(GL_MODELVIEW); //载入单位化矩阵 glLoadIdentity(); //坐标中心向Z轴平移-G_fDistance (使坐标中心位于摄像机前方) glTranslatef(0.0, 0.0, -G_fDistance); glRotatef(G_fAngle_horizon, 0.0f, 1.0f, 0.0f); glRotatef(G_fAngle_vertical, 1.0f, 0.0f, 0.0f); //////////////////////////////////////////////// //绘制物体 //画一个正方形面 glColor4f(1.0f, 0.0f, 0.0f, 0.0f); // glColor3ub(255, 0, 255); glBegin(GL_QUADS); glVertex3f (-1.0, -1.0f, 0.0f); glVertex3f (1.0, -1.0f, 0.0f); glVertex3f (1.0, 1.0f, 0.0f); glVertex3f (-1.0, 1.0f, 0.0f); glEnd(); //画一个茶壶 glColor4f(0.0f, 1.0f, 0.0f, 1.0f); glutWireTeapot(1.0); // glutSolidTeapot(1.0); //交换前后缓冲区 glutSwapBuffers(); // glFlush(); } void processSpecialKeys(int key, int x, int y) { switch(key) { case GLUT_KEY_LEFT: G_fAngle_horizon -= 5.0f; break; case GLUT_KEY_RIGHT: G_fAngle_horizon += 5.0f; break; case GLUT_KEY_UP: G_fAngle_vertical -= 5.0f; break; case GLUT_KEY_DOWN: G_fAngle_vertical += 5.0f; break; } // glutPostRedisplay(); } void processNormalKeys(unsigned char key,int x,int y) { switch(key) { case 97: //"a" G_fDistance -= 0.3f; break; case 65: //"A" G_fDistance += 0.3f; break; case 27: //"esc" exit(0); } // glutPostRedisplay(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// stdafx.cpp : source file that includes just the standard includes // OpenGLStarter.pch will be the pre-compiled header // stdafx.obj will contain the pre-compiled type information #include "stdafx.h" // TODO: reference any additional headers you need in STDAFX.H // and not in this file
VS2013编译出现错误:
错误 1 error MSB8020: The build tools for v140 (Platform Toolset = 'v140') cannot be found. To build using the v140 build tools, please install v140 build tools. Alternatively, you may upgrade to the current Visual Studio tools by selecting the Project menu or right-click the solution, and then selecting "Upgrade Solution...". C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.Cpp.Platform.targets 64 5 OpenGLStarter
解决办法:
将 "项目——属性——配置属性——常规——平台工具集” 选项更改为你所用的VS版本平台集的选项即可,我这里用的是VS2013,便将其更改为v120的,v140应为VS2015/2017版本的平台工具集;
下载链接:
http://pan.baidu.com/s/1X_3MJUiLHNtsfEab_GMomg 提取码:k976