-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal.cpp
More file actions
1308 lines (1173 loc) · 37.9 KB
/
final.cpp
File metadata and controls
1308 lines (1173 loc) · 37.9 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <GL/glut.h>
#include <GL/glaux.h>
#include <math.h>
#include <string>
#include <mmsystem.h>
#include <iostream>
#include "hd.h"
#include <vector>
#ifndef GL_POINT_SPRITE
#define GL_POINT_SPRITE 0x8861
#endif
#include <set>
#include <random>
using namespace std;
#define Faster 1
#define Slower 2
#define Switch_view 3
#define Orbit_view 4
GLfloat PI = acos(-1.0);
GLUquadricObj* qobj;
GLubyte* pImg;
GLint iWidth, iHeight;
GLfloat fps = 90;
//string str = "nmp4";
//str.c_str();
string Texture_Path[12] = {
"texture_sun_8K.bmp",
"texture_Mercury_8K.bmp",
"texture_Venus_8K.bmp",
"texture_earth_2K.bmp",
"texture_Mars_8K.bmp",
"texture_Jupiter_8K.bmp",
"texture_Saturn_8K.bmp",
"texture_Uranus_2K.bmp",
"texture_Neptune_2K.bmp",
"texture_moon_2K.bmp",
"texture_back_2K.bmp",
"texture_huan_2K.bmp"
};
string skyTexture_Path[6] = {
"right.bmp",
"left.bmp",
"top.bmp",
"bottom.bmp",
"front.bmp",
"back.bmp"
};
GLuint texName[12];
GLuint skyboxTextures[6];
AUX_RGBImageRec* TextureImage[12];
AUX_RGBImageRec* skyTextureImage[12];
GLfloat view_type = 1; // 视图类型 1--模拟, 2--拟真
GLfloat view_orbit = 1; // 轨道显示
GLfloat base_r = 3; // 基准轨道半径
GLfloat base = 0.1; // 基准半径
GLfloat base_gz = 2 * PI / (fps * 20); // 地球公转一圈的时间为30s
GLfloat base_zz = 360.0 / fps / 5; // 地球自转一圈的时间为5s
const float piover180 = 0.0174532925f;//折算度和弧度的折算因子3.1415926/180
float angle = 0.0;//angle绕y轴的旋转角
// 视角相关变量
GLdouble eyeX = -5, eyeY = 5, eyeZ = 6;
GLdouble centerX = 0.0, centerY = 0.0, centerZ = 0.0;
GLdouble upX = 0.0, upY = 0.0, upZ = 1.0;
// 移动速度
GLfloat moveSpeed = 0.05;
GLfloat rotateSpeed = 3.0;
int LastXPos, LastYPos, LastZPos; //上次鼠标点击处x、y坐标
BOOL IsLBDown = FALSE; //鼠标左键是否按下
GLfloat xrot; // 绕X轴旋转的角度
GLfloat yrot; // 绕Y轴旋转的角度
GLfloat zrot; // 绕Z轴旋转的角度
float heading; //头向左右偏转的角度
//此处定义镜头在xoz平面上移动的x坐标和z坐标
float xpos;
float zpos;
int currentFollowIndex = -1;
int position[3] = { -5,5,4 };
int position2[3] = { -5,5,3 };
GLfloat LastX, LastY;
GLfloat Yrot; // Y 旋转角度
//GLfloat walkbias = 0; //当人行走时头部产生上下摆动的幅度。我们使用简单的sine正弦波来调节镜头的Y轴位置。
//GLfloat walkbiasangle = 0;
GLfloat lookupdown = 0.0f, lookleftright = 0.0f; //定义上下看的旋转角度
GLfloat z = 0.0f; // Depth Into The Screen
int flag = 1;
// 天体结构
struct Body {
GLfloat x, y, z; // 位置
GLfloat cx, cy, cz; // 摄像机位置
GLfloat dx, dy, dz; // 速度
GLfloat orbitRadius; // 轨道半径
GLfloat radius; // 半径
GLfloat angle; // 角度,用于计算位置
GLfloat angularVelocity; // 角速度
GLfloat r, g, b; // 颜色
int number; // 序号
GLfloat rotation; // 自转开始角度
GLfloat dr; // 自转速度
GLfloat bias; // 相机偏移量
GLfloat tilt; // 偏移角
};
struct vec3 {
float x, y, z;
vec3(float x, float y, float z) : x(x), y(y), z(z) {}
};
struct bs
{
GLfloat x, y, z; // 位置
};
// 小行星组
const int xxx_number = 3000;
Body xxxs[xxx_number] = {};
// 背景星星
const int bs_number = 6000;
Body backstar[bs_number] = {};
// 天体列表
Body sun = {
0, 0, 0,
0, 0, 0,
0, 0, 0,
0,
1, // 天体半径,地球在屏幕上的大小
0.0, // 初始角度
0.01, // 角速度
1,1,1,0,//rgb,name
0,base_zz / 25.4, //roation
0,
0
}; // 太阳在原点
Body Mercury = {
0, 0, 0, // 初始位置在太阳旁边
0, 0, 0,
0, 0, 0, // 初始速度为0
base_r * 0.3871, // 轨道半径,可以是任意单位,这里用1.0表示简化计算
base * 0.382, // 天体半径,地球在屏幕上的大小
0.0, // 初始角度
base_gz * 365 / 88, // 角速度
1,1,1,1,
0,base_zz / 59,
28,
0
};
Body Venus = {
0, 0, 0,
0, 0, 0, // 初始位置在太阳旁边
0, 0, 0, // 初始速度为0
base_r * 0.7233, // 轨道半径,可以是任意单位,这里用1.0表示简化计算
base * 0.949 , // 天体半径,地球在屏幕上的大小
0.0, // 初始角度
base_gz * 365 / 224, // 角速度
1,1,1,2,
0,base_zz / (-243),
12,
177
};
Body moon = {
0, 0, 0,
0, 0, 0,
0, 0, 0,
base * 1.5, // 轨道半径,
base * 0.273, // 天体半径,地球在屏幕上的大小
0.0, // 初始角度
base_gz * 365 / 27, // 角速度
1,1,1,9,
0,base_zz / 27,
40,
0
}; // 月亮
Body earth = {
0, 0, 0, // 初始位置在太阳旁边
0, 0, 0,
0, 0, 0, // 初始速度为0
base_r * 1, // 轨道半径,可以是任意单位,这里用1.0表示简化计算
base, // 天体半径,地球在屏幕上的大小
0.0, // 初始角度
base_gz, // 角速度
1,1,1,3,
0,base_zz,
12,
23
};
Body Mars = {
0, 0, 0, // 初始位置在太阳旁边
0, 0, 0,
0, 0, 0, // 初始速度为0
base_r * 1.5237, // 轨道半径,可以是任意单位,这里用1.0表示简化计算
base * 0.532, // 天体半径,地球在屏幕上的大小
0.0, // 初始角度
base_gz * 365 / 687, // 角速度
1,1,1,4,
0,base_zz,
21,
25
};
Body Jupiter = {
0, 0, 0, // 初始位置在太阳旁边
0, 0, 0,
0, 0, 0, // 初始速度为0
base_r * 5.2026, // 轨道半径,可以是任意单位,这里用1.0表示简化计算
base * 11.209, // 天体半径,地球在屏幕上的大小
radians(0), // 初始角度
base_gz / 11.86, // 角速度
1,1,1,5,
0,base_zz * 24 / 10,
3,
3
};
Body Saturn = {
0, 0, 0, // 初始位置在太阳旁边
0, 0, 0,
0, 0, 0, // 初始速度为0
base_r * 9.5549, // 轨道半径,可以是任意单位,这里用1.0表示简化计算
base * 9.449, // 天体半径,地球在屏幕上的大小
radians(-30), // 初始角度
base_gz / 29.5, // 角速度
1,1,1,6,
0,base_zz * 24 / 10.5,
5,
30
};
Body Uranus = {
0, 0, 0,
0, 0, 0, // 初始位置在太阳旁边
0, 0, 0, // 初始速度为0
base_r * 19.2184, // 轨道半径,可以是任意单位,这里用1.0表示简化计算
base * 4.007, // 天体半径,地球在屏幕上的大小
radians(-60), // 初始角度
base_gz / 84, // 角速度
1,1,1,7,
0,base_zz * 24 / 17,
7,
98
};
Body Neptune = {
0, 0, 0,
0, 0, 0, // 初始位置在太阳旁边
0, 0, 0, // 初始速度为0
base_r * 30.1104, // 轨道半径,可以是任意单位,这里用1.0表示简化计算
base * 3.883, // 天体半径,地球在屏幕上的大小
radians(-90), // 初始角度
base_gz / 164.82, // 角速度
1,1,1,8,
0,base_zz * 24 / 16,
7,
30
};
Body hx = {
0, 0, 0,
0, 0, 0, // 初始位置在太阳旁边
2, 2, 0, // 初始速度为0
base_r * 1.5, // 轨道半径,可以是任意单位,这里用1.0表示简化计算
base * 0.3, // 天体半径,地球在屏幕上的大小
radians(0), // 初始角度
base_gz / 2.82, // 角速度
1,1,1,-1,
0,base_zz * 24 / 16,
10,
30
};
Body planet[11] = { sun, Mercury, Venus, earth, Mars, Jupiter, Saturn, Uranus, Neptune, moon, hx };
Body save[11] = { sun, Mercury, Venus, earth, Mars, Jupiter, Saturn, Uranus, Neptune, moon, hx };
// 粒子类
class Particle {
public:
float x, y, z; // 位置
float vx, vy, vz; // 速度
float life; // 生命周期
float size; // 大小
vec3 color; // 颜色
Particle(float x, float y, float z, float vx, float vy, float vz, float life, float size, vec3 color)
: x(x), y(y), z(z), vx(vx), vy(vy), vz(vz), life(life), size(size), color(color) {}
Particle(vec3 xyz, float life, float size, vec3 color)
: x(xyz.x), y(xyz.y), z(xyz.z), life(life), size(size), color(color) {}
void update() {
// 更新位置
x += vx;
y += vy;
z += vz;
// 更新生命周期-
life -= 0.01;
// 可以添加更多的物理效果,如重力、空气阻力等
}
bool isDead() {
return life <= 0;
}
};
double dist(double x, double y, double z, double a, double b, double c)
{
return sqrt((x - a) * (x - a) + (y - b) * (y - b) + (z - c) * (z - c));
}
// 设置随机数种子
void setRandomSeed() {
std::srand(static_cast<unsigned int>(std::time(nullptr)));
}
// 生成范围内的随机浮点数
float randomFloat(float min, float max) {
return min + static_cast<float>(std::rand()) / (static_cast<float>(RAND_MAX / (max - min)));
}
// 粒子发射器类
class ParticleEmitter {
private:
std::vector<Particle> particles;
public:
void updateParticleSize(Particle particle) {
// 根据距离调整大小
float distance = sqrt(particle.x * particle.x + particle.y * particle.y + particle.z * particle.z);
float size = 10.0f / distance; // 反比于距离
glPointSize(size);
}
void updatelife(Body particle,double &li)
{
float distance = sqrt(particle.x * particle.x + particle.y * particle.y + particle.z * particle.z);
li = 0.5f / distance; // 反比于距离
}
void emitParticle(float x, float y, float z) {
// 创建一个新的粒子
double len = dist(planet[10].x, planet[10].y, planet[10].z, 0, 0, 0) * 60;
double li;
updatelife(planet[10], li);
Particle p(x, y, z, planet[10].x / len, planet[10].y / len, planet[10].z / len , li, 10.0, vec3(1.0, 1.0, 1.0)); // 流星颜色偏黄
//Particle p(x, y, z, tangentX, tangentY, tangentY, 0.5, 10.0, vec3(1.0, 1.0, 1.0)); // 流星颜色偏黄
particles.push_back(p);
}
void update() {
// 更新所有粒子
for (auto it = particles.begin(); it != particles.end(); ) {
it->update();
if (it->isDead()) {
it = particles.erase(it);
}
else {
++it;
}
}
}
void render() {
// 启用点精灵
glPushMatrix();
glEnable(GL_POINT_SPRITE);
// 渲染所有粒子
for (const auto& p : particles) {
GLfloat tempColor[3] = { p.color.x, p.color.y, p.color.z };
// 设置点的大小
updateParticleSize(p);
glBegin(GL_POINTS);
glColor3fv(tempColor);
//if (p.color.y == 0.9)
glVertex3f(p.x, p.y, p.z);
glEnd();
}
// 禁用点精灵
glDisable(GL_POINT_SPRITE);
//glEnable(GL_DEPTH_TEST);
glPopMatrix();
}
};
// 全局粒子发射器
ParticleEmitter emitter;
// 随机生成半径为r的球体上的坐标
vec3 randomPointOnSphere(float r) {
// 随机生成极角 theta 和方位角 phi
float theta = randomFloat(0.0f, (float)PI); // theta 在 [0, π] 范围内
float phi = randomFloat(0.0f, 2.0f * (float)PI); // phi 在 [0, 2π] 范围内
// 使用球坐标转换为笛卡尔坐标
float x = r * sin(theta) * cos(phi);
float y = r * sin(theta) * sin(phi);
float z = r * cos(theta);
return vec3(x, y, z);
}
// 更新天体位置
void updatePositions(int value) {
for (int i = 0; i < 11; i++)
{
int fh;
if (view_type == 1) fh = -1;
else fh = 1;
if (i == 9) // 更新月球的轨道数据
{
planet[i].rotation += planet[i].dr;
// 更新月球的角度
planet[i].angle += planet[i].angularVelocity;
planet[i].x = planet[3].x + planet[i].orbitRadius * cos(planet[i].angle);
planet[i].y = planet[3].y + planet[i].orbitRadius * sin(planet[i].angle);
//[i].z = 0;
planet[i].cx = planet[3].x + (planet[i].orbitRadius + fh * planet[i].radius * planet[i].bias) * cos(planet[i].angle);
planet[i].cy = planet[3].y + (planet[i].orbitRadius + fh * planet[i].radius * planet[i].bias) * sin(planet[i].angle);
planet[i].cz = planet[i].radius; // 假设在二维平面上运动
}
else
{
planet[i].rotation += planet[i].dr;
// 更新地球的角度
planet[i].angle += planet[i].angularVelocity;
// 使用三角函数计算新的位置
planet[i].x = planet[i].orbitRadius * cos(planet[i].angle);
planet[i].y = planet[i].orbitRadius * sin(planet[i].angle);
planet[i].x += planet[i].dx;
planet[i].y += planet[i].dy;
//planet[i].z = 0; // 假设在二维平面上运动
planet[i].cx = (planet[i].orbitRadius + fh * planet[i].radius * planet[i].bias) * cos(planet[i].angle + planet[i].angularVelocity * 10);
planet[i].cy = (planet[i].orbitRadius + fh * planet[i].radius * planet[i].bias) * sin(planet[i].angle + planet[i].angularVelocity * 10);
planet[i].cz = planet[i].radius * 0; // 假设在二维平面上运动
}
}
for (int i = 0; i < xxx_number; i++)
{
xxxs[i].rotation += xxxs[i].dr;
xxxs[i].angle += xxxs[i].angularVelocity;
// 使用三角函数计算新的位置
xxxs[i].x = xxxs[i].orbitRadius * cos(xxxs[i].angle);
xxxs[i].y = xxxs[i].orbitRadius * sin(xxxs[i].angle);
}
emitter.emitParticle(planet[10].x, planet[10].y, planet[10].z); // 在原点发射一个粒子
glutPostRedisplay(); // 标记窗口需要重绘
glutTimerFunc(1000 / fps, updatePositions, 0); // 重新设置定时器
}
// 绘制轨道
void DrawCircle(GLdouble x, GLdouble y, GLdouble R, int number) {
glPushMatrix();
//glRotatef(90.0, 1.0, 0.0, 0.0);
if (number == -1) glColor4f(0.5, 0.5, 0.5, 1.0);
else glColor4f(1.0, 1.0, 1.0, 1.0);
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 1000; i++) {
GLdouble angle1 = i * 2 * 3.14 / 1000;
glVertex3d(x + R * cos(angle1), y + R * sin(angle1), 0);
}
glEnd();
glPopMatrix();
}
// 绘制天体
void drawBody(Body body) {
glPushMatrix();
qobj = gluNewQuadric();
glEnable(GL_TEXTURE_2D);
glTranslatef(body.x, body.y, body.z);
glRotatef(body.tilt, 1.0, 0.0, 0.0);
glRotatef(body.rotation, 0.0, 0.0, 1.0);
if (body.number != -1)
{
glBindTexture(GL_TEXTURE_2D, texName[body.number]);
gluQuadricTexture(qobj, GL_TRUE);//纹理函数
}
glColor3f(body.r, body.g, body.b);
gluSphere(qobj, body.radius, 60, 60);//二次曲面qobj
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
// 绘制坐标系
void draw_xyz()
{
glPushMatrix();
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex3d(0, 0, 0);
glVertex3d(10, 0, 0);
glColor3f(0.0, 1.0, 0.0);
glVertex3d(0, 0, 0);
glVertex3d(0, 10, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3d(0, 0, 0);
glVertex3d(0, 0, 10);
glEnd();
glPopMatrix();
}
GLfloat radians(GLfloat x)
{
return x * PI / 180.0;
}
// 渲染光环
void renderTorus() {
GLUquadric* huan = gluNewQuadric();
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glColor3f(1, 1, 1);
glBindTexture(GL_TEXTURE_2D, texName[11]);
gluQuadricTexture(huan, GL_TRUE); // 启用纹理
gluQuadricNormals(huan, GLU_SMOOTH); // 设置法线
glTranslatef(planet[6].x, planet[6].y, planet[6].z); // 根据需要调整光环的位置
glRotated(planet[6].angularVelocity, 0, 0, 1);
glRotatef(planet[6].tilt, 1.0, 0.0, 0.0);
gluDisk(huan, planet[6].radius * 1.3, planet[6].radius * 2.2, 50, 50); // 内半径为1.0,外半径为1.1,环宽0.1
glDisable(GL_TEXTURE_2D);
//glEnd();
glPopMatrix();
glPushMatrix();
huan = gluNewQuadric();
glTranslatef(planet[7].x, planet[7].y, planet[7].z); // 根据需要调整光环的位置
glRotated(planet[7].angularVelocity, 0, 0, 1);
glRotatef(planet[7].tilt, 1.0, 0.0, 0.0);
glColor3f(1, 1, 1);
gluDisk(huan, planet[7].radius * 1.3, planet[7].radius * 1.35, 50, 50); // 内半径为1.0,外半径为1.1,环宽0.1
glPopMatrix();
}
// 绘制天空盒的函数
void renderSkybox() {
glDisable(GL_DEPTH_TEST); // 禁用深度测试,防止天空盒被深度缓冲影响
glEnable(GL_TEXTURE_2D);
glPushMatrix(); // 保存当前的模型视图矩阵
// 绑定纹理并渲染立方体的每个面
glBindTexture(GL_TEXTURE_2D, skyboxTextures[0]); // 右面
glBegin(GL_QUADS);
glTexCoord2f(1.0, 0.0); glVertex3f(100.0, -100.0, -100.0);
glTexCoord2f(0.0, 0.0); glVertex3f(100.0, 100.0, -100.0);
glTexCoord2f(0.0, 1.0); glVertex3f(100.0, 100.0, 100.0);
glTexCoord2f(1.0, 1.0); glVertex3f(100.0, -100.0, 100.0);
glEnd();
glBindTexture(GL_TEXTURE_2D, skyboxTextures[1]); // 左面
glBegin(GL_QUADS);
glTexCoord2f(1.0, 0.0); glVertex3f(-100.0, -100.0, -100.0);
glTexCoord2f(0.0, 0.0); glVertex3f(-100.0, 100.0, -100.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-100.0, 100.0, 100.0);
glTexCoord2f(1.0, 1.0); glVertex3f(-100.0, -100.0, 100.0);
glEnd();
glBindTexture(GL_TEXTURE_2D, skyboxTextures[2]); // 顶面
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-100.0, 100.0, -100.0);
glTexCoord2f(1.0, 0.0); glVertex3f(100.0, 100.0, -100.0);
glTexCoord2f(1.0, 1.0); glVertex3f(100.0, 100.0, 100.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-100.0, 100.0, 100.0);
glEnd();
glBindTexture(GL_TEXTURE_2D, skyboxTextures[3]); // 底面
glBegin(GL_QUADS);
glTexCoord2f(1.0, 0.0); glVertex3f(-100.0, -100.0, -100.0);
glTexCoord2f(0.0, 0.0); glVertex3f(100.0, -100.0, -100.0);
glTexCoord2f(0.0, 1.0); glVertex3f(100.0, -100.0, 100.0);
glTexCoord2f(1.0, 1.0); glVertex3f(-100.0, -100.0, 100.0);
glEnd();
glBindTexture(GL_TEXTURE_2D, skyboxTextures[4]); // 前面
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-100.0, -100.0, 100.0);
glTexCoord2f(1.0, 0.0); glVertex3f(100.0, -100.0, 100.0);
glTexCoord2f(1.0, 1.0); glVertex3f(100.0, 100.0, 100.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-100.0, 100.0, 100.0);
glEnd();
glBindTexture(GL_TEXTURE_2D, skyboxTextures[5]); // 后面
glBegin(GL_QUADS);
glTexCoord2f(1.0, 0.0); glVertex3f(-100.0, -100.0, -100.0);
glTexCoord2f(0.0, 0.0); glVertex3f(100.0, -100.0, -100.0);
glTexCoord2f(0.0, 1.0); glVertex3f(100.0, 100.0, -100.0);
glTexCoord2f(1.0, 1.0); glVertex3f(-100.0, 100.0, -100.0);
glEnd();
glPopMatrix(); // 恢复之前的模型视图矩阵
glDisable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST); // 启用深度测试
}
//音乐
void music()
{
MCI_OPEN_PARMS op; //播放背景音乐
op.dwCallback = NULL;
op.lpstrAlias = NULL;
op.lpstrDeviceType = "MPEGAudio";
op.lpstrElementName = "A Moment Apart-ODESZA#htUPm(1).mp3";
op.wDeviceID = NULL;
UINT rs;
rs = mciSendCommand(NULL, MCI_OPEN, MCI_OPEN_ELEMENT, (DWORD)&op);
if (rs == 0)
{
MCI_PLAY_PARMS pp;
pp.dwCallback = NULL;
pp.dwFrom = 0;
mciSendCommand(op.wDeviceID, MCI_PLAY, MCI_NOTIFY, (DWORD)&pp);
}
}
// OpenGL渲染回调函数
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 绘制椭圆轨道
//drawEllipse(a, b);
if (view_type == 1)
{
renderSkybox(); // 渲染背景
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLfloat xtrans = -xpos; // 用于游戏者沿X轴平移时的大小
GLfloat ztrans = -zpos; // 用于游戏者沿Z轴平移时的大小
GLfloat sceneroty = 360.0 - Yrot; // 使得左右键与旋转方向一致
glRotatef(lookupdown, 1.0f, 0.0f, 0.0f); //相应PageUp、PageDown进行向上/向下看的旋转,沿x轴旋转
glRotatef(lookleftright, 0.0f, 1.0f, 0.0f); //相应PageUp、PageDown进行向上/向下看的旋转,沿x轴旋转
glRotatef(sceneroty, 0.0f, 1.0f, 0.0f); // 根据游戏者正面所对方向所作的左右旋转
glTranslatef(xtrans, 0, ztrans); //以游戏者为中心的平移场景
gluLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);
if (currentFollowIndex >= 0) updateCamera();
glRotatef(xrot, 1.0f, 0.0f, 0.0f); //沿x轴旋转
glRotatef(yrot, 0.0f, 1.0f, 0.0f); //沿y轴旋转
light_sun();
light_center();
int base = 5;
for (int i = 1; i < 11; i++)
{
planet[i].orbitRadius = base * (0.2 + i * 0.2);
}
planet[0].radius = base * 0.2; planet[0].bias;
planet[1].radius = base * 0.05; planet[1].bias = 5;
planet[2].radius = base * 0.05; planet[2].bias = 5;
planet[3].radius = base * 0.06; planet[3].bias = 4.4;
planet[4].radius = base * 0.06; planet[4].bias = 4.45;
planet[5].radius = base * 0.15; planet[5].bias = 2.35;
planet[6].radius = base * 0.1; planet[6].bias = 4.25;
planet[7].radius = base * 0.08; planet[7].bias = 4.1;
planet[8].radius = base * 0.07; planet[8].bias = 3.9;
planet[9].radius = base * 0.02; planet[9].bias = 11.5;
planet[9].orbitRadius = 0.5;
planet[10].orbitRadius = 4.5;
for (int i = 0; i < 11; i++)
{
if (i != 9 && view_orbit == 1) DrawCircle(planet[i].dx, planet[i].dy, planet[i].orbitRadius, planet[i].number);
glColor3f(1.0, 1.0, 1.0);
glPushMatrix();
//glRotatef(planet[i].tilt, 1.0, 0.0, 0.0);
drawBody(planet[i]);
glPopMatrix();
}
renderTorus();
// 更新和渲染粒子
emitter.update();
emitter.render();
}
else
{
renderSkybox(); // 渲染背景
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLfloat xtrans = -xpos; // 用于游戏者沿X轴平移时的大小
GLfloat ztrans = -zpos; // 用于游戏者沿Z轴平移时的大小
GLfloat sceneroty = 360.0 - Yrot; // 使得左右键与旋转方向一致
glRotatef(lookupdown, 1.0f, 0.0f, 0.0f); //相应PageUp、PageDown进行向上/向下看的旋转,沿x轴旋转
glRotatef(lookleftright, 0.0f, 1.0f, 0.0f); //相应PageUp、PageDown进行向上/向下看的旋转,沿x轴旋转
glRotatef(sceneroty, 0.0f, 1.0f, 0.0f); // 根据游戏者正面所对方向所作的左右旋转
glTranslatef(xtrans, 0, ztrans); //以游戏者为中心的平移场景
gluLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);
if (currentFollowIndex >= 0) updateCamera();
glRotatef(xrot, 1.0f, 0.0f, 0.0f); //沿x轴旋转
glRotatef(yrot, 0.0f, 1.0f, 0.0f); //沿y轴旋转
light_sun();
light_center();
//draw_xyz();
for (int i = 0; i < 11; i++)
{
if (i != 9 && view_orbit == 1) DrawCircle(planet[i].dx, planet[i].dy, planet[i].orbitRadius,planet[i].number);
glColor3f(1.0, 1.0, 1.0);
glPushMatrix();
drawBody(planet[i]);
glPopMatrix();
}
Drawxxx();
renderTorus();
// 更新和渲染粒子
emitter.update();
emitter.render();
//glEnable(GL_DEPTH_TEST);
}
Drawbackground();
glutSwapBuffers();
}
// 窗口大小改变时的回调函数
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// 切换视图是初始化天体的属性和摄像机位置
void changeview()
{
for (int i = 0; i < 11; i++) planet[i] = save[i];
if (view_type == 2)
{
eyeX = position2[0], eyeY = position2[1], eyeZ = position2[1];
centerX = 0.0, centerY = 0.0, centerZ = 0.0;
upX = 0.0, upY = 0.0, upZ = 1.0;
xrot = 15;
}
else
{
eyeX = position[0], eyeY = position[1], eyeZ = position[2];
centerX = 0.0, centerY = 0.0, centerZ = 0.0;
upX = 0.0, upY = 0.0, upZ = 1.0;
}
}
//选择菜单项处理
void processMenuEvents(int option)
{
//option,就是传递过来的value的值。
switch (option) {
case Faster:
for (int i = 0; i < 11; i++)
{
planet[i].angularVelocity *= 2;
planet[i].dr *= 2;
}
break;
case Slower:
for (int i = 0; i < 11; i++)
{
planet[i].angularVelocity /= 2;
planet[i].dr /= 2;
}
break;
case Switch_view:
view_type = 3 - view_type;
changeview();
break;
case Orbit_view:
view_orbit = 1 - view_orbit;
break;
}
}
//创建菜单
void createGLUTMenus()
{
int menu;
// 创建菜单并告诉GLUT,processMenuEvents处理菜单事件。
menu = glutCreateMenu(processMenuEvents);
//给菜单增加条目
glutAddMenuEntry("Speed Up", Faster);
glutAddMenuEntry("Speed Down", Slower);
glutAddMenuEntry("Switch_View", Switch_view);
glutAddMenuEntry("Orbit_View", Orbit_view);
// 把菜单和鼠标右键关联起来。
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
//对特殊键盘的交互响应处理
void specialKey(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_PAGE_UP: //按下PageUp,向上看
z -= 0.02f;
lookupdown -= 1.0f;
break;
case GLUT_KEY_PAGE_DOWN: //按下PageDown,向下看
z += 0.02f;
lookupdown += 1.0f;
break;
case GLUT_KEY_END:
lookleftright -= 1.0f;
break;
case GLUT_KEY_HOME:
lookleftright += 1.0f;
break;
case GLUT_KEY_UP:
//计算镜头在x和z轴方向上的移动量,heading为头的偏移方向与z轴夹角
xpos -= (float)sin(heading * piover180) * 0.05f;// 沿游戏者所在的X平面移动
zpos -= (float)cos(heading * piover180) * 0.05f;// 沿游戏者所在的Z平面移动
break;
case GLUT_KEY_DOWN:
xpos += (float)sin(heading * piover180) * 0.05f;// 沿游戏者所在的X平面移动
zpos += (float)cos(heading * piover180) * 0.05f;// 沿游戏者所在的Z平面移动
break;
case GLUT_KEY_LEFT:
heading += 1.0f;
Yrot = heading; // 向右侧旋转场景
break;
case GLUT_KEY_RIGHT:
heading -= 1.0f;
Yrot = heading; // 向左旋转场景
break;
}
}
// 根据当前跟随天体更新相机位置和目标点
void updateCamera() {
if (currentFollowIndex > 0) {
eyeX = planet[currentFollowIndex].cx;
eyeY = planet[currentFollowIndex].cy;
eyeZ = planet[currentFollowIndex].cz;
if (currentFollowIndex == 9) {
centerX = planet[3].x;
centerY = planet[3].y;
centerZ = planet[3].z;
}
else if (view_type == 1)
{
centerX = planet[currentFollowIndex].x;
centerY = planet[currentFollowIndex].y;
centerZ = planet[currentFollowIndex].z;
}
// 简单设置相机位置在天体后方一定距离,可根据需求调整
}
else
{
if (view_type == 2)
{
eyeX = position2[0], eyeY = position2[1], eyeZ = position2[1];
centerX = 0.0, centerY = 0.0, centerZ = 0.0;
upX = 0.0, upY = 0.0, upZ = 1.0;
}
else
{
eyeX = position[0], eyeY = position[1], eyeZ = position[1];
centerX = 0.0, centerY = 0.0, centerZ = 0.0;
upX = 0.0, upY = 0.0, upZ = 1.0;
}
}
}
void keyboard(unsigned char value, int x, int y)
{
switch (value) {
case '0': {
currentFollowIndex = 0; // 默认视角
view_orbit = 0;
break;
}
case '1': {
currentFollowIndex = 1; // 跟随水星
view_orbit = 0;
xrot = 0;
break;
}
case '2': {
currentFollowIndex = 2; // 跟随
view_orbit = 0;
break;
}
case '3': {
currentFollowIndex = 3; // 跟随
view_orbit = 0;
break;
}
case '4': {
currentFollowIndex = 4; // 跟随
view_orbit = 0;
break;
}
case '5': {
currentFollowIndex = 5; // 跟随
view_orbit = 0;
break;
}
case '6': {
currentFollowIndex = 6; // 跟随
view_orbit = 0;
break;
}
case '7': {
currentFollowIndex = 7; // 跟随
view_orbit = 0;
break;
}
case '8': {
currentFollowIndex = 8; // 跟随
view_orbit = 0;
break;
}
case '9': {
currentFollowIndex = 9; // 跟随
view_orbit = 0;
break;
}
case ' ': {
xrot = yrot = 0;
xpos = zpos = 0;
lookupdown = lookleftright = heading = Yrot = 0;
if (view_type == 2) xrot = 15;
break;
}
case 'h': {
view_orbit = 1 - view_orbit;
break;
}
case '+': {
for (int i = 0; i < 11; i++)
{
planet[i].angularVelocity *= 2;
planet[i].dr *= 2;
}
break;
}
case '-': {
for (int i = 0; i < 11; i++)
{
planet[i].angularVelocity /= 2;
planet[i].dr /= 2;
}
break;
}
case '\t':
{
view_type = 3 - view_type;
changeview();
break;
}
glutPostRedisplay();
}
}
void light_sun() {
GLfloat light_position0[] = { 0.0, 0.0, 0.0, 1.0 }; // 光源位置
glLightfv(GL_LIGHT0, GL_POSITION, light_position0); // 创建光源
//太阳光
GLfloat sun_mat_ambient[4] = { 0.5,0.5,0.5,0.0 }; // 环境光
GLfloat sun_mat_diffuse[4] = { 1.0,1.0,1.0,1.0 }; // 漫反射
GLfloat sun_mat_specular[4] = { 1.0,1.0,1.0,1.0 }; // 镜面反射
GLfloat sun_mat_shininess[] = { 100.0 }; // 光泽度指数
//GLfloat sun_mat_emission[4] = { 0.7,0.7,0.7,1.0 }; // 自发光
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, sun_mat_ambient); // 材质设定
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, sun_mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, sun_mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, sun_mat_shininess);
//glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, sun_mat_emission);
}
void light_center()
{
GLfloat light_position1[] = { 2.0, 0.0, 0.0, 1.0 }; // 光源位置
GLfloat light_position2[] = { 0.0, 2.0,0.0, 1.0 }; // 光源位置
GLfloat light_position3[] = { 0.0, -2.0,0.0, 1.0 }; // 光源位置
GLfloat light_position4[] = { 0.0, 0.0,2.0, 1.0 }; // 光源位置
GLfloat light_position5[] = { 0.0, 0.0,-2.0, 1.0 }; // 光源位置