1. 到http://www.xmission.com/~nate/glut.html 下载 glut-3.7.6-bin.zip,按如下方式安装解压出来的文件
glut32.dll ->C:\Windows\System or C:\WinNT\System
glut32.lib -> C:\ProgramFiles\Microsoft Visual Studio 8\VC\PlatformSDK\lib
glut.h -> C:\ProgramFiles\Microsoft Visual Studio 8\VC\PlatformSDK\include\gl
2. glut.h和stdlib.h一起使用会出现这个错误:error C2381: 'exit' :redefinition; __declspec(noreturn) differs
所以我们得修改glut.h,注释掉146行,并加入一条新的语句,改后的代码为:
1 // extern _CRTIMP void __cdecl exit(int);
3. 新建一个Win 32 Console Application工程,添加一个drawcircle.cpp文件,其内容为:
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <math.h>
6 #include <GL/glut.h>
7
8 #define ESC 27
9 #define SPACE 32
10 int Height=400, Width=400;
11 int depth=0, circleRadius=2, cnt=1;
12
13 static floatvdata[4][3] = {
14 {1.0,0.0, 0.0}, {0.0, 1.0, 0.0},
15 {-1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}
16 };
17
18 void normalize(floatv[3]) {
19 float d =sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
20
21 if (d == 0) {
22 printf("zero lengthvector");
23 return;
24 }
25 v[0]/= d; v[1] /= d; v[2] /= d;
26 }
27
28 void drawtriangle(float*v1, float *v2, float *v3)
29 {
30 glBegin(GL_TRIANGLES);
31 glVertex3fv(v1);
32 glVertex3fv(v2);
33 glVertex3fv(v3);
34 glEnd();
35 }
36
37 void subdivideCircle(intradius, float *v1, float *v2, int depth)
38 {
39 float v11[3], v22[3], v00[3] = {0, 0,0}, v12[3];
40 int i;
41
42 if (depth == 0) {
43 glColor3f(v1[0]*v1[0], v1[1]*v1[1],v1[2]*v1[2]);
44
45 for (i=0; i<3; i++) {
46 v11[i] = v1[i]*radius;
47 v22[i] = v2[i]*radius;
48 }
49 drawtriangle(v11, v22, v00);
50 return;
51 }
52 v12[0] = v1[0]+v2[0];
53 v12[1] = v1[1]+v2[1];
54 v12[2] = v1[2]+v2[2];
55 normalize(v12);
56 subdivideCircle(radius,v1, v12, depth - 1);
57 subdivideCircle(radius, v12, v2, depth -1);
58 }
59
60
61 void drawCircle(intcircleRadius)
62 // draw a circle with center at the origin inxy plane
63 {
64 subdivideCircle(circleRadius, vdata[0],vdata[1], depth);
65 subdivideCircle(circleRadius, vdata[1],vdata[2], depth);
66 subdivideCircle(circleRadius, vdata[2],vdata[3], depth);
67 subdivideCircle(circleRadius, vdata[3],vdata[0], depth);
68 }
69
70 void display(void)
71 {
72 if (circleRadius>Width/2 ||circleRadius==1)
73 {
74 cnt=-cnt;
75 depth++;
76 depth = depth % 5;
77 }
78 circleRadius+=cnt;
79 glClear(GL_COLOR_BUFFER_BIT);
80 drawCircle(circleRadius);
81 glutSwapBuffers();
82 }
83
84 static void Reshape(int w, int h)
85 {
86 glClearColor (0.0, 0.0, 0.0, 1.0);
87 glClear(GL_COLOR_BUFFER_BIT);
88 Width= w; Height = h;
89 glViewport (0, 0, Width, Height);
90 glMatrixMode (GL_PROJECTION);
91 glLoadIdentity ();
92 glOrtho(-Width/2, Width/2, -Height/2,Height/2, -1.0, 1.0);
93 }
94
95
96
97 static voidKey(unsigned char key, int x, int y)
98 {
99 switch (key) {
100 case ESC:
101 exit(0);
102 case SPACE:
103 glutIdleFunc(NULL); display();
104 break;
105 default:
106 glutIdleFunc(display);
107 }
108}
109
110int main(int argc, char **argv)
111{
112 glutInit(&argc, argv);
113 glutInitDisplayMode(GLUT_DOUBLE);
114 glutInitWindowSize(Width, Height);
115 glutCreateWindow("Example1.5.circle.c: press SPACE & another key");
116 glutKeyboardFunc(Key);
117 glutReshapeFunc(Reshape);
118 glutDisplayFunc(display);
119 glutIdleFunc(display);
120 glutMainLoop();
121}
4. 工程 --> 右键 --> Properties,打开工程配置对话框,
a. 选择Configuration下拉框 ,选择 AllConfiguration
b. In the left pane, select the linker subtree and then click the Input option. Add the following code to the
Additional Dependencies text in theright pane.
Copy and Paste: opengl32.lib glu32.lib glut32.lib
5. 编译运行,如果成功,可以看到一个三维动画