-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmk_animation.cc
More file actions
3124 lines (2888 loc) · 72.6 KB
/
Copy pathmk_animation.cc
File metadata and controls
3124 lines (2888 loc) · 72.6 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
#define __USE_XOPEN 1
#include <stdint.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define SIDE 11
#define NBITS 4
#define GLVLS (1<<NBITS)
/* Random integer 0 <= x < N. */
static int
irand(int n)
{
return rand() / (RAND_MAX/n+1);
}
/* Random double 0 <= x <= N. */
static double
drand(double n)
{
return (double)rand() / ((double)RAND_MAX/n);
}
/* Random unit vector of length a, uniform distribution in angular space. */
static void
vrand(double a, double *x, double *y, double *z)
{
/*
Sample a random direction uniformly.
Uses the fact that cylinder projection of the sphere is area preserving,
so sample uniformly the cylinder, and project onto the sphere.
*/
double v = drand(2*M_PI);
double u = drand(2.0) - 1.0;
double r = sqrt(1.0 - u*u);
*x = a*r*cos(v);
*y = a*r*sin(v);
*z = a*u;
}
typedef uint8_t frame_xyz[SIDE][SIDE][SIDE];
struct anim_piece {
void (*frame_func)(frame_xyz, int, void**);
int num_frames;
void *data;
};
static void
ef_clear(frame_xyz fb, uint8_t col= 0)
{
memset(fb, col, sizeof(frame_xyz));
}
static void
ef_afterglow(frame_xyz fb, unsigned subtract)
{
for (int x= 0; x < SIDE; x++)
for (int y= 0; y < SIDE; y++)
for (int z= 0; z < SIDE; z++)
fb[x][y][z]= fb[x][y][z] > subtract ? fb[x][y][z] - subtract : 0;
}
static void
ef_scroll_y_up(frame_xyz fb)
{
for (int x= 0; x < SIDE; ++x)
for (int y= SIDE-2; y >= 0; --y)
for (int z= 0; z < SIDE; ++z)
fb[x][y+1][z]= fb[x][y][z];
}
static void
ef_scroll_y_down(frame_xyz fb)
{
for (int x= 0; x < SIDE; ++x)
for (int y= 1; y < SIDE; ++y)
for (int z= 0; z < SIDE; ++z)
fb[x][y-1][z]= fb[x][y][z];
}
static void
bubble5_a(frame_xyz F, int frame, void **data)
{
ef_clear(F);
for (int x= 0; x < 5; x++)
{
for (int y= 0; y < 5; y++)
{
for (int z= 0; z < 5; z++)
{
double d= sqrt((x-2)*(x-2)+(y-2)*(y-2)+(z-2)*(z-2));
double h= (double)frame / 2.4 + d*4;
int col= (int)h % GLVLS;
F[x][y][z]= col;
}
}
}
}
static void
fade_out(frame_xyz F, int frame, void **data)
{
if (!*data)
*data= malloc(sizeof(frame_xyz));
frame_xyz *orig_F= static_cast<frame_xyz *>(*data);
if (frame == 0)
{
// Save starting frame.
memcpy(orig_F, F, sizeof(frame_xyz));
}
double fade_factor= (double)(15-frame) / 15;
if (fade_factor < 0)
fade_factor= 0;
for (int x= 0; x < SIDE; ++x)
for (int y= 0; y < SIDE; ++y)
for (int z= 0; z < SIDE; ++z)
F[x][y][z]= (int)(0.5 + (double)((*orig_F)[x][y][z]) * fade_factor);
}
/*
Draw a plane given starting point R0 and normal vector N.
For now, requires that the plane is "mostly horizontal", meaning that the
largest component of the normal is in the z direction. Later we will
generalise to arbitrary normal, by selecting the two driving directions
to be the smaller two components of the normal.
*/
static void
draw_plane(double x0, double y0, double z0, double nx, double ny, double nz,
frame_xyz F, int sidelen)
{
if (nx > nz || ny > nz)
return; /* ToDo */
/*
We span the plane with the two vectors A=(1,0,-nx/nz) and B=(0,1,-ny/nz).
These are normal to N and linearly independent, so they _do_span the plane.
And they are convenient for scan conversion, as they have unit component
in the x respectively y direction.
We shift the starting point to
Q0 = R0 - x0*A - y0*B = (0, 0, z0-x0*nx/nz-y0*ny/nz)
Then we can generate the points of the plane as simply
R = Q0 + u*A + v*B = (u, v, z0+(u-x0)*nx/nz+(v-y0)*ny/nz)
This makes it easy to scan-convert with one voxel per column.
*/
for (int i = 0; i < sidelen; ++i)
{
for (int j = 0; j < sidelen; ++j)
{
int k = round(z0 + (i-x0)*nx/nz + (j-y0)*ny/nz);
if (k >= 0 && k < sidelen)
F[i][j][k] = 15;
}
}
}
/* Draw a line from start to end. Handles clipping. */
static void
draw_line(frame_xyz F, double x0, double y0, double z0,
double x1, double y1, double z1, int val)
{
/*
Find the major axis (x, y, or z) along which the line direction has the
largest component. We will draw the line so that a plane normal to this
axis intersects the drawn line in exactly one voxel.
*/
double dx = x1 - x0;
double dy = y1 - y0;
double dz = z1 - z0;
double d, start, end, a, b, d1, d2;
int dir;
if (fabs(dx) >= fabs(dy) && fabs(dx) >= fabs(dz))
{
/*
The equation for the line is
(x,y,z) = (x0,y0,z0) + t(dx,dy,dz)
Let u = t*dx to rewrite this as
(x,y,z) = (x0,y0,z0) + u*(1,dy/dx,dz/dx)
Use this to draw along the x-axis in unit steps.
(Similar for the y and z directions).
*/
d = dx; d1 = dy; d2 = dz;
start = x0; end = x1;
a = y0; b = z0;
dir = 0;
}
else if (fabs(dy) >= fabs(dx) && fabs(dy) >= fabs(dz))
{
d = dy; d1 = dx; d2 = dz;
start = y0; end = y1;
a = x0; b = z0;
dir = 1;
}
else
{
d = dz; d1 = dx; d2 = dy;
start = z0; end = z1;
a = x0;
b = y0;
dir = 2;
}
if (d < 0)
{
/* Swap so we can draw "left-to-right" to simplify bounds conditions. */
double tmp;
tmp = start; start = end; end = tmp;
a += d1; b += d2;
d = -d; d1 = -d1; d2 = -d2;
}
int i = round(start);
if (i < 0)
i = 0;
int last_i = round(end);
if (last_i >= SIDE)
last_i = SIDE-1;
/* Adjust starting point for rounding and possibly clipping of i. */
a = a + (i - start) * (d1/d);
b = b + (i - start) * (d2/d);
while (i <= last_i)
{
int j = round(a);
int k = round(b);
if (j >= 0 && j < SIDE && k >= 0 && k < SIDE)
{
switch (dir)
{
case 0: F[i][j][k] = val; break;
case 1: F[j][i][k] = val; break;
case 2: F[j][k][i] = val; break;
}
}
a += d1/d;
b += d2/d;
++i;
}
}
/* Cross product. */
static void
ut_cross_prod(double x1, double y1, double z1,
double x2, double y2, double z2,
double *rx, double *ry, double *rz)
{
*rx = y1*z2-y2*z1;
*ry = z1*x2-z2*x1;
*rz = x1*y2-x2*y1;
}
/* Rotate plane coordinates a given angle V. */
static void ut_rotate(double *x, double *y, double v)
{
double x2 = cos(v) * *x - sin(v) * *y;
*y = sin(v) * *x + cos(v) * *y;
*x = x2;
}
/* Rotate a vector around an axis. */
static void
ut_rotate_axis(double ax, double ay, double az,
double *px, double *py, double *pz, double v)
{
/*
First we construct two unit normals to the given axis.
We do this by starting with a vector parallel to the x/y/z axis that
has the smallest component along the given axis, and then doing
cross-producs.
Then we project the vector to rotate onto the normal plane spanned by
the two normals.
We then rotate the vector in that plane, and finally contruct the
rotated vector back from the two normals and original vector.
*/
double a = sqrt(ax*ax+ay*ay+az*az);
ax /= a;
ay /= a;
az /= a;
double n1x, n1y, n1z, n2x, n2y, n2z, n1, n2;
if (fabs(ax) <= fabs(ay) && fabs(ax) <= fabs(az))
{
n1x = 1; n1y = 0; n1z = 0;
}
else if (fabs(ay) <= fabs(ax) && fabs(ay) <= fabs(az))
{
n1x = 0; n1y = 1; n1z = 0;
}
else if (fabs(az) <= fabs(ax) && fabs(az) <= fabs(ay))
{
n1x = 0; n1y = 0; n1z = 1;
}
ut_cross_prod(ax, ay, az, n1x, n1y, n1z, &n2x, &n2y, &n2z);
n2 = sqrt(n2x*n2x+n2y*n2y+n2z*n2z);
n2x /= n2;
n2y /= n2;
n2z /= n2;
ut_cross_prod(ax, ay, az, n2x, n2y, n2z, &n1x, &n1y, &n1z);
/*
n1 will be a unit vector already, as it is the cross product of two
perpendicular unit vectors. So this normalisation is really redundant ...
*/
n1 = sqrt(n1x*n1x+n1y*n1y+n1z*n1z);
n1x /= n1;
n1y /= n1;
n1z /= n1;
double x = *px; double y = *py; double z = *pz;
double c1 = x*n1x + y*n1y + z*n1z;
double c2 = x*n2x + y*n2y + z*n2z;
double c3 = x*ax + y*ay + z*az;
ut_rotate(&c1, &c2, v);
*px = c1*n1x + c2*n2x + c3*ax;
*py = c1*n1y + c2*n2y + c3*ay;
*pz = c1*n1z + c2*n2z + c3*az;
}
/* Animation with misc. wireframe objects. */
struct st_wireframe {
struct { double x1,y1,z1,x2,y2,z2; } lines[50];
int num;
};
static void
ut_wireframe_add_line(struct st_wireframe *c,
double x1, double y1, double z1,
double x2, double y2, double z2)
{
int i = c->num;
if (i < sizeof(c->lines)/sizeof(c->lines[0]))
{
c->lines[i].x1 = x1;
c->lines[i].y1 = y1;
c->lines[i].z1 = z1;
c->lines[i].x2 = x2;
c->lines[i].y2 = y2;
c->lines[i].z2 = z2;
c->num = i+1;
}
}
static void
an_wireframe(frame_xyz F, int frame, void **data)
{
static const double omega1 = 1.45/15.38;
static const double omega2 = 1.45/11.71;
static const double omega3 = 1.45/28.44;
static const int stage_dur = 175;
static const int fade_dur = 30;
if (frame == 0)
*data = malloc(sizeof(struct st_wireframe));
struct st_wireframe *c= static_cast<struct st_wireframe *>(*data);
if (frame == 0)
{
c->num = 0;
}
if (frame % stage_dur == 0)
{
c->num = 0;
switch ((frame/stage_dur) % 9)
{
case 0:
/* Cube. */
ut_wireframe_add_line(c, -3, -3, -3, 3, -3, -3);
ut_wireframe_add_line(c, -3, 3, -3, 3, 3, -3);
ut_wireframe_add_line(c, -3, -3, 3, 3, -3, 3);
ut_wireframe_add_line(c, -3, 3, 3, 3, 3, 3);
ut_wireframe_add_line(c, -3, -3, -3, -3, 3, -3);
ut_wireframe_add_line(c, 3, -3, -3, 3, 3, -3);
ut_wireframe_add_line(c, -3, -3, 3, -3, 3, 3);
ut_wireframe_add_line(c, 3, -3, 3, 3, 3, 3);
ut_wireframe_add_line(c, -3, -3, -3, -3, -3, 3);
ut_wireframe_add_line(c, 3, -3, -3, 3, -3, 3);
ut_wireframe_add_line(c, -3, 3, -3, -3, 3, 3);
ut_wireframe_add_line(c, 3, 3, -3, 3, 3, 3);
break;
case 1:
{
/* Tetraeder */
static const double M = 4.5;
double a = M*sqrt(3)/2.0;
ut_wireframe_add_line(c, -a, -0.5*M, -0.5*M, a, -0.5*M, -0.5*M);
ut_wireframe_add_line(c, -a, -0.5*M, -0.5*M, 0, M, -0.5*M);
ut_wireframe_add_line(c, 0, M, -0.5*M, a, -0.5*M, -0.5*M);
ut_wireframe_add_line(c, -a, -0.5*M, -0.5*M, 0, 0, M);
ut_wireframe_add_line(c, a, -0.5*M, -0.5*M, 0, 0, M);
ut_wireframe_add_line(c, 0, M, -0.5*M, 0, 0, M);
break;
}
case 2:
{
/* "L" */
double A = -2.5;
double B = 4;
ut_wireframe_add_line(c, -A, 0, B, -A, 0, -B);
ut_wireframe_add_line(c, -A, 0, -B, A, 0, -B);
break;
}
case 3:
case 7:
{
/* "A" */
double A = 3.4;
double B = 4;
double C = -1;
double D = A*(B-C)/(2*B);
ut_wireframe_add_line(c, -A, 0, -B, 0, 0, B);
ut_wireframe_add_line(c, A, 0, -B, 0, 0, B);
ut_wireframe_add_line(c, -D, 0, C, D, 0, C);
break;
}
case 4:
{
/* "B" */
double A = 3;
double B = 4;
double C = 2;
ut_wireframe_add_line(c, -A, 0, B, 0, 0, B);
ut_wireframe_add_line(c, 0, 0, B, A, 0, C);
ut_wireframe_add_line(c, A, 0, C, 0, 0, 0);
ut_wireframe_add_line(c, 0, 0, 0, A, 0, -C);
ut_wireframe_add_line(c, A, 0, -C, 0, 0, -B);
ut_wireframe_add_line(c, 0, 0, -B, -A, 0, -B);
ut_wireframe_add_line(c, -A, 0, -B, -A, 0, B);
break;
}
case 5:
{
/* I */
double A = -2;
double B = 4;
ut_wireframe_add_line(c, -A, 0, B, A, 0, B);
ut_wireframe_add_line(c, -A, 0, -B, A, 0, -B);
ut_wireframe_add_line(c, 0, 0, -B, 0, 0, B);
break;
}
case 6:
case 8:
{
/* T */
double A = -2.5;
double B = 4;
ut_wireframe_add_line(c, -A, 0, B, A, 0, B);
ut_wireframe_add_line(c, 0, 0, -B, 0, 0, B);
break;
}
default:
fprintf(stderr, "an_wireframe: error: fix modulus in switch() "
"to match number of objects.\n");
abort();
}
}
ef_clear(F);
for (int i = 0 ; i < c->num; ++i)
{
double x1 = c->lines[i].x1;
double y1 = c->lines[i].y1;
double z1 = c->lines[i].z1;
double x2 = c->lines[i].x2;
double y2 = c->lines[i].y2;
double z2 = c->lines[i].z2;
ut_rotate(&x1, &z1, (double)frame*omega3);
ut_rotate(&x2, &z2, (double)frame*omega3);
ut_rotate(&x1, &y1, (double)frame*omega1);
ut_rotate(&x2, &y2, (double)frame*omega1);
ut_rotate(&y1, &z1, (double)frame*omega2);
ut_rotate(&y2, &z2, (double)frame*omega2);
/* Handle fade-in / fade-out. */
int col;
double factor = -1;
int d = frame % stage_dur;
if (d < fade_dur)
factor = (double)d / (double)fade_dur;
else if (stage_dur - d < fade_dur)
factor = (double)(stage_dur - d) / (double)fade_dur;
if (factor >= 0)
{
col = round(15.49*factor);
x1 *= factor;
y1 *= factor;
z1 *= factor;
x2 *= factor;
y2 *= factor;
z2 *= factor;
}
else
col = 15;
x1 += ((double)SIDE-1)/2;
y1 += ((double)SIDE-1)/2;
z1 += ((double)SIDE-1)/2;
x2 += ((double)SIDE-1)/2;
y2 += ((double)SIDE-1)/2;
z2 += ((double)SIDE-1)/2;
draw_line(F, x1, y1, z1, x2, y2, z2, col);
}
}
struct st_fountain {
int num;
double count;
struct { double x, y, z, vx, vy, vz, damp; int base; } p[200];
};
static void
an_fountain(frame_xyz F, int frame, void **data)
{
static const int rate = 1.7;
static const double spread = 0.19*M_PI/2.0;
static const double min_damp = 0.12;
static const double max_damp = 0.35;
static const double min_height = 6.5;
static const double max_height = 10.2;
static const double radius = 0.8;
static const double g = 0.057;
static const double v_damp = 0.012;
if (frame == 0)
*data = malloc(sizeof(struct st_fountain));
struct st_fountain *c= static_cast<struct st_fountain *>(*data);
if (frame == 0)
{
c->num = 0;
}
c->count += drand(rate);
while (c->count > 0 && c->num < sizeof(c->p)/sizeof(c->p[0]))
{
/* Add a new one. */
/*
We don't want to take a uniform distribution of the vertical
angle - that would give too much bias to mostly vertical directions.
*/
double v = spread*(1 - pow(drand(1), 1.5));
double u = drand(2*M_PI);
double h = min_height + drand(max_height - min_height);
double V = sqrt(2*g*h);
double r = drand(radius);
int i = c->num++;
c->p[i].vx = V*cos(u)*sin(v);
c->p[i].vy = V*sin(u)*sin(v);
c->p[i].vz = V*cos(v);
c->p[i].x = r*cos(u) + ((double)SIDE-1)/2;
c->p[i].y = r*sin(u) + ((double)SIDE-1)/2;
c->p[i].z = -2;
c->p[i].base = frame;
c->p[i].damp = min_damp + drand(max_damp - min_damp);
--c->count;
}
for (int i = 0; i < c->num; ++i)
{
c->p[i].x += c->p[i].vx;
c->p[i].y += c->p[i].vy;
c->p[i].z += c->p[i].vz;
c->p[i].vz -= g;
c->p[i].vx -= v_damp*c->p[i].vx;
c->p[i].vy -= v_damp*c->p[i].vy;
if (c->p[i].z < 0 && frame - c->p[i].base > 5)
c->p[i].z = 0;
}
ef_clear(F);
for (int i = 0; i < c->num; )
{
int x = round(c->p[i].x);
int y = round(c->p[i].y);
int z = round(c->p[i].z);
double col = 15.0 - c->p[i].damp * (frame - c->p[i].base);
if (col <= 0)
{
/* Delete it. */
c->p[i] = c->p[--c->num];
continue;
}
if (x >= 0 && x < SIDE && y >= 0 && y < SIDE && z >= 0 && z < SIDE)
F[x][y][z] = round(col);
++i;
}
}
struct st_fireworks {
int num_phase1;
int num_phase2;
struct { double x[3],y[3],z[3],vx,vy,vz,s,col; int base_frame, delay;
double gl_base, gl_period, gl_amp; } p1[10];
struct { double x,y,z,vx,vy,vz,col; int base_frame, delay;
double fade_factor; } p2[300];
};
static void
ut_fireworks_shiftem(struct st_fireworks *c, int i)
{
for (int j = sizeof(c->p1[0].x)/sizeof(c->p1[0].x[0]) - 1; j > 0; --j)
{
c->p1[i].x[j] = c->p1[i].x[j-1];
c->p1[i].y[j] = c->p1[i].y[j-1];
c->p1[i].z[j] = c->p1[i].z[j-1];
}
}
static void
an_fireworks(frame_xyz F, int frame, void **data)
{
if (frame == 0)
*data = malloc(sizeof(struct st_fireworks));
struct st_fireworks *c= static_cast<struct st_fireworks *>(*data);
static const int max_phase1 = sizeof(c->p1)/sizeof(c->p1[0]);
static const int max_phase2 = sizeof(c->p2)/sizeof(c->p2[0]);
static const double g = 0.045;
static const int new_freq = 85;
static const double min_height = 6;
static const double max_height = 10;
static const int min_start_delay = 32;
static const int max_start_delay = 67;
static const int min_end_delay = 50;
static const int max_end_delay = 100;
const double V = 0.5;
static const double resist = 0.11;
static const double min_fade_factor = 0.22;
static const double max_fade_factor = 0.27;
if (frame == 0)
{
c->num_phase1 = 0;
c->num_phase2 = 0;
}
/* Start a new one occasionally. */
if (c->num_phase1 == 0 || (c->num_phase1 < max_phase1 && irand(new_freq) == 0))
{
int i = c->num_phase1++;
c->p1[i].x[0] = SIDE/2.0 - 2.0 + drand(4);
c->p1[i].y[0] = SIDE/2.0 - 2.0 + drand(4);
c->p1[i].z[0] = 0;
for (int j = 0; j < sizeof(c->p1[0].x)/sizeof(c->p1[0].x[0]) - 1; ++j)
ut_fireworks_shiftem(c, i);
c->p1[i].vx = drand(0.4) - 0.2;
c->p1[i].vy = drand(0.4) - 0.2;
c->p1[i].s = min_height + drand(max_height - min_height);
c->p1[i].vz = sqrt(2*g*c->p1[i].s);
c->p1[i].col = 8;
c->p1[i].base_frame = frame;
c->p1[i].delay = min_start_delay + irand(max_start_delay - min_start_delay);
c->p1[i].gl_base = frame;
c->p1[i].gl_period = 0;
}
for (int i = 0; i < c->num_phase1; )
{
int d = frame - c->p1[i].base_frame;
if (d < c->p1[i].delay)
{
/* Waiting for launch - make fuse glow effect. */
int gl_delta = frame - c->p1[i].gl_base;
if (gl_delta >= c->p1[i].gl_period)
{
c->p1[i].gl_base = frame;
c->p1[i].gl_period = 8 + irand(6);
c->p1[i].gl_amp = 0.7 + drand(0.3);
gl_delta = 0;
}
double glow = c->p1[i].gl_amp*sin((double)gl_delta/c->p1[i].gl_period*M_PI);
c->p1[i].col = round(7.0 + 5.0*glow);
++i;
}
else if (c->p1[i].z[0] > c->p1[i].s)
{
/* Kaboom! */
/* Delete this one, and create a bunch of phase2 ones (if room). */
int k = 10 + irand(20);
while (k-- > 0)
{
if (c->num_phase2 >= max_phase2)
break; /* No more room */
int j = c->num_phase2++;
/* Sample a random direction uniformly. */
double vx;
double vy;
double vz;
vrand(V, &vx, &vy, &vz);
c->p2[j].x = c->p1[i].x[0];
c->p2[j].y = c->p1[i].y[0];
c->p2[j].z = c->p1[i].z[0];
c->p2[j].vx = c->p1[i].vx + vx;
c->p2[j].vy = c->p1[i].vy + vy;
c->p2[j].vz = c->p1[i].vz + vz;
c->p2[j].col = 15;
c->p2[j].base_frame = frame;
c->p2[j].delay = min_end_delay + irand(max_end_delay - min_end_delay);
c->p2[j].fade_factor =
min_fade_factor + drand(max_fade_factor - min_fade_factor);
}
c->p1[i] = c->p1[--c->num_phase1];
}
else
{
ut_fireworks_shiftem(c, i);
c->p1[i].col =12;
c->p1[i].x[0] += c->p1[i].vx;
c->p1[i].y[0] += c->p1[i].vy;
c->p1[i].z[0] += c->p1[i].vz;
c->p1[i].vz -= g;
++i;
}
}
for (int i = 0; i < c->num_phase2;)
{
c->p2[i].x += c->p2[i].vx;
c->p2[i].y += c->p2[i].vy;
c->p2[i].z += c->p2[i].vz;
c->p2[i].vx -= resist*c->p2[i].vx;
c->p2[i].vy -= resist*c->p2[i].vy;
c->p2[i].vz -= resist*c->p2[i].vz + g;
double col = 15 - c->p2[i].fade_factor*(frame - c->p2[i].base_frame);
c->p2[i].col = col < 0 ? 0 : round(col);
if (c->p2[i].z <= 0)
{
c->p2[i].z = 0;
if (c->p2[i].delay-- <= 0)
{
/* Delete it. */
c->p2[i] = c->p2[--c->num_phase2];
}
else
++i;
}
else
++i;
}
ef_clear(F);
/*
Draw stage2 first, so we don't overwrite a new rocket with an old, dark
ember.
*/
for (int i = 0; i < c->num_phase2; ++i)
{
int x = round(c->p2[i].x);
int y = round(c->p2[i].y);
int z = round(c->p2[i].z);
if (x >= 0 && x < SIDE && y >= 0 && y < SIDE && z >= 0 && z < SIDE)
F[x][y][z] = round(c->p2[i].col);
}
for (int i = 0; i < c->num_phase1; ++i)
{
for (int j = 0; j < sizeof(c->p1[0].x)/sizeof(c->p1[0].x[0]); ++j)
{
int x = round(c->p1[i].x[j]);
int y = round(c->p1[i].y[j]);
int z = round(c->p1[i].z[j]);
if (x >= 0 && x < SIDE && y >= 0 && y < SIDE && z >= 0 && z < SIDE)
F[x][y][z] = round(c->p1[i].col);
}
}
}
static const char *
font9[256];
static void
init_font9()
{
font9['A']=
" X "
" XXX "
" XXX "
" XX XX "
" XX XX "
" XXXXXXX "
" XXXXXXX "
"XX XX"
"XX XX";
font9['B']=
"XXXXX "
"XXXXXX"
"XX XX"
"XX XX"
"XXXXX "
"XX XX"
"XX XX"
"XXXXXX"
"XXXXX ";
font9['I']=
"XXXXXX"
"XXXXXX"
" XX "
" XX "
" XX "
" XX "
" XX "
"XXXXXX"
"XXXXXX";
font9['L']=
"XX "
"XX "
"XX "
"XX "
"XX "
"XX "
"XX "
"XXXXXXX"
"XXXXXXX";
font9['T']=
"XXXXXXXX"
"XXXXXXXX"
" XX "
" XX "
" XX "
" XX "
" XX "
" XX "
" XX ";
}
static void
an_flytext9(frame_xyz F, int frame, void **data)
{
static const int inter_letter_spacing= 8;
const char *text= (const char *)*data;
if ((frame % 2) == 0)
ef_afterglow(F, 2);
frame/= 2;
for (int y= 0; y < SIDE ; ++y)
{
if (((y - frame) % inter_letter_spacing) != 0)
continue;
int idx= (frame + (SIDE-1-y))/inter_letter_spacing;
int ch= text[idx % strlen(text)];
const char *glyph= font9[ch];
if (!glyph)
continue;
int glyph_size= strlen(glyph);
int glyph_width= glyph_size/9;
for (int z= 9; z >= 1; --z)
{
for (int i= 0; i < glyph_width; ++i)
{
if (*glyph++ != ' ')
F[i+(SIDE-glyph_width)/2][y][z]= 15;
}
}
}
}
static void
an_scrolltext_9(frame_xyz F, int frame, void **data)
{
const char *text= (const char *)*data;
/* Find the total text length, in pixels. */
size_t len = 0;
const char *p = text;
while (*p)
{
const char *glyph = font9[*p++];
if (!glyph)
continue;
len += strlen(glyph)/9 + 2;
}
ef_clear(F);
/* There are 4*(SIDE-1) positions available. */
int pos= (frame/2) % (len + 4*(SIDE-1)) - (4*(SIDE-1)-1);
int cur_pos = -1;
int cur_glyph_pos = -1;
int glyph_width = -2;
int cur_idx = -1;
const char *cur_glyph;
for (int i= 0; i < 4*(SIDE-1); ++i, ++pos)
{
if (pos < 0 || (size_t)pos >= len)
continue;
int x, y;
if (i < SIDE)
{
x= 0;
y= (SIDE-1) - i;
}
else if (i < 2*SIDE-1)
{
x= i - (SIDE-1);
y= 0;
}
else if (i < 3*SIDE-2)
{
x= (SIDE-1);
y = i - (2*SIDE-2);
}
else
{
x= 4*(SIDE-1) - i;
y= (SIDE-1);
}
uint8_t col;
if (i < 3)
col= 3 + i*4;
else if (i > 4*(SIDE-1)-4)
col= 3 + (4*(SIDE-1)-3-i)*4;
else
col= 15;
/* Find the correct glyph position, possibly moving to the next glyph. */
while (cur_pos < pos)
{
++cur_pos;
++cur_glyph_pos;
while (cur_glyph_pos >= glyph_width+2)
{
cur_idx = (cur_idx + 1) % strlen(text);
cur_glyph = font9[text[cur_idx]];
cur_glyph_pos = 0;
if (cur_glyph)
glyph_width = strlen(cur_glyph)/9;
else
glyph_width = -2;
}
}
for (int z= 0; z < 9; z++)
{
if (cur_glyph_pos < glyph_width &&
cur_glyph[(8-z)*glyph_width + cur_glyph_pos] != ' ')
F[x][y][z+1]= col;
}
}
}
struct st_migrating_dots {
struct { double x,y,z,v; int target, delay, col, new_col; } dots[SIDE*SIDE];
/* 0/1 is bottom/top, 2/3 is left/right, 4/5 is front/back. */
int start_plane, end_plane;
int base_frame;
int wait;
int stage1;
int text_idx;
};
static const int migrating_dots_col1 = 15;
static const int migrating_dots_col2 = 9;
static int
ut_migrating_dots_get_colour(struct st_migrating_dots *c, int idx,
const char *glyph, int glyph_width)
{
if (!glyph)
return migrating_dots_col1;
int x, y;
switch(c->end_plane)
{
case 0:
case 1:
if (c->start_plane/2 == 1)
{
x = c->dots[idx].target;
y = (SIDE-1) - c->dots[idx].y;
}
else
{
x = c->dots[idx].x;
y = (SIDE-1) - c->dots[idx].target;
}