-
Notifications
You must be signed in to change notification settings - Fork 2
/
particles.cpp
1266 lines (1074 loc) · 35.8 KB
/
particles.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
/* --------------------------------------------------------------------
EXTREME TUXRACER
Copyright (C) 1999-2001 Jasmin F. Patry (Tuxracer)
Copyright (C) 2010 Extreme Tuxracer Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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.
---------------------------------------------------------------------*/
#include "particles.h"
#include "textures.h"
#include "ogl.h"
#include "course.h"
#include "view.h"
#include "env.h"
// ====================================================================
// gui particles 2D
// ====================================================================
#define MAX_num_snowparticles 10000
#define BASE_num_snowparticles 800
#define GRAVITY_FACTOR 0.015
#define BASE_VELOCITY 0.05
#define VELOCITY_RANGE 0.02
#define PUSH_DECAY_TIME_CONSTANT 0.2
#define PUSH_DIST_DECAY 100
#define PUSH_FACTOR 0.5
#define MAX_PUSH_FORCE 5
#define AIR_DRAG 0.4
#define PARTICLE_MIN_SIZE 1
#define PARTICLE_SIZE_RANGE 10
typedef struct {
TVector2 pt;
double size;
TVector2 vel;
TVector2 tex_min;
TVector2 tex_max;
} TGuiParticle;
static TGuiParticle particles[MAX_num_snowparticles];
static int num_snowparticles = BASE_num_snowparticles;
static GLfloat part_col[4] = {1, 1, 1, 0.5 };
static TVector2 push_position = {0, 0};
static TVector2 last_push_position;
static double last_update_time = -1;
static bool push_position_initialized = false;
static double frand () {return (double)rand() / RAND_MAX; }
static void make_particle (int i, double x, double y) {
double p_dist;
int type;
particles[i].pt.x = x;
particles[i].pt.y = y;
p_dist = frand();
particles[i].size = PARTICLE_MIN_SIZE + (1.0 - p_dist) * PARTICLE_SIZE_RANGE;
particles[i].vel.x = 0;
particles[i].vel.y = -BASE_VELOCITY - p_dist * VELOCITY_RANGE;
type = (int) (frand() * (4.0 - EPS));
if (type == 0) {
particles[i].tex_min = MakeVector2 (0.0, 0.0);
particles[i].tex_max = MakeVector2 (0.5, 0.5);
} else if (type == 1) {
particles[i].tex_min = MakeVector2 (0.5, 0.0);
particles[i].tex_max = MakeVector2 (1.0, 0.5);
} else if (type == 2) {
particles[i].tex_min = MakeVector2 (0.5, 0.5);
particles[i].tex_max = MakeVector2 (1.0, 1.0);
} else {
particles[i].tex_min = MakeVector2 (0.0, 0.5);
particles[i].tex_max = MakeVector2 (0.5, 1.0);
}
}
void init_ui_snow (void) {
int i;
for (i=0; i<num_snowparticles; i++) make_particle (i, frand(), frand());
push_position = MakeVector2 (0.0, 0.0);
}
void update_ui_snow (double time_step) {
TVector2 *v, f;
TVector2 *pt;
double size;
double dist_from_push, p_dist;
TVector2 push_vector;
int i;
double push_timestep, time;
time = Winsys.ClockTime ();
push_vector.x = 0;
push_vector.y = 0;
push_timestep = 0;
if (push_position_initialized) {
push_vector.x = push_position.x - last_push_position.x;
push_vector.y = push_position.y - last_push_position.y;
push_timestep = time - last_update_time;
}
last_push_position = push_position;
last_update_time = time;
for (i=0; i<num_snowparticles; i++) {
pt = &particles[i].pt;
v = &particles[i].vel;
size = particles[i].size;
f.x = 0;
f.y = 0;
dist_from_push = (pow((pt->x - push_position.x), 2) +
pow((pt->y - push_position.y), 2));
if (push_timestep > 0) {
f.x = PUSH_FACTOR * push_vector.x / push_timestep;
f.y = PUSH_FACTOR * push_vector.y / push_timestep;
f.x = MIN (MAX_PUSH_FORCE, f.x);
f.x = MAX (-MAX_PUSH_FORCE, f.x);
f.y = MIN (MAX_PUSH_FORCE, f.y);
f.y = MAX (-MAX_PUSH_FORCE, f.y);
f.x *= 1.0/(PUSH_DIST_DECAY*dist_from_push + 1) *
size/PARTICLE_SIZE_RANGE;
f.y *= 1.0/(PUSH_DIST_DECAY*dist_from_push + 1) *
size/PARTICLE_SIZE_RANGE;
}
v->x += (f.x - v->x * AIR_DRAG) * time_step;
v->y += (f.y - GRAVITY_FACTOR - v->y * AIR_DRAG) * time_step;
pt->x += v->x * time_step * (size / PARTICLE_SIZE_RANGE);
pt->y += v->y * time_step * (size / PARTICLE_SIZE_RANGE);
if (pt->x < 0) {
pt->x = 1;
} else if (pt->x > 1) {
pt->x = 0.0;
}
}
for (i=0; i<num_snowparticles; i++) {
TGuiParticle *p = &particles[i];
if (p->pt.y < -0.05) {
if (num_snowparticles > BASE_num_snowparticles && frand() > 0.5) {
*p = particles[num_snowparticles-1];
num_snowparticles -= 1;
} else {
p->pt.x = frand();
p->pt.y = 1+frand()*BASE_VELOCITY;
p_dist = frand();
p->size = PARTICLE_MIN_SIZE + (1.0 - p_dist) * PARTICLE_SIZE_RANGE;
p->vel.x = 0;
p->vel.y = -BASE_VELOCITY-p_dist*VELOCITY_RANGE;
}
}
}
if (time_step < PUSH_DECAY_TIME_CONSTANT) {
push_vector.x *= 1.0 - time_step/PUSH_DECAY_TIME_CONSTANT;
push_vector.y *= 1.0 - time_step/PUSH_DECAY_TIME_CONSTANT;
} else {
push_vector.x = 0.0;
push_vector.y = 0.0;
}
}
void draw_ui_snow (void) {
TVector2 *pt, *tex_min, *tex_max;
double size;
double xres, yres;
int i;
xres = param.x_resolution;
yres = param.y_resolution;
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
Tex.BindTex (SNOW_PART);
glColor4f(part_col[0], part_col[1], part_col[2], part_col[3]);
part_col[3] = 0.3;
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
for (i=0; i<num_snowparticles; i++) {
pt = &particles[i].pt;
size = particles[i].size;
tex_min = &particles[i].tex_min;
tex_max = &particles[i].tex_max;
const float dx = pt->x * xres;
const float dy = pt->y * yres;
const GLfloat vtx[] = {
dx, dy,
dx+size, dy,
dx+size, dy+size,
dx, dy+size
};
const GLfloat tex[] = {
tex_min->x, tex_min->y,
tex_max->x, tex_min->y,
tex_max->x, tex_max->y,
tex_min->x, tex_max->y
};
glVertexPointer(2, GL_FLOAT, 0, vtx);
glTexCoordPointer(2, GL_FLOAT, 0, tex);
glDrawArrays(GL_TRIANGLE_FAN,0,4);
}
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
void reset_ui_snow_cursor_pos (TVector2 pos) {
double xres, yres;
xres = param.x_resolution;
yres = param.y_resolution;
push_position = MakeVector2 (pos.x/(double)xres, pos.y/(double)yres);
last_push_position = push_position;
push_position_initialized = true;
}
void push_ui_snow (TVector2 pos) {
double xres, yres;
xres = param.x_resolution;
yres = param.y_resolution;
push_position = MakeVector2 (pos.x/(double)xres, pos.y/(double)yres);
if (!push_position_initialized) last_push_position = push_position;
push_position_initialized = true;
}
void make_ui_snow (TVector2 pos) {
double xres, yres;
xres = param.x_resolution;
yres = param.y_resolution;
if (num_snowparticles < MAX_num_snowparticles) {
make_particle (num_snowparticles, pos.x/xres, pos.y/yres);
num_snowparticles++;
}
}
// ====================================================================
// tux particles
// ====================================================================
#define MAX_PARTICLES 500000
#define START_RADIUS 0.04
#define OLD_PART_SIZE 0.12 // orig 0.07
#define NEW_PART_SIZE 0.035 // orig 0.02
#define MIN_AGE -0.2
#define MAX_AGE 1.0
#define VARIANCE_FACTOR 0.8
#define PARTICLE_SHADOW_HEIGHT 0.05
#define PARTICLE_SHADOW_ALPHA 0.1
#define MAX_TURN_PARTICLES 500
#define BRAKE_PARTICLES 2000
#define MAX_ROLL_PARTICLES 3000
#define PARTICLE_SPEED_FACTOR 40
#define MAX_PARTICLE_ANGLE 80
#define MAX_PARTICLE_ANGLE_SPEED 50
#define PARTICLE_SPEED_MULTIPLIER 0.3
#define MAX_PARTICLE_SPEED 2
typedef struct _Particle {
TVector3 pt;
short type;
double base_size;
double cur_size;
double terrain_height;
double age;
double death;
double alpha;
TVector3 vel;
struct _Particle *next;
} Particle;
static Particle* head = NULL;
static int num_particles = 0;
void draw_billboard (CControl *ctrl,
TVector3 center_pt, double width, double height,
bool use_world_y_axis,
TVector2 min_tex_coord, TVector2 max_tex_coord)
{
TVector3 x_vec;
TVector3 y_vec;
TVector3 z_vec;
x_vec.x = ctrl->view_mat[0][0];
x_vec.y = ctrl->view_mat[0][1];
x_vec.z = ctrl->view_mat[0][2];
if (use_world_y_axis) {
y_vec = MakeVector (0, 1, 0);
x_vec = ProjectToPlane (y_vec, x_vec);
NormVector (&x_vec);
z_vec = CrossProduct (x_vec, y_vec);
} else {
y_vec.x = ctrl->view_mat[1][0];
y_vec.y = ctrl->view_mat[1][1];
y_vec.z = ctrl->view_mat[1][2];
z_vec.x = ctrl->view_mat[2][0];
z_vec.y = ctrl->view_mat[2][1];
z_vec.z = ctrl->view_mat[2][2];
}
TVector3 pt = AddVectors (center_pt, ScaleVector (-width/2.0, x_vec));
TVector3 pt0 = AddVectors (pt, ScaleVector (-height/2.0, y_vec));
TVector3 pt1 = AddVectors (pt0, ScaleVector (width, x_vec));
TVector3 pt2 = AddVectors (pt1, ScaleVector (height, y_vec));
TVector3 pt3 = AddVectors (pt2, ScaleVector (-width, x_vec));
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
const GLfloat vtx[] = {
pt0.x, pt0.y, pt0.z,
pt1.x, pt1.y, pt1.z,
pt2.x, pt2.y, pt2.z,
pt3.x, pt3.y, pt3.z
};
const GLfloat nrm[] = {
z_vec.x, z_vec.y, z_vec.z,
z_vec.x, z_vec.y, z_vec.z,
z_vec.x, z_vec.y, z_vec.z,
z_vec.x, z_vec.y, z_vec.z
};
const GLfloat tex[] = {
min_tex_coord.x, min_tex_coord.y,
max_tex_coord.x, min_tex_coord.y,
max_tex_coord.x, max_tex_coord.y,
min_tex_coord.x, max_tex_coord.y
};
glNormalPointer(GL_FLOAT, 0, nrm);
glVertexPointer(3, GL_FLOAT, 0, vtx);
glTexCoordPointer(2, GL_FLOAT, 0, tex);
glDrawArrays(GL_TRIANGLE_FAN,0,4);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
void create_new_particles (TVector3 loc, TVector3 vel, int num) {
Particle *newp;
int i;
double speed;
speed = NormVector (&vel);
if (num_particles + num > MAX_PARTICLES) {
Message ("maximum number of particles exceeded", "");
}
for (i=0; i<num; i++) {
newp = (Particle*)malloc (sizeof (Particle));
if (newp == NULL) Message ("out of memory", "");
num_particles += 1;
newp->next = head;
head = newp;
newp->pt.x = loc.x + 2.*(frand() - 0.5) * START_RADIUS;
newp->pt.y = loc.y;
newp->pt.z = loc.z + 2.*(frand() - 0.5) * START_RADIUS;
newp->type = (int) (frand() * (4.0 - EPS));
newp->base_size = (frand() + 0.5) * OLD_PART_SIZE;
newp->cur_size = NEW_PART_SIZE;
newp->age = frand() * MIN_AGE;
newp->death = frand() * MAX_AGE;
newp->vel = AddVectors (
ScaleVector (speed, vel),
MakeVector (VARIANCE_FACTOR * (frand() - 0.5) * speed,
VARIANCE_FACTOR * (frand() - 0.5) * speed,
VARIANCE_FACTOR * (frand() - 0.5) * speed ));
}
}
void update_particles (double time_step) {
Particle **p, *q;
double ycoord;
for (p = &head; *p != NULL;) {
(**p).age += time_step;
if ((**p).age < 0) continue;
(**p).pt = AddVectors ((**p).pt, ScaleVector (time_step, (**p).vel));
ycoord = Course.FindYCoord ((**p).pt.x, (**p).pt.z);
if ((**p).pt.y < ycoord - 3) {(**p).age = (**p).death + 1;}
if ((**p).age >= (**p).death) {
q = *p;
*p = q->next;
free(q);
num_particles -= 1;
continue;
}
(**p).alpha = ((**p).death - (**p).age) / (**p).death;
(**p).cur_size = NEW_PART_SIZE +
(OLD_PART_SIZE - NEW_PART_SIZE) * ((**p).age / (**p).death);
(**p).vel.y += -EARTH_GRAV * time_step;
p = &((**p).next);
}
}
void draw_particles (CControl *ctrl) {
Particle *p;
TVector2 min_tex_coord, max_tex_coord;
set_gl_options (PARTICLES);
Tex.BindTex (SNOW_PART);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glColor4f(part_col[0], part_col[1], part_col[2], part_col[3]);
part_col[3] = 0.8; // !!!!!!!!!
for (p=head; p!=NULL; p = p->next) {
if (p->age < 0) continue;
if (p->type == 0 || p->type == 1) {
min_tex_coord.y = 0;
max_tex_coord.y = 0.5;
} else {
min_tex_coord.y = 0.5;
max_tex_coord.y = 1.0;
}
if (p->type == 0 || p->type == 3) {
min_tex_coord.x = 0;
max_tex_coord.x = 0.5;
} else {
min_tex_coord.x = 0.5;
max_tex_coord.x = 1.0;
}
TColor particle_colour = Env.ParticleColor ();
glColor4f (particle_colour.r,
particle_colour.g,
particle_colour.b,
particle_colour.a * p->alpha);
draw_billboard (ctrl, p->pt, p->cur_size, p->cur_size,
false, min_tex_coord, max_tex_coord);
}
}
void clear_particles() {
Particle *p, *q;
p = head;
for (;;) {
if (p == NULL) break;
q=p;
p=p->next;
free(q);
}
head = NULL;
num_particles = 0;
}
double adjust_particle_count (double particles) {
if (particles < 1) {
if (((double) rand()) / RAND_MAX < particles) return 1.0;
else return 0.0;
} else return particles;
}
void generate_particles (CControl *ctrl, double dtime, TVector3 pos, double speed) {
TVector3 left_part_pt, right_part_pt;
double brake_particles;
double turn_particles;
double roll_particles;
double surf_y;
double left_particles, right_particles;
TVector3 left_part_vel, right_part_vel;
TMatrix rot_mat;
TVector3 xvec;
TTerrType *TerrList = Course.TerrList;
surf_y = Course.FindYCoord (pos.x, pos.z);
int id;
id = Course.GetTerrainIdx (pos.x, pos.z, 0.5);
if (id >= 0 && TerrList[id].particles > 0 && pos.y < surf_y) {
xvec = CrossProduct (ctrl->cdirection, ctrl->plane_nml);
right_part_pt = left_part_pt = pos;
right_part_pt = AddVectors (
right_part_pt,
ScaleVector (TUX_WIDTH/2.0, xvec));
left_part_pt = AddVectors (
left_part_pt,
ScaleVector (-TUX_WIDTH/2.0, xvec));
right_part_pt.y = left_part_pt.y = surf_y;
brake_particles = dtime *
BRAKE_PARTICLES * (ctrl->is_braking ? 1.0 : 0.0)
* min (speed / PARTICLE_SPEED_FACTOR, 1.0);
turn_particles = dtime * MAX_TURN_PARTICLES
* min (speed / PARTICLE_SPEED_FACTOR, 1.0);
roll_particles = dtime * MAX_ROLL_PARTICLES
* min (speed / PARTICLE_SPEED_FACTOR, 1.0);
left_particles = turn_particles *
fabs (min(ctrl->turn_fact, 0.)) +
brake_particles +
roll_particles * fabs (min(ctrl->turn_animation, 0.));
right_particles = turn_particles *
fabs (max(ctrl->turn_fact, 0.)) +
brake_particles +
roll_particles * fabs (max(ctrl->turn_animation, 0.));
left_particles = adjust_particle_count (left_particles);
right_particles = adjust_particle_count (right_particles);
RotateAboutVectorMatrix(
rot_mat, ctrl->cdirection,
MAX (-MAX_PARTICLE_ANGLE,
-MAX_PARTICLE_ANGLE * speed / MAX_PARTICLE_ANGLE_SPEED));
left_part_vel = TransformVector (rot_mat, ctrl->plane_nml);
left_part_vel = ScaleVector (MIN (MAX_PARTICLE_SPEED,
speed * PARTICLE_SPEED_MULTIPLIER),left_part_vel);
RotateAboutVectorMatrix(
rot_mat, ctrl->cdirection,
MIN (MAX_PARTICLE_ANGLE,
MAX_PARTICLE_ANGLE * speed / MAX_PARTICLE_ANGLE_SPEED));
right_part_vel = TransformVector (rot_mat, ctrl->plane_nml);
right_part_vel = ScaleVector (MIN (MAX_PARTICLE_SPEED,
speed * PARTICLE_SPEED_MULTIPLIER),right_part_vel);
create_new_particles (left_part_pt, left_part_vel,
(int)left_particles);
create_new_particles (right_part_pt, right_part_vel,
(int)right_particles);
}
}
// --------------------------------------------------------------------
// snow flakes
// --------------------------------------------------------------------
#define SNOW_WIND_DRIFT 0.1
CFlakes::CFlakes () {
for (int i=0; i<MAX_FLAKEAREAS; i++) areas[i].flakes = 0;
numAreas = 0;
}
CFlakes::~CFlakes () {
Reset ();
}
void CFlakes::Reset () {
for (int i=0; i<MAX_FLAKEAREAS; i++) {
if (areas[i].flakes != 0) delete [] areas[i].flakes;
areas[i].flakes = 0;
}
numAreas = 0;
}
static CFlakes Flakes;
void CFlakes::CreateArea (
int num_flakes,
float xrange,
float ytop,
float yrange,
float zback,
float zrange,
float minSize,
float maxSize,
float speed,
bool rotate) {
if (numAreas >= MAX_FLAKEAREAS) return;
areas[numAreas].num_flakes = num_flakes;
areas[numAreas].xrange = xrange;
areas[numAreas].ytop = ytop;
areas[numAreas].yrange = yrange;
areas[numAreas].zback = zback;
areas[numAreas].zrange = zrange;
areas[numAreas].minSize = minSize;
areas[numAreas].maxSize = maxSize;
areas[numAreas].speed = speed;
areas[numAreas].rotate_flake = rotate;
areas[numAreas].flakes = new TFlake[num_flakes];
numAreas++;
}
void CFlakes::MakeSnowFlake (int ar, int i) {
float p_dist;
int type;
areas[ar].flakes[i].pt.x = XRandom (areas[ar].left, areas[ar].right);
areas[ar].flakes[i].pt.y = -XRandom (areas[ar].top, areas[ar].bottom);
areas[ar].flakes[i].pt.z = areas[ar].back - FRandom () * (areas[ar].back - areas[ar].front);
p_dist = FRandom ();
areas[ar].flakes[i].size = XRandom (areas[ar].minSize, areas[ar].maxSize);
areas[ar].flakes[i].vel.x = 0;
areas[ar].flakes[i].vel.z = 0;
areas[ar].flakes[i].vel.y = -areas[ar].flakes[i].size * areas[ar].speed;
type = (int) (FRandom () * 3.9999);
if (type == 0) {
areas[ar].flakes[i].tex_min = MakeVector2 (0.0, 0.875);
areas[ar].flakes[i].tex_max = MakeVector2 (0.125, 1.0);
} else if (type == 1) {
areas[ar].flakes[i].tex_min = MakeVector2 (0.125, 0.875);
areas[ar].flakes[i].tex_max = MakeVector2 (0.25, 1.0);
} else if (type == 2) {
areas[ar].flakes[i].tex_min = MakeVector2 (0.0, 0.75);
areas[ar].flakes[i].tex_max = MakeVector2 (0.125, 0.875);
} else {
areas[ar].flakes[i].tex_min = MakeVector2 (0.125, 0.75);
areas[ar].flakes[i].tex_max = MakeVector2 (0.25, 0.875);
}
}
void CFlakes::GenerateSnowFlakes (CControl *ctrl) {
if (g_game.snow_id < 1) return;
snow_lastpos = ctrl->cpos;
for (int ar=0; ar<numAreas; ar++) {
for (int i=0; i<areas[ar].num_flakes; i++) MakeSnowFlake (ar, i);
}
}
void CFlakes::UpdateAreas (CControl *ctrl) {
for (int ar=0; ar<numAreas; ar++) {
areas[ar].left = ctrl->cpos.x - areas[ar].xrange / 2;
areas[ar].right = areas[ar].left + areas[ar].xrange;
areas[ar].back = ctrl->cpos.z - areas[ar].zback;
areas[ar].front = areas[ar].back - areas[ar].zrange;
areas[ar].top = ctrl->cpos.y + areas[ar].ytop;
areas[ar].bottom = areas[ar].top - areas[ar].yrange;
}
}
#define YDRIFT 0.8
#define ZDRIFT 0.6
void CFlakes::Init (int grade, CControl *ctrl) {
Reset ();
switch (grade) {
case 1:
// CreateArea (400, 5, 4, 4, -2, 4, 0.01, 0.02, 5, true);
// CreateArea (400, 12, 5, 8, 2, 8, 0.03, 0.045, 5, false);
// CreateArea (400, 30, 6, 15, 10, 15, 0.06, 0.12, 5, false);
CreateArea (400, 5, 4, 4, -2, 4, 0.015, 0.03, 5, true);
CreateArea (400, 12, 5, 8, 2, 8, 0.045, 0.07, 5, false);
CreateArea (400, 30, 6, 15, 10, 15, 0.09, 0.18, 5, false);
// CreateArea (400, 5, 4, 4, -2, 4, 0.02, 0.04, 5, true);
// CreateArea (400, 12, 5, 8, 2, 8, 0.06, 0.09, 5, false);
// CreateArea (400, 30, 6, 15, 10, 15, 0.15, 0.25, 5, false);
break;
case 2:
// CreateArea (500, 5, 4, 4, -2, 4, 0.02, 0.03, 5, true);
// CreateArea (500, 12, 5, 8, 2, 8, 0.045, 0.07, 5, false);
// CreateArea (500, 30, 6, 15, 10, 15, 0.1, 0.15, 5, false);
CreateArea (500, 5, 4, 4, -2, 4, 0.03, 0.045, 5, true);
CreateArea (500, 12, 5, 8, 2, 8, 0.07, 0.1, 5, false);
CreateArea (500, 30, 6, 15, 10, 15, 0.15, 0.22, 5, false);
// CreateArea (500, 5, 4, 4, -2, 4, 0.04, 0.06, 5, true);
// CreateArea (500, 12, 5, 8, 2, 8, 0.09, 0.15, 5, false);
// CreateArea (500, 30, 6, 15, 10, 15, 0.2, 0.32, 5, false);
break;
case 3:
// CreateArea (1000, 5, 4, 4, -2, 4, 0.025, 0.04, 5, true);
// CreateArea (1000, 12, 5, 9, 2, 8, 0.06, 0.10, 5, false);
// CreateArea (1000, 30, 6, 15, 10, 15, 0.12, 0.2, 5, false);
CreateArea (1000, 5, 4, 4, -2, 4, 0.037, 0.05, 5, true);
CreateArea (1000, 12, 5, 9, 2, 8, 0.09, 0.15, 5, false);
CreateArea (1000, 30, 6, 15, 10, 15, 0.18, 0.35, 5, false);
// CreateArea (800, 5, 4, 4, -2, 4, 0.05, 0.08, 5, true);
// CreateArea (800, 12, 5, 9, 2, 8, 0.12, 0.20, 5, false);
// CreateArea (800, 30, 6, 15, 10, 15, 0.25, 0.5, 5, false);
break;
default: break;
}
UpdateAreas (ctrl);
GenerateSnowFlakes (ctrl);
}
void CFlakes::Update (double timestep, CControl *ctrl) {
int i;
float ydiff, zdiff;
if (g_game.snow_id < 1) return;
UpdateAreas (ctrl);
zdiff = ctrl->cpos.z - snow_lastpos.z;
if (g_game.mode != GAME_OVER) {
ydiff = ctrl->cpos.y - snow_lastpos.y;
} else ydiff = 0;
TVector3 winddrift = ScaleVector (SNOW_WIND_DRIFT, Wind.WindDrift ());
float xcoeff = winddrift.x * timestep;
float ycoeff = (ydiff * YDRIFT) + (winddrift.z * timestep);
float zcoeff = (zdiff * ZDRIFT) + (winddrift.z * timestep);
for (int ar=0; ar<numAreas; ar++) {
for (i=0; i<areas[ar].num_flakes; i++) {
areas[ar].flakes[i].pt.x += xcoeff;
areas[ar].flakes[i].pt.y += areas[ar].flakes[i].vel.y * timestep + ycoeff;
areas[ar].flakes[i].pt.z += zcoeff;
if (areas[ar].flakes[i].pt.y < areas[ar].bottom) {
areas[ar].flakes[i].pt.y += areas[ar].yrange;
} else if (areas[ar].flakes[i].pt.x < areas[ar].left) {
areas[ar].flakes[i].pt.x += areas[ar].xrange;
} else if (areas[ar].flakes[i].pt.x > areas[ar].right) {
areas[ar].flakes[i].pt.x -= areas[ar].xrange;
} else if (areas[ar].flakes[i].pt.y > areas[ar].top) {
areas[ar].flakes[i].pt.y -= areas[ar].yrange;
} else if (areas[ar].flakes[i].pt.z < areas[ar].front) {
areas[ar].flakes[i].pt.z += areas[ar].zrange;
} else if (areas[ar].flakes[i].pt.z > areas[ar].back) {
areas[ar].flakes[i].pt.z -= areas[ar].zrange;
}
}
}
snow_lastpos = ctrl->cpos;
}
void CFlakes::DrawArea (int ar, CControl *ctrl) {
TVector2 *tex_min, *tex_max;
TVector3 *pt;
float size;
int i;
TFlake flake;
if (g_game.snow_id < 1) return;
TPlane lp = get_left_clip_plane ();
TPlane rp = get_right_clip_plane ();
float dir_angle (atan (ctrl->viewdir.x / ctrl->viewdir.z) * 180 / 3.14159);
set_gl_options (PARTICLES);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
Tex.BindTex (T_WIDGETS);
TColor particle_colour = Env.ParticleColor ();
glColor4f (particle_colour.r, particle_colour.g, particle_colour.b, particle_colour.a);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
for (i=0; i < areas[ar].num_flakes; i++) {
flake = areas[ar].flakes[i];
pt = &flake.pt;
size = flake.size;
tex_min = &flake.tex_min;
tex_max = &flake.tex_max;
if ((DistanceToPlane (lp, *pt) < 0) && (DistanceToPlane (rp, *pt) < 0)) {
glPushMatrix();
glTranslatef (pt->x, pt->y, pt->z);
if (areas[ar].rotate_flake) glRotatef (dir_angle, 0, 1, 0);
const GLfloat vtx[] = {
0.0, 0.0, 0.0,
size, 0.0, 0.0,
size, size, 0.0,
0.0, size, 0.0
};
const GLfloat tex[] = {
tex_min->x, tex_min->y,
tex_max->x, tex_min->y,
tex_max->x, tex_max->y,
tex_min->x, tex_max->y
};
glVertexPointer(3, GL_FLOAT, 0, vtx);
glTexCoordPointer(2, GL_FLOAT, 0, tex);
glDrawArrays(GL_TRIANGLE_FAN,0,4);
glPopMatrix();
}
}
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
void CFlakes::Draw (CControl *ctrl) {
for (int ar=0; ar<numAreas; ar++) DrawArea (ar, ctrl);
}
// --------------------------------------------------------------------
// snow curtains
// --------------------------------------------------------------------
#define NUM_CHANGES 6
#define CHANGE_DRIFT 15
#define CHANGE_SPEED 0.05
#define CURTAIN_WINDDRIFT 0.35
typedef struct {
float min;
float max;
float curr;
float step;
bool forward;
} TChange;
TChange changes[NUM_CHANGES];
void InitChanges () {
for (int i=0; i<NUM_CHANGES; i++) {
changes[i].min = XRandom (-0.15, -0.05);
changes[i].max = XRandom (0.05, 0.15);
changes[i].curr = (changes[i].min + changes[i].max) / 2;
changes[i].step = CHANGE_SPEED;
changes[i].forward = true;
}
}
void UpdateChanges (double timestep) {
TChange *ch;
for (int i=0; i<NUM_CHANGES; i++) {
ch = &changes[i];
if (ch->forward) {
ch->curr += ch->step * timestep;
if (ch->curr > ch->max) ch->forward = false;
} else {
ch->curr -= ch->step * timestep;
if (ch->curr < ch->min) ch->forward = true;
}
}
}
static CCurtain Curtain;
CCurtain::CCurtain () {}
CCurtain::~CCurtain () {}
void CCurtain::CurtainVec (float angle, float zdist, float &x, float &z) {
x = zdist * sin (angle * 3.14159 / 180);
if (angle > 90 || angle < -90) z = sqrt (zdist * zdist - x * x);
else z = -sqrt (zdist * zdist - x * x);
}
void CCurtain::Draw (CControl* /*ctrl*/) {
TVector3 *pt;
if (g_game.snow_id < 1) return;
set_gl_options (PARTICLES);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
TColor particle_colour = Env.ParticleColor ();
glColor4f (particle_colour.r, particle_colour.g, particle_colour.b, 1.0);
const GLfloat tex[] = {
0,0, 1,0, 1,1, 0,1
};
// glEnable (GL_NORMALIZE);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
for (int i=0; i<MAX_CURTAINS; i++) {
if (enabled[i]) {
Tex.BindTex (texture[i]);
float halfsize = size[i] / 2;
const GLfloat vtx[] = {
-halfsize, -halfsize, 0.0,
+halfsize, -halfsize, 0.0,
+halfsize, +halfsize, 0.0,
-halfsize, +halfsize, 0.0
};
// glNormal3f (0, 0, 1);
glVertexPointer(3, GL_FLOAT, 0, vtx);
glTexCoordPointer(2, GL_FLOAT, 0, tex);
for (int co=0; co<numCols[i]; co++) {
for (int row=0; row<numRows[i]; row++) {
pt = &curtains[i][co][row].pt;
glPushMatrix();
glTranslatef (pt->x, pt->y, pt->z);
glRotatef (-curtains[i][co][row].angle, 0, 1, 0);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glPopMatrix();
}
}
}
}
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
void CCurtain::Update (float timestep, CControl *ctrl) {
if (g_game.snow_id < 1) return;
float x, z;
TCurtainElement *curt = 0;
TVector3 drift = Wind.WindDrift ();
UpdateChanges (timestep);
for (int i=0; i<MAX_CURTAINS; i++) {
if (enabled[i]) {
for (int co=0; co<numCols[i]; co++) {
for (int row=0; row<numRows[i]; row++) {
curt = &curtains[i][co][row];
curt->angle += changes[chg[i][row]].curr * timestep * CHANGE_DRIFT;
curt->angle += drift.x * timestep * CURTAIN_WINDDRIFT;
curt->height -= speed[i] * timestep;
if (curt->angle > lastangle[i] + angledist[i]) curt->angle = startangle[i];
if (curt->angle < startangle[i] - angledist[i]) curt->angle = lastangle[i];
CurtainVec (curt->angle, zdist[i], x, z);
curt->pt.x = ctrl->cpos.x + x;
curt->pt.z = ctrl->cpos.z + z ;
curt->pt.y = ctrl->cpos.y + curt->height;
if (curt->height < minheight[i] - size[i]) curt->height += numRows[i] * size[i];
}
}
}
}
Draw (ctrl);
}
void CCurtain::Reset () {
for (int i=0; i<MAX_CURTAINS; i++) enabled[i] = false;
}
void CCurtain::GenerateCurtain (int nr, int num_rows, float z_dist, float tex_size,
float base_speed, float start_angle, float min_height, int dense) {
if (nr < 0 || nr >= MAX_CURTAINS) return;
enabled[nr] = true;
numRows[nr] = num_rows;
zdist[nr] = z_dist;
size[nr] = tex_size;
speed[nr] = base_speed;
startangle[nr] = start_angle;
minheight[nr] = min_height;
switch (dense) {
case 1: texture[nr] = T_SNOW1; break;
case 2: texture[nr] = T_SNOW2; break;
case 3: texture[nr] = T_SNOW3; break;
}
angledist[nr] = atan (size[nr] / 2 / zdist[nr]) * 360 / 3.14159;
numCols[nr] = (int)(-2 * startangle[nr] / angledist[nr]) + 1;
if (numCols[nr] > MAX_CURTAIN_COLS) numCols[nr] = MAX_CURTAIN_COLS;
lastangle[nr] = startangle[nr] + (numCols[nr]-1) * angledist[nr];
for (int i=0; i<numRows[nr]; i++) chg[nr][i] = IRandom (0, 5);
}
void CCurtain::SetStartParams (CControl *ctrl) {
float x, z;
TCurtainElement *curt = 0;
for (int i=0; i<MAX_CURTAINS; i++) {
if (enabled[i]) {
for (int co=0; co<numCols[i]; co++) {
for (int row=0; row<numRows[i]; row++) {
curt = &curtains[i][co][row];
curt->height = minheight[i] + row * size[i];
curt->angle = co * angledist[i] + startangle[i];
CurtainVec (curt->angle, zdist[i], x, z);
curt->pt.x = ctrl->cpos.x + x;
curt->pt.z = ctrl->cpos.z + z;
curt->pt.y = ctrl->cpos.y + curt->height;
}
}
}
}
}
void CCurtain::Init (CControl *ctrl) {
Reset ();
InitChanges ();
switch (g_game.snow_id) {