Windows环境下的GLUT下载地址:
http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip
GLUT代表OpenGL应用工具包,英文全称为OpenGL Utility Toolkit,是一个和窗口系统无关的软件包,解压后,glut.h,glut.dll,glut32.dll,glut.lib,glut32.lib。
将下载的压缩包解开,将得到5个文件
(1)把glut.h复制到x:\Program Files\Microsoft\Visual Studio 10.0\VC\include\gl文件夹中,如果没有gl这个文件夹则可以自己新建一个。(x是你安装VS的盘符号)
(2)把解压得到的glut.lib和glut32.lib放到静态函数库所在文件夹(即与include并排的lib文件夹下)。
(3)把解压得到的glut.dll和glut32.dll放到操作系统目录下面的system32文件夹内。(典型的位置为:C:\Windows\system32文件夹内(32位系统)或C:\Windows\SysWOW64(64位系统))
(注:如在开发应用程序时用到OpenGL辅助库函数,则还需下载相应动态链接库,包含glaux.dll, glaux.lib, glaux.h,相应步骤同上)
在Visual C++中先右击项目,选择属性,找到连接器标签,最后在输入中的附加依赖库加上 glut32.lib. (opengl32.lib glu32.lib)
在源文件中输入以下代码:
#pragma once
#pragma execution_character_set("utf-8")
#include
#include "stdafx.h"
#include
void Initialize();
void MouseHandler(int button, int state, int x, int y);
void KeyboardHandler(unsigned char key, int x, int y);
void MainMenuHandler(int option);
void Animate();
void Reshape(int width, int height);
void Display();
/****************************************************************************
main()
Setup GLUT and OpenGL, drop into the event loop
*****************************************************************************/
int main(int argc, char **argv)
{
// Setup the basic GLUT stuff
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
// Create the window
glutInitWindowSize(1024, 768);
glutInitWindowPosition(100, 150);
glutCreateWindow("BOGLGP Chapter 1");
Initialize();
// Register the event callback functions
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutMouseFunc(MouseHandler);
glutKeyboardFunc(KeyboardHandler);
glutIdleFunc(Animate);
// At this point, control is relinquished to the GLUT event handler.
// Control is returned as events occur, via the callback functions.
glutMainLoop();
return 0;
} // end main()
/****************************************************************************
Initialize()
One time setup, including creating menus, creating a light, setting the
shading mode and clear color, and loading textures.
*****************************************************************************/
void Initialize()
{
// set up the only meny
int mainMenu;
mainMenu = glutCreateMenu(MainMenuHandler);
glutSetMenu(mainMenu);
glutAddMenuEntry("Exit", 0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glEnable(GL_DEPTH_TEST);
} // end Initialize()
/****************************************************************************
MouseHandler()
Handle mouse events. For this simple demo, just exit on a left click.
*****************************************************************************/
void MouseHandler(int button, int state, int x, int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
{
exit(0);
} break;
default:
break;
}
// force a screen redraw
glutPostRedisplay();
} // end MouseHandler()
/****************************************************************************
KeyboardHandler()
Keyboard handler. Again, we'll just exit when q is pressed.
*****************************************************************************/
void KeyboardHandler(unsigned char key, int x, int y)
{
switch (key)
{
case 'q': // exit
{
exit(0);
} break;
default:
{
} break;
}
glutPostRedisplay();
} // end KeyboardHandler()
/****************************************************************************
MainMenuHandler()
Main menu callback.
*****************************************************************************/
void MainMenuHandler(int option)
{
switch (option)
{
case 0:
{
exit(0);
} break;
default:
break;
}
glutPostRedisplay();
} // end MainMenuHandler()
/****************************************************************************
Animate()
Rotate the cube by 4 degrees and force a redisplay.
*****************************************************************************/
void Animate()
{
glutPostRedisplay();
} // end Animate()
/****************************************************************************
Reshape()
Reset the viewport for window changes
*****************************************************************************/
void Reshape(int width, int height)
{
if (height == 0)
return;
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, width / height, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
} // end Reshape
/****************************************************************************
Display()
Clear and redraw the scene.
*****************************************************************************/
void Display()
{
// set up the camera
glLoadIdentity();
gluLookAt(0.0, 1.0, 6.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);
// clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw a triangle
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(2.0, 2.5, -1.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-3.5, -2.5, -1.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(2.0, -4.0, 0.0);
glEnd();
// draw a polygon
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(-1.0, 2.0, 0.0);
glColor3f(1.0, 1.0, 0.0);
glVertex3f(-3.0, -0.5, 0.0);
glColor3f(0.0, 1.0, 1.0);
glVertex3f(-1.5, -3.0, 0.0);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(1.0, -2.0, 0.0);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(1.0, 1.0, 0.0);
glEnd();
// draw everything and swap the display buffer
glutSwapBuffers();
} // end Display()