2 * $Logfile: /Freespace2/code/Starfield/StarField.cpp $
7 * Code to handle and draw starfields, background space image bitmaps, floating
11 * Revision 1.1 2002/05/03 03:28:10 root
15 * 34 9/07/99 4:01p Dave
16 * Fixed up a string.tbl paroblem (self destruct message). Make sure IPX
17 * does everything properly (setting up address when binding). Remove
18 * black rectangle background from UI_INPUTBOX.
20 * 33 9/01/99 10:14a Dave
23 * 32 8/30/99 5:01p Dave
24 * Made d3d do less state changing in the nebula. Use new chat server for
27 * 31 8/19/99 10:59a Dave
28 * Packet loss detection.
30 * 30 7/27/99 3:52p Dave
31 * Make star drawing a bit more robust to help lame D3D cards.
33 * 29 7/21/99 8:10p Dave
34 * First run of supernova effect.
36 * 28 7/13/99 2:01p Dave
37 * Don't draw background bitmaps in the nebula.
39 * 27 6/08/99 2:34p Jasenw
40 * Made perspective bitmaps render in Fred.
42 * 26 6/04/99 1:18p Dave
43 * Fixed briefing model rendering problems. Made show background option in
44 * fred toggle nebula rendering.
46 * 25 6/03/99 6:37p Dave
47 * More TNT fun. Made perspective bitmaps more flexible.
49 * 24 5/28/99 1:45p Dave
50 * Fixed up perspective bitmap drawing.
52 * 23 5/20/99 7:00p Dave
53 * Added alternate type names for ships. Changed swarm missile table
56 * 22 5/11/99 10:03a Dave
57 * Put a bunch of stuff into tables.
59 * 21 5/11/99 9:10a Dave
60 * Move default sun position.
62 * 20 5/09/99 6:00p Dave
63 * Lots of cool new effects. E3 build tweaks.
65 * 19 4/26/99 8:49p Dave
66 * Made all pof based nebula stuff full customizable through fred.
68 * 18 4/25/99 7:43p Dave
69 * Misc small bug fixes. Made sun draw properly.
71 * 17 4/23/99 5:53p Dave
72 * Started putting in new pof nebula support into Fred.
74 * 16 4/07/99 6:22p Dave
75 * Fred and Freespace support for multiple background bitmaps and suns.
76 * Fixed link errors on all subprojects. Moved encrypt_init() to
77 * cfile_init() and lcl_init(), since its safe to call twice.
79 * 15 3/31/99 8:24p Dave
80 * Beefed up all kinds of stuff, incluging beam weapons, nebula effects
81 * and background nebulae. Added per-ship non-dimming pixel colors.
83 * 14 3/20/99 5:09p Dave
84 * Fixed release build fred warnings and unhandled exception.
86 * 13 3/20/99 3:46p Dave
87 * Added support for model-based background nebulae. Added 3 new
90 * 12 3/20/99 2:04p Dave
91 * Removed unnecessary planet rendering.
93 * 11 3/19/99 9:51a Dave
94 * Checkin to repair massive source safe crash. Also added support for
95 * pof-style nebulae, and some new weapons code.
97 * 12 3/15/99 6:45p Daveb
98 * Put in rough nebula bitmap support.
100 * 11 3/11/99 5:53p Dave
101 * More network optimization. Spliced in Dell OEM planet bitmap crap.
103 * 10 2/03/99 11:44a Dave
104 * Fixed d3d transparent textures.
106 * 9 12/09/98 7:34p Dave
107 * Cleanup up nebula effect. Tweaked many values.
109 * 8 12/01/98 10:32a Johnson
110 * Fixed direct3d font problems. Fixed sun bitmap problem. Fixed direct3d
113 * 7 12/01/98 8:06a Dave
114 * Temporary checkin to fix some texture transparency problems in d3d.
116 * 6 11/14/98 5:33p Dave
117 * Lots of nebula work. Put in ship contrails.
119 * 5 11/05/98 5:55p Dave
120 * Big pass at reducing #includes
122 * 4 10/13/98 9:29a Dave
123 * Started neatening up freespace.h. Many variables renamed and
124 * reorganized. Added AlphaColors.[h,cpp]
126 * 3 10/07/98 11:16a Dave
129 * 2 10/07/98 10:54a Dave
132 * 1 10/07/98 10:51a Dave
134 * 106 5/23/98 4:14p John
135 * Added code to preload textures to video card for AGP. Added in code
136 * to page in some bitmaps that weren't getting paged in at level start.
138 * 105 5/13/98 2:53p John
139 * Made subspace effect work under software. Had to add new inner loop to
140 * tmapper. Added glows to end of subspace effect. Made subspace effect
141 * levels use gamepalette-subspace palette.
143 * 104 5/13/98 10:28a John
144 * made subpsace forward sliding 40% faster.
146 * 103 5/10/98 4:18p John
147 * Reversed the subspace effect direction
149 * 102 5/08/98 8:38p John
150 * Subspace tweaks. Made two layers rotate independentyl.
152 * 101 5/08/98 1:32p John
153 * Added code for using two layered subspace effects.
155 * 100 5/06/98 5:30p John
156 * Removed unused cfilearchiver. Removed/replaced some unused/little used
157 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
158 * DirectX header files and libs that fixed the Direct3D alpha blending
161 * 99 4/22/98 4:09p John
162 * String externalization
164 * 98 4/22/98 3:28p John
167 * 97 4/13/98 4:54p John
168 * Made uv rotate independently on subspace effect. Put in DCF function
169 * for setting subspace speeds.
171 * 96 4/12/98 5:55p John
172 * Made models work with subspace. Made subspace rotate also.
174 * 95 4/11/98 6:53p John
175 * Added first rev of subspace effect.
177 * 94 4/08/98 11:31a Dave
178 * AL: Fix syntax error for non-demo
180 * 93 4/08/98 10:46a Lawrance
181 * #ifdef out asteroid check for demo
183 * 92 4/08/98 9:25a John
184 * Made asteroid missions not show suns
186 * 91 4/07/98 4:17p John
187 * Made Fred be able to move suns. Made suns actually affect the lighting
190 * 90 4/07/98 11:19a Hoffoss
191 * Changed code to only use Sun01 for a bitmap by default, as John
198 #include "floating.h"
202 #include "starfield.h"
205 #include "freespace.h"
208 #include "linklist.h"
209 #include "lighting.h"
210 #include "asteroid.h"
211 #include "missionparse.h"
213 #include "alphacolors.h"
214 #include "supernova.h"
216 #define MAX_DEBRIS_VCLIPS 4
217 #define DEBRIS_ROT_MIN 10000
218 #define DEBRIS_ROT_RANGE 8
219 #define DEBRIS_ROT_RANGE_SCALER 10000
220 #define RND_MAX_MASK 0x3fff
221 #define HALF_RND_MAX 0x2000
223 typedef struct debris_vclip {
237 const int MAX_DEBRIS = 200;
238 const int MAX_STARS = 2000;
239 const float MAX_DIST = 50.0f;
240 const float MAX_DIST_RANGE = 60.0f;
241 const float MIN_DIST_RANGE = 14.0f;
242 const float BASE_SIZE = 0.12f;
243 float BASE_SIZE_NEB = 0.5f;
245 static int Subspace_model_inner = -1;
246 static int Subspace_model_outer = -1;
249 fix starfield_timestamp = 0;
251 // for drawing cool stuff on the background - comes from a table
252 starfield_bitmap Starfield_bitmaps[MAX_STARFIELD_BITMAPS];
253 starfield_bitmap_instance Starfield_bitmap_instance[MAX_STARFIELD_BITMAPS];
254 int Num_starfield_bitmaps = 0;
256 // sun bitmaps and sun glow bitmaps
257 starfield_bitmap Sun_bitmaps[MAX_STARFIELD_BITMAPS];
258 starfield_bitmap_instance Suns[MAX_STARFIELD_BITMAPS];
261 int last_stars_filled = 0;
262 color star_colors[8];
263 color star_aacolors[8];
265 typedef struct star {
267 vector last_star_pos;
270 star Stars[MAX_STARS];
272 old_debris odebris[MAX_DEBRIS];
275 debris_vclip debris_vclips_normal[MAX_DEBRIS_VCLIPS] = { { -1, -1, "debris01" }, { -1, -1, "debris02" }, { -1, -1, "debris03" }, { -1, -1, "debris04" } };
276 debris_vclip debris_vclips_nebula[MAX_DEBRIS_VCLIPS] = { { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" } };
277 debris_vclip *debris_vclips = debris_vclips_normal;
280 int stars_debris_loaded = 0;
283 int Stars_background_inited = 0; // if we're inited
284 int Nmodel_num = -1; // model num
285 int Nmodel_bitmap = -1; // model texture
287 // given a starfield_bitmap_instance, return a pointer to its parent, for suns
288 starfield_bitmap *stars_lookup_sun(starfield_bitmap_instance *s)
298 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
299 if(!stricmp(Sun_bitmaps[idx].filename, s->filename)){
300 return &Sun_bitmaps[idx];
308 void stars_load_debris()
312 // if we're in nebula mode
313 if(The_mission.flags & MISSION_FLAG_FULLNEB){
314 debris_vclips = debris_vclips_nebula;
316 debris_vclips = debris_vclips_normal;
319 for (i=0; i<MAX_DEBRIS_VCLIPS; i++ ) {
320 debris_vclips[i].bm = bm_load_animation( debris_vclips[i].name, &debris_vclips[i].nframes, NULL, 1 );
321 if ( debris_vclips[i].bm < 0 ) {
322 // try loading it as a single bitmap
323 debris_vclips[i].bm = bm_load(debris_vclips[i].name);
324 debris_vclips[i].nframes = 1;
326 if(debris_vclips[i].bm <= 0){
327 Error( LOCATION, "Couldn't load animation/bitmap '%s'\n", debris_vclips[i].name );
331 stars_debris_loaded = 1;
334 // call on game startup
337 starfield_bitmap *bm;
339 char filename[MAX_FILENAME_LEN+1] = "";
340 char glow_filename[MAX_FILENAME_LEN+1] = "";
344 read_file_text("stars.tbl");
347 // make all bitmaps invalid
348 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
349 Starfield_bitmaps[idx].bitmap = -1;
350 Starfield_bitmaps[idx].glow_bitmap = -1;
351 strcpy(Starfield_bitmaps[idx].filename, "");
352 strcpy(Starfield_bitmaps[idx].glow_filename, "");
354 Sun_bitmaps[idx].bitmap = -1;
355 Sun_bitmaps[idx].glow_bitmap = -1;
356 strcpy(Sun_bitmaps[idx].filename, "");
357 strcpy(Sun_bitmaps[idx].glow_filename, "");
362 while(!optional_string("#end")){
363 // intensity alpha bitmap
364 if(optional_string("$Bitmap:")){
365 stuff_string(filename, F_NAME, NULL);
366 if(count < MAX_STARFIELD_BITMAPS){
367 bm = &Starfield_bitmaps[count++];
368 strcpy(bm->filename, filename);
370 bm->bitmap = bm_load(bm->filename);
371 Assert(bm->bitmap != -1);
373 // if fred is running we should lock the bitmap now
374 if(Fred_running && (bm->bitmap >= 0)){
375 bm_lock(bm->bitmap, 8, BMP_TEX_OTHER);
376 bm_unlock(bm->bitmap);
380 // green xparency bitmap
381 else if(optional_string("$BitmapX:")){
382 stuff_string(filename, F_NAME, NULL);
383 if(count < MAX_STARFIELD_BITMAPS){
384 bm = &Starfield_bitmaps[count++];
385 strcpy(bm->filename, filename);
387 bm->bitmap = bm_load(bm->filename);
388 Assert(bm->bitmap != -1);
390 // if fred is running we should lock as a 0, 255, 0 bitmap now
391 if(Fred_running && (bm->bitmap >= 0)){
392 bm_lock(bm->bitmap, 8, BMP_TEX_XPARENT);
393 bm_unlock(bm->bitmap);
401 while(!optional_string("#end")){
402 if(optional_string("$Sun:")){
403 stuff_string(filename, F_NAME, NULL);
406 required_string("$Sunglow:");
407 stuff_string(glow_filename, F_NAME, NULL);
409 // associated lighting values
410 required_string("$SunRGBI:");
416 if(count < MAX_STARFIELD_BITMAPS){
417 bm = &Sun_bitmaps[count++];
418 strcpy(bm->filename, filename);
419 strcpy(bm->glow_filename, glow_filename);
421 bm->bitmap = bm_load(bm->filename);
422 bm->glow_bitmap = bm_load(bm->glow_filename);
423 Assert(bm->bitmap != -1);
424 Assert(bm->glow_bitmap != -1);
430 // if fred is running we should lock the bitmap now
433 bm_lock(bm->bitmap, 8, BMP_TEX_OTHER);
434 bm_unlock(bm->bitmap);
436 if(bm->glow_bitmap >= 0){
437 bm_lock(bm->glow_bitmap, 8, BMP_TEX_OTHER);
438 bm_unlock(bm->glow_bitmap);
445 // normal debris pieces
447 while(!optional_string("#end")){
448 required_string("$Debris:");
449 stuff_string(filename, F_NAME, NULL);
451 if(count < MAX_DEBRIS_VCLIPS){
452 strcpy(debris_vclips_normal[count++].name, filename);
457 // nebula debris pieces
459 while(!optional_string("#end")){
460 required_string("$DebrisNeb:");
461 stuff_string(filename, F_NAME, NULL);
463 if(count < MAX_DEBRIS_VCLIPS){
464 strcpy(debris_vclips_nebula[count++].name, filename);
470 // call this in game_post_level_init() so we know whether we're running in full nebula mode or not
471 void stars_level_init()
475 float dist, dist_max;
477 // reset to -1 so we reload it each mission (if we need to)
479 if(Nmodel_bitmap != -1){
480 bm_unload(Nmodel_bitmap);
484 // if (!stars_debris_loaded){
488 // following code randomly distributes star points within a sphere volume, which
489 // avoids there being denser areas along the edges and in corners that we had in the
490 // old rectangular distribution scheme.
491 dist_max = (float) (HALF_RND_MAX * HALF_RND_MAX);
492 for (i=0; i<MAX_STARS; i++) {
494 while (dist >= dist_max) {
495 v.x = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
496 v.y = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
497 v.z = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
499 dist = v.x * v.x + v.y * v.y + v.z * v.z;
501 vm_vec_copy_normalize(&Stars[i].pos, &v);
504 for (i=0; i<MAX_DEBRIS; i++) {
505 odebris[i].active = 0;
508 for (i=0; i<8; i++ ) {
509 ubyte intensity = (ubyte)((i + 1) * 24);
510 gr_init_alphacolor(&star_aacolors[i], 255, 255, 255, intensity, AC_TYPE_BLEND );
511 gr_init_color(&star_colors[i], intensity, intensity, intensity );
514 last_stars_filled = 0;
516 // if we have no sun instances, create one
518 mprintf(("Adding default sun\n"));
521 strcpy(Suns[0].filename, Sun_bitmaps[0].filename);
522 Suns[0].scale_x = 1.0f;
523 Suns[0].scale_y = 1.0f;
526 memset(&Suns[0].ang, 0, sizeof(angles));
527 Suns[0].ang.h = fl_radian(60.0f);
536 extern object * Player_obj;
538 #define STAR_AMOUNT_DEFAULT 0.75f
539 #define STAR_DIM_DEFAULT 7800.0f
540 #define STAR_CAP_DEFAULT 75.0f
541 #define STAR_MAX_LENGTH_DEFAULT 0.04f // 312
543 float Star_amount = STAR_AMOUNT_DEFAULT;
544 float Star_dim = STAR_DIM_DEFAULT;
545 float Star_cap = STAR_CAP_DEFAULT;
546 float Star_max_length = STAR_MAX_LENGTH_DEFAULT;
548 #define STAR_FLAG_TAIL (1<<0) // Draw a tail when moving
549 #define STAR_FLAG_DIM (1<<1) // Dim as you move
550 #define STAR_FLAG_ANTIALIAS (1<<2) // Draw the star using antialiased lines
551 #define STAR_FLAG_DEFAULT (STAR_FLAG_TAIL | STAR_FLAG_DIM)
553 uint Star_flags = STAR_FLAG_DEFAULT;
556 DCF(stars,"Set parameters for starfield")
559 dc_get_arg(ARG_STRING);
560 if ( !strcmp( Dc_arg, "tail" )) {
561 dc_get_arg(ARG_FLOAT);
562 if ( (Dc_arg_float < 0.0f) || (Dc_arg_float > 1.0f) ) {
565 Star_amount = Dc_arg_float;
567 } else if ( !strcmp( Dc_arg, "len" )) {
568 dc_get_arg(ARG_FLOAT);
569 Star_max_length = Dc_arg_float;
570 } else if ( !strcmp( Dc_arg, "dim" )) {
571 dc_get_arg(ARG_FLOAT);
572 if ( Dc_arg_float < 0.0f ) {
575 Star_dim = Dc_arg_float;
577 } else if ( !strcmp( Dc_arg, "flag" )) {
578 dc_get_arg(ARG_STRING);
579 if ( !strcmp( Dc_arg, "tail" )) {
580 Star_flags ^= STAR_FLAG_TAIL;
581 } else if ( !strcmp( Dc_arg, "dim" )) {
582 Star_flags ^= STAR_FLAG_DIM;
583 } else if ( !strcmp( Dc_arg, "aa" )) {
584 Star_flags ^= STAR_FLAG_ANTIALIAS;
588 } else if ( !strcmp( Dc_arg, "cap" )) {
589 dc_get_arg(ARG_FLOAT);
590 if ( (Dc_arg_float < 0.0f) || (Dc_arg_float > 255.0f) ) {
593 Star_cap = Dc_arg_float;
595 } else if ( !strcmp( Dc_arg, "m0" ) ) {
600 Star_max_length = STAR_MAX_LENGTH_DEFAULT;
601 } else if ( !strcmp( Dc_arg, "m1" ) || !strcmp( Dc_arg, "default" )) {
602 Star_amount = STAR_AMOUNT_DEFAULT;
603 Star_dim = STAR_DIM_DEFAULT;
604 Star_cap = STAR_CAP_DEFAULT;
605 Star_flags = STAR_FLAG_DEFAULT;
606 Star_max_length = STAR_MAX_LENGTH_DEFAULT;
607 } else if ( !strcmp( Dc_arg, "m2" )) {
611 Star_flags = STAR_FLAG_TAIL|STAR_FLAG_DIM|STAR_FLAG_ANTIALIAS;
612 Star_max_length = STAR_MAX_LENGTH_DEFAULT;
613 } else if ( !strcmp( Dc_arg, "num" )) {
615 if ( (Dc_arg_int < 0) || (Dc_arg_int > MAX_STARS) ) {
618 Num_stars = Dc_arg_int;
621 // print usage, not stats
627 dc_printf( "Usage: stars keyword\nWhere keyword can be in the following forms:\n" );
628 dc_printf( "stars default Resets stars to all default values\n" );
629 dc_printf( "stars num X Sets number of stars to X. Between 0 and %d.\n", MAX_STARS );
630 dc_printf( "stars tail X Where X is the percent of 'tail' between 0 and 1.0\n" );
631 dc_printf( "stars dim X Where X is the amount stars dim between 0 and 255.0\n" );
632 dc_printf( "stars cap X Where X is the cap of dimming between 0 and 255.\n" );
633 dc_printf( "stars len X Where X is the cap of length.\n" );
634 dc_printf( "stars m0 Macro0. Old 'pixel type' crappy stars. flags=none\n" );
635 dc_printf( "stars m1 Macro1. (default) tail=.75, dim=20.0, cap=75.0, flags=dim,tail\n" );
636 dc_printf( "stars m2 Macro2. tail=.75, dim=20.0, cap=75.0, flags=dim,tail,aa\n" );
637 dc_printf( "stars flag X Toggles flag X, where X is tail or dim or aa (aa=antialias)\n" );
638 dc_printf( "\nHINT: set cap to 0 to get dim rate and tail down, then use\n" );
639 dc_printf( "cap to keep the lines from going away when moving too fast.\n" );
640 dc_printf( "\nUse '? stars' to see current values.\n" );
641 Dc_status = 0; // don't print status if help is printed. Too messy.
645 dc_printf( "Num_stars: %d\n", Num_stars );
646 dc_printf( "Tail: %.2f\n", Star_amount );
647 dc_printf( "Dim: %.2f\n", Star_dim );
648 dc_printf( "Cap: %.2f\n", Star_cap );
649 dc_printf( "Max length: %.2f\n", Star_max_length );
650 dc_printf( "Flags:\n" );
651 dc_printf( " Tail: %s\n", (Star_flags&STAR_FLAG_TAIL?"On":"Off") );
652 dc_printf( " Dim: %s\n", (Star_flags&STAR_FLAG_DIM?"On":"Off") );
653 dc_printf( " Antialias: %s\n", (Star_flags&STAR_FLAG_ANTIALIAS?"On":"Off") );
654 dc_printf( "\nTHESE AREN'T SAVED TO DISK, SO IF YOU TWEAK\n" );
655 dc_printf( "THESE AND LIKE THEM, WRITE THEM DOWN!!\n" );
660 int reload_old_debris = 1; // If set to one, then reload all the last_pos of the debris
662 // Call this if camera "cuts" or moves long distances
663 // so blur effect doesn't draw lines all over the screen.
664 void stars_camera_cut()
666 last_stars_filled = 0;
667 reload_old_debris = 1;
670 //#define TIME_STAR_CODE // enable to time star code
673 extern float Viewer_zoom;
675 // get the world coords of the sun pos on the unit sphere.
676 void stars_get_sun_pos(int sun_n, vector *pos)
682 Assert(sun_n < Num_suns);
683 if((sun_n >= Num_suns) || (sun_n < 0)){
687 // rotate the sun properly
688 temp = vmd_zero_vector;
692 vm_angles_2_matrix(&rot, &Suns[sun_n].ang);
693 vm_vec_rotate(pos, &temp, &rot);
697 void stars_draw_sun( int show_sun )
703 starfield_bitmap *bm;
704 float local_scale = 1.0f;
710 for(idx=0; idx<Num_suns; idx++){
712 bm = stars_lookup_sun(&Suns[idx]);
718 sun_pos = vmd_zero_vector;
720 stars_get_sun_pos(idx, &sun_pos);
724 vm_vec_normalize(&sun_dir);
726 // add the light source corresponding to the sun
727 light_add_directional(&sun_dir, bm->i, bm->r, bm->g, bm->b);
730 if(supernova_active()){
731 local_scale = 1.0f + (SUPERNOVA_SUN_SCALE * supernova_pct_complete());
734 // draw the sun itself, keep track of how many we drew
735 gr_set_bitmap(bm->bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.999f);
736 g3_rotate_faraway_vertex(&sun_vex, &sun_pos);
737 if(!g3_draw_bitmap(&sun_vex, 0, 0.05f * Suns[idx].scale_x * local_scale, TMAP_FLAG_TEXTURED)){
743 // draw the corresponding glow for sun_n
744 void stars_draw_sun_glow(int sun_n)
746 starfield_bitmap *bm;
747 vector sun_pos, sun_dir;
749 float local_scale = 1.0f;
752 Assert(sun_n < Num_suns);
753 if((sun_n >= Num_suns) || (sun_n < 0)){
758 bm = stars_lookup_sun(&Suns[sun_n]);
764 sun_pos = vmd_zero_vector;
766 stars_get_sun_pos(sun_n, &sun_pos);
770 vm_vec_normalize(&sun_dir);
773 if(supernova_active()){
774 local_scale = 1.0f + (SUPERNOVA_SUN_SCALE * supernova_pct_complete());
777 // draw the sun itself, keep track of how many we drew
778 gr_set_bitmap(bm->glow_bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.5f);
779 g3_rotate_faraway_vertex(&sun_vex, &sun_pos);
780 g3_draw_bitmap(&sun_vex, 0, 0.10f * Suns[sun_n].scale_x * local_scale, TMAP_FLAG_TEXTURED);
784 void stars_draw_bitmaps( int show_bitmaps )
789 // if we're in the nebula, don't render any backgrounds
790 if(The_mission.flags & MISSION_FLAG_FULLNEB){
795 if(!Detail.planets_suns){
799 // render all bitmaps
800 for(idx=0; idx<Num_starfield_bitmaps; idx++){
801 // lookup the info index
802 star_index = stars_find_bitmap(Starfield_bitmap_instance[idx].filename);
809 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.9999f);
810 g3_draw_perspective_bitmap(&Starfield_bitmap_instance[idx].ang, Starfield_bitmap_instance[idx].scale_x, Starfield_bitmap_instance[idx].scale_y, Starfield_bitmap_instance[idx].div_x, Starfield_bitmap_instance[idx].div_y, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
812 if(Starfield_bitmaps[star_index].xparent){
813 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap);
814 g3_draw_perspective_bitmap(&Starfield_bitmap_instance[idx].ang, Starfield_bitmap_instance[idx].scale_x, Starfield_bitmap_instance[idx].scale_y, Starfield_bitmap_instance[idx].div_x, Starfield_bitmap_instance[idx].div_y, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT | TMAP_FLAG_XPARENT);
816 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.9999f);
817 g3_draw_perspective_bitmap(&Starfield_bitmap_instance[idx].ang, Starfield_bitmap_instance[idx].scale_x, Starfield_bitmap_instance[idx].scale_y, Starfield_bitmap_instance[idx].div_x, Starfield_bitmap_instance[idx].div_y, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
824 void calculate_bitmap_matrix(starfield_bitmaps *bm, vector *v)
826 vm_vector_2_matrix(&bm->m, v, NULL, NULL);
827 vm_orthogonalize_matrix(&bm->m);
830 void calculate_bitmap_points(starfield_bitmaps *bm, float bank)
833 vector fvec, uvec, rvec, tmp;
836 vm_orthogonalize_matrix(&bm->m);
838 tangles.p = tangles.h = 0.0f;
840 vm_rotate_matrix_by_angles(&bm->m, &tangles);
844 vm_vec_scale(&fvec, bm->dist );
848 vm_vec_sub(&tmp, &fvec, &uvec);
849 vm_vec_sub(&bm->points[3], &tmp, &rvec);
851 vm_vec_sub(&tmp, &fvec, &uvec);
852 vm_vec_add(&bm->points[2], &tmp, &rvec);
854 vm_vec_add(&tmp, &fvec, &uvec);
855 vm_vec_add(&bm->points[1], &tmp, &rvec);
857 vm_vec_add(&tmp, &fvec, &uvec);
858 vm_vec_sub(&bm->points[0], &tmp, &rvec);
861 vm_vec_normalize(&bm->points[i]);
866 extern int Interp_subspace;
867 extern float Interp_subspace_offset_u;
868 extern float Interp_subspace_offset_u;
869 extern float Interp_subspace_offset_v;
871 float subspace_offset_u = 0.0f;
872 float subspace_offset_u_inner = 0.0f;
873 float subspace_offset_v = 0.0f;
875 float subspace_u_speed = 0.07f; // how fast u changes
876 float subspace_v_speed = 0.05f; // how fast v changes
878 int Subspace_glow_bitmap = -1;
880 float Subspace_glow_frame = 0.0f;
881 float Subspace_glow_rate = 1.0f;
885 DCF(subspace_set,"Set parameters for subspace effect")
888 dc_get_arg(ARG_STRING);
889 if ( !strcmp( Dc_arg, "u" )) {
890 dc_get_arg(ARG_FLOAT);
891 if ( Dc_arg_float < 0.0f ) {
894 subspace_u_speed = Dc_arg_float;
896 } else if ( !strcmp( Dc_arg, "v" )) {
897 dc_get_arg(ARG_FLOAT);
898 if ( Dc_arg_float < 0.0f ) {
901 subspace_v_speed = Dc_arg_float;
904 // print usage, not stats
910 dc_printf( "Usage: subspace keyword\nWhere keyword can be in the following forms:\n" );
911 dc_printf( "subspace u X Where X is how fast u moves.\n", MAX_STARS );
912 dc_printf( "subspace v X Where X is how fast v moves.\n" );
913 dc_printf( "\nUse '? subspace' to see current values.\n" );
914 Dc_status = 0; // don't print status if help is printed. Too messy.
918 dc_printf( "u: %.2f\n", subspace_u_speed );
919 dc_printf( "v: %.2f\n", subspace_v_speed );
924 void subspace_render()
926 if ( Subspace_model_inner == -1 ) {
927 Subspace_model_inner = model_load( "subspace_small.pof", NULL, NULL );
928 Assert(Subspace_model_inner>-1);
931 if ( Subspace_model_outer == -1 ) {
932 Subspace_model_outer = model_load( "subspace_big.pof", NULL, NULL );
933 Assert(Subspace_model_outer>-1);
936 if ( Subspace_glow_bitmap == -1 ) {
937 Subspace_glow_bitmap = bm_load( NOX("SunGlow01"));
938 Assert(Subspace_glow_bitmap>-1);
941 Subspace_glow_frame += flFrametime * 1.0f;
943 float total_time = i2fl(NOISE_NUM_FRAMES) / 15.0f;
946 if ( Subspace_glow_frame < 0.0f ) Subspace_glow_frame = 0.0f;
947 if ( Subspace_glow_frame > 100.0f ) Subspace_glow_frame = 0.0f;
949 while ( Subspace_glow_frame > total_time ) {
950 Subspace_glow_frame -= total_time;
952 int framenum = fl2i( (Subspace_glow_frame*NOISE_NUM_FRAMES) / total_time );
953 if ( framenum < 0 ) framenum = 0;
954 if ( framenum >= NOISE_NUM_FRAMES ) framenum = NOISE_NUM_FRAMES-1;
956 subspace_offset_u += flFrametime*subspace_u_speed;
957 if (subspace_offset_u > 1.0f ) {
958 subspace_offset_u -= 1.0f;
961 subspace_offset_u_inner += flFrametime*subspace_u_speed*3.0f;
962 if (subspace_offset_u > 1.0f ) {
963 subspace_offset_u -= 1.0f;
966 subspace_offset_v += flFrametime*subspace_v_speed;
967 if (subspace_offset_v > 1.0f ) {
968 subspace_offset_v -= 1.0f;
974 angles angs = { 0.0f, 0.0f, 0.0f };
975 angs.b = subspace_offset_v * PI2;
977 vm_angles_2_matrix(&tmp,&angs);
979 int saved_gr_zbuffering = gr_zbuffer_get();
981 gr_zbuffer_set(GR_ZBUFF_NONE);
983 if ( !D3D_enabled ) {
985 int render_flags = MR_NO_LIGHTING | MR_ALWAYS_REDRAW;
988 Interp_subspace_offset_u = 1.0f - subspace_offset_u;
989 Interp_subspace_offset_v = 0.0f;
991 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
992 render_flags |= MR_SHOW_THRUSTERS;
993 model_render( Subspace_model_outer, &tmp, &Eye_position, render_flags ); //MR_NO_CORRECT|MR_SHOW_OUTLINE
997 int render_flags = MR_NO_LIGHTING | MR_ALWAYS_REDRAW;
1000 Interp_subspace_offset_u = 1.0f - subspace_offset_u;
1001 Interp_subspace_offset_v = 0.0f;
1003 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1004 render_flags |= MR_SHOW_THRUSTERS;
1005 model_render( Subspace_model_outer, &tmp, &Eye_position, render_flags ); //MR_NO_CORRECT|MR_SHOW_OUTLINE
1007 Interp_subspace = 1;
1008 Interp_subspace_offset_u = 1.0f - subspace_offset_u_inner;
1009 Interp_subspace_offset_v = 0.0f;
1011 angs.b = -subspace_offset_v * PI2;
1013 vm_angles_2_matrix(&tmp,&angs);
1015 model_set_outline_color(255,255,255);
1017 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1018 render_flags |= MR_SHOW_THRUSTERS;
1020 model_render( Subspace_model_inner, &tmp, &Eye_position, render_flags ); //MR_NO_CORRECT|MR_SHOW_OUTLINE
1023 Interp_subspace = 0;
1024 gr_zbuffer_set(saved_gr_zbuffering);
1027 void stars_draw( int show_stars, int show_suns, int show_nebulas, int show_subspace )
1033 int gr_zbuffering_save = gr_zbuffer_get();
1034 gr_zbuffer_set(GR_ZBUFF_NONE);
1036 if ( show_subspace ) {
1041 if (Num_stars >= MAX_STARS){
1042 Num_stars = MAX_STARS;
1045 #ifdef TIME_STAR_CODE
1047 xt1 = timer_get_fixed_seconds();
1050 if ( show_nebulas && (Game_detail_flags & DETAIL_FLAG_NEBULAS) && (Neb2_render_mode != NEB2_RENDER_POF) && (Neb2_render_mode != NEB2_RENDER_LAME)) {
1054 // draw background stuff
1055 if((Neb2_render_mode != NEB2_RENDER_POLY) && (Neb2_render_mode != NEB2_RENDER_LAME) && show_stars){
1056 // semi-hack, do we don't fog the background
1057 int neb_save = Neb2_render_mode;
1058 Neb2_render_mode = NEB2_RENDER_NONE;
1059 extern void stars_draw_background();
1060 stars_draw_background();
1061 Neb2_render_mode = neb_save;
1064 if (show_stars && ( Game_detail_flags & DETAIL_FLAG_STARS) && !(The_mission.flags & MISSION_FLAG_FULLNEB) && (supernova_active() < 3)) {
1069 if ( !last_stars_filled ) {
1070 for (sp=Stars,i=0; i<Num_stars; i++, sp++ ) {
1072 g3_rotate_faraway_vertex(&p2, &sp->pos);
1073 sp->last_star_pos.x = p2.x;
1074 sp->last_star_pos.y = p2.y;
1075 sp->last_star_pos.z = p2.z;
1081 tmp_num_stars = (Detail.num_stars*Num_stars)/MAX_DETAIL_LEVEL;
1082 if (tmp_num_stars < 0 ) {
1084 } else if ( tmp_num_stars > Num_stars ) {
1085 tmp_num_stars = Num_stars;
1088 for (sp=Stars,i=0; i<tmp_num_stars; i++, sp++ ) {
1092 memset(&p1, 0, sizeof(vertex));
1094 // This makes a star look "proper" by not translating the
1095 // point around the viewer's eye before rotation. In other
1096 // words, when the ship translates, the stars do not change.
1098 g3_rotate_faraway_vertex(&p2, &sp->pos);
1102 g3_project_vertex(&p2);
1103 if ( p2.flags & PF_OVERFLOW ) {
1110 if ( can_draw && (Star_flags & (STAR_FLAG_TAIL|STAR_FLAG_DIM)) ) {
1112 dist = vm_vec_dist_quick( &sp->last_star_pos, (vector *)&p2.x );
1115 if ( dist > Star_max_length ) {
1116 ratio = Star_max_length / dist;
1117 dist = Star_max_length;
1121 ratio *= Star_amount;
1123 p1.x = p2.x + (sp->last_star_pos.x-p2.x)*ratio;
1124 p1.y = p2.y + (sp->last_star_pos.y-p2.y)*ratio;
1125 p1.z = p2.z + (sp->last_star_pos.z-p2.z)*ratio;
1127 p1.flags = 0; // not projected
1128 g3_code_vertex( &p1 );
1133 g3_project_vertex(&p1);
1134 if ( p1.flags & PF_OVERFLOW ) {
1140 sp->last_star_pos.x = p2.x;
1141 sp->last_star_pos.y = p2.y;
1142 sp->last_star_pos.z = p2.z;
1144 if ( !can_draw ) continue;
1148 if ( Star_flags & STAR_FLAG_DIM ) {
1150 float colorf = 255.0f - dist*Star_dim;
1152 if ( colorf < Star_cap )
1155 color = (fl2i(colorf)*(i&7))/256;
1161 if ( (Star_flags & STAR_FLAG_ANTIALIAS) || (D3D_enabled) ) {
1162 gr_set_color_fast( &star_aacolors[color] );
1164 // if the two points are the same, fudge it, since some D3D cards (G200 and G400) are lame.
1165 if( (fl2i(p1.sx) == fl2i(p2.sx)) && (fl2i(p1.sy) == fl2i(p2.sy)) ){
1170 // use alphablended line so that dark stars don't look bad on top of nebulas
1171 gr_set_color_fast( &star_aacolors[color] );
1172 if ( Star_flags & STAR_FLAG_TAIL ) {
1173 gr_line(fl2i(p1.sx),fl2i(p1.sy),fl2i(p2.sx),fl2i(p2.sy));
1175 gr_pixel( fl2i(p2.sx),fl2i(p2.sy) );
1181 last_stars_filled = 1;
1183 #ifdef TIME_STAR_CODE
1184 xt2 = timer_get_fixed_seconds();
1185 mprintf(( "Stars: %d\n", xt2-xt1 ));
1189 if ( (Game_detail_flags & DETAIL_FLAG_MOTION) && (!Fred_running) && (supernova_active() < 3) ) {
1191 gr_set_color( 0, 0, 0 );
1194 if(The_mission.flags & MISSION_FLAG_FULLNEB){
1195 gr_fog_set(GR_FOGMODE_NONE, 0, 0, 0);
1198 old_debris * d = odebris;
1199 for (i=0; i<MAX_DEBRIS; i++, d++ ) {
1203 d->pos.x = f2fl(myrand() - RAND_MAX/2);
1204 d->pos.y = f2fl(myrand() - RAND_MAX/2);
1205 d->pos.z = f2fl(myrand() - RAND_MAX/2);
1207 vm_vec_normalize(&d->pos);
1209 vm_vec_scale(&d->pos, MAX_DIST);
1210 vm_vec_add2(&d->pos, &Eye_position );
1211 // vm_vec_add2(&d->pos, &Player_obj->pos );
1213 d->vclip = i % MAX_DEBRIS_VCLIPS; //rand()
1215 // if we're in full neb mode
1216 if((The_mission.flags & MISSION_FLAG_FULLNEB) && (Neb2_render_mode != NEB2_RENDER_NONE)){
1217 d->size = i2fl(myrand() % 4)*BASE_SIZE_NEB;
1219 d->size = i2fl(myrand() % 4)*BASE_SIZE;
1222 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1225 if ( reload_old_debris ) {
1226 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1229 g3_rotate_vertex(&p, &d->pos);
1232 int frame = Missiontime / (DEBRIS_ROT_MIN + (i % DEBRIS_ROT_RANGE) * DEBRIS_ROT_RANGE_SCALER);
1233 frame %= debris_vclips[d->vclip].nframes;
1235 if((The_mission.flags & MISSION_FLAG_FULLNEB) && (Neb2_render_mode != NEB2_RENDER_NONE)){
1236 gr_set_bitmap( debris_vclips[d->vclip].bm + frame, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.3f);
1238 gr_set_bitmap( debris_vclips[d->vclip].bm + frame );
1242 vm_vec_add( &tmp, &d->last_pos, &Eye_position );
1243 g3_draw_laser( &d->pos,d->size,&tmp,d->size, TMAP_FLAG_TEXTURED|TMAP_FLAG_XPARENT, 25.0f );
1246 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1248 vdist = vm_vec_mag_quick(&d->last_pos);
1250 if (vdist > MAX_DIST_RANGE)
1252 else if (vdist < MIN_DIST_RANGE)
1256 // vm_vec_sub( &tmp, &d->pos, &Player_obj->pos );
1257 // vdist = vm_vec_dot( &tmp, &Player_obj->orient.fvec );
1258 // if ( vdist < 0.0f )
1263 reload_old_debris = 0;
1267 stars_draw_sun( show_suns );
1268 stars_draw_bitmaps( show_suns );
1270 gr_zbuffer_set( gr_zbuffering_save );
1273 void stars_page_in()
1277 // Initialize the subspace stuff
1279 if ( Game_subspace_effect ) {
1281 Subspace_model_inner = model_load( "subspace_small.pof", NULL, NULL );
1282 Assert(Subspace_model_inner>-1);
1283 Subspace_model_outer = model_load( "subspace_big.pof", NULL, NULL );
1284 Assert(Subspace_model_outer>-1);
1288 pm = model_get(Subspace_model_inner);
1290 nprintf(( "Paging", "Paging in textures for subspace effect.\n" ));
1292 for (j=0; j<pm->n_textures; j++ ) {
1293 int bitmap_num = pm->original_textures[j];
1295 if ( bitmap_num > -1 ) {
1296 bm_page_in_texture( bitmap_num );
1300 pm = model_get(Subspace_model_outer);
1302 nprintf(( "Paging", "Paging in textures for subspace effect.\n" ));
1304 for (j=0; j<pm->n_textures; j++ ) {
1305 int bitmap_num = pm->original_textures[j];
1307 if ( bitmap_num > -1 ) {
1308 bm_page_in_texture( bitmap_num );
1312 Subspace_model_inner = -1;
1313 Subspace_model_outer = -1;
1316 Subspace_glow_bitmap = bm_load( NOX("SunGlow01"));
1317 bm_page_in_xparent_texture(Subspace_glow_bitmap);
1319 // page in starfield bitmaps
1322 while((idx < MAX_STARFIELD_BITMAPS) && (Starfield_bitmaps[idx].bitmap != -1)){
1323 if(Starfield_bitmaps[idx].xparent){
1324 bm_page_in_xparent_texture(Starfield_bitmaps[idx].bitmap);
1326 bm_page_in_texture(Starfield_bitmaps[idx].bitmap);
1333 // sun bitmaps and glows
1335 while((idx < MAX_STARFIELD_BITMAPS) && (Sun_bitmaps[idx].bitmap != -1) && (Sun_bitmaps[idx].glow_bitmap != -1)){
1336 bm_page_in_texture(Sun_bitmaps[idx].bitmap);
1337 bm_page_in_texture(Sun_bitmaps[idx].glow_bitmap);
1343 for (i=0; i<MAX_DEBRIS_VCLIPS; i++ ) {
1344 for (j=0; j<debris_vclips[i].nframes; j++ ) {
1345 bm_page_in_xparent_texture(debris_vclips[i].bm + j);
1350 // background nebula models and planets
1351 void stars_draw_background()
1353 if((Nmodel_num < 0) || (Nmodel_bitmap < 0)){
1357 // draw the model at the player's eye wif no z-buffering
1358 model_set_alpha(1.0f);
1359 model_set_forced_texture(Nmodel_bitmap);
1360 model_render(Nmodel_num, &vmd_identity_matrix, &Eye_position, MR_NO_ZBUFFER | MR_NO_CULL | MR_ALL_XPARENT | MR_NO_LIGHTING | MR_FORCE_TEXTURE);
1361 model_set_forced_texture(-1);
1364 // call this to set a specific model as the background model
1365 void stars_set_background_model(char *model_name, char *texture_name)
1367 Nmodel_num = model_load(model_name, 0, NULL);
1368 Nmodel_bitmap = bm_load(texture_name);
1371 // lookup a starfield bitmap, return index or -1 on fail
1372 int stars_find_bitmap(char *name)
1377 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
1378 if(!strcmp(name, Starfield_bitmaps[idx].filename)){
1387 // lookup a sun by bitmap filename, return index or -1 on fail
1388 int stars_find_sun(char *name)
1393 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
1394 if(!strcmp(name, Sun_bitmaps[idx].filename)){