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