为了使作图部分更简单,从而更好地理解曲线、曲面的理论,所以使用了OpenGL的GLUT工具来实现。
从OpenGL网站http://www.opengl.org下载GLUT,下载后有以下几个文件且需要手动安装:
glut.h ——— glut头文件;
glut32.lib—— glut静态库;
glut32.dll—— glut动态库;
安装分为以下几步:
1. 将glut.h放到编译器默认的包含文件夹中,如 ...\include\GL, gl.h和glu.h应该就在那个文件夹内;
2. 将glut32.lib放到编译器默认的静态库中,如 ...\Lib\;
3. 最后将glut32.dll放在操作系统的System32文件夹中: 如C:\Windows\System32;
安装完GLUT库后,若要在Visual C++中使用,需要做以下设置:
在菜单Project->Settings中,或按快捷键Alt+F7出现Project Settings对话框,在Link选项中,在如图A1所示位置添加上要使用的OpenGL库:
opengl32.lib glu32.lib glut32.lib
图A1. 添加GLUT库
一个简单的GLUT示例程序的源程序如下所示:
1 // An Example OpenGL Program
2
3 #include <gl\glut.h>
4
5 void Initialize(void);
6 void DrawScene(void);
7
8 void main(int argc, char* argv[]) {
9 glutInit(&argc, argv); // Initialize GLUT
10 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); // Set display mode
11 glutInitWindowPosition(50,100); // Set top-left display window position
12 glutInitWindowSize(400, 300); // set display window width and height
13 glutCreateWindow("An Example OpenGL Program"); // Create display window
14
15 Initialize(); // Execute initialization procedure
16 glutDisplayFunc(DrawScene); // Send graphics to display window
17
18 glutMainLoop(); // Display everything and wait
19 }
20
21 /*
22 */
23 void Initialize(void) {
24 //glClearColor(1.0, 1.0, 1.0, 0.0); // Set Display-window color to white
25 glMatrixMode(GL_PROJECTION); // Set projection parameters
26 glLoadIdentity();
27 gluOrtho2D(0.0, 200, 0.0, 150); //
28 } // Initialize
29
30 /*
31 */
32 void DrawScene(void) {
33 glClear(GL_COLOR_BUFFER_BIT); // Clear display window
34
35 glColor3f(1.0, 0.0, 0.0f); // set line segment geometry color to red
36 glBegin(GL_LINES);
37 glVertex2i(0, 0);
38 glVertex2i(190, 140);
39 glEnd();
40
41 glFlush(); // Process all OpenGL routines as quickly possible
42 } // DrawScene
43
2
3 #include <gl\glut.h>
4
5 void Initialize(void);
6 void DrawScene(void);
7
8 void main(int argc, char* argv[]) {
9 glutInit(&argc, argv); // Initialize GLUT
10 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); // Set display mode
11 glutInitWindowPosition(50,100); // Set top-left display window position
12 glutInitWindowSize(400, 300); // set display window width and height
13 glutCreateWindow("An Example OpenGL Program"); // Create display window
14
15 Initialize(); // Execute initialization procedure
16 glutDisplayFunc(DrawScene); // Send graphics to display window
17
18 glutMainLoop(); // Display everything and wait
19 }
20
21 /*
22 */
23 void Initialize(void) {
24 //glClearColor(1.0, 1.0, 1.0, 0.0); // Set Display-window color to white
25 glMatrixMode(GL_PROJECTION); // Set projection parameters
26 glLoadIdentity();
27 gluOrtho2D(0.0, 200, 0.0, 150); //
28 } // Initialize
29
30 /*
31 */
32 void DrawScene(void) {
33 glClear(GL_COLOR_BUFFER_BIT); // Clear display window
34
35 glColor3f(1.0, 0.0, 0.0f); // set line segment geometry color to red
36 glBegin(GL_LINES);
37 glVertex2i(0, 0);
38 glVertex2i(190, 140);
39 glEnd();
40
41 glFlush(); // Process all OpenGL routines as quickly possible
42 } // DrawScene
43