]> icculus.org git repositories - taylor/freespace2.git/blob - src/starfield/starfield.cpp
Initial revision
[taylor/freespace2.git] / src / starfield / starfield.cpp
1 /*
2  * $Logfile: /Freespace2/code/Starfield/StarField.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * Code to handle and draw starfields, background space image bitmaps, floating
8  * debris, etc.
9  *
10  * $Log$
11  * Revision 1.1  2002/05/03 03:28:10  root
12  * Initial revision
13  *
14  * 
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.
19  * 
20  * 33    9/01/99 10:14a Dave
21  * Pirate bob.
22  * 
23  * 32    8/30/99 5:01p Dave
24  * Made d3d do less state changing in the nebula. Use new chat server for
25  * PXO.
26  * 
27  * 31    8/19/99 10:59a Dave
28  * Packet loss detection.
29  * 
30  * 30    7/27/99 3:52p Dave
31  * Make star drawing a bit more robust to help lame D3D cards.
32  * 
33  * 29    7/21/99 8:10p Dave
34  * First run of supernova effect.
35  * 
36  * 28    7/13/99 2:01p Dave
37  * Don't draw background bitmaps in the nebula.
38  * 
39  * 27    6/08/99 2:34p Jasenw
40  * Made perspective bitmaps render in Fred.
41  * 
42  * 26    6/04/99 1:18p Dave
43  * Fixed briefing model rendering problems. Made show background option in
44  * fred toggle nebula rendering.
45  * 
46  * 25    6/03/99 6:37p Dave
47  * More TNT fun. Made perspective bitmaps more flexible.
48  * 
49  * 24    5/28/99 1:45p Dave
50  * Fixed up perspective bitmap drawing.
51  * 
52  * 23    5/20/99 7:00p Dave
53  * Added alternate type names for ships. Changed swarm missile table
54  * entries.
55  * 
56  * 22    5/11/99 10:03a Dave
57  * Put a bunch of stuff into tables.
58  * 
59  * 21    5/11/99 9:10a Dave
60  * Move default sun position.
61  * 
62  * 20    5/09/99 6:00p Dave
63  * Lots of cool new effects. E3 build tweaks.
64  * 
65  * 19    4/26/99 8:49p Dave
66  * Made all pof based nebula stuff full customizable through fred.
67  * 
68  * 18    4/25/99 7:43p Dave
69  * Misc small bug fixes. Made sun draw properly.
70  * 
71  * 17    4/23/99 5:53p Dave
72  * Started putting in new pof nebula support into Fred.
73  * 
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.
78  * 
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.
82  * 
83  * 14    3/20/99 5:09p Dave
84  * Fixed release build fred warnings and unhandled exception.
85  * 
86  * 13    3/20/99 3:46p Dave
87  * Added support for model-based background nebulae. Added 3 new
88  * sexpressions.
89  * 
90  * 12    3/20/99 2:04p Dave
91  * Removed unnecessary planet rendering.
92  * 
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.
96  * 
97  * 12    3/15/99 6:45p Daveb
98  * Put in rough nebula bitmap support.
99  * 
100  * 11    3/11/99 5:53p Dave
101  * More network optimization. Spliced in Dell OEM planet bitmap crap.
102  * 
103  * 10    2/03/99 11:44a Dave
104  * Fixed d3d transparent textures.
105  * 
106  * 9     12/09/98 7:34p Dave
107  * Cleanup up nebula effect. Tweaked many values.
108  * 
109  * 8     12/01/98 10:32a Johnson
110  * Fixed direct3d font problems. Fixed sun bitmap problem. Fixed direct3d
111  * starfield problem.
112  * 
113  * 7     12/01/98 8:06a Dave
114  * Temporary checkin to fix some texture transparency problems in d3d.
115  * 
116  * 6     11/14/98 5:33p Dave
117  * Lots of nebula work. Put in ship contrails.
118  * 
119  * 5     11/05/98 5:55p Dave
120  * Big pass at reducing #includes
121  * 
122  * 4     10/13/98 9:29a Dave
123  * Started neatening up freespace.h. Many variables renamed and
124  * reorganized. Added AlphaColors.[h,cpp]
125  * 
126  * 3     10/07/98 11:16a Dave
127  * Remove warning.
128  * 
129  * 2     10/07/98 10:54a Dave
130  * Initial checkin.
131  * 
132  * 1     10/07/98 10:51a Dave
133  * 
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.
137  * 
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.
142  * 
143  * 104   5/13/98 10:28a John
144  * made subpsace forward sliding 40% faster.
145  * 
146  * 103   5/10/98 4:18p John
147  * Reversed the subspace effect direction
148  * 
149  * 102   5/08/98 8:38p John
150  * Subspace tweaks.  Made two layers rotate independentyl.  
151  * 
152  * 101   5/08/98 1:32p John
153  * Added code for using two layered subspace effects.
154  * 
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
159  * problems.
160  * 
161  * 99    4/22/98 4:09p John
162  * String externalization
163  * 
164  * 98    4/22/98 3:28p John
165  * Fixed XSTR bug
166  * 
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.
170  * 
171  * 96    4/12/98 5:55p John
172  * Made models work with subspace.  Made subspace rotate also.
173  * 
174  * 95    4/11/98 6:53p John
175  * Added first rev of subspace effect.
176  * 
177  * 94    4/08/98 11:31a Dave
178  * AL: Fix syntax error for non-demo
179  * 
180  * 93    4/08/98 10:46a Lawrance
181  * #ifdef out asteroid check for demo
182  * 
183  * 92    4/08/98 9:25a John
184  * Made asteroid missions not show suns
185  * 
186  * 91    4/07/98 4:17p John
187  * Made Fred be able to move suns.  Made suns actually affect the lighting
188  * in the game.
189  * 
190  * 90    4/07/98 11:19a Hoffoss
191  * Changed code to only use Sun01 for a bitmap by default, as John
192  * requested.
193  *
194  * $NoKeywords: $
195  */
196
197 #include "pstypes.h"
198 #include "floating.h"
199 #include "vecmat.h"
200 #include "3d.h"
201 #include "2d.h"
202 #include "starfield.h"
203 #include "bmpman.h"
204 #include "key.h"
205 #include "freespace.h"  
206 #include "timer.h"
207 #include "nebula.h"
208 #include "linklist.h"
209 #include "lighting.h"
210 #include "asteroid.h"
211 #include "missionparse.h"
212 #include "neb.h"
213 #include "alphacolors.h"
214 #include "supernova.h"
215
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
222
223 typedef struct debris_vclip {
224         int     bm;
225         int     nframes;
226         char  *name;
227 } debris_vclip;
228
229 typedef struct {
230         vector pos;
231         vector last_pos;
232         int active;
233         int vclip;
234         float size;     
235 } old_debris;
236
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;
244
245 static int Subspace_model_inner = -1;           
246 static int Subspace_model_outer = -1;           
247
248 int Num_stars = 500;
249 fix starfield_timestamp = 0;
250
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;
255
256 // sun bitmaps and sun glow bitmaps
257 starfield_bitmap Sun_bitmaps[MAX_STARFIELD_BITMAPS];
258 starfield_bitmap_instance Suns[MAX_STARFIELD_BITMAPS];
259 int Num_suns = 0;
260
261 int last_stars_filled = 0;
262 color star_colors[8];
263 color star_aacolors[8];
264
265 typedef struct star {
266         vector pos;
267         vector last_star_pos;
268 } star;
269
270 star Stars[MAX_STARS];
271
272 old_debris odebris[MAX_DEBRIS];
273
274 //XSTR:OFF
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;
278 //XSTR:ON
279
280 int stars_debris_loaded = 0;
281
282 // background data
283 int Stars_background_inited = 0;                        // if we're inited
284 int Nmodel_num = -1;                                                    // model num
285 int Nmodel_bitmap = -1;                                         // model texture
286
287 // given a starfield_bitmap_instance, return a pointer to its parent, for suns
288 starfield_bitmap *stars_lookup_sun(starfield_bitmap_instance *s)
289 {
290         int idx;
291
292         // sanity
293         if(s == NULL){
294                 return NULL;
295         }
296
297         // lookup
298         for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
299                 if(!stricmp(Sun_bitmaps[idx].filename, s->filename)){
300                         return &Sun_bitmaps[idx];
301                 }
302         }
303
304         // no findy
305         return NULL;
306 }
307
308 void stars_load_debris()
309 {
310         int i;
311
312         // if we're in nebula mode
313         if(The_mission.flags & MISSION_FLAG_FULLNEB){
314                 debris_vclips = debris_vclips_nebula;
315         } else {
316                 debris_vclips = debris_vclips_normal;
317         }
318
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;
325
326                         if(debris_vclips[i].bm <= 0){
327                                 Error( LOCATION, "Couldn't load animation/bitmap '%s'\n", debris_vclips[i].name );
328                         }
329                 }
330         }
331         stars_debris_loaded = 1;
332 }
333
334 // call on game startup
335 void stars_init()
336 {
337         starfield_bitmap *bm;   
338         int count, idx;
339         char filename[MAX_FILENAME_LEN+1] = "";
340         char glow_filename[MAX_FILENAME_LEN+1] = "";
341         float r, g, b, i;
342
343         // parse stars.tbl
344         read_file_text("stars.tbl");
345         reset_parse();
346
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, "");
353
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, "");
358         }
359
360         // starfield bitmaps
361         count = 0;
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);
369                                 bm->xparent = 0;
370                                 bm->bitmap = bm_load(bm->filename);                             
371                                 Assert(bm->bitmap != -1);
372
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);
377                                 } 
378                         }
379                 }
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);
386                                 bm->xparent = 1;
387                                 bm->bitmap = bm_load(bm->filename);
388                                 Assert(bm->bitmap != -1);
389
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);
394                                 } 
395                         }
396                 }
397         }
398
399         // sun bitmaps
400         count = 0;
401         while(!optional_string("#end")){
402                 if(optional_string("$Sun:")){
403                         stuff_string(filename, F_NAME, NULL);
404
405                         // associated glow
406                         required_string("$Sunglow:");
407                         stuff_string(glow_filename, F_NAME, NULL);
408
409                         // associated lighting values
410                         required_string("$SunRGBI:");
411                         stuff_float(&r);
412                         stuff_float(&g);
413                         stuff_float(&b);
414                         stuff_float(&i);
415
416                         if(count < MAX_STARFIELD_BITMAPS){
417                                 bm = &Sun_bitmaps[count++];
418                                 strcpy(bm->filename, filename);
419                                 strcpy(bm->glow_filename, glow_filename);
420                                 bm->xparent = 1;
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);
425                                 bm->r = r;
426                                 bm->g = g;
427                                 bm->b = b;
428                                 bm->i = i;
429
430                                 // if fred is running we should lock the bitmap now
431                                 if(Fred_running){
432                                         if(bm->bitmap >= 0){
433                                                 bm_lock(bm->bitmap, 8, BMP_TEX_OTHER);
434                                                 bm_unlock(bm->bitmap);
435                                         }
436                                         if(bm->glow_bitmap >= 0){
437                                                 bm_lock(bm->glow_bitmap, 8, BMP_TEX_OTHER);
438                                                 bm_unlock(bm->glow_bitmap);
439                                         }
440                                 } 
441                         }
442                 }
443         }       
444
445         // normal debris pieces
446         count = 0;
447         while(!optional_string("#end")){
448                 required_string("$Debris:");
449                 stuff_string(filename, F_NAME, NULL);
450
451                 if(count < MAX_DEBRIS_VCLIPS){
452                         strcpy(debris_vclips_normal[count++].name, filename);
453                 }
454         }
455         Assert(count == 4);
456
457         // nebula debris pieces
458         count = 0;
459         while(!optional_string("#end")){
460                 required_string("$DebrisNeb:");
461                 stuff_string(filename, F_NAME, NULL);
462
463                 if(count < MAX_DEBRIS_VCLIPS){
464                         strcpy(debris_vclips_nebula[count++].name, filename);
465                 }
466         }
467         Assert(count == 4);
468 }
469
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()
472 {
473         int i;
474         vector v;
475         float dist, dist_max;
476
477         // reset to -1 so we reload it each mission (if we need to)
478         Nmodel_num = -1;                
479         if(Nmodel_bitmap != -1){
480                 bm_unload(Nmodel_bitmap);
481                 Nmodel_bitmap = -1;
482         }
483
484         // if (!stars_debris_loaded){
485                 stars_load_debris();
486         // }
487
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++) {
493                 dist = dist_max;
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);
498
499                         dist = v.x * v.x + v.y * v.y + v.z * v.z;
500                 }
501                 vm_vec_copy_normalize(&Stars[i].pos, &v);
502         }
503
504         for (i=0; i<MAX_DEBRIS; i++) {
505                 odebris[i].active = 0;
506         }
507
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 );
512         }
513
514         last_stars_filled = 0;
515
516         // if we have no sun instances, create one
517         if(Num_suns <= 0){
518                 mprintf(("Adding default sun\n"));
519                 
520                 // stuff some values
521                 strcpy(Suns[0].filename, Sun_bitmaps[0].filename);
522                 Suns[0].scale_x = 1.0f;
523                 Suns[0].scale_y = 1.0f;
524                 Suns[0].div_x = 1;
525                 Suns[0].div_y = 1;
526                 memset(&Suns[0].ang, 0, sizeof(angles));
527                 Suns[0].ang.h = fl_radian(60.0f);
528
529                 // one sun
530                 Num_suns = 1;
531         }               
532 }
533
534
535 #include "object.h"
536 extern object * Player_obj;
537
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
542
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;        
547
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)
552
553 uint Star_flags = STAR_FLAG_DEFAULT;
554
555 //XSTR:OFF
556 DCF(stars,"Set parameters for starfield")
557 {
558         if ( Dc_command )       {
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) )   {
563                                 Dc_help = 1;
564                         } else {
565                                 Star_amount = Dc_arg_float;
566                         } 
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 )      {
573                                 Dc_help = 1;
574                         } else {
575                                 Star_dim = Dc_arg_float;
576                         } 
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;
585                         } else {
586                                 Dc_help = 1;    
587                         }
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) ) {
591                                 Dc_help = 1;
592                         } else {
593                                 Star_cap = Dc_arg_float;
594                         } 
595                 } else if ( !strcmp( Dc_arg, "m0" )  )  {
596                         Star_amount = 0.0f;
597                         Star_dim = 0.0f;
598                         Star_cap = 0.0f;
599                         Star_flags = 0;
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" ))    {
608                         Star_amount = 0.75f;
609                         Star_dim = 20.0f;
610                         Star_cap = 75.0f;
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" ))   {
614                         dc_get_arg(ARG_INT);
615                         if ( (Dc_arg_int < 0) || (Dc_arg_int > MAX_STARS) )     {
616                                 Dc_help = 1;
617                         } else {
618                                 Num_stars = Dc_arg_int;
619                         } 
620                 } else {
621                         // print usage, not stats
622                         Dc_help = 1;
623                 }
624         }
625
626         if ( Dc_help )  {
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.
642         }
643
644         if ( Dc_status )        {
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" );
656         }
657 }
658 //XSTR:ON
659
660 int reload_old_debris = 1;              // If set to one, then reload all the last_pos of the debris
661
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()
665 {
666         last_stars_filled = 0;
667         reload_old_debris = 1;
668 }
669
670 //#define TIME_STAR_CODE                // enable to time star code
671
672 extern int Sun_drew;
673 extern float Viewer_zoom;
674
675 // get the world coords of the sun pos on the unit sphere.
676 void stars_get_sun_pos(int sun_n, vector *pos)
677 {
678         vector temp;
679         matrix rot;
680
681         // sanity
682         Assert(sun_n < Num_suns);
683         if((sun_n >= Num_suns) || (sun_n < 0)){
684                 return;
685         }
686
687         // rotate the sun properly
688         temp = vmd_zero_vector;
689         temp.z = 1.0f;
690         
691         // rotation matrix
692         vm_angles_2_matrix(&rot, &Suns[sun_n].ang);
693         vm_vec_rotate(pos, &temp, &rot);
694 }
695
696 // draw sun
697 void stars_draw_sun( int show_sun )
698 {       
699         int idx;
700         vector sun_pos;
701         vector sun_dir;
702         vertex sun_vex; 
703         starfield_bitmap *bm;
704         float local_scale = 1.0f;
705
706         // no suns drew yet
707         Sun_drew = 0;
708
709         // draw all suns
710         for(idx=0; idx<Num_suns; idx++){                
711                 // get the instance
712                 bm = stars_lookup_sun(&Suns[idx]);
713                 if(bm == NULL){
714                         continue;
715                 }
716
717                 // get sun pos
718                 sun_pos = vmd_zero_vector;
719                 sun_pos.y = 1.0f;
720                 stars_get_sun_pos(idx, &sun_pos);
721                 
722                 // get the direction            
723                 sun_dir = sun_pos;
724                 vm_vec_normalize(&sun_dir);
725
726                 // add the light source corresponding to the sun
727                 light_add_directional(&sun_dir, bm->i, bm->r, bm->g, bm->b);
728
729                 // if supernova
730                 if(supernova_active()){
731                         local_scale = 1.0f + (SUPERNOVA_SUN_SCALE * supernova_pct_complete());
732                 }
733
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)){
738                         Sun_drew++;
739                 }
740         }
741 }
742
743 // draw the corresponding glow for sun_n
744 void stars_draw_sun_glow(int sun_n)
745 {
746         starfield_bitmap *bm;           
747         vector sun_pos, sun_dir;
748         vertex sun_vex; 
749         float local_scale = 1.0f;
750
751         // sanity
752         Assert(sun_n < Num_suns);
753         if((sun_n >= Num_suns) || (sun_n < 0)){
754                 return;
755         }
756
757         // get the instance
758         bm = stars_lookup_sun(&Suns[sun_n]);
759         if(bm == NULL){
760                 return;
761         }
762
763         // get sun pos
764         sun_pos = vmd_zero_vector;
765         sun_pos.y = 1.0f;
766         stars_get_sun_pos(sun_n, &sun_pos);     
767
768         // get the direction            
769         sun_dir = sun_pos;
770         vm_vec_normalize(&sun_dir);     
771
772         // if supernova
773         if(supernova_active()){
774                 local_scale = 1.0f + (SUPERNOVA_SUN_SCALE * supernova_pct_complete());
775         }
776
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);     
781 }
782
783 // draw bitmaps
784 void stars_draw_bitmaps( int show_bitmaps )
785 {
786         int idx;
787         int star_index; 
788
789         // if we're in the nebula, don't render any backgrounds
790         if(The_mission.flags & MISSION_FLAG_FULLNEB){
791                 return;
792         }
793
794         // detail settings
795         if(!Detail.planets_suns){
796                 return;
797         }
798         
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);
803                 if(star_index < 0){
804                         continue;
805                 }
806         
807                 // set the bitmap                               
808                 if(Fred_running){
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);
811                 } else {
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);
815                         } else {                                
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);
818                         }
819                 }
820         }
821 }
822
823 /*
824 void calculate_bitmap_matrix(starfield_bitmaps *bm, vector *v)
825 {
826         vm_vector_2_matrix(&bm->m, v, NULL, NULL);
827         vm_orthogonalize_matrix(&bm->m);
828 }
829
830 void calculate_bitmap_points(starfield_bitmaps *bm, float bank)
831 {
832         int i;
833         vector fvec, uvec, rvec, tmp;
834         angles tangles;
835
836         vm_orthogonalize_matrix(&bm->m);
837         if (bank) {
838                 tangles.p = tangles.h = 0.0f;
839                 tangles.b = bank;
840                 vm_rotate_matrix_by_angles(&bm->m, &tangles);
841         }
842
843         fvec = bm->m.fvec;
844         vm_vec_scale(&fvec, bm->dist );
845         uvec = bm->m.uvec;
846         rvec = bm->m.rvec;
847
848         vm_vec_sub(&tmp, &fvec, &uvec);
849         vm_vec_sub(&bm->points[3], &tmp, &rvec);
850
851         vm_vec_sub(&tmp, &fvec, &uvec);
852         vm_vec_add(&bm->points[2], &tmp, &rvec);
853
854         vm_vec_add(&tmp, &fvec, &uvec);
855         vm_vec_add(&bm->points[1], &tmp, &rvec);
856
857         vm_vec_add(&tmp, &fvec, &uvec);
858         vm_vec_sub(&bm->points[0], &tmp, &rvec);
859
860         for (i=0; i<4; i++){
861                 vm_vec_normalize(&bm->points[i]);
862         }
863 }
864 */
865
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;
870
871 float subspace_offset_u = 0.0f;
872 float subspace_offset_u_inner = 0.0f;
873 float subspace_offset_v = 0.0f;
874
875 float subspace_u_speed = 0.07f;                 // how fast u changes
876 float subspace_v_speed = 0.05f;                 // how fast v changes
877
878 int Subspace_glow_bitmap = -1;
879
880 float Subspace_glow_frame = 0.0f;
881 float Subspace_glow_rate = 1.0f;
882
883
884 //XSTR:OFF
885 DCF(subspace_set,"Set parameters for subspace effect")
886 {
887         if ( Dc_command )       {
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 )      {
892                                 Dc_help = 1;
893                         } else {
894                                 subspace_u_speed = Dc_arg_float;
895                         } 
896                 } else if ( !strcmp( Dc_arg, "v" ))     {
897                         dc_get_arg(ARG_FLOAT);
898                         if ( Dc_arg_float < 0.0f )      {
899                                 Dc_help = 1;
900                         } else {
901                                 subspace_v_speed = Dc_arg_float;
902                         } 
903                 } else {
904                         // print usage, not stats
905                         Dc_help = 1;
906                 }
907         }
908
909         if ( Dc_help )  {
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.
915         }
916
917         if ( Dc_status )        {
918                 dc_printf( "u: %.2f\n", subspace_u_speed );
919                 dc_printf( "v: %.2f\n", subspace_v_speed );
920         }
921 }
922 //XSTR:ON
923
924 void subspace_render()
925 {
926         if ( Subspace_model_inner == -1 )       {
927                 Subspace_model_inner = model_load( "subspace_small.pof", NULL, NULL );
928                 Assert(Subspace_model_inner>-1);
929         }
930
931         if ( Subspace_model_outer == -1 )       {
932                 Subspace_model_outer = model_load( "subspace_big.pof", NULL, NULL );
933                 Assert(Subspace_model_outer>-1);
934         }
935
936         if ( Subspace_glow_bitmap == -1 )       {
937                 Subspace_glow_bitmap = bm_load( NOX("SunGlow01"));
938                 Assert(Subspace_glow_bitmap>-1);
939         }
940
941         Subspace_glow_frame += flFrametime * 1.0f;
942
943         float total_time = i2fl(NOISE_NUM_FRAMES) / 15.0f;
944
945         // Sanity checks
946         if ( Subspace_glow_frame < 0.0f )       Subspace_glow_frame = 0.0f;
947         if ( Subspace_glow_frame > 100.0f ) Subspace_glow_frame = 0.0f;
948
949         while ( Subspace_glow_frame > total_time )      {
950                 Subspace_glow_frame -= total_time;
951         }
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;
955
956         subspace_offset_u += flFrametime*subspace_u_speed;
957         if (subspace_offset_u > 1.0f )  {
958                 subspace_offset_u -= 1.0f;
959         }
960
961         subspace_offset_u_inner += flFrametime*subspace_u_speed*3.0f;
962         if (subspace_offset_u > 1.0f )  {
963                 subspace_offset_u -= 1.0f;
964         }
965
966         subspace_offset_v += flFrametime*subspace_v_speed;
967         if (subspace_offset_v > 1.0f )  {
968                 subspace_offset_v -= 1.0f;
969         }
970
971         
972
973         matrix tmp;
974         angles angs = { 0.0f, 0.0f, 0.0f };
975         angs.b = subspace_offset_v * PI2;
976         
977         vm_angles_2_matrix(&tmp,&angs);
978         
979         int saved_gr_zbuffering =       gr_zbuffer_get();
980
981         gr_zbuffer_set(GR_ZBUFF_NONE);
982
983         if ( !D3D_enabled )     {
984
985                 int render_flags = MR_NO_LIGHTING | MR_ALWAYS_REDRAW;
986
987                 Interp_subspace = 1;    
988                 Interp_subspace_offset_u = 1.0f - subspace_offset_u;
989                 Interp_subspace_offset_v = 0.0f;
990
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 
994
995         } else {
996
997                 int render_flags = MR_NO_LIGHTING | MR_ALWAYS_REDRAW;
998
999                 Interp_subspace = 1;    
1000                 Interp_subspace_offset_u = 1.0f - subspace_offset_u;
1001                 Interp_subspace_offset_v = 0.0f;
1002
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 
1006                 
1007                 Interp_subspace = 1;    
1008                 Interp_subspace_offset_u = 1.0f - subspace_offset_u_inner;
1009                 Interp_subspace_offset_v = 0.0f;        
1010
1011                 angs.b = -subspace_offset_v * PI2;
1012
1013                 vm_angles_2_matrix(&tmp,&angs);
1014
1015                 model_set_outline_color(255,255,255);
1016
1017                 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1018                 render_flags |= MR_SHOW_THRUSTERS;
1019
1020                 model_render( Subspace_model_inner, &tmp, &Eye_position, render_flags  );       //MR_NO_CORRECT|MR_SHOW_OUTLINE 
1021         }
1022
1023         Interp_subspace = 0;
1024         gr_zbuffer_set(saved_gr_zbuffering);
1025 }
1026
1027 void stars_draw( int show_stars, int show_suns, int show_nebulas, int show_subspace )
1028 {
1029         int i;
1030         float vdist;
1031
1032
1033         int gr_zbuffering_save = gr_zbuffer_get();
1034         gr_zbuffer_set(GR_ZBUFF_NONE);
1035
1036         if ( show_subspace )    {
1037                 subspace_render();
1038         }
1039
1040         
1041         if (Num_stars >= MAX_STARS){
1042                 Num_stars = MAX_STARS;
1043         }
1044
1045 #ifdef TIME_STAR_CODE
1046         fix xt1, xt2;
1047         xt1 = timer_get_fixed_seconds();
1048 #endif
1049         
1050         if ( show_nebulas && (Game_detail_flags & DETAIL_FLAG_NEBULAS) && (Neb2_render_mode != NEB2_RENDER_POF) && (Neb2_render_mode != NEB2_RENDER_LAME))      {
1051                 nebula_render();
1052         }
1053
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;
1062         }
1063
1064         if (show_stars && ( Game_detail_flags & DETAIL_FLAG_STARS) && !(The_mission.flags & MISSION_FLAG_FULLNEB) && (supernova_active() < 3))  {
1065                 //Num_stars = 1;
1066         
1067                 star *sp;
1068
1069                 if ( !last_stars_filled )       {
1070                         for (sp=Stars,i=0; i<Num_stars; i++, sp++ ) {
1071                                 vertex p2;
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;
1076                         }
1077                 }
1078
1079                 int tmp_num_stars;
1080
1081                 tmp_num_stars = (Detail.num_stars*Num_stars)/MAX_DETAIL_LEVEL;
1082                 if (tmp_num_stars < 0 ) {
1083                         tmp_num_stars = 0;
1084                 } else if ( tmp_num_stars > Num_stars ) {
1085                         tmp_num_stars = Num_stars;
1086                 }
1087                 
1088                 for (sp=Stars,i=0; i<tmp_num_stars; i++, sp++ ) {
1089                         vertex p1, p2;                  
1090                         int can_draw = 1;                       
1091
1092                         memset(&p1, 0, sizeof(vertex));
1093
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.
1097
1098                         g3_rotate_faraway_vertex(&p2, &sp->pos);
1099                         if ( p2.codes ) {
1100                                 can_draw = 0;
1101                         } else {
1102                                 g3_project_vertex(&p2);
1103                                 if ( p2.flags & PF_OVERFLOW )   {
1104                                         can_draw = 0;
1105                                 }
1106                         }
1107
1108                         float dist = 0.0f;
1109
1110                         if ( can_draw && (Star_flags & (STAR_FLAG_TAIL|STAR_FLAG_DIM)) )        {
1111
1112                                 dist = vm_vec_dist_quick( &sp->last_star_pos, (vector *)&p2.x );
1113
1114                                 float ratio;
1115                                 if ( dist > Star_max_length )   {
1116                                         ratio = Star_max_length / dist;
1117                                         dist = Star_max_length;
1118                                 } else {
1119                                         ratio = 1.0f;
1120                                 }
1121                                 ratio *= Star_amount;
1122
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;
1126
1127                                 p1.flags = 0;   // not projected
1128                                 g3_code_vertex( &p1 );
1129
1130                                 if ( p1.codes ) {
1131                                         can_draw = 0;
1132                                 } else {
1133                                         g3_project_vertex(&p1);
1134                                         if ( p1.flags & PF_OVERFLOW )   {
1135                                                 can_draw = 0;
1136                                         }
1137                                 }
1138                         }
1139
1140                         sp->last_star_pos.x = p2.x;
1141                         sp->last_star_pos.y = p2.y;
1142                         sp->last_star_pos.z = p2.z;
1143
1144                         if ( !can_draw )        continue;
1145
1146                         int color;
1147
1148                         if ( Star_flags & STAR_FLAG_DIM )       {
1149
1150                                 float colorf = 255.0f - dist*Star_dim;
1151
1152                                 if ( colorf < Star_cap )
1153                                         colorf = Star_cap;
1154
1155                                 color = (fl2i(colorf)*(i&7))/256;
1156
1157                         } else {
1158                                 color = i & 7;
1159                         }
1160
1161                         if ( (Star_flags & STAR_FLAG_ANTIALIAS) || (D3D_enabled) )      {
1162                                 gr_set_color_fast( &star_aacolors[color] );
1163
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)) ){                                     
1166                                         p1.sx += 1.0f;
1167                                 }                                                               
1168                                 gr_aaline(&p1,&p2);
1169                         } else {
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));
1174                                 } else {
1175                                         gr_pixel( fl2i(p2.sx),fl2i(p2.sy) );
1176                                 }
1177                         }
1178                 }
1179         }
1180
1181         last_stars_filled = 1;
1182
1183 #ifdef TIME_STAR_CODE
1184         xt2 = timer_get_fixed_seconds();
1185         mprintf(( "Stars: %d\n", xt2-xt1 ));
1186 #endif
1187         
1188
1189         if ( (Game_detail_flags & DETAIL_FLAG_MOTION) && (!Fred_running) && (supernova_active() < 3) )  {
1190
1191                 gr_set_color( 0, 0, 0 );
1192
1193                 // turn off fogging
1194                 if(The_mission.flags & MISSION_FLAG_FULLNEB){
1195                         gr_fog_set(GR_FOGMODE_NONE, 0, 0, 0);
1196                 }
1197
1198                 old_debris * d = odebris; 
1199                 for (i=0; i<MAX_DEBRIS; i++, d++ ) {
1200                         vertex p;
1201
1202                         if (!d->active) {
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);
1206
1207                                 vm_vec_normalize(&d->pos);
1208
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 );
1212                                 d->active = 1;
1213                                 d->vclip = i % MAX_DEBRIS_VCLIPS;       //rand()
1214
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;
1218                                 } else {
1219                                         d->size = i2fl(myrand() % 4)*BASE_SIZE;
1220                                 }
1221
1222                                 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1223                         }
1224
1225                         if ( reload_old_debris )        {
1226                                 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1227                         }
1228                         
1229                         g3_rotate_vertex(&p, &d->pos);
1230
1231                         if (p.codes == 0) {
1232                                 int frame = Missiontime / (DEBRIS_ROT_MIN + (i % DEBRIS_ROT_RANGE) * DEBRIS_ROT_RANGE_SCALER);
1233                                 frame %= debris_vclips[d->vclip].nframes;
1234
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);  
1237                                 } else {
1238                                         gr_set_bitmap( debris_vclips[d->vclip].bm + frame );                                            
1239                                 }
1240                                         
1241                                 vector tmp;
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 );                                     
1244                         }
1245
1246                         vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1247
1248                         vdist = vm_vec_mag_quick(&d->last_pos);
1249
1250                         if (vdist > MAX_DIST_RANGE)
1251                                 d->active = 0;
1252                         else if (vdist < MIN_DIST_RANGE)
1253                                 d->active = 0;
1254
1255         //              vector tmp;
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 )
1259         //                      d->active = 0;
1260
1261                 }
1262
1263                 reload_old_debris = 0;
1264         }
1265
1266
1267         stars_draw_sun( show_suns );    
1268         stars_draw_bitmaps( show_suns );
1269
1270         gr_zbuffer_set( gr_zbuffering_save );
1271 }
1272
1273 void stars_page_in()
1274 {
1275         int i, j;
1276
1277         // Initialize the subspace stuff
1278
1279         if ( Game_subspace_effect )     {
1280
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);
1285
1286                 polymodel *pm;
1287                 
1288                 pm = model_get(Subspace_model_inner);
1289                 
1290                 nprintf(( "Paging", "Paging in textures for subspace effect.\n" ));
1291
1292                 for (j=0; j<pm->n_textures; j++ )       {
1293                         int bitmap_num = pm->original_textures[j];
1294
1295                         if ( bitmap_num > -1 )  {
1296                                 bm_page_in_texture( bitmap_num );
1297                         }
1298                 }
1299
1300                 pm = model_get(Subspace_model_outer);
1301                 
1302                 nprintf(( "Paging", "Paging in textures for subspace effect.\n" ));
1303
1304                 for (j=0; j<pm->n_textures; j++ )       {
1305                         int bitmap_num = pm->original_textures[j];
1306
1307                         if ( bitmap_num > -1 )  {
1308                                 bm_page_in_texture( bitmap_num );
1309                         }
1310                 }
1311         } else {
1312                 Subspace_model_inner = -1;
1313                 Subspace_model_outer = -1;
1314         }
1315
1316         Subspace_glow_bitmap = bm_load( NOX("SunGlow01"));
1317         bm_page_in_xparent_texture(Subspace_glow_bitmap);
1318
1319         // page in starfield bitmaps
1320         int idx;
1321         idx = 0;
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);
1325                 } else { 
1326                         bm_page_in_texture(Starfield_bitmaps[idx].bitmap);
1327                 }
1328
1329                 // next;
1330                 idx++;
1331         }
1332
1333         // sun bitmaps and glows
1334         idx = 0;
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);
1338
1339                 // next 
1340                 idx++;
1341         }
1342
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);
1346                 }
1347         }       
1348 }
1349
1350 // background nebula models and planets
1351 void stars_draw_background()
1352 {                               
1353         if((Nmodel_num < 0) || (Nmodel_bitmap < 0)){
1354                 return;
1355         }
1356
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);
1362 }
1363
1364 // call this to set a specific model as the background model
1365 void stars_set_background_model(char *model_name, char *texture_name)
1366 {
1367         Nmodel_num = model_load(model_name, 0, NULL);
1368         Nmodel_bitmap = bm_load(texture_name);
1369 }
1370
1371 // lookup a starfield bitmap, return index or -1 on fail
1372 int stars_find_bitmap(char *name)
1373 {
1374         int idx;
1375
1376         // lookup
1377         for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
1378                 if(!strcmp(name, Starfield_bitmaps[idx].filename)){
1379                         return idx;
1380                 }
1381         }
1382
1383         // not found 
1384         return -1;
1385 }
1386
1387 // lookup a sun by bitmap filename, return index or -1 on fail
1388 int stars_find_sun(char *name)
1389 {
1390         int idx;
1391
1392         // lookup
1393         for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
1394                 if(!strcmp(name, Sun_bitmaps[idx].filename)){
1395                         return idx;
1396                 }
1397         }
1398
1399         // not found 
1400         return -1;
1401 }