1:配置
1.下载glut压缩包,下面给出链接地址:http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip;
2.下载完毕后,我们解压缩 ,可以看到有5个文件,2个.lib文件,2个.dll文件,一个.h文件。
3.将glut.h复制vs2013的VC文件夹下:C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC,如果在incluce目录下没有GL文件夹,要自己创建一个。
4.将得到的glut.lib和glut32.lib复制到vs2013的lib文件夹下:vs2013\VC\lib。
5.将glut.dll,glut32.dll复制到到操作系统目录下面的C:\Windows\SysWOW64或C:\Windows\system32文件夹内。因为我的电脑是64位的所以放到了SysWOW64中,大家也可以都复制过去。
以上就完成了glut包的配置了,然后打开vs2013新建一个Win32应用程序就可以啦,在头文件中加入#include "GL\glut.h"就可以写代码了
2.测试代码
#include <GL\glut.h>
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRectf(-0.5f, -0.5f, 0.5f, 0.5f); //画矩形
glFlush();
}
int main(int argc, char *argv[])
{
//初始化
glutInit(&argc, argv);
//显示模式
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
//窗口位置
glutInitWindowPosition(100, 100);
//窗口尺寸
glutInitWindowSize(400, 400);
//创建窗口,窗口标题为“FirstOpenGLProgram”
glutCreateWindow("FirstOpenGLProgram");
//绘制回掉函数
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
3、简单代码分析
好了,基本步骤已经完成,接下来我们要阅读简单的代码并对OpenGL进行理解了:
我们要用opengl画一个cube,创建cube.cpp,代码如下
#include <windows.h>
#include <GL/glut.h>
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Anything you want to display on the screen, draw it here. * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Clear the screen. Set the current color to white.
* Draw the wire frame cube.
*/
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glLoadIdentity(); /* clear the matrix */
//--------------- you can define your own transformation-----------
glTranslatef(0.0, 0.0, -5.0); /* viewing transformation */
glScalef(1.0, 2.0, 1.0); /* modeling transformation */
//---------------- you can draw something else here-----------
glutWireCube(1.0); /* draw the cube */
glFlush();
}
/* Called when the window is first opened and whenever
* the window is reconfigured (moved or resized).
*/
void reshape(int w, int h)
{
/* define the projection */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode(GL_MODELVIEW); /* back to modelview matrix */
glViewport(0, 0, w, h); /* define the viewport */
}
/* Main Loop
* Open window with initial window size, title bar,
* RGBA display mode, and handle input events.
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//define size and the relative positon of the applicaiton window on the display
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
//init the defined window with "argv[1]" as topic showed on the top the window
glutCreateWindow(argv[0]);
// opengl setup
init();
//define callbacks
glutDisplayFunc(display);
glutReshapeFunc(reshape);
//enter the loop for display
glutMainLoop();
return 0;
}
运行结果: