Windows7+VS2010 配置OpenGL
1、OpenGL
OpenGL(Open Graphics Library)是指定义了一个跨编程语言、跨平台的编程接口规格的专业的图形程序接口。它用于三维图像(二维的亦可),是一个功能强大,调用方便的底层图形库。OpenGL 是行业领域中最为广泛接纳的 2D/3D 图形 API。
2、下载GLUT
下载网址:https://www.opengl.org/resources/
https://www.opengl.org/resources/libraries/
这里可以下载到GLUT3.7的源代码。
网址:http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip
这里下在的是链接库
3、文件配置
(1) glut.h
把glut.h头文件拷贝到C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include\gl目录下。
(2)glut.dll,glut32.dll
将glut.dll,glut32.dll两个dll拷贝到C:\Windows\System32目录
(3)glut.lib,glut32.lib
把glut.lib和glut32.lib 两个库拷贝到VS2010安装目录下,我的VS2010安装目录为:D:\Program File\VS2010\VC\lib。即将这两个库拷贝到这个目录下。
4、VS2010中配置
(1) 新建一个VC空项目,然后添加一个CPP源文件。
(2)通过属性管理器进行配置。
(3)包含目录与库目录
这里的目录就是之前文件拷贝的文件目录。
到此,已经配置完毕。
5、实例测试
在之前的cpp文件中添加如下代码:
#include<windows.h> #include<glut.h> #include<GLU.h> #include<GL.h> // Initial square position and size GLfloat x = 0.0f; GLfloat y = 0.0f; GLfloat rsize = 25; // Step size in x and y directions // (number of pixels to move each time) GLfloat xstep = 1.0f; GLfloat ystep = 1.0f; // Keep track of windows changing width and height GLfloat windowWidth; GLfloat windowHeight; /// // Called to draw scene void RenderScene(void) { // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT); // Set current drawing color to red // R G B glColor3f(1.0f, 0.0f, 0.0f); // Draw a filled rectangle with current color glRectf(x, y, x + rsize, y - rsize); // Flush drawing commands and swap glutSwapBuffers(); } /// // Called by GLUT library when idle (window not being // resized or moved) void TimerFunction(int value) { // Reverse direction when you reach left or right edge if(x > windowWidth-rsize || x < -windowWidth) xstep = -xstep; // Reverse direction when you reach top or bottom edge if(y > windowHeight || y < -windowHeight + rsize) ystep = -ystep; // Actually move the square x += xstep; y += ystep; // Check bounds. This is in case the window is made // smaller while the rectangle is bouncing and the // rectangle suddenly finds itself outside the new // clipping volume if(x > (windowWidth-rsize + xstep)) x = windowWidth-rsize-1; else if(x < -(windowWidth + xstep)) x = -windowWidth -1; if(y > (windowHeight + ystep)) y = windowHeight-1; else if(y < -(windowHeight - rsize + ystep)) y = -windowHeight + rsize - 1; // Redraw the scene with new coordinates glutPostRedisplay(); glutTimerFunc(33,TimerFunction, 1); } /// // Setup the rendering state void SetupRC(void) { // Set clear color to blue glClearColor(0.0f, 0.0f, 1.0f, 1.0f); } /// // Called by GLUT library when the window has chanaged size void ChangeSize(int w, int h) { GLfloat aspectRatio; // Prevent a divide by zero if(h == 0) h = 1; // Set Viewport to window dimensions glViewport(0, 0, w, h); // Reset coordinate system glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Establish clipping volume (left, right, bottom, top, near, far) aspectRatio = (GLfloat)w / (GLfloat)h; if (w <= h) { windowWidth = 100; windowHeight = 100 / aspectRatio; glOrtho (-100.0, 100.0, -windowHeight, windowHeight, 1.0, -1.0); } else { windowWidth = 100 * aspectRatio; windowHeight = 100; glOrtho (-windowWidth, windowWidth, -100.0, 100.0, 1.0, -1.0); } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /// // Main program entry point int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); glutInitWindowSize(800,600); glutCreateWindow("Bounce"); glutDisplayFunc(RenderScene); glutReshapeFunc(ChangeSize); glutTimerFunc(33, TimerFunction, 1); SetupRC(); glutMainLoop(); return 0; }
运行结果
参考博客:http://www.cnblogs.com/Linkliu/articles/leanC.html