forked from Huxpro/huxpro.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmovingman.cpp
More file actions
280 lines (277 loc) · 5.3 KB
/
movingman.cpp
File metadata and controls
280 lines (277 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include<GL/glut.h>
// 绘制立方体
float a = 0.025f;
float a_2 = 0.05f;
float b = 0.075;
//a,a_2,b三个参数用来设定所用长方体的长度
// 将立方体的八个顶点保存到一个数组里面
//头部是正方体
static const float vertex_head[][3] =
{
-a_2, -a_2, -a_2,
a_2, -a_2, -a_2,
-a_2, a_2, -a_2,
a_2, a_2, -a_2,
-a_2, -a_2, a_2,
a_2, -a_2, a_2,
-a_2, a_2, a_2,
a_2, a_2, a_2,
};
//胳膊和大腿是长方体
static const float vertex_longRec[][3] =
{
-a, -a-0.2, -a,
a, -a-0.2, -a,
-a, a, -a,
a, a, -a,
-a, -a-0.2, a,
a, -a-0.2, a,
-a, a, a,
a, a, a,
};
//小腿的数据
static const float vertex_longLeg[][3] =
{
-a, -a - 0.3, -a,
a, -a - 0.3, -a,
-a, a, -a,
a, a, -a,
-a, -a - 0.3, a,
a, -a - 0.3, a,
-a, a, a,
a, a, a,
};
//身体(脊椎)的数据
static const float vertex_body[][3] =
{
-a, -a-0.3, -a,
a, -a-0.3, -a,
-a, a, -a,
a, a, -a,
-a, -a-0.3, a,
a, -a-0.3, a,
-a, a, a,
a, a, a,
};
//腰宽和肩宽的数据
static const float vertex_width[][3] =
{
-a-b, -a, -a,
a+b, -a, -a,
-a-b, a, -a,
a+b, a, -a,
-a-b, -a, a,
a+b, -a, a,
-a-b, a, a,
a+b, a, a,
};
// 将要使用的顶点的序号保存到一个数组里面
static const GLint index_list[][2] =
{
{ 0, 1 },
{ 2, 3 },
{ 4, 5 },
{ 6, 7 },
{ 0, 2 },
{ 1, 3 },
{ 4, 6 },
{ 5, 7 },
{ 0, 4 },
{ 1, 5 },
{ 7, 3 },
{ 2, 6 }
};
// 绘制立方体
void DrawCube(const float part[][3])//传入矩阵参数,连线画得所需长方体
{
int i, j;
glBegin(GL_LINES);
for (i = 0; i<12; ++i) // 12 条线段
{
for (j = 0; j<2; ++j) // 每条线段 2个顶点
{
glVertex3fv(part[index_list[i][j]]);//每两个点之间连线
}
}
glEnd();
}
//整体移动函数
void movey() {
static float move = 0;
static int times1 = 0;
times1+=2;//移动频率
if (times1 > 20)
{
times1 = 0;
}
if (times1 % 20 == 0)
{
move -= 0.0004;//每次移动的距离
}
glTranslatef(-move,move,0);//移动函数
}
//左半边跑动(即旋转大臂)函数
void selfRotateR() {
static float rotateAngle = 0;
static int times1 = 0;
static int x = 1;
times1+=2;//旋转频率
if (times1 > 20)
{
times1 = 0;
}
if (times1 % 20 == 0)
{
rotateAngle += 1.5;//每次旋转角度
if (rotateAngle > 20)
{
rotateAngle = -rotateAngle;//回旋
x = -x;
}
}
glRotatef(-rotateAngle, x, 0, 0);//旋转函数rotate
}
//右半边跑动(即旋转大臂)函数
void selfRotateL() {
static float rotateAngle = 0;
static int times1 = 0;
static int x = 1;
times1+=2;//频率
if (times1 > 20)
{
times1 = 0;
}
if (times1 % 20 == 0)
{
rotateAngle -= 1.5;//每次旋转角度
if (rotateAngle < -20)
{
rotateAngle = -rotateAngle;//回旋
x = -x;
}
}
glRotatef(-rotateAngle, x, 0, 0);
}
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//head
glPushMatrix();
glColor3f(0, 1, 1);
glTranslatef(0, 0.8, 0);
movey();
glRotatef(15, 0, 1, 0);
glRotatef(15, 1, 0, 0);
DrawCube(vertex_head);
glPopMatrix();
//leftarm
glPushMatrix();
glTranslatef(-0.125,0.725, 0);
glColor3f(1, 0, 1);
selfRotateL();
movey();
glRotatef(15, 0, 1, 0);
glRotatef(15, 1, 0, 0);
DrawCube(vertex_longRec);
//leftarm2
glTranslatef(0, -0.2, 0);
glColor3f(1, 0, 1);
selfRotateL();
glRotatef(15, 0, -1, 0);
glRotatef(95, 1, 0, 0);
DrawCube(vertex_longRec);
glPopMatrix();
//upperwidth
glPushMatrix();
glTranslatef(0, 0.725, 0);
glColor3f(0, 0, 1);
movey();
glRotatef(15, 0, 1, 0);
glRotatef(15, 1, 0, 0);
DrawCube(vertex_width);
glPopMatrix();
//body
glPushMatrix();
glTranslatef(0, 0.675, 0);
glColor3f(1, 0, 0);
movey();
glRotatef(15, 0, 1, 0);
glRotatef(15, 1, 0, 0);
DrawCube(vertex_body);
glPopMatrix();
//rightArm
glPushMatrix();
glTranslatef(0.125, 0.725, 0);
glColor3f(1, 1, 1);
selfRotateR();
movey();
glRotatef(15, 0, 1, 0);
glRotatef(15, 1, 0, 0);
DrawCube(vertex_longRec);
//rightarm2
glTranslatef(0, -0.2, 0);
glColor3f(1, 1, 1);
selfRotateR();
//movey();
glRotatef(15, 0, -1, 0);
glRotatef(95, 1, 0, 0);
DrawCube(vertex_longRec);
glPopMatrix();
//underwidth
glPushMatrix();
glTranslatef(0, 0.345, 0);
glColor3f(0, 0, 1);
movey();
glRotatef(15, 0, 1, 0);
glRotatef(15, 1, 0, 0);
DrawCube(vertex_width);
glPopMatrix();
//leftleg
glPushMatrix();
glColor3f(0, 0.85, 1);
glTranslatef(0.075,0.315, 0);
selfRotateL();
movey();
glRotatef(15, 0, 1, 0);
glRotatef(15, 1, 0, 0);
DrawCube(vertex_longRec);
//leftleg2
glColor3f(0, 0.85, 1);
glTranslatef(0, -0.2, 0);
selfRotateL();
glRotatef(10, 0, -1, 0);
glRotatef(60, -1, 0, 0);
DrawCube(vertex_longLeg);
glPopMatrix();
//rightleg
glPushMatrix();
glTranslatef(-0.075, 0.315, 0);
glColor3f(1, 1, 1);
selfRotateR();
movey();
glRotatef(15, 0, 1, 0);
glRotatef(15, 1, 0, 0);
DrawCube(vertex_longRec);
//rightleg2
glTranslatef(0, -0.2, 0);
glColor3f(1, 1, 1);
selfRotateR();
glRotatef(10, 0, -1, 0);
glRotatef(60, -1, 0, 0);
DrawCube(vertex_longLeg);
glPopMatrix();
glutSwapBuffers();
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(200, 100);
glutInitWindowSize(1000, 1000);
glutCreateWindow("GLDemo");
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutMainLoop();
}