-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathstarfield.cpp
More file actions
3156 lines (2486 loc) · 84.2 KB
/
Copy pathstarfield.cpp
File metadata and controls
3156 lines (2486 loc) · 84.2 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
/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#include "globalincs/pstypes.h"
#include <climits>
#include <string>
#include "freespace.h"
#include "cmdline/cmdline.h"
#include "debugconsole/console.h"
#include "graphics/matrix.h"
#include "graphics/paths/PathRenderer.h"
#include "hud/hud.h"
#include "hud/hudtarget.h"
#include "io/timer.h"
#include "lighting/lighting.h"
#include "math/vecmat.h"
#include "mission/missionparse.h"
#include "model/modelrender.h"
#include "nebula/neb.h"
#include "options/Option.h"
#include "osapi/dialogs.h"
#include "parse/parselo.h"
#include "render/3d.h"
#include "render/batching.h"
#include "starfield/nebula.h"
#include "starfield/starfield.h"
#include "starfield/supernova.h"
#include "tracing/tracing.h"
#include "utils/Random.h"
#define DEBRIS_ROT_MIN 10000
#define DEBRIS_ROT_RANGE 8
#define DEBRIS_ROT_RANGE_SCALER 10000
#define RND_MAX_MASK 0x3fff
#define HALF_RND_MAX 0x2000
#define MAX_MOTION_DEBRIS 300
typedef struct {
vec3d pos;
int vclip;
float size;
} motion_debris_instance;
const float MAX_DIST_RANGE = 80.0f;
const float MIN_DIST_RANGE = 14.0f;
const float BASE_SIZE = 0.04f;
const float BASE_SIZE_NEB = 0.15f;
static int Subspace_model_inner = -1;
static int Subspace_model_outer = -1;
static int Rendering_to_env = 0;
int Num_stars = 500;
// A timestamp for animated skyboxes -MageKing17
TIMESTAMP Skybox_timestamp;
#define MAX_FLARE_COUNT 10
#define MAX_FLARE_BMP 6
typedef struct flare_info {
float pos;
float scale;
int tex_num;
} flare_info;
typedef struct flare_bitmap {
char filename[MAX_FILENAME_LEN];
int bitmap_id;
} flare_bitmap;
// global info (not individual instances)
typedef struct starfield_bitmap {
char filename[MAX_FILENAME_LEN]; // bitmap filename
char glow_filename[MAX_FILENAME_LEN]; // only for suns
int bitmap_id; // bitmap handle
int n_frames;
int fps;
int glow_bitmap; // only for suns
int glow_n_frames;
int glow_fps;
int xparent;
float r, g, b, i; // only for suns
int glare; // only for suns
int flare; // Is there a lens-flare for this sun?
flare_info flare_infos[MAX_FLARE_COUNT]; // each flare can use a texture in flare_bmp, with different scale
flare_bitmap flare_bitmaps[MAX_FLARE_BMP]; // bitmaps for different lens flares (can be re-used)
int n_flares; // number of flares actually used
int n_flare_bitmaps; // number of flare bitmaps available
int used_this_level;
int preload;
} starfield_bitmap;
// starfield bitmap instance
typedef struct starfield_bitmap_instance {
float scale_x, scale_y; // x and y scale
int div_x, div_y; // # of x and y divisions
angles ang; // angles from FRED
int star_bitmap_index; // index into starfield_bitmap array
int n_verts;
vertex *verts;
starfield_bitmap_instance() : scale_x(1.0f), scale_y(1.0f), div_x(1), div_y(1), star_bitmap_index(0), n_verts(0), verts(NULL) {
ang.p = 0.0f;
ang.b = 0.0f;
ang.h = 0.0f;
}
} starfield_bitmap_instance;
// for drawing cool stuff on the background - comes from a table
static SCP_vector<starfield_bitmap> Starfield_bitmaps;
static SCP_vector<starfield_bitmap_instance> Starfield_bitmap_instances;
// sun bitmaps and sun glow bitmaps
static SCP_vector<starfield_bitmap> Sun_bitmaps;
static SCP_vector<starfield_bitmap_instance> Suns;
// Goober5000
int Cur_background = -1;
SCP_vector<background_t> Backgrounds;
SCP_vector<int> Preload_background_indexes;
void stars_preload_background(int background_idx);
int last_stars_filled = 0;
color star_colors[8];
color star_aacolors[8];
typedef struct vDist {
int x;
int y;
} vDist;
std::unique_ptr<star[]> Stars = make_unique<star[]>(MAX_STARS);
motion_debris_instance Motion_debris[MAX_MOTION_DEBRIS];
SCP_vector<motion_debris_types> Motion_debris_info;
motion_debris_bitmaps* Motion_debris_ptr = nullptr;
// background data
int Stars_background_inited = 0; // if we're inited
int Nmodel_num = -1; // model num
int Nmodel_instance_num = -1; // model instance num
matrix Nmodel_orient = IDENTITY_MATRIX; // model orientation
int Nmodel_flags = DEFAULT_NMODEL_FLAGS; // model flags
int Nmodel_bitmap = -1; // model texture
bool Dynamic_environment = false;
bool Motion_debris_override = false;
bool Motion_debris_enabled = true;
auto MotionDebrisOption = options::OptionBuilder<bool>("Graphics.MotionDebris",
std::pair<const char*, int>{"Motion Debris", 1713},
std::pair<const char*, int>{"Enable or disable visible motion debris", 1714})
.category(std::make_pair("Graphics", 1825))
.bind_to_once(&Motion_debris_enabled)
.default_val(true)
.level(options::ExpertLevel::Advanced)
.importance(67)
.finish();
static int Default_env_map = -1;
static int Mission_env_map = -1;
static bool Env_cubemap_drawn = false;
static bool Irr_cubemap_drawn = false;
int get_motion_debris_by_name(const SCP_string &name)
{
int count = static_cast<int>(Motion_debris_info.size());
for (int i = 0; i < count; i++) {
if (lcase_equal(Motion_debris_info[i].name, name)) {
return i;
}
}
return -1;
}
void stars_release_motion_debris(motion_debris_bitmaps* vclips)
{
if (vclips == nullptr)
return;
for (int i = 0; i < MAX_MOTION_DEBRIS_BITMAPS; i++) {
if ( (vclips[i].bm >= 0) && bm_release(vclips[i].bm) ) {
vclips[i].bm = -1;
vclips[i].nframes = -1;
}
}
}
void stars_load_motion_debris(motion_debris_bitmaps* vclips)
{
Assertion(vclips != nullptr, "Motion debris not loaded!");
for (int i = 0; i < MAX_MOTION_DEBRIS_BITMAPS; i++) {
vclips[i].bm = bm_load_animation( vclips[i].name, &vclips[i].nframes, nullptr, nullptr, nullptr, true );
if ( vclips[i].bm < 0 ) {
// try loading it as a single bitmap
vclips[i].bm = bm_load(Motion_debris_ptr[i].name);
vclips[i].nframes = 1;
if (vclips[i].bm <= 0) {
Error( LOCATION, "Couldn't load animation/bitmap '%s'\n", vclips[i].name );
}
}
}
}
void stars_load_debris(int fullneb, const SCP_string &custom_name)
{
if (!Motion_debris_enabled || Motion_debris_info.empty()) {
return;
}
SCP_string debris_name = "";
if (!custom_name.empty()) {
debris_name = custom_name;
}
// if not using custom the load the default type for the mission
if (fullneb && debris_name.empty()) { // if we're in nebula mode
debris_name = "nebula";
} else if (debris_name.empty()) {
debris_name = "default";
}
int debris_index = get_motion_debris_by_name(debris_name);
//If we can't find the motion debris that's been called for then warn and
//set debris override to prevent downstream errors. Then abort.
if (debris_index < 0) {
Warning(LOCATION, "Motion debris '%s' not found in stars.tbl!\n", debris_name.c_str());
Motion_debris_override = true;
Motion_debris_ptr = nullptr;
return;
}
stars_release_motion_debris(Motion_debris_ptr);
stars_load_motion_debris(Motion_debris_info[debris_index].bitmaps);
Motion_debris_ptr = Motion_debris_info[debris_index].bitmaps;
}
const int MAX_PERSPECTIVE_DIVISIONS = 5;
const float p_phi = 10.0f, p_theta = 10.0f;
extern void stars_project_2d_onto_sphere( vec3d *pnt, float rho, float phi, float theta );
static void starfield_create_bitmap_buffer(const size_t si_idx)
{
vec3d s_points[MAX_PERSPECTIVE_DIVISIONS+1][MAX_PERSPECTIVE_DIVISIONS+1];
vertex v[4];
matrix m;
int idx, s_idx;
float ui, vi;
starfield_bitmap_instance *sbi = &Starfield_bitmap_instances[si_idx];
angles *a = &sbi->ang;
float scale_x = sbi->scale_x;
float scale_y = sbi->scale_y;
int div_x = sbi->div_x;
int div_y = sbi->div_y;
// cap division values
// div_x = div_x > MAX_PERSPECTIVE_DIVISIONS ? MAX_PERSPECTIVE_DIVISIONS : div_x;
div_x = 1;
div_y = div_y > MAX_PERSPECTIVE_DIVISIONS ? MAX_PERSPECTIVE_DIVISIONS : div_y;
if (sbi->verts != NULL) {
delete [] sbi->verts;
}
sbi->verts = new(std::nothrow) vertex[div_x * div_y * 6];
if (sbi->verts == NULL) {
sbi->star_bitmap_index = -1;
return;
}
sbi->n_verts = div_x * div_y * 6;
// texture increment values
ui = 1.0f / (float)div_x;
vi = 1.0f / (float)div_y;
// adjust for aspect ratio
//scale_x *= (gr_screen.clip_aspect + 0.55f); // fudge factor
//scale_x *= (gr_screen.clip_aspect + (0.7333333f/gr_screen.clip_aspect)); // fudge factor
scale_x *= 1.883333f; //fudge factor
float s_phi = 0.5f + (((p_phi * scale_x) / 360.0f) / 2.0f);
float s_theta = (((p_theta * scale_y) / 360.0f) / 2.0f);
float d_phi = -(((p_phi * scale_x) / 360.0f) / (float)(div_x));
float d_theta = -(((p_theta * scale_y) / 360.0f) / (float)(div_y));
// bank matrix
vm_angles_2_matrix(&m, a);
// generate the bitmap points
for(idx=0; idx<=div_x; idx++) {
for(s_idx=0; s_idx<=div_y; s_idx++) {
// get world spherical coords
stars_project_2d_onto_sphere(&s_points[idx][s_idx], 1000.0f, s_phi + ((float)idx*d_phi), s_theta + ((float)s_idx*d_theta));
// (un)rotate on the sphere
vm_vec_unrotate(&s_points[idx][s_idx], &s_points[idx][s_idx], &m);
}
}
memset(v, 0, sizeof(vertex) * 4);
int j = 0;
vertex *verts = sbi->verts;
for (idx = 0; idx < div_x; idx++) {
for (s_idx = 0; s_idx < div_y; s_idx++) {
// stuff texture coords
v[0].texture_position.u = ui * float(idx);
v[0].texture_position.v = vi * float(s_idx);
v[1].texture_position.u = ui * float(idx+1);
v[1].texture_position.v = vi * float(s_idx);
v[2].texture_position.u = ui * float(idx+1);
v[2].texture_position.v = vi * float(s_idx+1);
v[3].texture_position.u = ui * float(idx);
v[3].texture_position.v = vi * float(s_idx+1);
g3_transfer_vertex(&v[0], &s_points[idx][s_idx]);
g3_transfer_vertex(&v[1], &s_points[idx+1][s_idx]);
g3_transfer_vertex(&v[2], &s_points[idx+1][s_idx+1]);
g3_transfer_vertex(&v[3], &s_points[idx][s_idx+1]);
// poly 1
verts[j++] = v[0];
verts[j++] = v[1];
verts[j++] = v[2];
// poly 2
verts[j++] = v[0];
verts[j++] = v[2];
verts[j++] = v[3];
}
}
Assert( j == sbi->n_verts );
}
// take the Starfield_bitmap_instances[] and make all the vertex buffers that you'll need to draw it
static void starfield_generate_bitmap_buffers()
{
auto sb_instances = Starfield_bitmap_instances.size();
for (size_t idx = 0; idx < sb_instances; idx++)
{
if (Starfield_bitmap_instances[idx].star_bitmap_index < 0)
continue;
starfield_create_bitmap_buffer(idx);
}
}
static void starfield_bitmap_entry_init(starfield_bitmap *sbm)
{
int i;
Assert( sbm != NULL );
memset( sbm, 0, sizeof(starfield_bitmap) );
sbm->bitmap_id = -1;
sbm->glow_bitmap = -1;
sbm->glow_n_frames = 1;
for (i = 0; i < MAX_FLARE_BMP; i++) {
sbm->flare_bitmaps[i].bitmap_id = -1;
}
for (i = 0; i < MAX_FLARE_COUNT; i++) {
sbm->flare_infos[i].tex_num = -1;
}
}
#define CHECK_END() { \
if (in_check) { \
required_string("#end"); \
in_check = false; \
run_count = 0; \
} \
}
void parse_startbl(const char *filename)
{
char name[MAX_FILENAME_LEN], tempf[16];
starfield_bitmap sbm;
int idx;
bool in_check = false;
int rc = -1;
int run_count = 0;
try
{
read_file_text(filename, CF_TYPE_TABLES);
reset_parse();
// freaky! ;)
while (!check_for_eof()) {
optional_string("#Background Bitmaps");
while ((rc = optional_string_either("$Bitmap:", "$BitmapX:")) != -1) {
in_check = true;
starfield_bitmap_entry_init(&sbm);
stuff_string(sbm.filename, F_NAME, MAX_FILENAME_LEN);
sbm.xparent = rc; // 0 == intensity alpha bitmap, 1 == green xparency bitmap
if ((idx = stars_find_bitmap(sbm.filename)) >= 0) {
if (sbm.xparent == Starfield_bitmaps[idx].xparent) {
if (!Parsing_modular_table)
Warning(LOCATION, "Starfield bitmap '%s' listed more than once!! Only using the first entry!", sbm.filename);
}
else {
Warning(LOCATION, "Starfield bitmap '%s' already listed as a %s bitmap!! Only using the xparent version!",
sbm.filename, (rc) ? "xparent" : "non-xparent");
}
}
else {
Starfield_bitmaps.push_back(sbm);
}
}
CHECK_END();
optional_string("#Stars");
while (optional_string("$Sun:")) {
in_check = true;
starfield_bitmap_entry_init(&sbm);
stuff_string(sbm.filename, F_NAME, MAX_FILENAME_LEN);
// associated glow
required_string("$Sunglow:");
stuff_string(sbm.glow_filename, F_NAME, MAX_FILENAME_LEN);
// associated lighting values
required_string("$SunRGBI:");
stuff_float(&sbm.r);
stuff_float(&sbm.g);
stuff_float(&sbm.b);
stuff_float(&sbm.i);
if (optional_string("$SunSpecularRGB:")) {
SCP_string warning;
sprintf(warning, "Sun %s tried to set SunSpecularRGB. This feature has been deprecated and will be ignored.", sbm.filename);
float spec_r, spec_g, spec_b;
stuff_float(&spec_r);
stuff_float(&spec_g);
stuff_float(&spec_b);
if (fl_equal(sbm.r, spec_r) && fl_equal(sbm.g, spec_g) && fl_equal(sbm.b, spec_b))
mprintf(("%s\n", warning.c_str())); // default case is not significant
else
Warning(LOCATION, "%s", warning.c_str()); // customized case is significant
}
// lens flare stuff
if (optional_string("$Flare:")) {
sbm.flare = 1;
required_string("+FlareCount:");
stuff_int(&sbm.n_flares);
// if there's a flare, it has to have at least one texture
required_string("$FlareTexture1:");
stuff_string(sbm.flare_bitmaps[0].filename, F_NAME, MAX_FILENAME_LEN);
sbm.n_flare_bitmaps = 1;
for (idx = 1; idx < MAX_FLARE_BMP; idx++) {
// allow 9999 textures (theoretically speaking, that is)
sprintf(tempf, "$FlareTexture%d:", idx + 1);
if (optional_string(tempf)) {
sbm.n_flare_bitmaps++;
stuff_string(sbm.flare_bitmaps[idx].filename, F_NAME, MAX_FILENAME_LEN);
}
// else break; //don't allow flaretexture1 and then 3, etc.
}
required_string("$FlareGlow1:");
required_string("+FlareTexture:");
stuff_int(&sbm.flare_infos[0].tex_num);
required_string("+FlarePos:");
stuff_float(&sbm.flare_infos[0].pos);
required_string("+FlareScale:");
stuff_float(&sbm.flare_infos[0].scale);
sbm.n_flares = 1;
for (idx = 1; idx < MAX_FLARE_COUNT; idx++) {
// allow a lot of glows
sprintf(tempf, "$FlareGlow%d:", idx + 1);
if (optional_string(tempf)) {
sbm.n_flares++;
required_string("+FlareTexture:");
stuff_int(&sbm.flare_infos[idx].tex_num);
required_string("+FlarePos:");
stuff_float(&sbm.flare_infos[idx].pos);
required_string("+FlareScale:");
stuff_float(&sbm.flare_infos[idx].scale);
}
// else break; //don't allow "flare 1" and then "flare 3"
}
}
sbm.glare = !optional_string("$NoGlare:");
sbm.xparent = 1;
if ((idx = stars_find_sun(sbm.filename)) >= 0) {
if (Parsing_modular_table)
Sun_bitmaps[idx] = sbm;
else
Warning(LOCATION, "Sun bitmap '%s' listed more than once!! Only using the first entry!", sbm.filename);
}
else {
Sun_bitmaps.push_back(sbm);
}
}
CHECK_END();
optional_string("#Motion Debris");
// normal debris pieces
// leaving this for retail - Mjn
if (check_for_string("$Debris:")) {
mprintf(("Using deprecated motion debris parsing for Default motion debris!\n"));
motion_debris_types this_debris;
this_debris.name = "Default";
int count = 0;
while (optional_string("$Debris:")) {
in_check = true;
stuff_string(name, F_NAME, MAX_FILENAME_LEN);
if (count < MAX_MOTION_DEBRIS_BITMAPS) {
strcpy_s(this_debris.bitmaps[count++].name, name);
} else {
Warning(LOCATION, "Could not load normal motion debris '%s'; maximum of %d exceeded.", name, MAX_MOTION_DEBRIS_BITMAPS);
}
}
if (count == MAX_MOTION_DEBRIS_BITMAPS) {
Motion_debris_info.push_back(this_debris);
} else {
error_display(0, "Not enough bitmaps defined for motion debris '%s'. Skipping!\n", this_debris.name.c_str());
}
}
CHECK_END();
// nebula debris pieces
// leaving this for retail - Mjn
if (check_for_string("$DebrisNeb:")) {
mprintf(("Using deprecated motion debris parsing for Nebula motion debris!\n"));
motion_debris_types this_debris;
this_debris.name = "Nebula";
int count = 0;
while (optional_string("$DebrisNeb:")) {
in_check = true;
stuff_string(name, F_NAME, MAX_FILENAME_LEN);
if (count < MAX_MOTION_DEBRIS_BITMAPS) {
strcpy_s(this_debris.bitmaps[count++].name, name);
} else {
Warning(LOCATION, "Could not load nebula motion debris '%s'; maximum of %d exceeded.", name, MAX_MOTION_DEBRIS_BITMAPS);
}
}
if (count == MAX_MOTION_DEBRIS_BITMAPS) {
Motion_debris_info.push_back(this_debris);
} else {
error_display(0, "Not enough bitmaps defined for motion debris '%s'. Skipping!\n", this_debris.name.c_str());
}
}
CHECK_END();
// custom debris pieces
while (optional_string("$Motion Debris Name:")) {
in_check = true;
stuff_string(name, F_NAME, MAX_NAME_LEN);
motion_debris_types this_debris;
this_debris.name = name;
// check if we will replace an existing entry
int check = get_motion_debris_by_name(name);
motion_debris_types* debris_ptr;
//If we're going to create a new motion debris then set it up
if (check == -1) {
// initialize all the names as empty strings for later checking
for (int i = 0; i < MAX_MOTION_DEBRIS_BITMAPS; i++) {
this_debris.bitmaps[i].name[0] = '\0';
}
Motion_debris_info.push_back(this_debris);
check = static_cast<int>(Motion_debris_info.size()) - 1;
}
debris_ptr = &Motion_debris_info[check];
int count = 0;
while (count < MAX_MOTION_DEBRIS_BITMAPS){
required_string("+Bitmap:");
stuff_string(name, F_NAME, MAX_FILENAME_LEN);
strcpy_s(debris_ptr->bitmaps[count++].name, name);
}
for (int i = 0; i < MAX_MOTION_DEBRIS_BITMAPS; i++) {
if(debris_ptr->bitmaps[i].name[0] == '\0'){
error_display(0, "Not enough bitmaps defined for motion debris '%s'. Removing!\n", this_debris.name.c_str());
Motion_debris_info.erase(Motion_debris_info.begin() + check);
break;
}
}
}
CHECK_END();
// since it's possible for some idiot to have a tbl screwed up enough
// that this ends up in an endless loop, give an opportunity to advance
// through the file no matter what, because even the retail tbl has an
// extra "#end" line in it.
if (optional_string("#end") || (run_count++ > 5)) {
run_count = 0;
advance_to_eoln(NULL);
}
}
}
catch (const parse::ParseException& e)
{
mprintf(("TABLES: Unable to parse '%s'! Error message = %s.\n", filename, e.what()));
return;
}
}
void stars_load_all_bitmaps()
{
static int Star_bitmaps_loaded = 0;
if (Star_bitmaps_loaded)
return;
// pre-load all starfield bitmaps. ONLY SHOULD DO THIS FOR FRED!!
// this can get nasty when a lot of bitmaps are in use so spare it for
// the normal game and only do this in FRED
int mprintf_count = 0;
for (auto &sb : Starfield_bitmaps) {
if (sb.bitmap_id < 0) {
sb.bitmap_id = bm_load(sb.filename);
// maybe didn't load a static image so try for an animated one
if (sb.bitmap_id < 0) {
sb.bitmap_id = bm_load_animation(sb.filename, &sb.n_frames, &sb.fps, nullptr, nullptr, true);
if (sb.bitmap_id < 0) {
mprintf(("Unable to load starfield bitmap: '%s'!\n", sb.filename));
mprintf_count++;
}
}
}
}
if (mprintf_count > 0) {
Warning(LOCATION, "Unable to load %d starfield bitmap(s)!\n", mprintf_count);
}
for (auto &sb : Sun_bitmaps) {
// normal bitmap
if (sb.bitmap_id < 0) {
sb.bitmap_id = bm_load(sb.filename);
// maybe didn't load a static image so try for an animated one
if (sb.bitmap_id < 0) {
sb.bitmap_id = bm_load_animation(sb.filename, &sb.n_frames, &sb.fps, nullptr, nullptr, true);
if (sb.bitmap_id < 0) {
Warning(LOCATION, "Unable to load sun bitmap: '%s'!\n", sb.filename);
}
}
}
// glow bitmap
if (sb.glow_bitmap < 0) {
sb.glow_bitmap = bm_load(sb.glow_filename);
// maybe didn't load a static image so try for an animated one
if (sb.glow_bitmap < 0) {
sb.glow_bitmap = bm_load_animation(sb.glow_filename, &sb.glow_n_frames, &sb.glow_fps, nullptr, nullptr, true);
if (sb.glow_bitmap < 0) {
Warning(LOCATION, "Unable to load sun glow bitmap: '%s'!\n", sb.glow_filename);
}
}
}
if (sb.flare) {
for (int i = 0; i < MAX_FLARE_BMP; i++) {
if ( !strlen(sb.flare_bitmaps[i].filename) )
continue;
if (sb.flare_bitmaps[i].bitmap_id < 0) {
sb.flare_bitmaps[i].bitmap_id = bm_load(sb.flare_bitmaps[i].filename);
if (sb.flare_bitmaps[i].bitmap_id < 0) {
Warning(LOCATION, "Unable to load sun flare bitmap: '%s'!\n", sb.flare_bitmaps[i].filename);
continue;
}
}
}
}
}
Star_bitmaps_loaded = 1;
}
void stars_clear_instances()
{
for (auto &sbi : Starfield_bitmap_instances) {
delete [] sbi.verts;
sbi.verts = nullptr;
}
Starfield_bitmap_instances.clear();
Suns.clear();
}
// call on game startup
void stars_init()
{
// parse stars.tbl
parse_startbl("stars.tbl");
parse_modular_table("*-str.tbm", parse_startbl);
// Warn if we can't find the two retail motion debris types.
if (get_motion_debris_by_name("Default") < 0)
Warning(LOCATION, "Motion debris 'Default' not found in stars.tbl. Motion debris will be disabled!\n");
if (get_motion_debris_by_name("Nebula") < 0)
Warning(LOCATION, "Motion debris 'Nebula' not found in stars.tbl. Motion debris will be disabled!\n");
if (Cmdline_env) {
ENVMAP = Default_env_map = bm_load("cubemap");
}
}
// call only from game_shutdown()!!
void stars_close()
{
stars_clear_instances();
// any other code goes here
}
// called before mission parse so we can clear out all of the old stuff
void stars_pre_level_init(bool clear_backgrounds)
{
Num_stars = 500;
// we used to clear all the array entries, but now we can just wipe the vector
if (clear_backgrounds)
Backgrounds.clear();
stars_clear_instances();
stars_set_background_model(NULL, NULL);
stars_set_background_orientation();
// mark all starfield and sun bitmaps as unused for this mission and release any current bitmaps
// NOTE: that because of how we have to load the bitmaps it's important to release all of
// them first thing rather than after we have marked and loaded only what's needed
// NOTE2: there is a reason that we don't check for release before setting the handle to -1 so
// be aware that this is NOT a bug. also, bmpman should NEVER return 0 as a valid handle!
if ( !Fred_running ) {
for (auto &sb : Starfield_bitmaps) {
if (sb.bitmap_id > 0) {
bm_release(sb.bitmap_id);
sb.bitmap_id = -1;
}
sb.used_this_level = 0;
sb.preload = 0;
}
for (auto &sb : Sun_bitmaps) {
if (sb.bitmap_id > 0) {
bm_release(sb.bitmap_id);
sb.bitmap_id = -1;
}
if (sb.glow_bitmap > 0) {
bm_release(sb.glow_bitmap);
sb.glow_bitmap = -1;
}
for (int i = 0; i < MAX_FLARE_BMP; i++) {
if (sb.flare_bitmaps[i].bitmap_id > 0) {
bm_release(sb.flare_bitmaps[i].bitmap_id);
sb.flare_bitmaps[i].bitmap_id = -1;
}
}
sb.used_this_level = 0;
sb.preload = 0;
}
}
// also clear the preload indexes
Preload_background_indexes.clear();
Dynamic_environment = false;
Motion_debris_override = false;
Env_cubemap_drawn = false;
Irr_cubemap_drawn = false;
// reset the skybox timestamp, used for animated textures
Skybox_timestamp = _timestamp();
}
// setup the render target ready for this mission's environment map
static void environment_map_gen()
{
const int size = 512;
int gen_flags = (BMP_FLAG_RENDER_TARGET_STATIC | BMP_FLAG_CUBEMAP | BMP_FLAG_RENDER_TARGET_MIPMAP);
if ( !Cmdline_env ) {
return;
}
if (gr_screen.envmap_render_target >= 0) {
if ( !bm_release(gr_screen.envmap_render_target, 1) ) {
Warning(LOCATION, "Unable to release environment map render target.");
}
gr_screen.envmap_render_target = -1;
}
if ( Dynamic_environment || (The_mission.flags[Mission::Mission_Flags::Subspace]) ) {
Dynamic_environment = true;
gen_flags &= ~BMP_FLAG_RENDER_TARGET_STATIC;
gen_flags |= BMP_FLAG_RENDER_TARGET_DYNAMIC;
}
// bail if we are going to be static, and have an envmap specified already
else if ( strlen(The_mission.envmap_name) ) {
// Load the mission map so we can use it later
Mission_env_map = bm_load(The_mission.envmap_name);
return;
}
gr_screen.envmap_render_target = bm_make_render_target(size, size, gen_flags);
}
// setup the render target ready for this mission's environment map
static void irradiance_map_gen()
{
const int irr_size = 16;
int gen_flags = (BMP_FLAG_RENDER_TARGET_STATIC | BMP_FLAG_CUBEMAP | BMP_FLAG_RENDER_TARGET_MIPMAP);
if (!Cmdline_env) {
return;
}
if (gr_screen.irrmap_render_target >= 0) {
if (!bm_release(gr_screen.irrmap_render_target, 1)) {
Warning(LOCATION, "Unable to release environment map render target.");
}
gr_screen.irrmap_render_target = -1;
IRRMAP = -1;
}
if (Dynamic_environment || (The_mission.flags[Mission::Mission_Flags::Subspace])) {
Dynamic_environment = true;
gen_flags &= ~BMP_FLAG_RENDER_TARGET_STATIC;
gen_flags |= BMP_FLAG_RENDER_TARGET_DYNAMIC;
}
gr_screen.irrmap_render_target = bm_make_render_target(irr_size, irr_size, gen_flags);
IRRMAP = gr_screen.irrmap_render_target;
}
// call this in game_post_level_init() so we know whether we're running in full nebula mode or not
void stars_post_level_init()
{
int i;
vec3d v;
float dist, dist_max;
ubyte red,green,blue,alpha;
if (gr_screen.mode == GR_STUB) {
return;
}
// in FRED, make sure we always have at least one background
if (Fred_running)
{
if (Backgrounds.empty())
stars_add_blank_background(true);
}
// in FSO, see if we have any backgrounds to preload
// (since the backgrounds aren't parsed at the time we parse the sexps)
else
{
for (int idx : Preload_background_indexes)
stars_preload_background(idx);
}
stars_set_background_model(The_mission.skybox_model, NULL, The_mission.skybox_flags);
stars_set_background_orientation(&The_mission.skybox_orientation);
stars_load_debris( ((The_mission.flags[Mission::Mission_Flags::Fullneb]) || Nebula_sexp_used) );
// following code randomly distributes star points within a sphere volume, which
// avoids there being denser areas along the edges and in corners that we had in the
// old rectangular distribution scheme.
dist_max = (float) (HALF_RND_MAX * HALF_RND_MAX);
for (i=0; i<MAX_STARS; i++) {
dist = dist_max;
while (dist >= dist_max) {
v.xyz.x = (float) ((Random::next() & RND_MAX_MASK) - HALF_RND_MAX);
v.xyz.y = (float) ((Random::next() & RND_MAX_MASK) - HALF_RND_MAX);
v.xyz.z = (float) ((Random::next() & RND_MAX_MASK) - HALF_RND_MAX);
dist = v.xyz.x * v.xyz.x + v.xyz.y * v.xyz.y + v.xyz.z * v.xyz.z;
}
vm_vec_copy_normalize(&Stars[i].pos, &v);
{
red = (ubyte)Random::next(192, 255);
green = (ubyte)Random::next(192, 255);
blue = (ubyte)Random::next(192, 255);
alpha = (ubyte)Random::next(24, 216);
gr_init_alphacolor(&Stars[i].col, red, green, blue, alpha, AC_TYPE_BLEND);
}
}
memset( &Motion_debris, 0, sizeof(motion_debris_instance) * MAX_MOTION_DEBRIS );
for (i=0; i<8; i++ ) {
ubyte intensity = (ubyte)((i + 1) * 24);