2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Starfield/StarField.cpp $
15 * Code to handle and draw starfields, background space image bitmaps, floating
19 * Revision 1.4 2002/06/09 04:41:27 relnev
20 * added copyright header
22 * Revision 1.3 2002/06/02 04:26:34 relnev
25 * Revision 1.2 2002/05/07 03:16:52 theoddone33
26 * The Great Newline Fix
28 * Revision 1.1.1.1 2002/05/03 03:28:10 root
32 * 34 9/07/99 4:01p Dave
33 * Fixed up a string.tbl paroblem (self destruct message). Make sure IPX
34 * does everything properly (setting up address when binding). Remove
35 * black rectangle background from UI_INPUTBOX.
37 * 33 9/01/99 10:14a Dave
40 * 32 8/30/99 5:01p Dave
41 * Made d3d do less state changing in the nebula. Use new chat server for
44 * 31 8/19/99 10:59a Dave
45 * Packet loss detection.
47 * 30 7/27/99 3:52p Dave
48 * Make star drawing a bit more robust to help lame D3D cards.
50 * 29 7/21/99 8:10p Dave
51 * First run of supernova effect.
53 * 28 7/13/99 2:01p Dave
54 * Don't draw background bitmaps in the nebula.
56 * 27 6/08/99 2:34p Jasenw
57 * Made perspective bitmaps render in Fred.
59 * 26 6/04/99 1:18p Dave
60 * Fixed briefing model rendering problems. Made show background option in
61 * fred toggle nebula rendering.
63 * 25 6/03/99 6:37p Dave
64 * More TNT fun. Made perspective bitmaps more flexible.
66 * 24 5/28/99 1:45p Dave
67 * Fixed up perspective bitmap drawing.
69 * 23 5/20/99 7:00p Dave
70 * Added alternate type names for ships. Changed swarm missile table
73 * 22 5/11/99 10:03a Dave
74 * Put a bunch of stuff into tables.
76 * 21 5/11/99 9:10a Dave
77 * Move default sun position.
79 * 20 5/09/99 6:00p Dave
80 * Lots of cool new effects. E3 build tweaks.
82 * 19 4/26/99 8:49p Dave
83 * Made all pof based nebula stuff full customizable through fred.
85 * 18 4/25/99 7:43p Dave
86 * Misc small bug fixes. Made sun draw properly.
88 * 17 4/23/99 5:53p Dave
89 * Started putting in new pof nebula support into Fred.
91 * 16 4/07/99 6:22p Dave
92 * Fred and Freespace support for multiple background bitmaps and suns.
93 * Fixed link errors on all subprojects. Moved encrypt_init() to
94 * cfile_init() and lcl_init(), since its safe to call twice.
96 * 15 3/31/99 8:24p Dave
97 * Beefed up all kinds of stuff, incluging beam weapons, nebula effects
98 * and background nebulae. Added per-ship non-dimming pixel colors.
100 * 14 3/20/99 5:09p Dave
101 * Fixed release build fred warnings and unhandled exception.
103 * 13 3/20/99 3:46p Dave
104 * Added support for model-based background nebulae. Added 3 new
107 * 12 3/20/99 2:04p Dave
108 * Removed unnecessary planet rendering.
110 * 11 3/19/99 9:51a Dave
111 * Checkin to repair massive source safe crash. Also added support for
112 * pof-style nebulae, and some new weapons code.
114 * 12 3/15/99 6:45p Daveb
115 * Put in rough nebula bitmap support.
117 * 11 3/11/99 5:53p Dave
118 * More network optimization. Spliced in Dell OEM planet bitmap crap.
120 * 10 2/03/99 11:44a Dave
121 * Fixed d3d transparent textures.
123 * 9 12/09/98 7:34p Dave
124 * Cleanup up nebula effect. Tweaked many values.
126 * 8 12/01/98 10:32a Johnson
127 * Fixed direct3d font problems. Fixed sun bitmap problem. Fixed direct3d
130 * 7 12/01/98 8:06a Dave
131 * Temporary checkin to fix some texture transparency problems in d3d.
133 * 6 11/14/98 5:33p Dave
134 * Lots of nebula work. Put in ship contrails.
136 * 5 11/05/98 5:55p Dave
137 * Big pass at reducing #includes
139 * 4 10/13/98 9:29a Dave
140 * Started neatening up freespace.h. Many variables renamed and
141 * reorganized. Added AlphaColors.[h,cpp]
143 * 3 10/07/98 11:16a Dave
146 * 2 10/07/98 10:54a Dave
149 * 1 10/07/98 10:51a Dave
151 * 106 5/23/98 4:14p John
152 * Added code to preload textures to video card for AGP. Added in code
153 * to page in some bitmaps that weren't getting paged in at level start.
155 * 105 5/13/98 2:53p John
156 * Made subspace effect work under software. Had to add new inner loop to
157 * tmapper. Added glows to end of subspace effect. Made subspace effect
158 * levels use gamepalette-subspace palette.
160 * 104 5/13/98 10:28a John
161 * made subpsace forward sliding 40% faster.
163 * 103 5/10/98 4:18p John
164 * Reversed the subspace effect direction
166 * 102 5/08/98 8:38p John
167 * Subspace tweaks. Made two layers rotate independentyl.
169 * 101 5/08/98 1:32p John
170 * Added code for using two layered subspace effects.
172 * 100 5/06/98 5:30p John
173 * Removed unused cfilearchiver. Removed/replaced some unused/little used
174 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
175 * DirectX header files and libs that fixed the Direct3D alpha blending
178 * 99 4/22/98 4:09p John
179 * String externalization
181 * 98 4/22/98 3:28p John
184 * 97 4/13/98 4:54p John
185 * Made uv rotate independently on subspace effect. Put in DCF function
186 * for setting subspace speeds.
188 * 96 4/12/98 5:55p John
189 * Made models work with subspace. Made subspace rotate also.
191 * 95 4/11/98 6:53p John
192 * Added first rev of subspace effect.
194 * 94 4/08/98 11:31a Dave
195 * AL: Fix syntax error for non-demo
197 * 93 4/08/98 10:46a Lawrance
198 * #ifdef out asteroid check for demo
200 * 92 4/08/98 9:25a John
201 * Made asteroid missions not show suns
203 * 91 4/07/98 4:17p John
204 * Made Fred be able to move suns. Made suns actually affect the lighting
207 * 90 4/07/98 11:19a Hoffoss
208 * Changed code to only use Sun01 for a bitmap by default, as John
215 #include "floating.h"
219 #include "starfield.h"
222 #include "freespace.h"
225 #include "linklist.h"
226 #include "lighting.h"
227 #include "asteroid.h"
228 #include "missionparse.h"
230 #include "alphacolors.h"
231 #include "supernova.h"
233 #define MAX_DEBRIS_VCLIPS 4
234 #define DEBRIS_ROT_MIN 10000
235 #define DEBRIS_ROT_RANGE 8
236 #define DEBRIS_ROT_RANGE_SCALER 10000
237 #define RND_MAX_MASK 0x3fff
238 #define HALF_RND_MAX 0x2000
240 typedef struct debris_vclip {
254 const int MAX_DEBRIS = 200;
255 const int MAX_STARS = 2000;
256 const float MAX_DIST = 50.0f;
257 const float MAX_DIST_RANGE = 60.0f;
258 const float MIN_DIST_RANGE = 14.0f;
259 const float BASE_SIZE = 0.12f;
260 float BASE_SIZE_NEB = 0.5f;
262 static int Subspace_model_inner = -1;
263 static int Subspace_model_outer = -1;
266 fix starfield_timestamp = 0;
268 // for drawing cool stuff on the background - comes from a table
269 starfield_bitmap Starfield_bitmaps[MAX_STARFIELD_BITMAPS];
270 starfield_bitmap_instance Starfield_bitmap_instance[MAX_STARFIELD_BITMAPS];
271 int Num_starfield_bitmaps = 0;
273 // sun bitmaps and sun glow bitmaps
274 starfield_bitmap Sun_bitmaps[MAX_STARFIELD_BITMAPS];
275 starfield_bitmap_instance Suns[MAX_STARFIELD_BITMAPS];
278 int last_stars_filled = 0;
279 color star_colors[8];
280 color star_aacolors[8];
282 typedef struct star {
284 vector last_star_pos;
287 star Stars[MAX_STARS];
289 old_debris odebris[MAX_DEBRIS];
292 debris_vclip debris_vclips_normal[MAX_DEBRIS_VCLIPS] = { { -1, -1, "debris01" }, { -1, -1, "debris02" }, { -1, -1, "debris03" }, { -1, -1, "debris04" } };
293 debris_vclip debris_vclips_nebula[MAX_DEBRIS_VCLIPS] = { { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" } };
294 debris_vclip *debris_vclips = debris_vclips_normal;
297 int stars_debris_loaded = 0;
300 int Stars_background_inited = 0; // if we're inited
301 int Nmodel_num = -1; // model num
302 int Nmodel_bitmap = -1; // model texture
304 // given a starfield_bitmap_instance, return a pointer to its parent, for suns
305 starfield_bitmap *stars_lookup_sun(starfield_bitmap_instance *s)
315 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
316 if(!stricmp(Sun_bitmaps[idx].filename, s->filename)){
317 return &Sun_bitmaps[idx];
325 void stars_load_debris()
329 // if we're in nebula mode
330 if(The_mission.flags & MISSION_FLAG_FULLNEB){
331 debris_vclips = debris_vclips_nebula;
333 debris_vclips = debris_vclips_normal;
336 for (i=0; i<MAX_DEBRIS_VCLIPS; i++ ) {
337 debris_vclips[i].bm = bm_load_animation( debris_vclips[i].name, &debris_vclips[i].nframes, NULL, 1 );
338 if ( debris_vclips[i].bm < 0 ) {
339 // try loading it as a single bitmap
340 debris_vclips[i].bm = bm_load(debris_vclips[i].name);
341 debris_vclips[i].nframes = 1;
343 if(debris_vclips[i].bm <= 0){
344 Error( LOCATION, "Couldn't load animation/bitmap '%s'\n", debris_vclips[i].name );
348 stars_debris_loaded = 1;
351 // call on game startup
354 starfield_bitmap *bm;
356 char filename[MAX_FILENAME_LEN+1] = "";
357 char glow_filename[MAX_FILENAME_LEN+1] = "";
361 read_file_text("stars.tbl");
364 // make all bitmaps invalid
365 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
366 Starfield_bitmaps[idx].bitmap = -1;
367 Starfield_bitmaps[idx].glow_bitmap = -1;
368 strcpy(Starfield_bitmaps[idx].filename, "");
369 strcpy(Starfield_bitmaps[idx].glow_filename, "");
371 Sun_bitmaps[idx].bitmap = -1;
372 Sun_bitmaps[idx].glow_bitmap = -1;
373 strcpy(Sun_bitmaps[idx].filename, "");
374 strcpy(Sun_bitmaps[idx].glow_filename, "");
379 while(!optional_string("#end")){
380 // intensity alpha bitmap
381 if(optional_string("$Bitmap:")){
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 the bitmap now
391 if(Fred_running && (bm->bitmap >= 0)){
392 bm_lock(bm->bitmap, 8, BMP_TEX_OTHER);
393 bm_unlock(bm->bitmap);
397 // green xparency bitmap
398 else if(optional_string("$BitmapX:")){
399 stuff_string(filename, F_NAME, NULL);
400 if(count < MAX_STARFIELD_BITMAPS){
401 bm = &Starfield_bitmaps[count++];
402 strcpy(bm->filename, filename);
404 bm->bitmap = bm_load(bm->filename);
405 Assert(bm->bitmap != -1);
407 // if fred is running we should lock as a 0, 255, 0 bitmap now
408 if(Fred_running && (bm->bitmap >= 0)){
409 bm_lock(bm->bitmap, 8, BMP_TEX_XPARENT);
410 bm_unlock(bm->bitmap);
418 while(!optional_string("#end")){
419 if(optional_string("$Sun:")){
420 stuff_string(filename, F_NAME, NULL);
423 required_string("$Sunglow:");
424 stuff_string(glow_filename, F_NAME, NULL);
426 // associated lighting values
427 required_string("$SunRGBI:");
433 if(count < MAX_STARFIELD_BITMAPS){
434 bm = &Sun_bitmaps[count++];
435 strcpy(bm->filename, filename);
436 strcpy(bm->glow_filename, glow_filename);
438 bm->bitmap = bm_load(bm->filename);
439 bm->glow_bitmap = bm_load(bm->glow_filename);
440 Assert(bm->bitmap != -1);
441 Assert(bm->glow_bitmap != -1);
447 // if fred is running we should lock the bitmap now
450 bm_lock(bm->bitmap, 8, BMP_TEX_OTHER);
451 bm_unlock(bm->bitmap);
453 if(bm->glow_bitmap >= 0){
454 bm_lock(bm->glow_bitmap, 8, BMP_TEX_OTHER);
455 bm_unlock(bm->glow_bitmap);
462 // normal debris pieces
464 while(!optional_string("#end")){
465 required_string("$Debris:");
466 stuff_string(filename, F_NAME, NULL);
468 if(count < MAX_DEBRIS_VCLIPS){
469 strcpy(debris_vclips_normal[count++].name, filename);
474 // nebula debris pieces
476 while(!optional_string("#end")){
477 required_string("$DebrisNeb:");
478 stuff_string(filename, F_NAME, NULL);
480 if(count < MAX_DEBRIS_VCLIPS){
481 strcpy(debris_vclips_nebula[count++].name, filename);
487 // call this in game_post_level_init() so we know whether we're running in full nebula mode or not
488 void stars_level_init()
492 float dist, dist_max;
494 // reset to -1 so we reload it each mission (if we need to)
496 if(Nmodel_bitmap != -1){
497 bm_unload(Nmodel_bitmap);
501 // if (!stars_debris_loaded){
505 // following code randomly distributes star points within a sphere volume, which
506 // avoids there being denser areas along the edges and in corners that we had in the
507 // old rectangular distribution scheme.
508 dist_max = (float) (HALF_RND_MAX * HALF_RND_MAX);
509 for (i=0; i<MAX_STARS; i++) {
511 while (dist >= dist_max) {
512 v.x = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
513 v.y = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
514 v.z = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
516 dist = v.x * v.x + v.y * v.y + v.z * v.z;
518 vm_vec_copy_normalize(&Stars[i].pos, &v);
521 for (i=0; i<MAX_DEBRIS; i++) {
522 odebris[i].active = 0;
525 for (i=0; i<8; i++ ) {
526 ubyte intensity = (ubyte)((i + 1) * 24);
527 gr_init_alphacolor(&star_aacolors[i], 255, 255, 255, intensity, AC_TYPE_BLEND );
528 gr_init_color(&star_colors[i], intensity, intensity, intensity );
531 last_stars_filled = 0;
533 // if we have no sun instances, create one
535 mprintf(("Adding default sun\n"));
538 strcpy(Suns[0].filename, Sun_bitmaps[0].filename);
539 Suns[0].scale_x = 1.0f;
540 Suns[0].scale_y = 1.0f;
543 memset(&Suns[0].ang, 0, sizeof(angles));
544 Suns[0].ang.h = fl_radian(60.0f);
553 extern object * Player_obj;
555 #define STAR_AMOUNT_DEFAULT 0.75f
556 #define STAR_DIM_DEFAULT 7800.0f
557 #define STAR_CAP_DEFAULT 75.0f
558 #define STAR_MAX_LENGTH_DEFAULT 0.04f // 312
560 float Star_amount = STAR_AMOUNT_DEFAULT;
561 float Star_dim = STAR_DIM_DEFAULT;
562 float Star_cap = STAR_CAP_DEFAULT;
563 float Star_max_length = STAR_MAX_LENGTH_DEFAULT;
565 #define STAR_FLAG_TAIL (1<<0) // Draw a tail when moving
566 #define STAR_FLAG_DIM (1<<1) // Dim as you move
567 #define STAR_FLAG_ANTIALIAS (1<<2) // Draw the star using antialiased lines
568 #define STAR_FLAG_DEFAULT (STAR_FLAG_TAIL | STAR_FLAG_DIM)
570 uint Star_flags = STAR_FLAG_DEFAULT;
573 DCF(stars,"Set parameters for starfield")
576 dc_get_arg(ARG_STRING);
577 if ( !strcmp( Dc_arg, "tail" )) {
578 dc_get_arg(ARG_FLOAT);
579 if ( (Dc_arg_float < 0.0f) || (Dc_arg_float > 1.0f) ) {
582 Star_amount = Dc_arg_float;
584 } else if ( !strcmp( Dc_arg, "len" )) {
585 dc_get_arg(ARG_FLOAT);
586 Star_max_length = Dc_arg_float;
587 } else if ( !strcmp( Dc_arg, "dim" )) {
588 dc_get_arg(ARG_FLOAT);
589 if ( Dc_arg_float < 0.0f ) {
592 Star_dim = Dc_arg_float;
594 } else if ( !strcmp( Dc_arg, "flag" )) {
595 dc_get_arg(ARG_STRING);
596 if ( !strcmp( Dc_arg, "tail" )) {
597 Star_flags ^= STAR_FLAG_TAIL;
598 } else if ( !strcmp( Dc_arg, "dim" )) {
599 Star_flags ^= STAR_FLAG_DIM;
600 } else if ( !strcmp( Dc_arg, "aa" )) {
601 Star_flags ^= STAR_FLAG_ANTIALIAS;
605 } else if ( !strcmp( Dc_arg, "cap" )) {
606 dc_get_arg(ARG_FLOAT);
607 if ( (Dc_arg_float < 0.0f) || (Dc_arg_float > 255.0f) ) {
610 Star_cap = Dc_arg_float;
612 } else if ( !strcmp( Dc_arg, "m0" ) ) {
617 Star_max_length = STAR_MAX_LENGTH_DEFAULT;
618 } else if ( !strcmp( Dc_arg, "m1" ) || !strcmp( Dc_arg, "default" )) {
619 Star_amount = STAR_AMOUNT_DEFAULT;
620 Star_dim = STAR_DIM_DEFAULT;
621 Star_cap = STAR_CAP_DEFAULT;
622 Star_flags = STAR_FLAG_DEFAULT;
623 Star_max_length = STAR_MAX_LENGTH_DEFAULT;
624 } else if ( !strcmp( Dc_arg, "m2" )) {
628 Star_flags = STAR_FLAG_TAIL|STAR_FLAG_DIM|STAR_FLAG_ANTIALIAS;
629 Star_max_length = STAR_MAX_LENGTH_DEFAULT;
630 } else if ( !strcmp( Dc_arg, "num" )) {
632 if ( (Dc_arg_int < 0) || (Dc_arg_int > MAX_STARS) ) {
635 Num_stars = Dc_arg_int;
638 // print usage, not stats
644 dc_printf( "Usage: stars keyword\nWhere keyword can be in the following forms:\n" );
645 dc_printf( "stars default Resets stars to all default values\n" );
646 dc_printf( "stars num X Sets number of stars to X. Between 0 and %d.\n", MAX_STARS );
647 dc_printf( "stars tail X Where X is the percent of 'tail' between 0 and 1.0\n" );
648 dc_printf( "stars dim X Where X is the amount stars dim between 0 and 255.0\n" );
649 dc_printf( "stars cap X Where X is the cap of dimming between 0 and 255.\n" );
650 dc_printf( "stars len X Where X is the cap of length.\n" );
651 dc_printf( "stars m0 Macro0. Old 'pixel type' crappy stars. flags=none\n" );
652 dc_printf( "stars m1 Macro1. (default) tail=.75, dim=20.0, cap=75.0, flags=dim,tail\n" );
653 dc_printf( "stars m2 Macro2. tail=.75, dim=20.0, cap=75.0, flags=dim,tail,aa\n" );
654 dc_printf( "stars flag X Toggles flag X, where X is tail or dim or aa (aa=antialias)\n" );
655 dc_printf( "\nHINT: set cap to 0 to get dim rate and tail down, then use\n" );
656 dc_printf( "cap to keep the lines from going away when moving too fast.\n" );
657 dc_printf( "\nUse '? stars' to see current values.\n" );
658 Dc_status = 0; // don't print status if help is printed. Too messy.
662 dc_printf( "Num_stars: %d\n", Num_stars );
663 dc_printf( "Tail: %.2f\n", Star_amount );
664 dc_printf( "Dim: %.2f\n", Star_dim );
665 dc_printf( "Cap: %.2f\n", Star_cap );
666 dc_printf( "Max length: %.2f\n", Star_max_length );
667 dc_printf( "Flags:\n" );
668 dc_printf( " Tail: %s\n", (Star_flags&STAR_FLAG_TAIL?"On":"Off") );
669 dc_printf( " Dim: %s\n", (Star_flags&STAR_FLAG_DIM?"On":"Off") );
670 dc_printf( " Antialias: %s\n", (Star_flags&STAR_FLAG_ANTIALIAS?"On":"Off") );
671 dc_printf( "\nTHESE AREN'T SAVED TO DISK, SO IF YOU TWEAK\n" );
672 dc_printf( "THESE AND LIKE THEM, WRITE THEM DOWN!!\n" );
677 int reload_old_debris = 1; // If set to one, then reload all the last_pos of the debris
679 // Call this if camera "cuts" or moves long distances
680 // so blur effect doesn't draw lines all over the screen.
681 void stars_camera_cut()
683 last_stars_filled = 0;
684 reload_old_debris = 1;
687 //#define TIME_STAR_CODE // enable to time star code
690 extern float Viewer_zoom;
692 // get the world coords of the sun pos on the unit sphere.
693 void stars_get_sun_pos(int sun_n, vector *pos)
699 Assert(sun_n < Num_suns);
700 if((sun_n >= Num_suns) || (sun_n < 0)){
704 // rotate the sun properly
705 temp = vmd_zero_vector;
709 vm_angles_2_matrix(&rot, &Suns[sun_n].ang);
710 vm_vec_rotate(pos, &temp, &rot);
714 void stars_draw_sun( int show_sun )
720 starfield_bitmap *bm;
721 float local_scale = 1.0f;
727 for(idx=0; idx<Num_suns; idx++){
729 bm = stars_lookup_sun(&Suns[idx]);
735 sun_pos = vmd_zero_vector;
737 stars_get_sun_pos(idx, &sun_pos);
741 vm_vec_normalize(&sun_dir);
743 // add the light source corresponding to the sun
744 light_add_directional(&sun_dir, bm->i, bm->r, bm->g, bm->b);
747 if(supernova_active()){
748 local_scale = 1.0f + (SUPERNOVA_SUN_SCALE * supernova_pct_complete());
751 // draw the sun itself, keep track of how many we drew
752 gr_set_bitmap(bm->bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.999f);
753 g3_rotate_faraway_vertex(&sun_vex, &sun_pos);
754 if(!g3_draw_bitmap(&sun_vex, 0, 0.05f * Suns[idx].scale_x * local_scale, TMAP_FLAG_TEXTURED)){
760 // draw the corresponding glow for sun_n
761 void stars_draw_sun_glow(int sun_n)
763 starfield_bitmap *bm;
764 vector sun_pos, sun_dir;
766 float local_scale = 1.0f;
769 Assert(sun_n < Num_suns);
770 if((sun_n >= Num_suns) || (sun_n < 0)){
775 bm = stars_lookup_sun(&Suns[sun_n]);
781 sun_pos = vmd_zero_vector;
783 stars_get_sun_pos(sun_n, &sun_pos);
787 vm_vec_normalize(&sun_dir);
790 if(supernova_active()){
791 local_scale = 1.0f + (SUPERNOVA_SUN_SCALE * supernova_pct_complete());
794 // draw the sun itself, keep track of how many we drew
795 gr_set_bitmap(bm->glow_bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.5f);
796 g3_rotate_faraway_vertex(&sun_vex, &sun_pos);
797 g3_draw_bitmap(&sun_vex, 0, 0.10f * Suns[sun_n].scale_x * local_scale, TMAP_FLAG_TEXTURED);
801 void stars_draw_bitmaps( int show_bitmaps )
806 // if we're in the nebula, don't render any backgrounds
807 if(The_mission.flags & MISSION_FLAG_FULLNEB){
812 if(!Detail.planets_suns){
816 // render all bitmaps
817 for(idx=0; idx<Num_starfield_bitmaps; idx++){
818 // lookup the info index
819 star_index = stars_find_bitmap(Starfield_bitmap_instance[idx].filename);
826 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.9999f);
827 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);
829 if(Starfield_bitmaps[star_index].xparent){
830 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap);
831 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);
833 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.9999f);
834 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);
841 void calculate_bitmap_matrix(starfield_bitmaps *bm, vector *v)
843 vm_vector_2_matrix(&bm->m, v, NULL, NULL);
844 vm_orthogonalize_matrix(&bm->m);
847 void calculate_bitmap_points(starfield_bitmaps *bm, float bank)
850 vector fvec, uvec, rvec, tmp;
853 vm_orthogonalize_matrix(&bm->m);
855 tangles.p = tangles.h = 0.0f;
857 vm_rotate_matrix_by_angles(&bm->m, &tangles);
861 vm_vec_scale(&fvec, bm->dist );
865 vm_vec_sub(&tmp, &fvec, &uvec);
866 vm_vec_sub(&bm->points[3], &tmp, &rvec);
868 vm_vec_sub(&tmp, &fvec, &uvec);
869 vm_vec_add(&bm->points[2], &tmp, &rvec);
871 vm_vec_add(&tmp, &fvec, &uvec);
872 vm_vec_add(&bm->points[1], &tmp, &rvec);
874 vm_vec_add(&tmp, &fvec, &uvec);
875 vm_vec_sub(&bm->points[0], &tmp, &rvec);
878 vm_vec_normalize(&bm->points[i]);
883 extern int Interp_subspace;
884 extern float Interp_subspace_offset_u;
885 extern float Interp_subspace_offset_u;
886 extern float Interp_subspace_offset_v;
888 float subspace_offset_u = 0.0f;
889 float subspace_offset_u_inner = 0.0f;
890 float subspace_offset_v = 0.0f;
892 float subspace_u_speed = 0.07f; // how fast u changes
893 float subspace_v_speed = 0.05f; // how fast v changes
895 int Subspace_glow_bitmap = -1;
897 float Subspace_glow_frame = 0.0f;
898 float Subspace_glow_rate = 1.0f;
902 DCF(subspace_set,"Set parameters for subspace effect")
905 dc_get_arg(ARG_STRING);
906 if ( !strcmp( Dc_arg, "u" )) {
907 dc_get_arg(ARG_FLOAT);
908 if ( Dc_arg_float < 0.0f ) {
911 subspace_u_speed = Dc_arg_float;
913 } else if ( !strcmp( Dc_arg, "v" )) {
914 dc_get_arg(ARG_FLOAT);
915 if ( Dc_arg_float < 0.0f ) {
918 subspace_v_speed = Dc_arg_float;
921 // print usage, not stats
927 dc_printf( "Usage: subspace keyword\nWhere keyword can be in the following forms:\n" );
928 dc_printf( "subspace u X Where X is how fast u moves.\n", MAX_STARS );
929 dc_printf( "subspace v X Where X is how fast v moves.\n" );
930 dc_printf( "\nUse '? subspace' to see current values.\n" );
931 Dc_status = 0; // don't print status if help is printed. Too messy.
935 dc_printf( "u: %.2f\n", subspace_u_speed );
936 dc_printf( "v: %.2f\n", subspace_v_speed );
941 void subspace_render()
943 if ( Subspace_model_inner == -1 ) {
944 Subspace_model_inner = model_load( "subspace_small.pof", 0, NULL );
945 Assert(Subspace_model_inner>-1);
948 if ( Subspace_model_outer == -1 ) {
949 Subspace_model_outer = model_load( "subspace_big.pof", 0, NULL );
950 Assert(Subspace_model_outer>-1);
953 if ( Subspace_glow_bitmap == -1 ) {
954 Subspace_glow_bitmap = bm_load( NOX("SunGlow01"));
955 Assert(Subspace_glow_bitmap>-1);
958 Subspace_glow_frame += flFrametime * 1.0f;
960 float total_time = i2fl(NOISE_NUM_FRAMES) / 15.0f;
963 if ( Subspace_glow_frame < 0.0f ) Subspace_glow_frame = 0.0f;
964 if ( Subspace_glow_frame > 100.0f ) Subspace_glow_frame = 0.0f;
966 while ( Subspace_glow_frame > total_time ) {
967 Subspace_glow_frame -= total_time;
969 int framenum = fl2i( (Subspace_glow_frame*NOISE_NUM_FRAMES) / total_time );
970 if ( framenum < 0 ) framenum = 0;
971 if ( framenum >= NOISE_NUM_FRAMES ) framenum = NOISE_NUM_FRAMES-1;
973 subspace_offset_u += flFrametime*subspace_u_speed;
974 if (subspace_offset_u > 1.0f ) {
975 subspace_offset_u -= 1.0f;
978 subspace_offset_u_inner += flFrametime*subspace_u_speed*3.0f;
979 if (subspace_offset_u > 1.0f ) {
980 subspace_offset_u -= 1.0f;
983 subspace_offset_v += flFrametime*subspace_v_speed;
984 if (subspace_offset_v > 1.0f ) {
985 subspace_offset_v -= 1.0f;
991 angles angs = { 0.0f, 0.0f, 0.0f };
992 angs.b = subspace_offset_v * PI2;
994 vm_angles_2_matrix(&tmp,&angs);
996 int saved_gr_zbuffering = gr_zbuffer_get();
998 gr_zbuffer_set(GR_ZBUFF_NONE);
1000 if ( !D3D_enabled ) {
1002 int render_flags = MR_NO_LIGHTING | MR_ALWAYS_REDRAW;
1004 Interp_subspace = 1;
1005 Interp_subspace_offset_u = 1.0f - subspace_offset_u;
1006 Interp_subspace_offset_v = 0.0f;
1008 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1009 render_flags |= MR_SHOW_THRUSTERS;
1010 model_render( Subspace_model_outer, &tmp, &Eye_position, render_flags ); //MR_NO_CORRECT|MR_SHOW_OUTLINE
1014 int render_flags = MR_NO_LIGHTING | MR_ALWAYS_REDRAW;
1016 Interp_subspace = 1;
1017 Interp_subspace_offset_u = 1.0f - subspace_offset_u;
1018 Interp_subspace_offset_v = 0.0f;
1020 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1021 render_flags |= MR_SHOW_THRUSTERS;
1022 model_render( Subspace_model_outer, &tmp, &Eye_position, render_flags ); //MR_NO_CORRECT|MR_SHOW_OUTLINE
1024 Interp_subspace = 1;
1025 Interp_subspace_offset_u = 1.0f - subspace_offset_u_inner;
1026 Interp_subspace_offset_v = 0.0f;
1028 angs.b = -subspace_offset_v * PI2;
1030 vm_angles_2_matrix(&tmp,&angs);
1032 model_set_outline_color(255,255,255);
1034 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1035 render_flags |= MR_SHOW_THRUSTERS;
1037 model_render( Subspace_model_inner, &tmp, &Eye_position, render_flags ); //MR_NO_CORRECT|MR_SHOW_OUTLINE
1040 Interp_subspace = 0;
1041 gr_zbuffer_set(saved_gr_zbuffering);
1044 void stars_draw( int show_stars, int show_suns, int show_nebulas, int show_subspace )
1050 int gr_zbuffering_save = gr_zbuffer_get();
1051 gr_zbuffer_set(GR_ZBUFF_NONE);
1053 if ( show_subspace ) {
1058 if (Num_stars >= MAX_STARS){
1059 Num_stars = MAX_STARS;
1062 #ifdef TIME_STAR_CODE
1064 xt1 = timer_get_fixed_seconds();
1067 if ( show_nebulas && (Game_detail_flags & DETAIL_FLAG_NEBULAS) && (Neb2_render_mode != NEB2_RENDER_POF) && (Neb2_render_mode != NEB2_RENDER_LAME)) {
1071 // draw background stuff
1072 if((Neb2_render_mode != NEB2_RENDER_POLY) && (Neb2_render_mode != NEB2_RENDER_LAME) && show_stars){
1073 // semi-hack, do we don't fog the background
1074 int neb_save = Neb2_render_mode;
1075 Neb2_render_mode = NEB2_RENDER_NONE;
1076 extern void stars_draw_background();
1077 stars_draw_background();
1078 Neb2_render_mode = neb_save;
1081 if (show_stars && ( Game_detail_flags & DETAIL_FLAG_STARS) && !(The_mission.flags & MISSION_FLAG_FULLNEB) && (supernova_active() < 3)) {
1086 if ( !last_stars_filled ) {
1087 for (sp=Stars,i=0; i<Num_stars; i++, sp++ ) {
1089 g3_rotate_faraway_vertex(&p2, &sp->pos);
1090 sp->last_star_pos.x = p2.x;
1091 sp->last_star_pos.y = p2.y;
1092 sp->last_star_pos.z = p2.z;
1098 tmp_num_stars = (Detail.num_stars*Num_stars)/MAX_DETAIL_LEVEL;
1099 if (tmp_num_stars < 0 ) {
1101 } else if ( tmp_num_stars > Num_stars ) {
1102 tmp_num_stars = Num_stars;
1105 for (sp=Stars,i=0; i<tmp_num_stars; i++, sp++ ) {
1109 memset(&p1, 0, sizeof(vertex));
1111 // This makes a star look "proper" by not translating the
1112 // point around the viewer's eye before rotation. In other
1113 // words, when the ship translates, the stars do not change.
1115 g3_rotate_faraway_vertex(&p2, &sp->pos);
1119 g3_project_vertex(&p2);
1120 if ( p2.flags & PF_OVERFLOW ) {
1127 if ( can_draw && (Star_flags & (STAR_FLAG_TAIL|STAR_FLAG_DIM)) ) {
1129 dist = vm_vec_dist_quick( &sp->last_star_pos, (vector *)&p2.x );
1132 if ( dist > Star_max_length ) {
1133 ratio = Star_max_length / dist;
1134 dist = Star_max_length;
1138 ratio *= Star_amount;
1140 p1.x = p2.x + (sp->last_star_pos.x-p2.x)*ratio;
1141 p1.y = p2.y + (sp->last_star_pos.y-p2.y)*ratio;
1142 p1.z = p2.z + (sp->last_star_pos.z-p2.z)*ratio;
1144 p1.flags = 0; // not projected
1145 g3_code_vertex( &p1 );
1150 g3_project_vertex(&p1);
1151 if ( p1.flags & PF_OVERFLOW ) {
1157 sp->last_star_pos.x = p2.x;
1158 sp->last_star_pos.y = p2.y;
1159 sp->last_star_pos.z = p2.z;
1161 if ( !can_draw ) continue;
1165 if ( Star_flags & STAR_FLAG_DIM ) {
1167 float colorf = 255.0f - dist*Star_dim;
1169 if ( colorf < Star_cap )
1172 color = (fl2i(colorf)*(i&7))/256;
1178 if ( (Star_flags & STAR_FLAG_ANTIALIAS) || (D3D_enabled) ) {
1179 gr_set_color_fast( &star_aacolors[color] );
1181 // if the two points are the same, fudge it, since some D3D cards (G200 and G400) are lame.
1182 if( (fl2i(p1.sx) == fl2i(p2.sx)) && (fl2i(p1.sy) == fl2i(p2.sy)) ){
1187 // use alphablended line so that dark stars don't look bad on top of nebulas
1188 gr_set_color_fast( &star_aacolors[color] );
1189 if ( Star_flags & STAR_FLAG_TAIL ) {
1190 gr_line(fl2i(p1.sx),fl2i(p1.sy),fl2i(p2.sx),fl2i(p2.sy));
1192 gr_pixel( fl2i(p2.sx),fl2i(p2.sy) );
1198 last_stars_filled = 1;
1200 #ifdef TIME_STAR_CODE
1201 xt2 = timer_get_fixed_seconds();
1202 mprintf(( "Stars: %d\n", xt2-xt1 ));
1206 if ( (Game_detail_flags & DETAIL_FLAG_MOTION) && (!Fred_running) && (supernova_active() < 3) ) {
1208 gr_set_color( 0, 0, 0 );
1211 if(The_mission.flags & MISSION_FLAG_FULLNEB){
1212 gr_fog_set(GR_FOGMODE_NONE, 0, 0, 0);
1215 old_debris * d = odebris;
1216 for (i=0; i<MAX_DEBRIS; i++, d++ ) {
1220 d->pos.x = f2fl(myrand() - RAND_MAX/2);
1221 d->pos.y = f2fl(myrand() - RAND_MAX/2);
1222 d->pos.z = f2fl(myrand() - RAND_MAX/2);
1224 vm_vec_normalize(&d->pos);
1226 vm_vec_scale(&d->pos, MAX_DIST);
1227 vm_vec_add2(&d->pos, &Eye_position );
1228 // vm_vec_add2(&d->pos, &Player_obj->pos );
1230 d->vclip = i % MAX_DEBRIS_VCLIPS; //rand()
1232 // if we're in full neb mode
1233 if((The_mission.flags & MISSION_FLAG_FULLNEB) && (Neb2_render_mode != NEB2_RENDER_NONE)){
1234 d->size = i2fl(myrand() % 4)*BASE_SIZE_NEB;
1236 d->size = i2fl(myrand() % 4)*BASE_SIZE;
1239 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1242 if ( reload_old_debris ) {
1243 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1246 g3_rotate_vertex(&p, &d->pos);
1249 int frame = Missiontime / (DEBRIS_ROT_MIN + (i % DEBRIS_ROT_RANGE) * DEBRIS_ROT_RANGE_SCALER);
1250 frame %= debris_vclips[d->vclip].nframes;
1252 if((The_mission.flags & MISSION_FLAG_FULLNEB) && (Neb2_render_mode != NEB2_RENDER_NONE)){
1253 gr_set_bitmap( debris_vclips[d->vclip].bm + frame, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.3f);
1255 gr_set_bitmap( debris_vclips[d->vclip].bm + frame );
1259 vm_vec_add( &tmp, &d->last_pos, &Eye_position );
1260 g3_draw_laser( &d->pos,d->size,&tmp,d->size, TMAP_FLAG_TEXTURED|TMAP_FLAG_XPARENT, 25.0f );
1263 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1265 vdist = vm_vec_mag_quick(&d->last_pos);
1267 if (vdist > MAX_DIST_RANGE)
1269 else if (vdist < MIN_DIST_RANGE)
1273 // vm_vec_sub( &tmp, &d->pos, &Player_obj->pos );
1274 // vdist = vm_vec_dot( &tmp, &Player_obj->orient.fvec );
1275 // if ( vdist < 0.0f )
1280 reload_old_debris = 0;
1284 stars_draw_sun( show_suns );
1285 stars_draw_bitmaps( show_suns );
1287 gr_zbuffer_set( gr_zbuffering_save );
1290 void stars_page_in()
1294 // Initialize the subspace stuff
1296 if ( Game_subspace_effect ) {
1298 Subspace_model_inner = model_load( "subspace_small.pof", 0, NULL );
1299 Assert(Subspace_model_inner>-1);
1300 Subspace_model_outer = model_load( "subspace_big.pof", 0, NULL );
1301 Assert(Subspace_model_outer>-1);
1305 pm = model_get(Subspace_model_inner);
1307 nprintf(( "Paging", "Paging in textures for subspace effect.\n" ));
1309 for (j=0; j<pm->n_textures; j++ ) {
1310 int bitmap_num = pm->original_textures[j];
1312 if ( bitmap_num > -1 ) {
1313 bm_page_in_texture( bitmap_num );
1317 pm = model_get(Subspace_model_outer);
1319 nprintf(( "Paging", "Paging in textures for subspace effect.\n" ));
1321 for (j=0; j<pm->n_textures; j++ ) {
1322 int bitmap_num = pm->original_textures[j];
1324 if ( bitmap_num > -1 ) {
1325 bm_page_in_texture( bitmap_num );
1329 Subspace_model_inner = -1;
1330 Subspace_model_outer = -1;
1333 Subspace_glow_bitmap = bm_load( NOX("SunGlow01"));
1334 bm_page_in_xparent_texture(Subspace_glow_bitmap);
1336 // page in starfield bitmaps
1339 while((idx < MAX_STARFIELD_BITMAPS) && (Starfield_bitmaps[idx].bitmap != -1)){
1340 if(Starfield_bitmaps[idx].xparent){
1341 bm_page_in_xparent_texture(Starfield_bitmaps[idx].bitmap);
1343 bm_page_in_texture(Starfield_bitmaps[idx].bitmap);
1350 // sun bitmaps and glows
1352 while((idx < MAX_STARFIELD_BITMAPS) && (Sun_bitmaps[idx].bitmap != -1) && (Sun_bitmaps[idx].glow_bitmap != -1)){
1353 bm_page_in_texture(Sun_bitmaps[idx].bitmap);
1354 bm_page_in_texture(Sun_bitmaps[idx].glow_bitmap);
1360 for (i=0; i<MAX_DEBRIS_VCLIPS; i++ ) {
1361 for (j=0; j<debris_vclips[i].nframes; j++ ) {
1362 bm_page_in_xparent_texture(debris_vclips[i].bm + j);
1367 // background nebula models and planets
1368 void stars_draw_background()
1370 if((Nmodel_num < 0) || (Nmodel_bitmap < 0)){
1374 // draw the model at the player's eye wif no z-buffering
1375 model_set_alpha(1.0f);
1376 model_set_forced_texture(Nmodel_bitmap);
1377 model_render(Nmodel_num, &vmd_identity_matrix, &Eye_position, MR_NO_ZBUFFER | MR_NO_CULL | MR_ALL_XPARENT | MR_NO_LIGHTING | MR_FORCE_TEXTURE);
1378 model_set_forced_texture(-1);
1381 // call this to set a specific model as the background model
1382 void stars_set_background_model(char *model_name, char *texture_name)
1384 Nmodel_num = model_load(model_name, 0, NULL);
1385 Nmodel_bitmap = bm_load(texture_name);
1388 // lookup a starfield bitmap, return index or -1 on fail
1389 int stars_find_bitmap(char *name)
1394 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
1395 if(!strcmp(name, Starfield_bitmaps[idx].filename)){
1404 // lookup a sun by bitmap filename, return index or -1 on fail
1405 int stars_find_sun(char *name)
1410 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
1411 if(!strcmp(name, Sun_bitmaps[idx].filename)){