2 * $Logfile: /Freespace2/code/Starfield/StarField.cpp $
7 * Code to handle and draw starfields, background space image bitmaps, floating
11 * Revision 1.2 2002/05/07 03:16:52 theoddone33
12 * The Great Newline Fix
14 * Revision 1.1.1.1 2002/05/03 03:28:10 root
18 * 34 9/07/99 4:01p Dave
19 * Fixed up a string.tbl paroblem (self destruct message). Make sure IPX
20 * does everything properly (setting up address when binding). Remove
21 * black rectangle background from UI_INPUTBOX.
23 * 33 9/01/99 10:14a Dave
26 * 32 8/30/99 5:01p Dave
27 * Made d3d do less state changing in the nebula. Use new chat server for
30 * 31 8/19/99 10:59a Dave
31 * Packet loss detection.
33 * 30 7/27/99 3:52p Dave
34 * Make star drawing a bit more robust to help lame D3D cards.
36 * 29 7/21/99 8:10p Dave
37 * First run of supernova effect.
39 * 28 7/13/99 2:01p Dave
40 * Don't draw background bitmaps in the nebula.
42 * 27 6/08/99 2:34p Jasenw
43 * Made perspective bitmaps render in Fred.
45 * 26 6/04/99 1:18p Dave
46 * Fixed briefing model rendering problems. Made show background option in
47 * fred toggle nebula rendering.
49 * 25 6/03/99 6:37p Dave
50 * More TNT fun. Made perspective bitmaps more flexible.
52 * 24 5/28/99 1:45p Dave
53 * Fixed up perspective bitmap drawing.
55 * 23 5/20/99 7:00p Dave
56 * Added alternate type names for ships. Changed swarm missile table
59 * 22 5/11/99 10:03a Dave
60 * Put a bunch of stuff into tables.
62 * 21 5/11/99 9:10a Dave
63 * Move default sun position.
65 * 20 5/09/99 6:00p Dave
66 * Lots of cool new effects. E3 build tweaks.
68 * 19 4/26/99 8:49p Dave
69 * Made all pof based nebula stuff full customizable through fred.
71 * 18 4/25/99 7:43p Dave
72 * Misc small bug fixes. Made sun draw properly.
74 * 17 4/23/99 5:53p Dave
75 * Started putting in new pof nebula support into Fred.
77 * 16 4/07/99 6:22p Dave
78 * Fred and Freespace support for multiple background bitmaps and suns.
79 * Fixed link errors on all subprojects. Moved encrypt_init() to
80 * cfile_init() and lcl_init(), since its safe to call twice.
82 * 15 3/31/99 8:24p Dave
83 * Beefed up all kinds of stuff, incluging beam weapons, nebula effects
84 * and background nebulae. Added per-ship non-dimming pixel colors.
86 * 14 3/20/99 5:09p Dave
87 * Fixed release build fred warnings and unhandled exception.
89 * 13 3/20/99 3:46p Dave
90 * Added support for model-based background nebulae. Added 3 new
93 * 12 3/20/99 2:04p Dave
94 * Removed unnecessary planet rendering.
96 * 11 3/19/99 9:51a Dave
97 * Checkin to repair massive source safe crash. Also added support for
98 * pof-style nebulae, and some new weapons code.
100 * 12 3/15/99 6:45p Daveb
101 * Put in rough nebula bitmap support.
103 * 11 3/11/99 5:53p Dave
104 * More network optimization. Spliced in Dell OEM planet bitmap crap.
106 * 10 2/03/99 11:44a Dave
107 * Fixed d3d transparent textures.
109 * 9 12/09/98 7:34p Dave
110 * Cleanup up nebula effect. Tweaked many values.
112 * 8 12/01/98 10:32a Johnson
113 * Fixed direct3d font problems. Fixed sun bitmap problem. Fixed direct3d
116 * 7 12/01/98 8:06a Dave
117 * Temporary checkin to fix some texture transparency problems in d3d.
119 * 6 11/14/98 5:33p Dave
120 * Lots of nebula work. Put in ship contrails.
122 * 5 11/05/98 5:55p Dave
123 * Big pass at reducing #includes
125 * 4 10/13/98 9:29a Dave
126 * Started neatening up freespace.h. Many variables renamed and
127 * reorganized. Added AlphaColors.[h,cpp]
129 * 3 10/07/98 11:16a Dave
132 * 2 10/07/98 10:54a Dave
135 * 1 10/07/98 10:51a Dave
137 * 106 5/23/98 4:14p John
138 * Added code to preload textures to video card for AGP. Added in code
139 * to page in some bitmaps that weren't getting paged in at level start.
141 * 105 5/13/98 2:53p John
142 * Made subspace effect work under software. Had to add new inner loop to
143 * tmapper. Added glows to end of subspace effect. Made subspace effect
144 * levels use gamepalette-subspace palette.
146 * 104 5/13/98 10:28a John
147 * made subpsace forward sliding 40% faster.
149 * 103 5/10/98 4:18p John
150 * Reversed the subspace effect direction
152 * 102 5/08/98 8:38p John
153 * Subspace tweaks. Made two layers rotate independentyl.
155 * 101 5/08/98 1:32p John
156 * Added code for using two layered subspace effects.
158 * 100 5/06/98 5:30p John
159 * Removed unused cfilearchiver. Removed/replaced some unused/little used
160 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
161 * DirectX header files and libs that fixed the Direct3D alpha blending
164 * 99 4/22/98 4:09p John
165 * String externalization
167 * 98 4/22/98 3:28p John
170 * 97 4/13/98 4:54p John
171 * Made uv rotate independently on subspace effect. Put in DCF function
172 * for setting subspace speeds.
174 * 96 4/12/98 5:55p John
175 * Made models work with subspace. Made subspace rotate also.
177 * 95 4/11/98 6:53p John
178 * Added first rev of subspace effect.
180 * 94 4/08/98 11:31a Dave
181 * AL: Fix syntax error for non-demo
183 * 93 4/08/98 10:46a Lawrance
184 * #ifdef out asteroid check for demo
186 * 92 4/08/98 9:25a John
187 * Made asteroid missions not show suns
189 * 91 4/07/98 4:17p John
190 * Made Fred be able to move suns. Made suns actually affect the lighting
193 * 90 4/07/98 11:19a Hoffoss
194 * Changed code to only use Sun01 for a bitmap by default, as John
201 #include "floating.h"
205 #include "starfield.h"
208 #include "freespace.h"
211 #include "linklist.h"
212 #include "lighting.h"
213 #include "asteroid.h"
214 #include "missionparse.h"
216 #include "alphacolors.h"
217 #include "supernova.h"
219 #define MAX_DEBRIS_VCLIPS 4
220 #define DEBRIS_ROT_MIN 10000
221 #define DEBRIS_ROT_RANGE 8
222 #define DEBRIS_ROT_RANGE_SCALER 10000
223 #define RND_MAX_MASK 0x3fff
224 #define HALF_RND_MAX 0x2000
226 typedef struct debris_vclip {
240 const int MAX_DEBRIS = 200;
241 const int MAX_STARS = 2000;
242 const float MAX_DIST = 50.0f;
243 const float MAX_DIST_RANGE = 60.0f;
244 const float MIN_DIST_RANGE = 14.0f;
245 const float BASE_SIZE = 0.12f;
246 float BASE_SIZE_NEB = 0.5f;
248 static int Subspace_model_inner = -1;
249 static int Subspace_model_outer = -1;
252 fix starfield_timestamp = 0;
254 // for drawing cool stuff on the background - comes from a table
255 starfield_bitmap Starfield_bitmaps[MAX_STARFIELD_BITMAPS];
256 starfield_bitmap_instance Starfield_bitmap_instance[MAX_STARFIELD_BITMAPS];
257 int Num_starfield_bitmaps = 0;
259 // sun bitmaps and sun glow bitmaps
260 starfield_bitmap Sun_bitmaps[MAX_STARFIELD_BITMAPS];
261 starfield_bitmap_instance Suns[MAX_STARFIELD_BITMAPS];
264 int last_stars_filled = 0;
265 color star_colors[8];
266 color star_aacolors[8];
268 typedef struct star {
270 vector last_star_pos;
273 star Stars[MAX_STARS];
275 old_debris odebris[MAX_DEBRIS];
278 debris_vclip debris_vclips_normal[MAX_DEBRIS_VCLIPS] = { { -1, -1, "debris01" }, { -1, -1, "debris02" }, { -1, -1, "debris03" }, { -1, -1, "debris04" } };
279 debris_vclip debris_vclips_nebula[MAX_DEBRIS_VCLIPS] = { { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" } };
280 debris_vclip *debris_vclips = debris_vclips_normal;
283 int stars_debris_loaded = 0;
286 int Stars_background_inited = 0; // if we're inited
287 int Nmodel_num = -1; // model num
288 int Nmodel_bitmap = -1; // model texture
290 // given a starfield_bitmap_instance, return a pointer to its parent, for suns
291 starfield_bitmap *stars_lookup_sun(starfield_bitmap_instance *s)
301 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
302 if(!stricmp(Sun_bitmaps[idx].filename, s->filename)){
303 return &Sun_bitmaps[idx];
311 void stars_load_debris()
315 // if we're in nebula mode
316 if(The_mission.flags & MISSION_FLAG_FULLNEB){
317 debris_vclips = debris_vclips_nebula;
319 debris_vclips = debris_vclips_normal;
322 for (i=0; i<MAX_DEBRIS_VCLIPS; i++ ) {
323 debris_vclips[i].bm = bm_load_animation( debris_vclips[i].name, &debris_vclips[i].nframes, NULL, 1 );
324 if ( debris_vclips[i].bm < 0 ) {
325 // try loading it as a single bitmap
326 debris_vclips[i].bm = bm_load(debris_vclips[i].name);
327 debris_vclips[i].nframes = 1;
329 if(debris_vclips[i].bm <= 0){
330 Error( LOCATION, "Couldn't load animation/bitmap '%s'\n", debris_vclips[i].name );
334 stars_debris_loaded = 1;
337 // call on game startup
340 starfield_bitmap *bm;
342 char filename[MAX_FILENAME_LEN+1] = "";
343 char glow_filename[MAX_FILENAME_LEN+1] = "";
347 read_file_text("stars.tbl");
350 // make all bitmaps invalid
351 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
352 Starfield_bitmaps[idx].bitmap = -1;
353 Starfield_bitmaps[idx].glow_bitmap = -1;
354 strcpy(Starfield_bitmaps[idx].filename, "");
355 strcpy(Starfield_bitmaps[idx].glow_filename, "");
357 Sun_bitmaps[idx].bitmap = -1;
358 Sun_bitmaps[idx].glow_bitmap = -1;
359 strcpy(Sun_bitmaps[idx].filename, "");
360 strcpy(Sun_bitmaps[idx].glow_filename, "");
365 while(!optional_string("#end")){
366 // intensity alpha bitmap
367 if(optional_string("$Bitmap:")){
368 stuff_string(filename, F_NAME, NULL);
369 if(count < MAX_STARFIELD_BITMAPS){
370 bm = &Starfield_bitmaps[count++];
371 strcpy(bm->filename, filename);
373 bm->bitmap = bm_load(bm->filename);
374 Assert(bm->bitmap != -1);
376 // if fred is running we should lock the bitmap now
377 if(Fred_running && (bm->bitmap >= 0)){
378 bm_lock(bm->bitmap, 8, BMP_TEX_OTHER);
379 bm_unlock(bm->bitmap);
383 // green xparency bitmap
384 else if(optional_string("$BitmapX:")){
385 stuff_string(filename, F_NAME, NULL);
386 if(count < MAX_STARFIELD_BITMAPS){
387 bm = &Starfield_bitmaps[count++];
388 strcpy(bm->filename, filename);
390 bm->bitmap = bm_load(bm->filename);
391 Assert(bm->bitmap != -1);
393 // if fred is running we should lock as a 0, 255, 0 bitmap now
394 if(Fred_running && (bm->bitmap >= 0)){
395 bm_lock(bm->bitmap, 8, BMP_TEX_XPARENT);
396 bm_unlock(bm->bitmap);
404 while(!optional_string("#end")){
405 if(optional_string("$Sun:")){
406 stuff_string(filename, F_NAME, NULL);
409 required_string("$Sunglow:");
410 stuff_string(glow_filename, F_NAME, NULL);
412 // associated lighting values
413 required_string("$SunRGBI:");
419 if(count < MAX_STARFIELD_BITMAPS){
420 bm = &Sun_bitmaps[count++];
421 strcpy(bm->filename, filename);
422 strcpy(bm->glow_filename, glow_filename);
424 bm->bitmap = bm_load(bm->filename);
425 bm->glow_bitmap = bm_load(bm->glow_filename);
426 Assert(bm->bitmap != -1);
427 Assert(bm->glow_bitmap != -1);
433 // if fred is running we should lock the bitmap now
436 bm_lock(bm->bitmap, 8, BMP_TEX_OTHER);
437 bm_unlock(bm->bitmap);
439 if(bm->glow_bitmap >= 0){
440 bm_lock(bm->glow_bitmap, 8, BMP_TEX_OTHER);
441 bm_unlock(bm->glow_bitmap);
448 // normal debris pieces
450 while(!optional_string("#end")){
451 required_string("$Debris:");
452 stuff_string(filename, F_NAME, NULL);
454 if(count < MAX_DEBRIS_VCLIPS){
455 strcpy(debris_vclips_normal[count++].name, filename);
460 // nebula debris pieces
462 while(!optional_string("#end")){
463 required_string("$DebrisNeb:");
464 stuff_string(filename, F_NAME, NULL);
466 if(count < MAX_DEBRIS_VCLIPS){
467 strcpy(debris_vclips_nebula[count++].name, filename);
473 // call this in game_post_level_init() so we know whether we're running in full nebula mode or not
474 void stars_level_init()
478 float dist, dist_max;
480 // reset to -1 so we reload it each mission (if we need to)
482 if(Nmodel_bitmap != -1){
483 bm_unload(Nmodel_bitmap);
487 // if (!stars_debris_loaded){
491 // following code randomly distributes star points within a sphere volume, which
492 // avoids there being denser areas along the edges and in corners that we had in the
493 // old rectangular distribution scheme.
494 dist_max = (float) (HALF_RND_MAX * HALF_RND_MAX);
495 for (i=0; i<MAX_STARS; i++) {
497 while (dist >= dist_max) {
498 v.x = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
499 v.y = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
500 v.z = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
502 dist = v.x * v.x + v.y * v.y + v.z * v.z;
504 vm_vec_copy_normalize(&Stars[i].pos, &v);
507 for (i=0; i<MAX_DEBRIS; i++) {
508 odebris[i].active = 0;
511 for (i=0; i<8; i++ ) {
512 ubyte intensity = (ubyte)((i + 1) * 24);
513 gr_init_alphacolor(&star_aacolors[i], 255, 255, 255, intensity, AC_TYPE_BLEND );
514 gr_init_color(&star_colors[i], intensity, intensity, intensity );
517 last_stars_filled = 0;
519 // if we have no sun instances, create one
521 mprintf(("Adding default sun\n"));
524 strcpy(Suns[0].filename, Sun_bitmaps[0].filename);
525 Suns[0].scale_x = 1.0f;
526 Suns[0].scale_y = 1.0f;
529 memset(&Suns[0].ang, 0, sizeof(angles));
530 Suns[0].ang.h = fl_radian(60.0f);
539 extern object * Player_obj;
541 #define STAR_AMOUNT_DEFAULT 0.75f
542 #define STAR_DIM_DEFAULT 7800.0f
543 #define STAR_CAP_DEFAULT 75.0f
544 #define STAR_MAX_LENGTH_DEFAULT 0.04f // 312
546 float Star_amount = STAR_AMOUNT_DEFAULT;
547 float Star_dim = STAR_DIM_DEFAULT;
548 float Star_cap = STAR_CAP_DEFAULT;
549 float Star_max_length = STAR_MAX_LENGTH_DEFAULT;
551 #define STAR_FLAG_TAIL (1<<0) // Draw a tail when moving
552 #define STAR_FLAG_DIM (1<<1) // Dim as you move
553 #define STAR_FLAG_ANTIALIAS (1<<2) // Draw the star using antialiased lines
554 #define STAR_FLAG_DEFAULT (STAR_FLAG_TAIL | STAR_FLAG_DIM)
556 uint Star_flags = STAR_FLAG_DEFAULT;
559 DCF(stars,"Set parameters for starfield")
562 dc_get_arg(ARG_STRING);
563 if ( !strcmp( Dc_arg, "tail" )) {
564 dc_get_arg(ARG_FLOAT);
565 if ( (Dc_arg_float < 0.0f) || (Dc_arg_float > 1.0f) ) {
568 Star_amount = Dc_arg_float;
570 } else if ( !strcmp( Dc_arg, "len" )) {
571 dc_get_arg(ARG_FLOAT);
572 Star_max_length = Dc_arg_float;
573 } else if ( !strcmp( Dc_arg, "dim" )) {
574 dc_get_arg(ARG_FLOAT);
575 if ( Dc_arg_float < 0.0f ) {
578 Star_dim = Dc_arg_float;
580 } else if ( !strcmp( Dc_arg, "flag" )) {
581 dc_get_arg(ARG_STRING);
582 if ( !strcmp( Dc_arg, "tail" )) {
583 Star_flags ^= STAR_FLAG_TAIL;
584 } else if ( !strcmp( Dc_arg, "dim" )) {
585 Star_flags ^= STAR_FLAG_DIM;
586 } else if ( !strcmp( Dc_arg, "aa" )) {
587 Star_flags ^= STAR_FLAG_ANTIALIAS;
591 } else if ( !strcmp( Dc_arg, "cap" )) {
592 dc_get_arg(ARG_FLOAT);
593 if ( (Dc_arg_float < 0.0f) || (Dc_arg_float > 255.0f) ) {
596 Star_cap = Dc_arg_float;
598 } else if ( !strcmp( Dc_arg, "m0" ) ) {
603 Star_max_length = STAR_MAX_LENGTH_DEFAULT;
604 } else if ( !strcmp( Dc_arg, "m1" ) || !strcmp( Dc_arg, "default" )) {
605 Star_amount = STAR_AMOUNT_DEFAULT;
606 Star_dim = STAR_DIM_DEFAULT;
607 Star_cap = STAR_CAP_DEFAULT;
608 Star_flags = STAR_FLAG_DEFAULT;
609 Star_max_length = STAR_MAX_LENGTH_DEFAULT;
610 } else if ( !strcmp( Dc_arg, "m2" )) {
614 Star_flags = STAR_FLAG_TAIL|STAR_FLAG_DIM|STAR_FLAG_ANTIALIAS;
615 Star_max_length = STAR_MAX_LENGTH_DEFAULT;
616 } else if ( !strcmp( Dc_arg, "num" )) {
618 if ( (Dc_arg_int < 0) || (Dc_arg_int > MAX_STARS) ) {
621 Num_stars = Dc_arg_int;
624 // print usage, not stats
630 dc_printf( "Usage: stars keyword\nWhere keyword can be in the following forms:\n" );
631 dc_printf( "stars default Resets stars to all default values\n" );
632 dc_printf( "stars num X Sets number of stars to X. Between 0 and %d.\n", MAX_STARS );
633 dc_printf( "stars tail X Where X is the percent of 'tail' between 0 and 1.0\n" );
634 dc_printf( "stars dim X Where X is the amount stars dim between 0 and 255.0\n" );
635 dc_printf( "stars cap X Where X is the cap of dimming between 0 and 255.\n" );
636 dc_printf( "stars len X Where X is the cap of length.\n" );
637 dc_printf( "stars m0 Macro0. Old 'pixel type' crappy stars. flags=none\n" );
638 dc_printf( "stars m1 Macro1. (default) tail=.75, dim=20.0, cap=75.0, flags=dim,tail\n" );
639 dc_printf( "stars m2 Macro2. tail=.75, dim=20.0, cap=75.0, flags=dim,tail,aa\n" );
640 dc_printf( "stars flag X Toggles flag X, where X is tail or dim or aa (aa=antialias)\n" );
641 dc_printf( "\nHINT: set cap to 0 to get dim rate and tail down, then use\n" );
642 dc_printf( "cap to keep the lines from going away when moving too fast.\n" );
643 dc_printf( "\nUse '? stars' to see current values.\n" );
644 Dc_status = 0; // don't print status if help is printed. Too messy.
648 dc_printf( "Num_stars: %d\n", Num_stars );
649 dc_printf( "Tail: %.2f\n", Star_amount );
650 dc_printf( "Dim: %.2f\n", Star_dim );
651 dc_printf( "Cap: %.2f\n", Star_cap );
652 dc_printf( "Max length: %.2f\n", Star_max_length );
653 dc_printf( "Flags:\n" );
654 dc_printf( " Tail: %s\n", (Star_flags&STAR_FLAG_TAIL?"On":"Off") );
655 dc_printf( " Dim: %s\n", (Star_flags&STAR_FLAG_DIM?"On":"Off") );
656 dc_printf( " Antialias: %s\n", (Star_flags&STAR_FLAG_ANTIALIAS?"On":"Off") );
657 dc_printf( "\nTHESE AREN'T SAVED TO DISK, SO IF YOU TWEAK\n" );
658 dc_printf( "THESE AND LIKE THEM, WRITE THEM DOWN!!\n" );
663 int reload_old_debris = 1; // If set to one, then reload all the last_pos of the debris
665 // Call this if camera "cuts" or moves long distances
666 // so blur effect doesn't draw lines all over the screen.
667 void stars_camera_cut()
669 last_stars_filled = 0;
670 reload_old_debris = 1;
673 //#define TIME_STAR_CODE // enable to time star code
676 extern float Viewer_zoom;
678 // get the world coords of the sun pos on the unit sphere.
679 void stars_get_sun_pos(int sun_n, vector *pos)
685 Assert(sun_n < Num_suns);
686 if((sun_n >= Num_suns) || (sun_n < 0)){
690 // rotate the sun properly
691 temp = vmd_zero_vector;
695 vm_angles_2_matrix(&rot, &Suns[sun_n].ang);
696 vm_vec_rotate(pos, &temp, &rot);
700 void stars_draw_sun( int show_sun )
706 starfield_bitmap *bm;
707 float local_scale = 1.0f;
713 for(idx=0; idx<Num_suns; idx++){
715 bm = stars_lookup_sun(&Suns[idx]);
721 sun_pos = vmd_zero_vector;
723 stars_get_sun_pos(idx, &sun_pos);
727 vm_vec_normalize(&sun_dir);
729 // add the light source corresponding to the sun
730 light_add_directional(&sun_dir, bm->i, bm->r, bm->g, bm->b);
733 if(supernova_active()){
734 local_scale = 1.0f + (SUPERNOVA_SUN_SCALE * supernova_pct_complete());
737 // draw the sun itself, keep track of how many we drew
738 gr_set_bitmap(bm->bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.999f);
739 g3_rotate_faraway_vertex(&sun_vex, &sun_pos);
740 if(!g3_draw_bitmap(&sun_vex, 0, 0.05f * Suns[idx].scale_x * local_scale, TMAP_FLAG_TEXTURED)){
746 // draw the corresponding glow for sun_n
747 void stars_draw_sun_glow(int sun_n)
749 starfield_bitmap *bm;
750 vector sun_pos, sun_dir;
752 float local_scale = 1.0f;
755 Assert(sun_n < Num_suns);
756 if((sun_n >= Num_suns) || (sun_n < 0)){
761 bm = stars_lookup_sun(&Suns[sun_n]);
767 sun_pos = vmd_zero_vector;
769 stars_get_sun_pos(sun_n, &sun_pos);
773 vm_vec_normalize(&sun_dir);
776 if(supernova_active()){
777 local_scale = 1.0f + (SUPERNOVA_SUN_SCALE * supernova_pct_complete());
780 // draw the sun itself, keep track of how many we drew
781 gr_set_bitmap(bm->glow_bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.5f);
782 g3_rotate_faraway_vertex(&sun_vex, &sun_pos);
783 g3_draw_bitmap(&sun_vex, 0, 0.10f * Suns[sun_n].scale_x * local_scale, TMAP_FLAG_TEXTURED);
787 void stars_draw_bitmaps( int show_bitmaps )
792 // if we're in the nebula, don't render any backgrounds
793 if(The_mission.flags & MISSION_FLAG_FULLNEB){
798 if(!Detail.planets_suns){
802 // render all bitmaps
803 for(idx=0; idx<Num_starfield_bitmaps; idx++){
804 // lookup the info index
805 star_index = stars_find_bitmap(Starfield_bitmap_instance[idx].filename);
812 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.9999f);
813 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);
815 if(Starfield_bitmaps[star_index].xparent){
816 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap);
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 | TMAP_FLAG_XPARENT);
819 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.9999f);
820 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);
827 void calculate_bitmap_matrix(starfield_bitmaps *bm, vector *v)
829 vm_vector_2_matrix(&bm->m, v, NULL, NULL);
830 vm_orthogonalize_matrix(&bm->m);
833 void calculate_bitmap_points(starfield_bitmaps *bm, float bank)
836 vector fvec, uvec, rvec, tmp;
839 vm_orthogonalize_matrix(&bm->m);
841 tangles.p = tangles.h = 0.0f;
843 vm_rotate_matrix_by_angles(&bm->m, &tangles);
847 vm_vec_scale(&fvec, bm->dist );
851 vm_vec_sub(&tmp, &fvec, &uvec);
852 vm_vec_sub(&bm->points[3], &tmp, &rvec);
854 vm_vec_sub(&tmp, &fvec, &uvec);
855 vm_vec_add(&bm->points[2], &tmp, &rvec);
857 vm_vec_add(&tmp, &fvec, &uvec);
858 vm_vec_add(&bm->points[1], &tmp, &rvec);
860 vm_vec_add(&tmp, &fvec, &uvec);
861 vm_vec_sub(&bm->points[0], &tmp, &rvec);
864 vm_vec_normalize(&bm->points[i]);
869 extern int Interp_subspace;
870 extern float Interp_subspace_offset_u;
871 extern float Interp_subspace_offset_u;
872 extern float Interp_subspace_offset_v;
874 float subspace_offset_u = 0.0f;
875 float subspace_offset_u_inner = 0.0f;
876 float subspace_offset_v = 0.0f;
878 float subspace_u_speed = 0.07f; // how fast u changes
879 float subspace_v_speed = 0.05f; // how fast v changes
881 int Subspace_glow_bitmap = -1;
883 float Subspace_glow_frame = 0.0f;
884 float Subspace_glow_rate = 1.0f;
888 DCF(subspace_set,"Set parameters for subspace effect")
891 dc_get_arg(ARG_STRING);
892 if ( !strcmp( Dc_arg, "u" )) {
893 dc_get_arg(ARG_FLOAT);
894 if ( Dc_arg_float < 0.0f ) {
897 subspace_u_speed = Dc_arg_float;
899 } else if ( !strcmp( Dc_arg, "v" )) {
900 dc_get_arg(ARG_FLOAT);
901 if ( Dc_arg_float < 0.0f ) {
904 subspace_v_speed = Dc_arg_float;
907 // print usage, not stats
913 dc_printf( "Usage: subspace keyword\nWhere keyword can be in the following forms:\n" );
914 dc_printf( "subspace u X Where X is how fast u moves.\n", MAX_STARS );
915 dc_printf( "subspace v X Where X is how fast v moves.\n" );
916 dc_printf( "\nUse '? subspace' to see current values.\n" );
917 Dc_status = 0; // don't print status if help is printed. Too messy.
921 dc_printf( "u: %.2f\n", subspace_u_speed );
922 dc_printf( "v: %.2f\n", subspace_v_speed );
927 void subspace_render()
929 if ( Subspace_model_inner == -1 ) {
930 Subspace_model_inner = model_load( "subspace_small.pof", NULL, NULL );
931 Assert(Subspace_model_inner>-1);
934 if ( Subspace_model_outer == -1 ) {
935 Subspace_model_outer = model_load( "subspace_big.pof", NULL, NULL );
936 Assert(Subspace_model_outer>-1);
939 if ( Subspace_glow_bitmap == -1 ) {
940 Subspace_glow_bitmap = bm_load( NOX("SunGlow01"));
941 Assert(Subspace_glow_bitmap>-1);
944 Subspace_glow_frame += flFrametime * 1.0f;
946 float total_time = i2fl(NOISE_NUM_FRAMES) / 15.0f;
949 if ( Subspace_glow_frame < 0.0f ) Subspace_glow_frame = 0.0f;
950 if ( Subspace_glow_frame > 100.0f ) Subspace_glow_frame = 0.0f;
952 while ( Subspace_glow_frame > total_time ) {
953 Subspace_glow_frame -= total_time;
955 int framenum = fl2i( (Subspace_glow_frame*NOISE_NUM_FRAMES) / total_time );
956 if ( framenum < 0 ) framenum = 0;
957 if ( framenum >= NOISE_NUM_FRAMES ) framenum = NOISE_NUM_FRAMES-1;
959 subspace_offset_u += flFrametime*subspace_u_speed;
960 if (subspace_offset_u > 1.0f ) {
961 subspace_offset_u -= 1.0f;
964 subspace_offset_u_inner += flFrametime*subspace_u_speed*3.0f;
965 if (subspace_offset_u > 1.0f ) {
966 subspace_offset_u -= 1.0f;
969 subspace_offset_v += flFrametime*subspace_v_speed;
970 if (subspace_offset_v > 1.0f ) {
971 subspace_offset_v -= 1.0f;
977 angles angs = { 0.0f, 0.0f, 0.0f };
978 angs.b = subspace_offset_v * PI2;
980 vm_angles_2_matrix(&tmp,&angs);
982 int saved_gr_zbuffering = gr_zbuffer_get();
984 gr_zbuffer_set(GR_ZBUFF_NONE);
986 if ( !D3D_enabled ) {
988 int render_flags = MR_NO_LIGHTING | MR_ALWAYS_REDRAW;
991 Interp_subspace_offset_u = 1.0f - subspace_offset_u;
992 Interp_subspace_offset_v = 0.0f;
994 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
995 render_flags |= MR_SHOW_THRUSTERS;
996 model_render( Subspace_model_outer, &tmp, &Eye_position, render_flags ); //MR_NO_CORRECT|MR_SHOW_OUTLINE
1000 int render_flags = MR_NO_LIGHTING | MR_ALWAYS_REDRAW;
1002 Interp_subspace = 1;
1003 Interp_subspace_offset_u = 1.0f - subspace_offset_u;
1004 Interp_subspace_offset_v = 0.0f;
1006 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1007 render_flags |= MR_SHOW_THRUSTERS;
1008 model_render( Subspace_model_outer, &tmp, &Eye_position, render_flags ); //MR_NO_CORRECT|MR_SHOW_OUTLINE
1010 Interp_subspace = 1;
1011 Interp_subspace_offset_u = 1.0f - subspace_offset_u_inner;
1012 Interp_subspace_offset_v = 0.0f;
1014 angs.b = -subspace_offset_v * PI2;
1016 vm_angles_2_matrix(&tmp,&angs);
1018 model_set_outline_color(255,255,255);
1020 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1021 render_flags |= MR_SHOW_THRUSTERS;
1023 model_render( Subspace_model_inner, &tmp, &Eye_position, render_flags ); //MR_NO_CORRECT|MR_SHOW_OUTLINE
1026 Interp_subspace = 0;
1027 gr_zbuffer_set(saved_gr_zbuffering);
1030 void stars_draw( int show_stars, int show_suns, int show_nebulas, int show_subspace )
1036 int gr_zbuffering_save = gr_zbuffer_get();
1037 gr_zbuffer_set(GR_ZBUFF_NONE);
1039 if ( show_subspace ) {
1044 if (Num_stars >= MAX_STARS){
1045 Num_stars = MAX_STARS;
1048 #ifdef TIME_STAR_CODE
1050 xt1 = timer_get_fixed_seconds();
1053 if ( show_nebulas && (Game_detail_flags & DETAIL_FLAG_NEBULAS) && (Neb2_render_mode != NEB2_RENDER_POF) && (Neb2_render_mode != NEB2_RENDER_LAME)) {
1057 // draw background stuff
1058 if((Neb2_render_mode != NEB2_RENDER_POLY) && (Neb2_render_mode != NEB2_RENDER_LAME) && show_stars){
1059 // semi-hack, do we don't fog the background
1060 int neb_save = Neb2_render_mode;
1061 Neb2_render_mode = NEB2_RENDER_NONE;
1062 extern void stars_draw_background();
1063 stars_draw_background();
1064 Neb2_render_mode = neb_save;
1067 if (show_stars && ( Game_detail_flags & DETAIL_FLAG_STARS) && !(The_mission.flags & MISSION_FLAG_FULLNEB) && (supernova_active() < 3)) {
1072 if ( !last_stars_filled ) {
1073 for (sp=Stars,i=0; i<Num_stars; i++, sp++ ) {
1075 g3_rotate_faraway_vertex(&p2, &sp->pos);
1076 sp->last_star_pos.x = p2.x;
1077 sp->last_star_pos.y = p2.y;
1078 sp->last_star_pos.z = p2.z;
1084 tmp_num_stars = (Detail.num_stars*Num_stars)/MAX_DETAIL_LEVEL;
1085 if (tmp_num_stars < 0 ) {
1087 } else if ( tmp_num_stars > Num_stars ) {
1088 tmp_num_stars = Num_stars;
1091 for (sp=Stars,i=0; i<tmp_num_stars; i++, sp++ ) {
1095 memset(&p1, 0, sizeof(vertex));
1097 // This makes a star look "proper" by not translating the
1098 // point around the viewer's eye before rotation. In other
1099 // words, when the ship translates, the stars do not change.
1101 g3_rotate_faraway_vertex(&p2, &sp->pos);
1105 g3_project_vertex(&p2);
1106 if ( p2.flags & PF_OVERFLOW ) {
1113 if ( can_draw && (Star_flags & (STAR_FLAG_TAIL|STAR_FLAG_DIM)) ) {
1115 dist = vm_vec_dist_quick( &sp->last_star_pos, (vector *)&p2.x );
1118 if ( dist > Star_max_length ) {
1119 ratio = Star_max_length / dist;
1120 dist = Star_max_length;
1124 ratio *= Star_amount;
1126 p1.x = p2.x + (sp->last_star_pos.x-p2.x)*ratio;
1127 p1.y = p2.y + (sp->last_star_pos.y-p2.y)*ratio;
1128 p1.z = p2.z + (sp->last_star_pos.z-p2.z)*ratio;
1130 p1.flags = 0; // not projected
1131 g3_code_vertex( &p1 );
1136 g3_project_vertex(&p1);
1137 if ( p1.flags & PF_OVERFLOW ) {
1143 sp->last_star_pos.x = p2.x;
1144 sp->last_star_pos.y = p2.y;
1145 sp->last_star_pos.z = p2.z;
1147 if ( !can_draw ) continue;
1151 if ( Star_flags & STAR_FLAG_DIM ) {
1153 float colorf = 255.0f - dist*Star_dim;
1155 if ( colorf < Star_cap )
1158 color = (fl2i(colorf)*(i&7))/256;
1164 if ( (Star_flags & STAR_FLAG_ANTIALIAS) || (D3D_enabled) ) {
1165 gr_set_color_fast( &star_aacolors[color] );
1167 // if the two points are the same, fudge it, since some D3D cards (G200 and G400) are lame.
1168 if( (fl2i(p1.sx) == fl2i(p2.sx)) && (fl2i(p1.sy) == fl2i(p2.sy)) ){
1173 // use alphablended line so that dark stars don't look bad on top of nebulas
1174 gr_set_color_fast( &star_aacolors[color] );
1175 if ( Star_flags & STAR_FLAG_TAIL ) {
1176 gr_line(fl2i(p1.sx),fl2i(p1.sy),fl2i(p2.sx),fl2i(p2.sy));
1178 gr_pixel( fl2i(p2.sx),fl2i(p2.sy) );
1184 last_stars_filled = 1;
1186 #ifdef TIME_STAR_CODE
1187 xt2 = timer_get_fixed_seconds();
1188 mprintf(( "Stars: %d\n", xt2-xt1 ));
1192 if ( (Game_detail_flags & DETAIL_FLAG_MOTION) && (!Fred_running) && (supernova_active() < 3) ) {
1194 gr_set_color( 0, 0, 0 );
1197 if(The_mission.flags & MISSION_FLAG_FULLNEB){
1198 gr_fog_set(GR_FOGMODE_NONE, 0, 0, 0);
1201 old_debris * d = odebris;
1202 for (i=0; i<MAX_DEBRIS; i++, d++ ) {
1206 d->pos.x = f2fl(myrand() - RAND_MAX/2);
1207 d->pos.y = f2fl(myrand() - RAND_MAX/2);
1208 d->pos.z = f2fl(myrand() - RAND_MAX/2);
1210 vm_vec_normalize(&d->pos);
1212 vm_vec_scale(&d->pos, MAX_DIST);
1213 vm_vec_add2(&d->pos, &Eye_position );
1214 // vm_vec_add2(&d->pos, &Player_obj->pos );
1216 d->vclip = i % MAX_DEBRIS_VCLIPS; //rand()
1218 // if we're in full neb mode
1219 if((The_mission.flags & MISSION_FLAG_FULLNEB) && (Neb2_render_mode != NEB2_RENDER_NONE)){
1220 d->size = i2fl(myrand() % 4)*BASE_SIZE_NEB;
1222 d->size = i2fl(myrand() % 4)*BASE_SIZE;
1225 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1228 if ( reload_old_debris ) {
1229 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1232 g3_rotate_vertex(&p, &d->pos);
1235 int frame = Missiontime / (DEBRIS_ROT_MIN + (i % DEBRIS_ROT_RANGE) * DEBRIS_ROT_RANGE_SCALER);
1236 frame %= debris_vclips[d->vclip].nframes;
1238 if((The_mission.flags & MISSION_FLAG_FULLNEB) && (Neb2_render_mode != NEB2_RENDER_NONE)){
1239 gr_set_bitmap( debris_vclips[d->vclip].bm + frame, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.3f);
1241 gr_set_bitmap( debris_vclips[d->vclip].bm + frame );
1245 vm_vec_add( &tmp, &d->last_pos, &Eye_position );
1246 g3_draw_laser( &d->pos,d->size,&tmp,d->size, TMAP_FLAG_TEXTURED|TMAP_FLAG_XPARENT, 25.0f );
1249 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1251 vdist = vm_vec_mag_quick(&d->last_pos);
1253 if (vdist > MAX_DIST_RANGE)
1255 else if (vdist < MIN_DIST_RANGE)
1259 // vm_vec_sub( &tmp, &d->pos, &Player_obj->pos );
1260 // vdist = vm_vec_dot( &tmp, &Player_obj->orient.fvec );
1261 // if ( vdist < 0.0f )
1266 reload_old_debris = 0;
1270 stars_draw_sun( show_suns );
1271 stars_draw_bitmaps( show_suns );
1273 gr_zbuffer_set( gr_zbuffering_save );
1276 void stars_page_in()
1280 // Initialize the subspace stuff
1282 if ( Game_subspace_effect ) {
1284 Subspace_model_inner = model_load( "subspace_small.pof", NULL, NULL );
1285 Assert(Subspace_model_inner>-1);
1286 Subspace_model_outer = model_load( "subspace_big.pof", NULL, NULL );
1287 Assert(Subspace_model_outer>-1);
1291 pm = model_get(Subspace_model_inner);
1293 nprintf(( "Paging", "Paging in textures for subspace effect.\n" ));
1295 for (j=0; j<pm->n_textures; j++ ) {
1296 int bitmap_num = pm->original_textures[j];
1298 if ( bitmap_num > -1 ) {
1299 bm_page_in_texture( bitmap_num );
1303 pm = model_get(Subspace_model_outer);
1305 nprintf(( "Paging", "Paging in textures for subspace effect.\n" ));
1307 for (j=0; j<pm->n_textures; j++ ) {
1308 int bitmap_num = pm->original_textures[j];
1310 if ( bitmap_num > -1 ) {
1311 bm_page_in_texture( bitmap_num );
1315 Subspace_model_inner = -1;
1316 Subspace_model_outer = -1;
1319 Subspace_glow_bitmap = bm_load( NOX("SunGlow01"));
1320 bm_page_in_xparent_texture(Subspace_glow_bitmap);
1322 // page in starfield bitmaps
1325 while((idx < MAX_STARFIELD_BITMAPS) && (Starfield_bitmaps[idx].bitmap != -1)){
1326 if(Starfield_bitmaps[idx].xparent){
1327 bm_page_in_xparent_texture(Starfield_bitmaps[idx].bitmap);
1329 bm_page_in_texture(Starfield_bitmaps[idx].bitmap);
1336 // sun bitmaps and glows
1338 while((idx < MAX_STARFIELD_BITMAPS) && (Sun_bitmaps[idx].bitmap != -1) && (Sun_bitmaps[idx].glow_bitmap != -1)){
1339 bm_page_in_texture(Sun_bitmaps[idx].bitmap);
1340 bm_page_in_texture(Sun_bitmaps[idx].glow_bitmap);
1346 for (i=0; i<MAX_DEBRIS_VCLIPS; i++ ) {
1347 for (j=0; j<debris_vclips[i].nframes; j++ ) {
1348 bm_page_in_xparent_texture(debris_vclips[i].bm + j);
1353 // background nebula models and planets
1354 void stars_draw_background()
1356 if((Nmodel_num < 0) || (Nmodel_bitmap < 0)){
1360 // draw the model at the player's eye wif no z-buffering
1361 model_set_alpha(1.0f);
1362 model_set_forced_texture(Nmodel_bitmap);
1363 model_render(Nmodel_num, &vmd_identity_matrix, &Eye_position, MR_NO_ZBUFFER | MR_NO_CULL | MR_ALL_XPARENT | MR_NO_LIGHTING | MR_FORCE_TEXTURE);
1364 model_set_forced_texture(-1);
1367 // call this to set a specific model as the background model
1368 void stars_set_background_model(char *model_name, char *texture_name)
1370 Nmodel_num = model_load(model_name, 0, NULL);
1371 Nmodel_bitmap = bm_load(texture_name);
1374 // lookup a starfield bitmap, return index or -1 on fail
1375 int stars_find_bitmap(char *name)
1380 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
1381 if(!strcmp(name, Starfield_bitmaps[idx].filename)){
1390 // lookup a sun by bitmap filename, return index or -1 on fail
1391 int stars_find_sun(char *name)
1396 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
1397 if(!strcmp(name, Sun_bitmaps[idx].filename)){