[NEHE Couse] 05.3D Objects
在这节课中终于从二维平面拓展到三维平面上来了,程序中绘制了两个3D物体,一个是四面体棱锥,一个是一个正方体,为了看起来效果好些,四棱锥每个顶点的颜色都不一样,然后颜色样式采用GL_SMOOTH来控制,通过键盘控制,按下键'S',可以控制四棱锥绕Y轴旋转,按下键'S',可以控制正方体绕点(0,0,0)和点(1,1,1)的连线所在的直线旋转,Esc键控制退出,空格键控制全屏与否。
此外为了让这两个3D物体看起来不至于变形,我在投影方式上采用函数glOrtho来控制。(投影矩阵应该由你在屏幕中显示物体的大小来控制,这与物体的坐标有直接的关系,同时这也取决于你,如果你想让物体看起来大些,那么投影区域你应该设置稍大于显示物体的包围盒即可,如果你想让物体看起来小些,投影区域应该大大于包围盒)。
程序如下:
1
/*
2
Introduction:Written by krisdy,finished at 2009.1.10
3
Apply to:NEHE Couse 05:3D Objects
4
*/
5
#include <gl/glut.h>
6
#include <stdlib.h>
7![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
#define WinWidth 500 //the width of the window
9
#define WinHeight 500 //the height of the window
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
static GLint t;
12
static GLfloat spin1,spin2; //control the rotate range
13![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
void init(void)
15
{
16
glShadeModel(GL_SMOOTH);
17
glClearColor(0.0f,0.0f,0.0f,0.0f);
18
glClearDepth(1.0f);
19
glEnable(GL_DEPTH_TEST);
20
}
21
void display(void)
22
{
23
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
24
glLoadIdentity();
25
glPushMatrix();
26
glTranslatef(-1.5f,0.0f,-6.0f);
27
glRotatef(spin1,0.0f,1.0f,0.0f); //revolve round y-axis
28
glBegin(GL_TRIANGLES);
29
glColor3f(1.0f,0.0f,0.0f);
30
glVertex3f(0.0f,1.0f,0.0f);
31
glColor3f(0.0f,0.0f,1.0f);
32
glVertex3f(1.0f,-1.0f,1.0f);
33
glColor3f(0.0f,1.0f,0.0f);
34
glVertex3f(1.0f,-1.0f,-1.0f);
35
glColor3f(1.0f,0.0f,0.0f);
36
glVertex3f(0.0f,1.0f,0.0f);
37
glColor3f(0.0f,1.0f,0.0f);
38
glVertex3f(1.0f,-1.0f,-1.0f);
39
glColor3f(0.0f,0.0f,1.0f);
40
glVertex3f(-1.0f,-1.0f,-1.0f);
41
glColor3f(1.0f,0.0f,0.0f);
42
glVertex3f(0.0f,1.0f,0.0f);
43
glColor3f(0.0f,0.0f,1.0f);
44
glVertex3f(-1.0f,-1.0f,-1.0f);
45
glColor3f(0.0f,1.0f,0.0f);
46
glVertex3f(-1.0f,-1.0f,1.0f);
47
glEnd();
48
glPopMatrix();
49
glPushMatrix();
50
glTranslatef(1.5f,0.0f,-7.0f);
51
//rotate round the line which connect (0,0,0) and (1,1,1) and extended..
52
glRotatef(spin2,1.0f,1.0f,1.0f);
53
glBegin(GL_QUADS);
54
glColor3f(0.0f,1.0f,0.0f);
55
glVertex3f( 1.0f, 1.0f,-1.0f);
56
glVertex3f(-1.0f, 1.0f,-1.0f);
57
glVertex3f(-1.0f, 1.0f, 1.0f);
58
glVertex3f( 1.0f, 1.0f, 1.0f);
59
glColor3f(1.0f,0.5f,0.0f);
60
glVertex3f( 1.0f,-1.0f, 1.0f);
61
glVertex3f(-1.0f,-1.0f, 1.0f);
62
glVertex3f(-1.0f,-1.0f,-1.0f);
63
glVertex3f( 1.0f,-1.0f,-1.0f);
64
glColor3f(1.0f,0.0f,0.0f);
65
glVertex3f( 1.0f, 1.0f, 1.0f);
66
glVertex3f(-1.0f, 1.0f, 1.0f);
67
glVertex3f(-1.0f,-1.0f, 1.0f);
68
glVertex3f( 1.0f,-1.0f, 1.0f);
69
glColor3f(1.0f,1.0f,0.0f);
70
glVertex3f( 1.0f,-1.0f,-1.0f);
71
glVertex3f(-1.0f,-1.0f,-1.0f);
72
glVertex3f(-1.0f, 1.0f,-1.0f);
73
glVertex3f( 1.0f, 1.0f,-1.0f);
74
glColor3f(0.0f,0.0f,1.0f);
75
glVertex3f(-1.0f, 1.0f, 1.0f);
76
glVertex3f(-1.0f, 1.0f,-1.0f);
77
glVertex3f(-1.0f,-1.0f,-1.0f);
78
glVertex3f(-1.0f,-1.0f, 1.0f);
79
glColor3f(1.0f,0.0f,1.0f);
80
glVertex3f( 1.0f, 1.0f,-1.0f);
81
glVertex3f( 1.0f, 1.0f, 1.0f);
82
glVertex3f( 1.0f,-1.0f, 1.0f);
83
glVertex3f( 1.0f,-1.0f,-1.0f);
84
glEnd();
85
glPopMatrix();
86
glutSwapBuffers();
87
}
88
void reshape(int w,int h)
89
{
90
GLfloat range = 5.0f;
91
if(h == 0)
92
h = 1;
93
glViewport(0,0,w,h);
94
glMatrixMode(GL_PROJECTION);
95
glLoadIdentity();
96
if(w < h)
97
glOrtho(-range,range,-GLfloat(range*h)/w,GLfloat(range*h)/w,-10.0f,10.0f);
98
else
99
glOrtho(-GLfloat(range*w)/h,GLfloat(range*w)/h,-range,range,-10.0f,10.0f);
100
glMatrixMode(GL_MODELVIEW);
101
glLoadIdentity();
102
}
103
void keyboard(unsigned char key,int x,int y)
104
{
105
switch(key)
106
{
107
//use the space key to decide whether the window will be full-screen displayed.
108
case 32:
109
if(t%2 == 0)
110
//requests that the current window be made full screen
111
glutFullScreen();
112
else
113
//requests a change to the size of the current window
114
glutReshapeWindow(WinWidth,WinHeight);
115
t++;
116
break;
117
//use the key w to control the change of spin1
118
case 'w':
119
case 'W':
120
spin1 = spin1 + 2.0f;
121
if(spin1 > 360.0f)
122
spin1 -= 360.0f;
123
glutPostRedisplay();
124
break;
125
//use the key s to control the change of spin2
126
case 's':
127
case 'S':
128
spin2 = spin2 + 2.0f;
129
if(spin2 > 360.0f)
130
spin2 -= 360.0f;
131
glutPostRedisplay();
132
break;
133
//use the Esc key to escape the application
134
case 27:
135
exit(NULL);
136
break;
137
default:
138
break;
139
}
140
}
141
int main(int argc,char *argv[])
142
{
143
glutInit(&argc,argv);
144
glutInitDisplayMode(GLUT_DEPTH|GLUT_RGB|GLUT_DOUBLE);
145
glutInitWindowSize(WinWidth,WinHeight);
146
glutInitWindowPosition(100,100);
147
glutCreateWindow("Lesson 05");
148![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
149
init();
150
glutDisplayFunc(display);
151
glutReshapeFunc(reshape);
152
glutKeyboardFunc(keyboard);
153
glutMainLoop();
154
return 0;
155
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
89
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
90
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
92
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
93
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
96
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
99
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
100
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
101
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
102
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
103
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
104
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
105
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
106
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
107
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
109
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
110
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
111
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
112
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
113
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
114
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
115
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
116
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
117
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
118
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
119
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
120
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
121
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
122
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
123
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
124
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
125
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
126
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
127
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
128
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
129
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
130
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
131
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
132
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
133
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
134
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
135
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
136
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
137
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
138
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
139
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
140
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
141
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
142
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
143
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
144
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
145
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
146
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
147
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
148
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
149
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
150
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
151
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
152
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
153
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
154
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
155
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
程序效果:
我没有什么雄心壮志,我只想给自己和关心自己的家人和朋友一个交代,仅此而已。