forked from nickzman/skyrocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Skyrocket.cpp
1180 lines (1073 loc) · 37.5 KB
/
Skyrocket.cpp
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
/*
* Copyright (C) 1999-2005 Terence M. Welsh
*
* This file is part of Skyrocket.
*
* Skyrocket is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Skyrocket is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// Skyrocket screen saver
/*#include <windows.h>
#include <stdio.h>
#include <rsWin32Saver/rsWin32Saver.h>
#include <rsText/rsText.h>
#include <math.h>
#include <time.h>
#include <vector>
#include <gl/gl.h>
#include <gl/glu.h>
#include <regstr.h>
#include <commctrl.h>
#include <resource.h>
#include <rsMath/rsMath.h>
#include <Skyrocket/particle.h>
#include <Skyrocket/world.h>*/
//#include "overlay.h"
#include "Skyrocket.h"
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <stdio.h>
#include "rsText.h"
#include <math.h>
#include <time.h>
#include <vector>
#include <list>
#include "resource.h"
#include <stdlib.h>
#include "rsMath.h"
#include "particle.h"
#include "world.h"
// Global variables
//LPCTSTR registryPath = ("Software\\Really Slick\\Skyrocket");
// HDC hdc;
// HGLRC hglrc;
// int readyToDraw = 0;
// list of particles
//std::list<particle> particles;
// Time from one frame to the next
// float elapsedTime = 0.0f;
// Window variables
/*int xsize, ysize, centerx, centery;
float aspectRatio;*/
// Camera variables
/*static rsVec lookFrom[3]; // 3 = position, target position, last position
static rsVec lookAt[3] // 3 = position, target position, last position
= {rsVec(0.0f, 1000.0f, 0.0f),
rsVec(0.0f, 1000.0f, 0.0f),
rsVec(0.0f, 1000.0f, 0.0f)};
rsVec cameraPos; // used for positioning sounds (same as lookFrom[0])
rsVec cameraVel; // used for doppler shift
*/
// Mouse variables
/*float mouseIdleTime;
int mouseButtons, mousex, mousey;
float mouseSpeed;*/
// the sound engine
SoundEngine* soundengine = NULL;
// flare display lists
//unsigned int flarelist[4];
// matrix junk for drawing flares in screen space
/*double modelMat[16], projMat[16];
GLint viewport[4];*/
// transformation needed for rendering particles
//float billboardMat[16];
// lifespans for smoke particles
/*float smokeTime[SMOKETIMES]; // lifespans of consecutive smoke particles
int whichSmoke[WHICHSMOKES]; // table to indicate which particles produce smoke
*/
// smoke display lists
//unsigned int smokelist[5];
// the world
//World* theWorld;
// text output
//rsText* textwriter;
/*int numRockets = 0;
std::vector<flareData> lensFlares;
int numFlares = 0;*/
// Parameters edited in the dialog box
/*int dMaxrockets;
int dSmoke;
int dExplosionsmoke;
int dWind;
int dAmbient;
int dStardensity;
int dFlare;
int dMoonglow;
int dMoon;
int dClouds;
int dEarth;
int dIllumination;
int dSound;*/
// Commands given from keyboard
//int kFireworks = 1;
//int kCamera = 1; // 0 = paused, 1 = autonomous, 2 = mouse control
//int kNewCamera = 0;
//bool kSlowMotion = false;
//int userDefinedExplosion = -1;
/*std::vector<particle> particles;
unsigned int last_particle = 0;
#define ZOOMROCKETINACTIVE 1000000000
unsigned int zoomRocket = ZOOMROCKETINACTIVE;*/
particle* addParticle(SkyrocketSaverSettings *inSettings){
// Advance to new particle if there is another in the vector.
// Otherwise, just overwrite the last particle (this will probably never happen)
if(inSettings->last_particle < inSettings->particles.size())
++inSettings->last_particle;
// Return pointer to new particle
return &(inSettings->particles[inSettings->last_particle-1]);
}
void removeParticle(unsigned int rempart, SkyrocketSaverSettings *inSettings){
// copy last particle over particle to be removed
--inSettings->last_particle;
if(rempart != inSettings->last_particle)
inSettings->particles[rempart] = inSettings->particles[inSettings->last_particle];
// correct zoomRocket index if necessary
if(inSettings->zoomRocket == inSettings->last_particle)
inSettings->zoomRocket = rempart;
}
void sortParticles(){
// Sorting doesn't appear to be necessary. Skyrocket still looks good without it.
}
// Rockets and explosions illuminate smoke
// Only explosions illuminate clouds
void illuminate(particle* ill,SkyrocketSaverSettings * inSettings){
float temp;
// desaturate illumination colors
rsVec newrgb(ill->rgb[0] * 0.6f + 0.4f, ill->rgb[1] * 0.6f + 0.4f, ill->rgb[2] * 0.6f + 0.4f);
// Smoke illumination
if((ill->type == ROCKET) || (ill->type == FOUNTAIN)){
float distsquared;
for(unsigned int i=0; i<inSettings->last_particle; ++i){
particle* smk(&(inSettings->particles[i]));
if(smk->type == SMOKE){
distsquared = (ill->xyz[0] - smk->xyz[0]) * (ill->xyz[0] - smk->xyz[0])
+ (ill->xyz[1] - smk->xyz[1]) * (ill->xyz[1] - smk->xyz[1])
+ (ill->xyz[2] - smk->xyz[2]) * (ill->xyz[2] - smk->xyz[2]);
if(distsquared < 40000.0f){
temp = (40000.0f - distsquared) * 0.000025f;
temp = temp * temp * ill->bright;
smk->rgb[0] += temp * newrgb[0];
if(smk->rgb[0] > 1.0f)
smk->rgb[0] = 1.0f;
smk->rgb[1] += temp * newrgb[1];
if(smk->rgb[1] > 1.0f)
smk->rgb[1] = 1.0f;
smk->rgb[2] += temp * newrgb[2];
if(smk->rgb[2] > 1.0f)
smk->rgb[2] = 1.0f;
}
}
}
}
if(ill->type == EXPLOSION){
float distsquared;
for(unsigned int i=0; i<inSettings->last_particle; ++i){
particle* smk(&(inSettings->particles[i]));
if(smk->type == SMOKE){
distsquared = (ill->xyz[0] - smk->xyz[0]) * (ill->xyz[0] - smk->xyz[0])
+ (ill->xyz[1] - smk->xyz[1]) * (ill->xyz[1] - smk->xyz[1])
+ (ill->xyz[2] - smk->xyz[2]) * (ill->xyz[2] - smk->xyz[2]);
if(distsquared < 640000.0f){
temp = (640000.0f - distsquared) * 0.0000015625f;
temp = temp * temp * ill->bright;
smk->rgb[0] += temp * newrgb[0];
if(smk->rgb[0] > 1.0f)
smk->rgb[0] = 1.0f;
smk->rgb[1] += temp * newrgb[1];
if(smk->rgb[1] > 1.0f)
smk->rgb[1] = 1.0f;
smk->rgb[2] += temp * newrgb[2];
if(smk->rgb[2] > 1.0f)
smk->rgb[2] = 1.0f;
}
}
}
}
// cloud illumination
if(ill->type == EXPLOSION && inSettings->dClouds){
int north, south, west, east; // limits of cloud indices to inspect
int halfmesh = CLOUDMESH / 2;
float distsquared;
// remember clouds have 20000-foot radius from the World class, hence 0.00005
// Hardcoded values like this are evil, but oh well
south = int((ill->xyz[2] - 1600.0f) * 0.00005f * float(halfmesh)) + halfmesh;
north = int((ill->xyz[2] + 1600.0f) * 0.00005f * float(halfmesh) + 0.5f) + halfmesh;
west = int((ill->xyz[0] - 1600.0f) * 0.00005f * float(halfmesh)) + halfmesh;
east = int((ill->xyz[0] + 1600.0f) * 0.00005f * float(halfmesh) + 0.5f) + halfmesh;
// bound these values just in case
if(south < 0) south = 0; if(south > CLOUDMESH-1) south = CLOUDMESH-1;
if(north < 0) north = 0; if(north > CLOUDMESH-1) north = CLOUDMESH-1;
if(west < 0) west = 0; if(west > CLOUDMESH-1) west = CLOUDMESH-1;
if(east < 0) east = 0; if(east > CLOUDMESH-1) east = CLOUDMESH-1;
//do any necessary cloud illumination
for(int i=west; i<=east; i++){
for(int j=south; j<=north; j++){
distsquared = (inSettings->theWorld->clouds[i][j][0] - ill->xyz[0]) * (inSettings->theWorld->clouds[i][j][0] - ill->xyz[0])
+ (inSettings->theWorld->clouds[i][j][1] - ill->xyz[1]) * (inSettings->theWorld->clouds[i][j][1] - ill->xyz[1])
+ (inSettings->theWorld->clouds[i][j][2] - ill->xyz[2]) * (inSettings->theWorld->clouds[i][j][2] - ill->xyz[2]);
if(distsquared < 2560000.0f){
temp = (2560000.0f - distsquared) * 0.000000390625f;
temp = temp * temp * ill->bright;
inSettings->theWorld->clouds[i][j][6] += temp * newrgb[0];
if(inSettings->theWorld->clouds[i][j][6] > 1.0f)
inSettings->theWorld->clouds[i][j][6] = 1.0f;
inSettings->theWorld->clouds[i][j][7] += temp * newrgb[1];
if(inSettings->theWorld->clouds[i][j][7] > 1.0f)
inSettings->theWorld->clouds[i][j][7] = 1.0f;
inSettings->theWorld->clouds[i][j][8] += temp * newrgb[2];
if(inSettings->theWorld->clouds[i][j][8] > 1.0f)
inSettings->theWorld->clouds[i][j][8] = 1.0f;
}
}
}
}
}
// pulling of other particles
void pulling(particle* suck,SkyrocketSaverSettings * inSettings){
rsVec diff;
float pulldistsquared;
float pullconst = (1.0f - suck->life) * 0.01f * inSettings->frameTime;
for(unsigned int i=0; i<inSettings->last_particle; ++i){
particle* puller(&(inSettings->particles[i]));
diff = suck->xyz - puller->xyz;
pulldistsquared = diff[0]*diff[0] + diff[1]*diff[1] + diff[2]*diff[2];
if(pulldistsquared < 250000.0f && pulldistsquared != 0.0f){
if(puller->type != SUCKER && puller->type != STRETCHER
&& puller->type != SHOCKWAVE && puller->type != BIGMAMA){
diff.normalize();
puller->vel += diff * ((250000.0f - pulldistsquared) * pullconst);
}
}
}
}
// pushing of other particles
void pushing(particle* shock,SkyrocketSaverSettings * inSettings){
rsVec diff;
float pushdistsquared;
float pushconst = (1.0f - shock->life) * 0.002f * inSettings->frameTime;
for(unsigned int i=0; i<inSettings->last_particle; ++i){
particle* pusher(&(inSettings->particles[i]));
diff = pusher->xyz - shock->xyz;
pushdistsquared = diff[0]*diff[0] + diff[1]*diff[1] + diff[2]*diff[2];
if(pushdistsquared < 640000.0f && pushdistsquared != 0.0f){
if(pusher->type != SUCKER && pusher->type != STRETCHER
&& pusher->type != SHOCKWAVE && pusher->type != BIGMAMA){
diff.normalize();
pusher->vel += diff * ((640000.0f - pushdistsquared) * pushconst);
}
}
}
}
// vertical stretching of other particles (x, z sucking; y pushing)
void stretching(particle* stretch,SkyrocketSaverSettings * inSettings){
rsVec diff;
float stretchdistsquared, temp;
float stretchconst = (1.0f - stretch->life) * 0.002f * inSettings->frameTime;
for(unsigned int i=0; i<inSettings->last_particle; ++i){
particle* stretcher(&(inSettings->particles[i]));
diff = stretch->xyz - stretcher->xyz;
stretchdistsquared = diff[0]*diff[0] + diff[1]*diff[1] + diff[2]*diff[2];
if(stretchdistsquared < 640000.0f && stretchdistsquared != 0.0f && stretcher->type != STRETCHER){
diff.normalize();
temp = (640000.0f - stretchdistsquared) * stretchconst;
stretcher->vel[0] += diff[0] * temp * 5.0f;
stretcher->vel[1] -= diff[1] * temp;
stretcher->vel[2] += diff[2] * temp * 5.0f;
}
}
}
// Makes list of lens flares. Must be a called even when action is paused
// because camera might still be moving.
void makeFlareList(SkyrocketSaverSettings * inSettings){
rsVec cameraDir, partDir;
const float shine(float(inSettings->dFlare) * 0.01f);
cameraDir = inSettings->lookAt[0] - inSettings->lookFrom[0];
cameraDir.normalize();
for(unsigned int i=0; i<inSettings->last_particle; ++i){
particle* curlight(&(inSettings->particles[i]));
if(curlight->type == EXPLOSION || curlight->type == SUCKER
|| curlight->type == SHOCKWAVE || curlight->type == STRETCHER
|| curlight->type == BIGMAMA){
double winx, winy, winz;
gluProject(curlight->xyz[0], curlight->xyz[1], curlight->xyz[2],
inSettings->modelMat, inSettings->projMat, inSettings->viewport,
&winx, &winy, &winz);
partDir = curlight->xyz - inSettings->cameraPos;
if(partDir.dot(cameraDir) > 1.0f){ // is light source in front of camera?
if(inSettings->numFlares == inSettings->lensFlares.size())
inSettings->lensFlares.resize(inSettings->lensFlares.size() + 10);
inSettings->lensFlares[inSettings->numFlares].x = (float(winx) / float(inSettings->xsize)) * inSettings->aspectRatio;
inSettings->lensFlares[inSettings->numFlares].y = float(winy) / float(inSettings->ysize);
rsVec vec = curlight->xyz - inSettings->cameraPos; // find distance attenuation factor
if(curlight->type == EXPLOSION){
inSettings->lensFlares[inSettings->numFlares].r = curlight->rgb[0];
inSettings->lensFlares[inSettings->numFlares].g = curlight->rgb[1];
inSettings->lensFlares[inSettings->numFlares].b = curlight->rgb[2];
float distatten = (10000.0f - vec.length()) * 0.0001f;
if(distatten < 0.0f)
distatten = 0.0f;
inSettings->lensFlares[inSettings->numFlares].a = curlight->bright * shine * distatten;
}
else{
inSettings->lensFlares[inSettings->numFlares].r = 1.0f;
inSettings->lensFlares[inSettings->numFlares].g = 1.0f;
inSettings->lensFlares[inSettings->numFlares].b = 1.0f;
float distatten = (20000.0f - vec.length()) * 0.00005f;
if(distatten < 0.0f)
distatten = 0.0f;
inSettings->lensFlares[inSettings->numFlares].a = curlight->bright * 2.0f * shine * distatten;
}
inSettings->numFlares++;
}
}
}
}
void randomLookFrom(int n, SkyrocketSaverSettings *inSettings){
inSettings->lookFrom[n][0] = rsRandf(6000.0f) - 3000.0f;
inSettings->lookFrom[n][1] = rsRandf(1200.0f) + 5.0f;
inSettings->lookFrom[n][2] = rsRandf(6000.0f) - 3000.0f;
}
void randomLookAt(int n, SkyrocketSaverSettings *inSettings){
inSettings->lookAt[n][0] = rsRandf(1600.0f) - 800.0f;
inSettings->lookAt[n][1] = rsRandf(800.0f) + 200.0f;
inSettings->lookAt[n][2] = rsRandf(1600.0f) - 800.0f;
}
void findHeadingAndPitch(rsVec lookFrom, rsVec lookAt, float& heading, float& pitch){
const float diffx(lookAt[0] - lookFrom[0]);
const float diffy(lookAt[1] - lookFrom[1]);
const float diffz(lookAt[2] - lookFrom[2]);
const float radius(sqrtf(diffx * diffx + diffz * diffz));
pitch = R2D * atan2f(diffy, radius);
heading = R2D * atan2f(-diffx, -diffz);
}
__private_extern__ void draw(SkyrocketSaverSettings * inSettings){
/*static float cameraAngle = 0.0f;
static const float firstHeading = rsRandf(2.0f * PIx2);
static const float firstRadius = rsRandf(2000.0f);*/
static int lastCameraMode = inSettings->kCamera;
static float cameraTime[3] // time, elapsed time, step (1.0 - 0.0)
= {20.0f, 0.0f, 0.0f};
static float fov = 60.0f;
static float zoom = 0.0f; // For interpolating from regular camera view to zoomed in view
static float zoomTime[2] = {300.0f, 0.0f}; // time until next zoom, duration of zoom
static float heading, pitch;
static float zoomHeading = 0.0f;
static float zoomPitch = 0.0f;
// Variables for printing text
static float computeTime = 0.0f;
static float drawTime = 0.0f;
//static rsTimer computeTimer, drawTimer;
// start compute time timer
//computeTimer.tick();
// super fast easter egg
static int superFast = rsRandi(1000);
if(!superFast)
inSettings->frameTime *= 5.0f;
// build viewing matrix
/*glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, aspectRatio, 1.0f, 40000.0f);
glGetDoublev(GL_PROJECTION_MATRIX, projMat);*/
////////////////////////////////
// update camera
///////////////////////////////
//static int first = 1;
if(inSettings->first){
randomLookFrom(1, inSettings); // new target position
// starting camera view is very far away
inSettings->lookFrom[2] = rsVec(rsRandf(1000.0f) + 6000.0f, 5.0f, rsRandf(4000.0f) - 2000.0f);
inSettings->textwriter = new rsText;
inSettings->first = 0;
}
// Make new random camera view
if(inSettings->kNewCamera){
cameraTime[0] = rsRandf(25.0f) + 5.0f;
cameraTime[1] = 0.0f;
cameraTime[2] = 0.0f;
// choose new positions
randomLookFrom(1, inSettings); // new target position
randomLookAt(1, inSettings); // new target position
// cut to a new view
randomLookFrom(2, inSettings); // new last position
randomLookAt(2, inSettings); // new last position
findHeadingAndPitch(inSettings->lookFrom[0], inSettings->lookAt[0], heading, pitch); // add by NZ - update theading and pitch
inSettings->kNewCamera = 0;
}
// Update the camera if it is active
if(inSettings->kCamera == 1){
if(lastCameraMode == 2){ // camera was controlled by mouse last frame
cameraTime[0] = 10.0f;
cameraTime[1] = 0.0f;
cameraTime[2] = 0.0f;
inSettings->lookFrom[2] = inSettings->lookFrom[0];
randomLookFrom(1, inSettings); // new target position
inSettings->lookAt[2] = inSettings->lookAt[0];
randomLookAt(1, inSettings); // new target position
}
cameraTime[1] += inSettings->frameTime;
cameraTime[2] = cameraTime[1] / cameraTime[0];
if(cameraTime[2] >= 1.0f){ // reset camera sequence
// reset timer
cameraTime[0] = rsRandf(25.0f) + 5.0f;
cameraTime[1] = 0.0f;
cameraTime[2] = 0.0f;
// choose new positions
inSettings->lookFrom[2] = inSettings->lookFrom[1]; // last = target
randomLookFrom(1, inSettings); // new target position
inSettings->lookAt[2] = inSettings->lookAt[1]; // last = target
randomLookAt(1, inSettings); // new target position
if(!rsRandi(4) && zoom == 0.0f){ // possibly cut to new view if camera isn't zoomed in
randomLookFrom(2, inSettings); // new last position
randomLookFrom(2, inSettings); // new last position
}
}
// change camera position and angle
float cameraStep = 0.5f * (1.0f - cosf(cameraTime[2] * PI));
inSettings->lookFrom[0] = inSettings->lookFrom[2] + ((inSettings->lookFrom[1] - inSettings->lookFrom[2]) * cameraStep);
inSettings->lookAt[0] = inSettings->lookAt[2] + ((inSettings->lookAt[1] - inSettings->lookAt[2]) * cameraStep);
// update variables used for sound and lens flares
inSettings->cameraVel = inSettings->lookFrom[0] - inSettings->cameraPos;
inSettings->cameraPos = inSettings->lookFrom[0];
// find heading and pitch
findHeadingAndPitch(inSettings->lookFrom[0], inSettings->lookAt[0], heading, pitch);
// zoom in on rockets with camera
zoomTime[0] -= inSettings->frameTime;
if(zoomTime[0] < 0.0f){
if(inSettings->zoomRocket == ZOOMROCKETINACTIVE){ // try to find a rocket to follow
for(unsigned int i=0; i<inSettings->last_particle; ++i){
if(inSettings->particles[i].type == ROCKET){
inSettings->zoomRocket = i;
if(inSettings->particles[inSettings->zoomRocket].tr > 4.0f){
zoomTime[1] = inSettings->particles[inSettings->zoomRocket].tr;
// get out of for loop if a suitable rocket has been found
i = inSettings->last_particle;
}
else
inSettings->zoomRocket = ZOOMROCKETINACTIVE;
}
}
if(inSettings->zoomRocket == ZOOMROCKETINACTIVE)
zoomTime[0] = 5.0f;
}
if(inSettings->zoomRocket != ZOOMROCKETINACTIVE){ // zoom in on this rocket
zoom += inSettings->frameTime * 0.5f;
if(zoom > 1.0f)
zoom = 1.0f;
zoomTime[1] -= inSettings->frameTime;
float h, p;
findHeadingAndPitch(inSettings->lookFrom[0], inSettings->particles[inSettings->zoomRocket].xyz, h, p);
// Don't wrap around
while(h - heading < -180.0f)
h += 360.0f;
while(h - heading > 180.0f)
h -= 360.0f;
while(zoomHeading - h < -180.0f)
zoomHeading += 360.0f;
while(zoomHeading - h > 180.0f)
zoomHeading -= 360.0f;
// Make zoomed heading and pitch follow rocket closely but not exactly.
// It would look weird because the rockets wobble sometimes.
zoomHeading += (h - zoomHeading) * 10.0f * inSettings->frameTime;
zoomPitch += (p - zoomPitch) * 5.0f * inSettings->frameTime;
// End zooming
if(zoomTime[1] < 0.0f){
inSettings->zoomRocket = ZOOMROCKETINACTIVE;
// Zoom in again no later than 3 minutes from now
zoomTime[0] = rsRandf(175.0f) + 5.0f;
}
}
}
}
// Still counting down to zoom in on a rocket,
// so keep zoomed out.
if(zoomTime[0] > 0.0f){
zoom -= inSettings->frameTime * 0.5f;
if(zoom < 0.0f)
zoom = 0.0f;
}
// Control camera with the mouse
if(inSettings->kCamera == 2){
// find heading and pitch to compute rotation component of modelview matrix
heading += 100.0f * inSettings->frameTime * inSettings->aspectRatio * float(inSettings->centerx - inSettings->mousex) / float(inSettings->xsize);
pitch += 100.0f * inSettings->frameTime * float(inSettings->centery - inSettings->mousey) / float(inSettings->ysize);
if(heading > 180.0f)
heading -= 360.0f;
if(heading < -180.0f)
heading += 360.0f;
if(pitch > 90.0f)
pitch = 90.0f;
if(pitch < -90.0f)
pitch = -90.0f;
if(inSettings->mouseButtons & MK_LBUTTON)
inSettings->mouseSpeed += 400.0f * inSettings->frameTime;
if(inSettings->mouseButtons & MK_RBUTTON)
inSettings->mouseSpeed -= 400.0f * inSettings->frameTime;
if((inSettings->mouseButtons & MK_MBUTTON) || ((inSettings->mouseButtons & MK_LBUTTON) && (inSettings->mouseButtons & MK_RBUTTON)))
inSettings->mouseSpeed = 0.0f;
if(inSettings->mouseSpeed > 4000.0f)
inSettings->mouseSpeed = 4000.0f;
if(inSettings->mouseSpeed < -4000.0f)
inSettings->mouseSpeed = -4000.0f;
// find lookFrom location to compute translation component of modelview matrix
float ch = cosf(D2R * heading);
float sh = sinf(D2R * heading);
float cp = cosf(D2R * pitch);
float sp = sinf(D2R * pitch);
inSettings->lookFrom[0][0] -= inSettings->mouseSpeed * sh * cp * inSettings->frameTime;
inSettings->lookFrom[0][1] += inSettings->mouseSpeed * sp * inSettings->frameTime;
inSettings->lookFrom[0][2] -= inSettings->mouseSpeed * ch * cp * inSettings->frameTime;
inSettings->cameraPos = inSettings->lookFrom[0];
// Calculate new lookAt position so that lens flares will be computed correctly
// and so that transition back to autonomous camera mode is smooth
inSettings->lookAt[0][0] = inSettings->lookFrom[0][0] - 500.0f * sh * cp;
inSettings->lookAt[0][1] = inSettings->lookFrom[0][1] + 500.0f * sp;
inSettings->lookAt[0][2] = inSettings->lookFrom[0][2] - 500.0f * ch * cp;
}
// Interpolate fov, heading, and pitch using zoom value
// zoom of {0,1} maps to fov of {60,6}
const float t(0.5f * (1.0f - cosf(RS_PI * zoom)));
fov = 60.0f - 54.0f * t;
heading = zoomHeading * t + heading * (1.0f - t);
pitch = zoomPitch * t + pitch * (1.0f - t);
// build viewing matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, inSettings->aspectRatio, 1.0f, 40000.0f);
glGetDoublev(GL_PROJECTION_MATRIX, inSettings->projMat);
// Build modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-pitch, 1, 0, 0);
glRotatef(-heading, 0, 1, 0);
glTranslatef(-(inSettings->lookFrom[0][0]), -(inSettings->lookFrom[0][1]), -(inSettings->lookFrom[0][2]));
// get modelview matrix for flares
glGetDoublev(GL_MODELVIEW_MATRIX, inSettings->modelMat);
// store this frame's camera mode for next frame
lastCameraMode = inSettings->kCamera;
// Update mouse idle time
/*if(kCamera == 2){
mouseIdleTime += frameTime;
if(mouseIdleTime > 300.0f) // return to autonomous camera mode after 5 minutes
kCamera = 1;
}*/
// update billboard rotation matrix for particles
glPushMatrix();
glLoadIdentity();
glRotatef(heading, 0, 1, 0);
glRotatef(pitch, 1, 0, 0);
glGetFloatv(GL_MODELVIEW_MATRIX, inSettings->billboardMat);
glPopMatrix();
// clear the screen
glClear(GL_COLOR_BUFFER_BIT);
// Slows fireworks, but not camera
if(inSettings->kSlowMotion)
inSettings->frameTime *= 0.5f;
// Make more particles if necessary (always keep 1000 extra).
// Ordinarily, you would resize as needed during the update loop, probably in the
// addParticle() function. But that logic doesn't work with this particle system
// because particles can spawn other particles. resizing the vector, and, thus,
// moving all particle addresses, doesn't work if you are in the middle of
// updating a particle.
//const unsigned int size(inSettings->particles.size());
if(inSettings->particles.size() - int(inSettings->last_particle) < 1000)
inSettings->particles.resize(inSettings->particles.size() + 1000, inSettings);
// Pause the animation?
if(inSettings->kFireworks){
// update world
inSettings->theWorld->update(inSettings->frameTime, inSettings);
// darken smoke
static float ambientlight = float(inSettings->dAmbient) * 0.01f;
for(unsigned int i=0; i<inSettings->last_particle; ++i){
particle* darkener(&(inSettings->particles[i]));
if(darkener->type == SMOKE)
darkener->rgb[0] = darkener->rgb[1] = darkener->rgb[2] = ambientlight;
}
// Change rocket firing rate
static float rocketTimer = 0.0f;
static float rocketTimeConst = 10.0f / float(inSettings->dMaxrockets);
static float changeRocketTimeConst = 20.0f;
changeRocketTimeConst -= inSettings->frameTime;
if(changeRocketTimeConst <= 0.0f){
float temp = rsRandf(4.0f);
rocketTimeConst = (temp * temp) + (10.0f / float(inSettings->dMaxrockets));
changeRocketTimeConst = rsRandf(30.0f) + 10.0f;
}
// add new rocket to list
rocketTimer -= inSettings->frameTime;
if((rocketTimer <= 0.0f) || (inSettings->userDefinedExplosion >= 0)){
if(inSettings->numRockets < inSettings->dMaxrockets){
particle* rock = addParticle(inSettings);
if(rsRandi(30) || (inSettings->userDefinedExplosion >= 0)){ // Usually launch a rocket
rock->initRocket(inSettings);
if(inSettings->userDefinedExplosion >= 0)
rock->explosiontype = inSettings->userDefinedExplosion;
else{
if(!rsRandi(2500)){ // big ones!
if(rsRandi(2))
rock->explosiontype = 19; // sucker and shockwave
else
rock->explosiontype = 20; // stretcher and bigmama
}
else{
// Distribution of regular explosions
if(rsRandi(2)){ // 0 - 2 (all types of spheres)
if(!rsRandi(10))
rock->explosiontype = 2;
else
rock->explosiontype = rsRandi(2);
}
else{
if(!rsRandi(3)) // ring, double sphere, sphere and ring
rock->explosiontype = rsRandi(3) + 3;
else{
if(rsRandi(2)){ // 6, 7, 8, 9, 10, 11
if(rsRandi(2))
rock->explosiontype = rsRandi(2) + 6;
else
rock->explosiontype = rsRandi(4) + 8;
}
else{
if(rsRandi(2)) // 12, 13, 14
rock->explosiontype = rsRandi(3) + 12;
else // 15 - 18
rock->explosiontype = rsRandi(4) + 15;
}
}
}
}
}
inSettings->numRockets++;
}
else{ // sometimes make fountains instead of rockets
rock->initFountain(inSettings);
int num_fountains = rsRandi(3);
for(int i=0; i<num_fountains; i++){
rock = addParticle(inSettings);
rock->initFountain(inSettings);
}
}
}
if(inSettings->dMaxrockets)
rocketTimer = rsRandf(rocketTimeConst);
else
rocketTimer = 60.0f; // arbitrary number since no rockets ever fire
if(inSettings->userDefinedExplosion >= 0){
inSettings->userDefinedExplosion = -1;
rocketTimer = 20.0f; // Wait 20 seconds after user launches a rocket before launching any more
}
}
// update particles
inSettings->numRockets = 0;
for(unsigned int i=0; i<inSettings->last_particle; i++){
particle* curpart(&(inSettings->particles[i]));
inSettings->particles[i].update(inSettings);
if(curpart->type == ROCKET)
inSettings->numRockets++;
curpart->findDepth(inSettings);
if(curpart->life <= 0.0f || curpart->xyz[1] < 0.0f){
switch(curpart->type){
case ROCKET:
if(curpart->xyz[1] <= 0.0f){
// move above ground for explosion so new particles aren't removed
curpart->xyz[1] = 0.1f;
curpart->vel[1] *= -0.7f;
}
if(curpart->explosiontype == 18)
curpart->initSpinner(inSettings);
else
curpart->initExplosion(inSettings);
break;
case POPPER:
switch(curpart->explosiontype){
case STAR:
curpart->explosiontype = 100;
curpart->initExplosion(inSettings);
break;
case STREAMER:
curpart->explosiontype = 101;
curpart->initExplosion(inSettings);
break;
case METEOR:
curpart->explosiontype = 102;
curpart->initExplosion(inSettings);
break;
case POPPER:
curpart->type = STAR;
curpart->rgb.set(1.0f, 0.8f, 0.6f);
curpart->t = inSettings->particles[i].tr = inSettings->particles[i].life = 0.2f;
}
break;
case SUCKER:
curpart->initShockwave(inSettings);
break;
case STRETCHER:
curpart->initBigmama(inSettings);
}
}
}
// remove particles from list
for(unsigned int i=0; i<inSettings->last_particle; i++){
particle* curpart(&(inSettings->particles[i]));
if(curpart->life <= 0.0f || curpart->xyz[1] < 0.0f)
removeParticle(i, inSettings);
}
sortParticles();
} // kFireworks
else{
// Only sort particles if they're not being updated (the camera could still be moving)
for(unsigned int i=0; i<inSettings->last_particle; i++)
inSettings->particles[i].findDepth(inSettings);
sortParticles();
}
// measure compute time
//computeTime += computeTimer.tick();
// start draw time timer
//drawTimer.tick();
// the world
inSettings->theWorld->draw(inSettings);
// draw particles
glEnable(GL_BLEND);
for(unsigned int i=0; i<inSettings->last_particle; i++)
inSettings->particles[i].draw(inSettings);
// draw lens flares
if(inSettings->dFlare){
makeFlareList(inSettings);
for(unsigned int i=0; i<inSettings->numFlares; ++i){
flare(inSettings->lensFlares[i].x, inSettings->lensFlares[i].y, inSettings->lensFlares[i].r,
inSettings->lensFlares[i].g, inSettings->lensFlares[i].b, inSettings->lensFlares[i].a, inSettings);
}
inSettings->numFlares = 0;
}
// measure draw time
//drawTime += drawTimer.tick();
// do sound stuff
if(soundengine){
float listenerOri[6];
listenerOri[0] = float(-(inSettings->modelMat[2]));
listenerOri[1] = float(-(inSettings->modelMat[6]));
listenerOri[2] = float(-(inSettings->modelMat[10]));
listenerOri[3] = float(inSettings->modelMat[1]);
listenerOri[4] = float(inSettings->modelMat[5]);
listenerOri[5] = float(inSettings->modelMat[9]);
soundengine->update(inSettings->cameraPos.v, inSettings->cameraVel.v, listenerOri, inSettings->frameTime, inSettings->kSlowMotion);
}
//draw_overlay(frameTime);
// print text
static float totalTime = 0.0f;
totalTime += inSettings->frameTime;
static std::vector<std::string> strvec;
static int frames = 0;
++frames;
if(frames == 20){
strvec.clear();
std::string str1 = " FPS = " + to_string(20.0f / totalTime);
strvec.push_back(str1);
std::string str2 = "compute time = " + to_string(computeTime / 20.0f);
strvec.push_back(str2);
std::string str3 = " draw time = " + to_string(drawTime / 20.0f);
strvec.push_back(str3);
totalTime = 0.0f;
computeTime = 0.0f;
drawTime = 0.0f;
frames = 0;
}
if(inSettings->kStatistics){
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0f, 50.0f * inSettings->aspectRatio, 0.0f, 50.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(1.0f, 48.0f, 0.0f);
glColor3f(1.0f, 0.6f, 0.0f);
inSettings->textwriter->draw(strvec);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
//wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE);
}
/*void IdleProc(){
// update time
static rsTimer timer;
frameTime = timer.tick();
if(readyToDraw && !isSuspended && !checkingPassword)
draw();
glFlush();
//wglSwapLayerBuffers(hdc, WGL_SWAP_MAIN_PLANE);
}*/
void initSaver(int width, int height,SkyrocketSaverSettings * inSettings){
//RECT rect;
// Initialize pseudorandom number generator
srand((unsigned)time(NULL));
rand(); rand(); rand(); rand(); rand();
rand(); rand(); rand(); rand(); rand();
rand(); rand(); rand(); rand(); rand();
rand(); rand(); rand(); rand(); rand();
// NZ: Set up defaults in inSettings:
inSettings->readyToDraw = 0;
inSettings->lookAt[0] = rsVec(0.0f, 1000.0f, 0.0f);
inSettings->lookAt[1] = rsVec(0.0f, 1000.0f, 0.0f);
inSettings->lookAt[2] = rsVec(0.0f, 1000.0f, 0.0f);
//inSettings->soundengine = NULL;
inSettings->numRockets = 0;
inSettings->numFlares = 0;
inSettings->kFireworks = 1;
inSettings->kNewCamera = 0;
inSettings->userDefinedExplosion = -1;
inSettings->zoomRocket = ZOOMROCKETINACTIVE;
inSettings->first = 1;
// Window initialization
//hdc = GetDC(hwnd);
//SetBestPixelFormat(hdc);
//hglrc = wglCreateContext(hdc);
//GetClientRect(hwnd, &rect);
//wglMakeCurrent(hdc, hglrc);
//xsize = rect.right - rect.left;
//ysize = rect.bottom - rect.top;
inSettings->xsize = width;
inSettings->ysize = height;
inSettings->centerx = inSettings->xsize / 2;
inSettings->centery = inSettings->ysize / 2;
glViewport(0, 0, inSettings->xsize, inSettings->ysize);
glGetIntegerv(GL_VIEWPORT, inSettings->viewport);
inSettings->aspectRatio = float(width) / float(height);
//glMatrixMode(GL_MODELVIEW_MATRIX);
// Set OpenGL state
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glFrontFace(GL_CCW);
glEnable(GL_CULL_FACE);
// Initialize data structures
initFlares(inSettings);
//initRockets();
if(inSettings->dSmoke)
initSmoke(inSettings);
inSettings->theWorld = new World(inSettings);
initShockwave();
if(inSettings->dSound && soundengine == NULL)
soundengine = new SoundEngine(float(inSettings->dSound) * 0.01f);
//init_overlay();
//outfile.open("outfile");
// Addition by NZ: If we're using a static camera, then randomize the camera.
if (inSettings->kCamera == 0)
{
inSettings->kNewCamera = 1;
}
}
__private_extern__ void setDefaults(SkyrocketSaverSettings * inSettings)
{
inSettings->dMaxrockets = 8;
inSettings->dSmoke = 5;
inSettings->dExplosionsmoke = 0;
inSettings->dWind = 20;
inSettings->dAmbient = 10;
inSettings->dStardensity = 20;
inSettings->dFlare = 20;
inSettings->dMoonglow = 20;
inSettings->dSound = 0;
inSettings->kCamera = 1;
inSettings->dMoon = 1;
inSettings->dClouds = 1;
inSettings->dEarth = 1;
inSettings->dIllumination = 1;
inSettings->kSlowMotion = false;
}
__private_extern__ void cleanup(SkyrocketSaverSettings * inSettings)
{
// Free memory
inSettings->particles.clear();
// clean up sound data structures
if(inSettings->dSound)
{
delete soundengine;
soundengine = NULL;