]> icculus.org git repositories - taylor/freespace2.git/blob - src/starfield/starfield.cpp
added copyright header
[taylor/freespace2.git] / src / starfield / starfield.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Starfield/StarField.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Code to handle and draw starfields, background space image bitmaps, floating
16  * debris, etc.
17  *
18  * $Log$
19  * Revision 1.4  2002/06/09 04:41:27  relnev
20  * added copyright header
21  *
22  * Revision 1.3  2002/06/02 04:26:34  relnev
23  * warning cleanup
24  *
25  * Revision 1.2  2002/05/07 03:16:52  theoddone33
26  * The Great Newline Fix
27  *
28  * Revision 1.1.1.1  2002/05/03 03:28:10  root
29  * Initial import.
30  *
31  * 
32  * 34    9/07/99 4:01p Dave
33  * Fixed up a string.tbl paroblem (self destruct message). Make sure IPX
34  * does everything properly (setting up address when binding). Remove
35  * black rectangle background from UI_INPUTBOX.
36  * 
37  * 33    9/01/99 10:14a Dave
38  * Pirate bob.
39  * 
40  * 32    8/30/99 5:01p Dave
41  * Made d3d do less state changing in the nebula. Use new chat server for
42  * PXO.
43  * 
44  * 31    8/19/99 10:59a Dave
45  * Packet loss detection.
46  * 
47  * 30    7/27/99 3:52p Dave
48  * Make star drawing a bit more robust to help lame D3D cards.
49  * 
50  * 29    7/21/99 8:10p Dave
51  * First run of supernova effect.
52  * 
53  * 28    7/13/99 2:01p Dave
54  * Don't draw background bitmaps in the nebula.
55  * 
56  * 27    6/08/99 2:34p Jasenw
57  * Made perspective bitmaps render in Fred.
58  * 
59  * 26    6/04/99 1:18p Dave
60  * Fixed briefing model rendering problems. Made show background option in
61  * fred toggle nebula rendering.
62  * 
63  * 25    6/03/99 6:37p Dave
64  * More TNT fun. Made perspective bitmaps more flexible.
65  * 
66  * 24    5/28/99 1:45p Dave
67  * Fixed up perspective bitmap drawing.
68  * 
69  * 23    5/20/99 7:00p Dave
70  * Added alternate type names for ships. Changed swarm missile table
71  * entries.
72  * 
73  * 22    5/11/99 10:03a Dave
74  * Put a bunch of stuff into tables.
75  * 
76  * 21    5/11/99 9:10a Dave
77  * Move default sun position.
78  * 
79  * 20    5/09/99 6:00p Dave
80  * Lots of cool new effects. E3 build tweaks.
81  * 
82  * 19    4/26/99 8:49p Dave
83  * Made all pof based nebula stuff full customizable through fred.
84  * 
85  * 18    4/25/99 7:43p Dave
86  * Misc small bug fixes. Made sun draw properly.
87  * 
88  * 17    4/23/99 5:53p Dave
89  * Started putting in new pof nebula support into Fred.
90  * 
91  * 16    4/07/99 6:22p Dave
92  * Fred and Freespace support for multiple background bitmaps and suns.
93  * Fixed link errors on all subprojects. Moved encrypt_init() to
94  * cfile_init() and lcl_init(), since its safe to call twice.
95  * 
96  * 15    3/31/99 8:24p Dave
97  * Beefed up all kinds of stuff, incluging beam weapons, nebula effects
98  * and background nebulae. Added per-ship non-dimming pixel colors.
99  * 
100  * 14    3/20/99 5:09p Dave
101  * Fixed release build fred warnings and unhandled exception.
102  * 
103  * 13    3/20/99 3:46p Dave
104  * Added support for model-based background nebulae. Added 3 new
105  * sexpressions.
106  * 
107  * 12    3/20/99 2:04p Dave
108  * Removed unnecessary planet rendering.
109  * 
110  * 11    3/19/99 9:51a Dave
111  * Checkin to repair massive source safe crash. Also added support for
112  * pof-style nebulae, and some new weapons code.
113  * 
114  * 12    3/15/99 6:45p Daveb
115  * Put in rough nebula bitmap support.
116  * 
117  * 11    3/11/99 5:53p Dave
118  * More network optimization. Spliced in Dell OEM planet bitmap crap.
119  * 
120  * 10    2/03/99 11:44a Dave
121  * Fixed d3d transparent textures.
122  * 
123  * 9     12/09/98 7:34p Dave
124  * Cleanup up nebula effect. Tweaked many values.
125  * 
126  * 8     12/01/98 10:32a Johnson
127  * Fixed direct3d font problems. Fixed sun bitmap problem. Fixed direct3d
128  * starfield problem.
129  * 
130  * 7     12/01/98 8:06a Dave
131  * Temporary checkin to fix some texture transparency problems in d3d.
132  * 
133  * 6     11/14/98 5:33p Dave
134  * Lots of nebula work. Put in ship contrails.
135  * 
136  * 5     11/05/98 5:55p Dave
137  * Big pass at reducing #includes
138  * 
139  * 4     10/13/98 9:29a Dave
140  * Started neatening up freespace.h. Many variables renamed and
141  * reorganized. Added AlphaColors.[h,cpp]
142  * 
143  * 3     10/07/98 11:16a Dave
144  * Remove warning.
145  * 
146  * 2     10/07/98 10:54a Dave
147  * Initial checkin.
148  * 
149  * 1     10/07/98 10:51a Dave
150  * 
151  * 106   5/23/98 4:14p John
152  * Added code to preload textures to video card for AGP.   Added in code
153  * to page in some bitmaps that weren't getting paged in at level start.
154  * 
155  * 105   5/13/98 2:53p John
156  * Made subspace effect work under software.  Had to add new inner loop to
157  * tmapper.  Added glows to end of subspace effect.  Made subspace effect
158  * levels use gamepalette-subspace palette.
159  * 
160  * 104   5/13/98 10:28a John
161  * made subpsace forward sliding 40% faster.
162  * 
163  * 103   5/10/98 4:18p John
164  * Reversed the subspace effect direction
165  * 
166  * 102   5/08/98 8:38p John
167  * Subspace tweaks.  Made two layers rotate independentyl.  
168  * 
169  * 101   5/08/98 1:32p John
170  * Added code for using two layered subspace effects.
171  * 
172  * 100   5/06/98 5:30p John
173  * Removed unused cfilearchiver.  Removed/replaced some unused/little used
174  * graphics functions, namely gradient_h and _v and pixel_sp.   Put in new
175  * DirectX header files and libs that fixed the Direct3D alpha blending
176  * problems.
177  * 
178  * 99    4/22/98 4:09p John
179  * String externalization
180  * 
181  * 98    4/22/98 3:28p John
182  * Fixed XSTR bug
183  * 
184  * 97    4/13/98 4:54p John
185  * Made uv rotate independently on subspace effect. Put in DCF function
186  * for setting subspace speeds.
187  * 
188  * 96    4/12/98 5:55p John
189  * Made models work with subspace.  Made subspace rotate also.
190  * 
191  * 95    4/11/98 6:53p John
192  * Added first rev of subspace effect.
193  * 
194  * 94    4/08/98 11:31a Dave
195  * AL: Fix syntax error for non-demo
196  * 
197  * 93    4/08/98 10:46a Lawrance
198  * #ifdef out asteroid check for demo
199  * 
200  * 92    4/08/98 9:25a John
201  * Made asteroid missions not show suns
202  * 
203  * 91    4/07/98 4:17p John
204  * Made Fred be able to move suns.  Made suns actually affect the lighting
205  * in the game.
206  * 
207  * 90    4/07/98 11:19a Hoffoss
208  * Changed code to only use Sun01 for a bitmap by default, as John
209  * requested.
210  *
211  * $NoKeywords: $
212  */
213
214 #include "pstypes.h"
215 #include "floating.h"
216 #include "vecmat.h"
217 #include "3d.h"
218 #include "2d.h"
219 #include "starfield.h"
220 #include "bmpman.h"
221 #include "key.h"
222 #include "freespace.h"  
223 #include "timer.h"
224 #include "nebula.h"
225 #include "linklist.h"
226 #include "lighting.h"
227 #include "asteroid.h"
228 #include "missionparse.h"
229 #include "neb.h"
230 #include "alphacolors.h"
231 #include "supernova.h"
232
233 #define MAX_DEBRIS_VCLIPS       4
234 #define DEBRIS_ROT_MIN                          10000
235 #define DEBRIS_ROT_RANGE                        8
236 #define DEBRIS_ROT_RANGE_SCALER 10000
237 #define RND_MAX_MASK    0x3fff
238 #define HALF_RND_MAX 0x2000
239
240 typedef struct debris_vclip {
241         int     bm;
242         int     nframes;
243         char  *name;
244 } debris_vclip;
245
246 typedef struct {
247         vector pos;
248         vector last_pos;
249         int active;
250         int vclip;
251         float size;     
252 } old_debris;
253
254 const int MAX_DEBRIS = 200;
255 const int MAX_STARS = 2000;
256 const float MAX_DIST = 50.0f;
257 const float MAX_DIST_RANGE = 60.0f;
258 const float MIN_DIST_RANGE = 14.0f;
259 const float BASE_SIZE = 0.12f;
260 float BASE_SIZE_NEB = 0.5f;
261
262 static int Subspace_model_inner = -1;           
263 static int Subspace_model_outer = -1;           
264
265 int Num_stars = 500;
266 fix starfield_timestamp = 0;
267
268 // for drawing cool stuff on the background - comes from a table
269 starfield_bitmap Starfield_bitmaps[MAX_STARFIELD_BITMAPS];
270 starfield_bitmap_instance Starfield_bitmap_instance[MAX_STARFIELD_BITMAPS];
271 int Num_starfield_bitmaps = 0;
272
273 // sun bitmaps and sun glow bitmaps
274 starfield_bitmap Sun_bitmaps[MAX_STARFIELD_BITMAPS];
275 starfield_bitmap_instance Suns[MAX_STARFIELD_BITMAPS];
276 int Num_suns = 0;
277
278 int last_stars_filled = 0;
279 color star_colors[8];
280 color star_aacolors[8];
281
282 typedef struct star {
283         vector pos;
284         vector last_star_pos;
285 } star;
286
287 star Stars[MAX_STARS];
288
289 old_debris odebris[MAX_DEBRIS];
290
291 //XSTR:OFF
292 debris_vclip debris_vclips_normal[MAX_DEBRIS_VCLIPS] = { { -1, -1, "debris01" }, { -1, -1, "debris02" }, { -1, -1, "debris03" }, { -1, -1, "debris04" } };
293 debris_vclip debris_vclips_nebula[MAX_DEBRIS_VCLIPS] = { { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" } };
294 debris_vclip *debris_vclips = debris_vclips_normal;
295 //XSTR:ON
296
297 int stars_debris_loaded = 0;
298
299 // background data
300 int Stars_background_inited = 0;                        // if we're inited
301 int Nmodel_num = -1;                                                    // model num
302 int Nmodel_bitmap = -1;                                         // model texture
303
304 // given a starfield_bitmap_instance, return a pointer to its parent, for suns
305 starfield_bitmap *stars_lookup_sun(starfield_bitmap_instance *s)
306 {
307         int idx;
308
309         // sanity
310         if(s == NULL){
311                 return NULL;
312         }
313
314         // lookup
315         for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
316                 if(!stricmp(Sun_bitmaps[idx].filename, s->filename)){
317                         return &Sun_bitmaps[idx];
318                 }
319         }
320
321         // no findy
322         return NULL;
323 }
324
325 void stars_load_debris()
326 {
327         int i;
328
329         // if we're in nebula mode
330         if(The_mission.flags & MISSION_FLAG_FULLNEB){
331                 debris_vclips = debris_vclips_nebula;
332         } else {
333                 debris_vclips = debris_vclips_normal;
334         }
335
336         for (i=0; i<MAX_DEBRIS_VCLIPS; i++ )    {
337                 debris_vclips[i].bm = bm_load_animation( debris_vclips[i].name, &debris_vclips[i].nframes, NULL, 1 );
338                 if ( debris_vclips[i].bm < 0 ) {
339                         // try loading it as a single bitmap
340                         debris_vclips[i].bm = bm_load(debris_vclips[i].name);
341                         debris_vclips[i].nframes = 1;
342
343                         if(debris_vclips[i].bm <= 0){
344                                 Error( LOCATION, "Couldn't load animation/bitmap '%s'\n", debris_vclips[i].name );
345                         }
346                 }
347         }
348         stars_debris_loaded = 1;
349 }
350
351 // call on game startup
352 void stars_init()
353 {
354         starfield_bitmap *bm;   
355         int count, idx;
356         char filename[MAX_FILENAME_LEN+1] = "";
357         char glow_filename[MAX_FILENAME_LEN+1] = "";
358         float r, g, b, i;
359
360         // parse stars.tbl
361         read_file_text("stars.tbl");
362         reset_parse();
363
364         // make all bitmaps invalid
365         for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
366                 Starfield_bitmaps[idx].bitmap = -1;
367                 Starfield_bitmaps[idx].glow_bitmap = -1;                
368                 strcpy(Starfield_bitmaps[idx].filename, "");
369                 strcpy(Starfield_bitmaps[idx].glow_filename, "");
370
371                 Sun_bitmaps[idx].bitmap = -1;           
372                 Sun_bitmaps[idx].glow_bitmap = -1;              
373                 strcpy(Sun_bitmaps[idx].filename, "");
374                 strcpy(Sun_bitmaps[idx].glow_filename, "");
375         }
376
377         // starfield bitmaps
378         count = 0;
379         while(!optional_string("#end")){
380                 // intensity alpha bitmap
381                 if(optional_string("$Bitmap:")){
382                         stuff_string(filename, F_NAME, NULL);
383                         if(count < MAX_STARFIELD_BITMAPS){
384                                 bm = &Starfield_bitmaps[count++];
385                                 strcpy(bm->filename, filename);
386                                 bm->xparent = 0;
387                                 bm->bitmap = bm_load(bm->filename);                             
388                                 Assert(bm->bitmap != -1);
389
390                                 // if fred is running we should lock the bitmap now
391                                 if(Fred_running && (bm->bitmap >= 0)){
392                                         bm_lock(bm->bitmap, 8, BMP_TEX_OTHER);
393                                         bm_unlock(bm->bitmap);
394                                 } 
395                         }
396                 }
397                 // green xparency bitmap
398                 else if(optional_string("$BitmapX:")){
399                         stuff_string(filename, F_NAME, NULL);
400                         if(count < MAX_STARFIELD_BITMAPS){
401                                 bm = &Starfield_bitmaps[count++];
402                                 strcpy(bm->filename, filename);
403                                 bm->xparent = 1;
404                                 bm->bitmap = bm_load(bm->filename);
405                                 Assert(bm->bitmap != -1);
406
407                                 // if fred is running we should lock as a 0, 255, 0 bitmap now
408                                 if(Fred_running && (bm->bitmap >= 0)){
409                                         bm_lock(bm->bitmap, 8, BMP_TEX_XPARENT);
410                                         bm_unlock(bm->bitmap);
411                                 } 
412                         }
413                 }
414         }
415
416         // sun bitmaps
417         count = 0;
418         while(!optional_string("#end")){
419                 if(optional_string("$Sun:")){
420                         stuff_string(filename, F_NAME, NULL);
421
422                         // associated glow
423                         required_string("$Sunglow:");
424                         stuff_string(glow_filename, F_NAME, NULL);
425
426                         // associated lighting values
427                         required_string("$SunRGBI:");
428                         stuff_float(&r);
429                         stuff_float(&g);
430                         stuff_float(&b);
431                         stuff_float(&i);
432
433                         if(count < MAX_STARFIELD_BITMAPS){
434                                 bm = &Sun_bitmaps[count++];
435                                 strcpy(bm->filename, filename);
436                                 strcpy(bm->glow_filename, glow_filename);
437                                 bm->xparent = 1;
438                                 bm->bitmap = bm_load(bm->filename);
439                                 bm->glow_bitmap = bm_load(bm->glow_filename);
440                                 Assert(bm->bitmap != -1);
441                                 Assert(bm->glow_bitmap != -1);
442                                 bm->r = r;
443                                 bm->g = g;
444                                 bm->b = b;
445                                 bm->i = i;
446
447                                 // if fred is running we should lock the bitmap now
448                                 if(Fred_running){
449                                         if(bm->bitmap >= 0){
450                                                 bm_lock(bm->bitmap, 8, BMP_TEX_OTHER);
451                                                 bm_unlock(bm->bitmap);
452                                         }
453                                         if(bm->glow_bitmap >= 0){
454                                                 bm_lock(bm->glow_bitmap, 8, BMP_TEX_OTHER);
455                                                 bm_unlock(bm->glow_bitmap);
456                                         }
457                                 } 
458                         }
459                 }
460         }       
461
462         // normal debris pieces
463         count = 0;
464         while(!optional_string("#end")){
465                 required_string("$Debris:");
466                 stuff_string(filename, F_NAME, NULL);
467
468                 if(count < MAX_DEBRIS_VCLIPS){
469                         strcpy(debris_vclips_normal[count++].name, filename);
470                 }
471         }
472         Assert(count == 4);
473
474         // nebula debris pieces
475         count = 0;
476         while(!optional_string("#end")){
477                 required_string("$DebrisNeb:");
478                 stuff_string(filename, F_NAME, NULL);
479
480                 if(count < MAX_DEBRIS_VCLIPS){
481                         strcpy(debris_vclips_nebula[count++].name, filename);
482                 }
483         }
484         Assert(count == 4);
485 }
486
487 // call this in game_post_level_init() so we know whether we're running in full nebula mode or not
488 void stars_level_init()
489 {
490         int i;
491         vector v;
492         float dist, dist_max;
493
494         // reset to -1 so we reload it each mission (if we need to)
495         Nmodel_num = -1;                
496         if(Nmodel_bitmap != -1){
497                 bm_unload(Nmodel_bitmap);
498                 Nmodel_bitmap = -1;
499         }
500
501         // if (!stars_debris_loaded){
502                 stars_load_debris();
503         // }
504
505 // following code randomly distributes star points within a sphere volume, which
506 // avoids there being denser areas along the edges and in corners that we had in the
507 // old rectangular distribution scheme.
508         dist_max = (float) (HALF_RND_MAX * HALF_RND_MAX);
509         for (i=0; i<MAX_STARS; i++) {
510                 dist = dist_max;
511                 while (dist >= dist_max) {
512                         v.x = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
513                         v.y = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
514                         v.z = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
515
516                         dist = v.x * v.x + v.y * v.y + v.z * v.z;
517                 }
518                 vm_vec_copy_normalize(&Stars[i].pos, &v);
519         }
520
521         for (i=0; i<MAX_DEBRIS; i++) {
522                 odebris[i].active = 0;
523         }
524
525         for (i=0; i<8; i++ )    {
526                 ubyte intensity = (ubyte)((i + 1) * 24);
527                 gr_init_alphacolor(&star_aacolors[i], 255, 255, 255, intensity, AC_TYPE_BLEND );
528                 gr_init_color(&star_colors[i], intensity, intensity, intensity );
529         }
530
531         last_stars_filled = 0;
532
533         // if we have no sun instances, create one
534         if(Num_suns <= 0){
535                 mprintf(("Adding default sun\n"));
536                 
537                 // stuff some values
538                 strcpy(Suns[0].filename, Sun_bitmaps[0].filename);
539                 Suns[0].scale_x = 1.0f;
540                 Suns[0].scale_y = 1.0f;
541                 Suns[0].div_x = 1;
542                 Suns[0].div_y = 1;
543                 memset(&Suns[0].ang, 0, sizeof(angles));
544                 Suns[0].ang.h = fl_radian(60.0f);
545
546                 // one sun
547                 Num_suns = 1;
548         }               
549 }
550
551
552 #include "object.h"
553 extern object * Player_obj;
554
555 #define STAR_AMOUNT_DEFAULT 0.75f
556 #define STAR_DIM_DEFAULT 7800.0f
557 #define STAR_CAP_DEFAULT 75.0f
558 #define STAR_MAX_LENGTH_DEFAULT 0.04f           // 312
559
560 float Star_amount = STAR_AMOUNT_DEFAULT;
561 float Star_dim = STAR_DIM_DEFAULT;
562 float Star_cap = STAR_CAP_DEFAULT;
563 float Star_max_length = STAR_MAX_LENGTH_DEFAULT;        
564
565 #define STAR_FLAG_TAIL                  (1<<0)  // Draw a tail when moving
566 #define STAR_FLAG_DIM                   (1<<1)  // Dim as you move
567 #define STAR_FLAG_ANTIALIAS     (1<<2)  // Draw the star using antialiased lines
568 #define STAR_FLAG_DEFAULT               (STAR_FLAG_TAIL | STAR_FLAG_DIM)
569
570 uint Star_flags = STAR_FLAG_DEFAULT;
571
572 //XSTR:OFF
573 DCF(stars,"Set parameters for starfield")
574 {
575         if ( Dc_command )       {
576                 dc_get_arg(ARG_STRING);
577                 if ( !strcmp( Dc_arg, "tail" )) {
578                         dc_get_arg(ARG_FLOAT);
579                         if ( (Dc_arg_float < 0.0f) || (Dc_arg_float > 1.0f) )   {
580                                 Dc_help = 1;
581                         } else {
582                                 Star_amount = Dc_arg_float;
583                         } 
584                 } else if ( !strcmp( Dc_arg, "len" ))   {
585                         dc_get_arg(ARG_FLOAT);
586                         Star_max_length = Dc_arg_float;
587                 } else if ( !strcmp( Dc_arg, "dim" ))   {
588                         dc_get_arg(ARG_FLOAT);
589                         if ( Dc_arg_float < 0.0f )      {
590                                 Dc_help = 1;
591                         } else {
592                                 Star_dim = Dc_arg_float;
593                         } 
594                 } else if ( !strcmp( Dc_arg, "flag" ))  {
595                         dc_get_arg(ARG_STRING);
596                         if ( !strcmp( Dc_arg, "tail" )) {
597                                 Star_flags ^= STAR_FLAG_TAIL;
598                         } else if ( !strcmp( Dc_arg, "dim" ))   {
599                                 Star_flags ^= STAR_FLAG_DIM;
600                         } else if ( !strcmp( Dc_arg, "aa" ))    {
601                                 Star_flags ^= STAR_FLAG_ANTIALIAS;
602                         } else {
603                                 Dc_help = 1;    
604                         }
605                 } else if ( !strcmp( Dc_arg, "cap" ))   {
606                         dc_get_arg(ARG_FLOAT);
607                         if ( (Dc_arg_float < 0.0f) || (Dc_arg_float > 255.0f) ) {
608                                 Dc_help = 1;
609                         } else {
610                                 Star_cap = Dc_arg_float;
611                         } 
612                 } else if ( !strcmp( Dc_arg, "m0" )  )  {
613                         Star_amount = 0.0f;
614                         Star_dim = 0.0f;
615                         Star_cap = 0.0f;
616                         Star_flags = 0;
617                         Star_max_length = STAR_MAX_LENGTH_DEFAULT;
618                 } else if ( !strcmp( Dc_arg, "m1" ) || !strcmp( Dc_arg, "default" ))    {
619                         Star_amount = STAR_AMOUNT_DEFAULT;
620                         Star_dim = STAR_DIM_DEFAULT;
621                         Star_cap = STAR_CAP_DEFAULT;
622                         Star_flags = STAR_FLAG_DEFAULT;
623                         Star_max_length = STAR_MAX_LENGTH_DEFAULT;
624                 } else if ( !strcmp( Dc_arg, "m2" ))    {
625                         Star_amount = 0.75f;
626                         Star_dim = 20.0f;
627                         Star_cap = 75.0f;
628                         Star_flags = STAR_FLAG_TAIL|STAR_FLAG_DIM|STAR_FLAG_ANTIALIAS;
629                         Star_max_length = STAR_MAX_LENGTH_DEFAULT;
630                 } else if ( !strcmp( Dc_arg, "num" ))   {
631                         dc_get_arg(ARG_INT);
632                         if ( (Dc_arg_int < 0) || (Dc_arg_int > MAX_STARS) )     {
633                                 Dc_help = 1;
634                         } else {
635                                 Num_stars = Dc_arg_int;
636                         } 
637                 } else {
638                         // print usage, not stats
639                         Dc_help = 1;
640                 }
641         }
642
643         if ( Dc_help )  {
644                 dc_printf( "Usage: stars keyword\nWhere keyword can be in the following forms:\n" );
645                 dc_printf( "stars default   Resets stars to all default values\n" );
646                 dc_printf( "stars num X     Sets number of stars to X.  Between 0 and %d.\n", MAX_STARS );
647                 dc_printf( "stars tail X    Where X is the percent of 'tail' between 0 and 1.0\n" );
648                 dc_printf( "stars dim X     Where X is the amount stars dim between 0 and 255.0\n" );
649                 dc_printf( "stars cap X     Where X is the cap of dimming between 0 and 255.\n" );
650                 dc_printf( "stars len X     Where X is the cap of length.\n" );
651                 dc_printf( "stars m0        Macro0. Old 'pixel type' crappy stars. flags=none\n" );
652                 dc_printf( "stars m1        Macro1. (default) tail=.75, dim=20.0, cap=75.0, flags=dim,tail\n" );
653                 dc_printf( "stars m2        Macro2. tail=.75, dim=20.0, cap=75.0, flags=dim,tail,aa\n" );
654                 dc_printf( "stars flag X    Toggles flag X, where X is tail or dim or aa (aa=antialias)\n" );
655                 dc_printf( "\nHINT: set cap to 0 to get dim rate and tail down, then use\n" );
656                 dc_printf( "cap to keep the lines from going away when moving too fast.\n" );
657                 dc_printf( "\nUse '? stars' to see current values.\n" );
658                 Dc_status = 0;  // don't print status if help is printed.  Too messy.
659         }
660
661         if ( Dc_status )        {
662                 dc_printf( "Num_stars: %d\n", Num_stars );
663                 dc_printf( "Tail: %.2f\n", Star_amount );
664                 dc_printf( "Dim: %.2f\n", Star_dim );
665                 dc_printf( "Cap: %.2f\n", Star_cap );
666                 dc_printf( "Max length: %.2f\n", Star_max_length );
667                 dc_printf( "Flags:\n" );
668                 dc_printf( "  Tail: %s\n", (Star_flags&STAR_FLAG_TAIL?"On":"Off") );
669                 dc_printf( "  Dim: %s\n", (Star_flags&STAR_FLAG_DIM?"On":"Off") );
670                 dc_printf( "  Antialias: %s\n", (Star_flags&STAR_FLAG_ANTIALIAS?"On":"Off") );
671                 dc_printf( "\nTHESE AREN'T SAVED TO DISK, SO IF YOU TWEAK\n" );
672                 dc_printf( "THESE AND LIKE THEM, WRITE THEM DOWN!!\n" );
673         }
674 }
675 //XSTR:ON
676
677 int reload_old_debris = 1;              // If set to one, then reload all the last_pos of the debris
678
679 // Call this if camera "cuts" or moves long distances
680 // so blur effect doesn't draw lines all over the screen.
681 void stars_camera_cut()
682 {
683         last_stars_filled = 0;
684         reload_old_debris = 1;
685 }
686
687 //#define TIME_STAR_CODE                // enable to time star code
688
689 extern int Sun_drew;
690 extern float Viewer_zoom;
691
692 // get the world coords of the sun pos on the unit sphere.
693 void stars_get_sun_pos(int sun_n, vector *pos)
694 {
695         vector temp;
696         matrix rot;
697
698         // sanity
699         Assert(sun_n < Num_suns);
700         if((sun_n >= Num_suns) || (sun_n < 0)){
701                 return;
702         }
703
704         // rotate the sun properly
705         temp = vmd_zero_vector;
706         temp.z = 1.0f;
707         
708         // rotation matrix
709         vm_angles_2_matrix(&rot, &Suns[sun_n].ang);
710         vm_vec_rotate(pos, &temp, &rot);
711 }
712
713 // draw sun
714 void stars_draw_sun( int show_sun )
715 {       
716         int idx;
717         vector sun_pos;
718         vector sun_dir;
719         vertex sun_vex; 
720         starfield_bitmap *bm;
721         float local_scale = 1.0f;
722
723         // no suns drew yet
724         Sun_drew = 0;
725
726         // draw all suns
727         for(idx=0; idx<Num_suns; idx++){                
728                 // get the instance
729                 bm = stars_lookup_sun(&Suns[idx]);
730                 if(bm == NULL){
731                         continue;
732                 }
733
734                 // get sun pos
735                 sun_pos = vmd_zero_vector;
736                 sun_pos.y = 1.0f;
737                 stars_get_sun_pos(idx, &sun_pos);
738                 
739                 // get the direction            
740                 sun_dir = sun_pos;
741                 vm_vec_normalize(&sun_dir);
742
743                 // add the light source corresponding to the sun
744                 light_add_directional(&sun_dir, bm->i, bm->r, bm->g, bm->b);
745
746                 // if supernova
747                 if(supernova_active()){
748                         local_scale = 1.0f + (SUPERNOVA_SUN_SCALE * supernova_pct_complete());
749                 }
750
751                 // draw the sun itself, keep track of how many we drew
752                 gr_set_bitmap(bm->bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.999f);
753                 g3_rotate_faraway_vertex(&sun_vex, &sun_pos);
754                 if(!g3_draw_bitmap(&sun_vex, 0, 0.05f * Suns[idx].scale_x * local_scale, TMAP_FLAG_TEXTURED)){
755                         Sun_drew++;
756                 }
757         }
758 }
759
760 // draw the corresponding glow for sun_n
761 void stars_draw_sun_glow(int sun_n)
762 {
763         starfield_bitmap *bm;           
764         vector sun_pos, sun_dir;
765         vertex sun_vex; 
766         float local_scale = 1.0f;
767
768         // sanity
769         Assert(sun_n < Num_suns);
770         if((sun_n >= Num_suns) || (sun_n < 0)){
771                 return;
772         }
773
774         // get the instance
775         bm = stars_lookup_sun(&Suns[sun_n]);
776         if(bm == NULL){
777                 return;
778         }
779
780         // get sun pos
781         sun_pos = vmd_zero_vector;
782         sun_pos.y = 1.0f;
783         stars_get_sun_pos(sun_n, &sun_pos);     
784
785         // get the direction            
786         sun_dir = sun_pos;
787         vm_vec_normalize(&sun_dir);     
788
789         // if supernova
790         if(supernova_active()){
791                 local_scale = 1.0f + (SUPERNOVA_SUN_SCALE * supernova_pct_complete());
792         }
793
794         // draw the sun itself, keep track of how many we drew
795         gr_set_bitmap(bm->glow_bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.5f);
796         g3_rotate_faraway_vertex(&sun_vex, &sun_pos);
797         g3_draw_bitmap(&sun_vex, 0, 0.10f * Suns[sun_n].scale_x * local_scale, TMAP_FLAG_TEXTURED);     
798 }
799
800 // draw bitmaps
801 void stars_draw_bitmaps( int show_bitmaps )
802 {
803         int idx;
804         int star_index; 
805
806         // if we're in the nebula, don't render any backgrounds
807         if(The_mission.flags & MISSION_FLAG_FULLNEB){
808                 return;
809         }
810
811         // detail settings
812         if(!Detail.planets_suns){
813                 return;
814         }
815         
816         // render all bitmaps
817         for(idx=0; idx<Num_starfield_bitmaps; idx++){
818                 // lookup the info index
819                 star_index = stars_find_bitmap(Starfield_bitmap_instance[idx].filename);
820                 if(star_index < 0){
821                         continue;
822                 }
823         
824                 // set the bitmap                               
825                 if(Fred_running){
826                         gr_set_bitmap(Starfield_bitmaps[star_index].bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.9999f);              
827                         g3_draw_perspective_bitmap(&Starfield_bitmap_instance[idx].ang, Starfield_bitmap_instance[idx].scale_x, Starfield_bitmap_instance[idx].scale_y, Starfield_bitmap_instance[idx].div_x, Starfield_bitmap_instance[idx].div_y, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
828                 } else {
829                         if(Starfield_bitmaps[star_index].xparent){
830                                 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap);            
831                                 g3_draw_perspective_bitmap(&Starfield_bitmap_instance[idx].ang, Starfield_bitmap_instance[idx].scale_x, Starfield_bitmap_instance[idx].scale_y, Starfield_bitmap_instance[idx].div_x, Starfield_bitmap_instance[idx].div_y, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT | TMAP_FLAG_XPARENT);
832                         } else {                                
833                                 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.9999f);              
834                                 g3_draw_perspective_bitmap(&Starfield_bitmap_instance[idx].ang, Starfield_bitmap_instance[idx].scale_x, Starfield_bitmap_instance[idx].scale_y, Starfield_bitmap_instance[idx].div_x, Starfield_bitmap_instance[idx].div_y, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
835                         }
836                 }
837         }
838 }
839
840 /*
841 void calculate_bitmap_matrix(starfield_bitmaps *bm, vector *v)
842 {
843         vm_vector_2_matrix(&bm->m, v, NULL, NULL);
844         vm_orthogonalize_matrix(&bm->m);
845 }
846
847 void calculate_bitmap_points(starfield_bitmaps *bm, float bank)
848 {
849         int i;
850         vector fvec, uvec, rvec, tmp;
851         angles tangles;
852
853         vm_orthogonalize_matrix(&bm->m);
854         if (bank) {
855                 tangles.p = tangles.h = 0.0f;
856                 tangles.b = bank;
857                 vm_rotate_matrix_by_angles(&bm->m, &tangles);
858         }
859
860         fvec = bm->m.fvec;
861         vm_vec_scale(&fvec, bm->dist );
862         uvec = bm->m.uvec;
863         rvec = bm->m.rvec;
864
865         vm_vec_sub(&tmp, &fvec, &uvec);
866         vm_vec_sub(&bm->points[3], &tmp, &rvec);
867
868         vm_vec_sub(&tmp, &fvec, &uvec);
869         vm_vec_add(&bm->points[2], &tmp, &rvec);
870
871         vm_vec_add(&tmp, &fvec, &uvec);
872         vm_vec_add(&bm->points[1], &tmp, &rvec);
873
874         vm_vec_add(&tmp, &fvec, &uvec);
875         vm_vec_sub(&bm->points[0], &tmp, &rvec);
876
877         for (i=0; i<4; i++){
878                 vm_vec_normalize(&bm->points[i]);
879         }
880 }
881 */
882
883 extern int Interp_subspace;
884 extern float Interp_subspace_offset_u;
885 extern float Interp_subspace_offset_u;
886 extern float Interp_subspace_offset_v;
887
888 float subspace_offset_u = 0.0f;
889 float subspace_offset_u_inner = 0.0f;
890 float subspace_offset_v = 0.0f;
891
892 float subspace_u_speed = 0.07f;                 // how fast u changes
893 float subspace_v_speed = 0.05f;                 // how fast v changes
894
895 int Subspace_glow_bitmap = -1;
896
897 float Subspace_glow_frame = 0.0f;
898 float Subspace_glow_rate = 1.0f;
899
900
901 //XSTR:OFF
902 DCF(subspace_set,"Set parameters for subspace effect")
903 {
904         if ( Dc_command )       {
905                 dc_get_arg(ARG_STRING);
906                 if ( !strcmp( Dc_arg, "u" ))    {
907                         dc_get_arg(ARG_FLOAT);
908                         if ( Dc_arg_float < 0.0f )      {
909                                 Dc_help = 1;
910                         } else {
911                                 subspace_u_speed = Dc_arg_float;
912                         } 
913                 } else if ( !strcmp( Dc_arg, "v" ))     {
914                         dc_get_arg(ARG_FLOAT);
915                         if ( Dc_arg_float < 0.0f )      {
916                                 Dc_help = 1;
917                         } else {
918                                 subspace_v_speed = Dc_arg_float;
919                         } 
920                 } else {
921                         // print usage, not stats
922                         Dc_help = 1;
923                 }
924         }
925
926         if ( Dc_help )  {
927                 dc_printf( "Usage: subspace keyword\nWhere keyword can be in the following forms:\n" );
928                 dc_printf( "subspace u X    Where X is how fast u moves.\n", MAX_STARS );
929                 dc_printf( "subspace v X    Where X is how fast v moves.\n" );
930                 dc_printf( "\nUse '? subspace' to see current values.\n" );
931                 Dc_status = 0;  // don't print status if help is printed.  Too messy.
932         }
933
934         if ( Dc_status )        {
935                 dc_printf( "u: %.2f\n", subspace_u_speed );
936                 dc_printf( "v: %.2f\n", subspace_v_speed );
937         }
938 }
939 //XSTR:ON
940
941 void subspace_render()
942 {
943         if ( Subspace_model_inner == -1 )       {
944                 Subspace_model_inner = model_load( "subspace_small.pof", 0, NULL );
945                 Assert(Subspace_model_inner>-1);
946         }
947
948         if ( Subspace_model_outer == -1 )       {
949                 Subspace_model_outer = model_load( "subspace_big.pof", 0, NULL );
950                 Assert(Subspace_model_outer>-1);
951         }
952
953         if ( Subspace_glow_bitmap == -1 )       {
954                 Subspace_glow_bitmap = bm_load( NOX("SunGlow01"));
955                 Assert(Subspace_glow_bitmap>-1);
956         }
957
958         Subspace_glow_frame += flFrametime * 1.0f;
959
960         float total_time = i2fl(NOISE_NUM_FRAMES) / 15.0f;
961
962         // Sanity checks
963         if ( Subspace_glow_frame < 0.0f )       Subspace_glow_frame = 0.0f;
964         if ( Subspace_glow_frame > 100.0f ) Subspace_glow_frame = 0.0f;
965
966         while ( Subspace_glow_frame > total_time )      {
967                 Subspace_glow_frame -= total_time;
968         }
969         int framenum = fl2i( (Subspace_glow_frame*NOISE_NUM_FRAMES) / total_time );
970         if ( framenum < 0 ) framenum = 0;
971         if ( framenum >= NOISE_NUM_FRAMES ) framenum = NOISE_NUM_FRAMES-1;
972
973         subspace_offset_u += flFrametime*subspace_u_speed;
974         if (subspace_offset_u > 1.0f )  {
975                 subspace_offset_u -= 1.0f;
976         }
977
978         subspace_offset_u_inner += flFrametime*subspace_u_speed*3.0f;
979         if (subspace_offset_u > 1.0f )  {
980                 subspace_offset_u -= 1.0f;
981         }
982
983         subspace_offset_v += flFrametime*subspace_v_speed;
984         if (subspace_offset_v > 1.0f )  {
985                 subspace_offset_v -= 1.0f;
986         }
987
988         
989
990         matrix tmp;
991         angles angs = { 0.0f, 0.0f, 0.0f };
992         angs.b = subspace_offset_v * PI2;
993         
994         vm_angles_2_matrix(&tmp,&angs);
995         
996         int saved_gr_zbuffering =       gr_zbuffer_get();
997
998         gr_zbuffer_set(GR_ZBUFF_NONE);
999
1000         if ( !D3D_enabled )     {
1001
1002                 int render_flags = MR_NO_LIGHTING | MR_ALWAYS_REDRAW;
1003
1004                 Interp_subspace = 1;    
1005                 Interp_subspace_offset_u = 1.0f - subspace_offset_u;
1006                 Interp_subspace_offset_v = 0.0f;
1007
1008                 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1009                 render_flags |= MR_SHOW_THRUSTERS;
1010                 model_render( Subspace_model_outer, &tmp, &Eye_position, render_flags );        //MR_NO_CORRECT|MR_SHOW_OUTLINE 
1011
1012         } else {
1013
1014                 int render_flags = MR_NO_LIGHTING | MR_ALWAYS_REDRAW;
1015
1016                 Interp_subspace = 1;    
1017                 Interp_subspace_offset_u = 1.0f - subspace_offset_u;
1018                 Interp_subspace_offset_v = 0.0f;
1019
1020                 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1021                 render_flags |= MR_SHOW_THRUSTERS;
1022                 model_render( Subspace_model_outer, &tmp, &Eye_position, render_flags );        //MR_NO_CORRECT|MR_SHOW_OUTLINE 
1023                 
1024                 Interp_subspace = 1;    
1025                 Interp_subspace_offset_u = 1.0f - subspace_offset_u_inner;
1026                 Interp_subspace_offset_v = 0.0f;        
1027
1028                 angs.b = -subspace_offset_v * PI2;
1029
1030                 vm_angles_2_matrix(&tmp,&angs);
1031
1032                 model_set_outline_color(255,255,255);
1033
1034                 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1035                 render_flags |= MR_SHOW_THRUSTERS;
1036
1037                 model_render( Subspace_model_inner, &tmp, &Eye_position, render_flags  );       //MR_NO_CORRECT|MR_SHOW_OUTLINE 
1038         }
1039
1040         Interp_subspace = 0;
1041         gr_zbuffer_set(saved_gr_zbuffering);
1042 }
1043
1044 void stars_draw( int show_stars, int show_suns, int show_nebulas, int show_subspace )
1045 {
1046         int i;
1047         float vdist;
1048
1049
1050         int gr_zbuffering_save = gr_zbuffer_get();
1051         gr_zbuffer_set(GR_ZBUFF_NONE);
1052
1053         if ( show_subspace )    {
1054                 subspace_render();
1055         }
1056
1057         
1058         if (Num_stars >= MAX_STARS){
1059                 Num_stars = MAX_STARS;
1060         }
1061
1062 #ifdef TIME_STAR_CODE
1063         fix xt1, xt2;
1064         xt1 = timer_get_fixed_seconds();
1065 #endif
1066         
1067         if ( show_nebulas && (Game_detail_flags & DETAIL_FLAG_NEBULAS) && (Neb2_render_mode != NEB2_RENDER_POF) && (Neb2_render_mode != NEB2_RENDER_LAME))      {
1068                 nebula_render();
1069         }
1070
1071         // draw background stuff        
1072         if((Neb2_render_mode != NEB2_RENDER_POLY) && (Neb2_render_mode != NEB2_RENDER_LAME) && show_stars){
1073                 // semi-hack, do we don't fog the background
1074                 int neb_save = Neb2_render_mode;
1075                 Neb2_render_mode = NEB2_RENDER_NONE;
1076                 extern void stars_draw_background();
1077                 stars_draw_background();
1078                 Neb2_render_mode = neb_save;
1079         }
1080
1081         if (show_stars && ( Game_detail_flags & DETAIL_FLAG_STARS) && !(The_mission.flags & MISSION_FLAG_FULLNEB) && (supernova_active() < 3))  {
1082                 //Num_stars = 1;
1083         
1084                 star *sp;
1085
1086                 if ( !last_stars_filled )       {
1087                         for (sp=Stars,i=0; i<Num_stars; i++, sp++ ) {
1088                                 vertex p2;
1089                                 g3_rotate_faraway_vertex(&p2, &sp->pos);
1090                                 sp->last_star_pos.x = p2.x;
1091                                 sp->last_star_pos.y = p2.y;
1092                                 sp->last_star_pos.z = p2.z;
1093                         }
1094                 }
1095
1096                 int tmp_num_stars;
1097
1098                 tmp_num_stars = (Detail.num_stars*Num_stars)/MAX_DETAIL_LEVEL;
1099                 if (tmp_num_stars < 0 ) {
1100                         tmp_num_stars = 0;
1101                 } else if ( tmp_num_stars > Num_stars ) {
1102                         tmp_num_stars = Num_stars;
1103                 }
1104                 
1105                 for (sp=Stars,i=0; i<tmp_num_stars; i++, sp++ ) {
1106                         vertex p1, p2;                  
1107                         int can_draw = 1;                       
1108
1109                         memset(&p1, 0, sizeof(vertex));
1110
1111                         // This makes a star look "proper" by not translating the
1112                         // point around the viewer's eye before rotation.  In other
1113                         // words, when the ship translates, the stars do not change.
1114
1115                         g3_rotate_faraway_vertex(&p2, &sp->pos);
1116                         if ( p2.codes ) {
1117                                 can_draw = 0;
1118                         } else {
1119                                 g3_project_vertex(&p2);
1120                                 if ( p2.flags & PF_OVERFLOW )   {
1121                                         can_draw = 0;
1122                                 }
1123                         }
1124
1125                         float dist = 0.0f;
1126
1127                         if ( can_draw && (Star_flags & (STAR_FLAG_TAIL|STAR_FLAG_DIM)) )        {
1128
1129                                 dist = vm_vec_dist_quick( &sp->last_star_pos, (vector *)&p2.x );
1130
1131                                 float ratio;
1132                                 if ( dist > Star_max_length )   {
1133                                         ratio = Star_max_length / dist;
1134                                         dist = Star_max_length;
1135                                 } else {
1136                                         ratio = 1.0f;
1137                                 }
1138                                 ratio *= Star_amount;
1139
1140                                 p1.x = p2.x + (sp->last_star_pos.x-p2.x)*ratio;
1141                                 p1.y = p2.y + (sp->last_star_pos.y-p2.y)*ratio;
1142                                 p1.z = p2.z + (sp->last_star_pos.z-p2.z)*ratio;
1143
1144                                 p1.flags = 0;   // not projected
1145                                 g3_code_vertex( &p1 );
1146
1147                                 if ( p1.codes ) {
1148                                         can_draw = 0;
1149                                 } else {
1150                                         g3_project_vertex(&p1);
1151                                         if ( p1.flags & PF_OVERFLOW )   {
1152                                                 can_draw = 0;
1153                                         }
1154                                 }
1155                         }
1156
1157                         sp->last_star_pos.x = p2.x;
1158                         sp->last_star_pos.y = p2.y;
1159                         sp->last_star_pos.z = p2.z;
1160
1161                         if ( !can_draw )        continue;
1162
1163                         int color;
1164
1165                         if ( Star_flags & STAR_FLAG_DIM )       {
1166
1167                                 float colorf = 255.0f - dist*Star_dim;
1168
1169                                 if ( colorf < Star_cap )
1170                                         colorf = Star_cap;
1171
1172                                 color = (fl2i(colorf)*(i&7))/256;
1173
1174                         } else {
1175                                 color = i & 7;
1176                         }
1177
1178                         if ( (Star_flags & STAR_FLAG_ANTIALIAS) || (D3D_enabled) )      {
1179                                 gr_set_color_fast( &star_aacolors[color] );
1180
1181                                 // if the two points are the same, fudge it, since some D3D cards (G200 and G400) are lame.                             
1182                                 if( (fl2i(p1.sx) == fl2i(p2.sx)) && (fl2i(p1.sy) == fl2i(p2.sy)) ){                                     
1183                                         p1.sx += 1.0f;
1184                                 }                                                               
1185                                 gr_aaline(&p1,&p2);
1186                         } else {
1187                                 // use alphablended line so that dark stars don't look bad on top of nebulas
1188                                 gr_set_color_fast( &star_aacolors[color] );
1189                                 if ( Star_flags & STAR_FLAG_TAIL )      {
1190                                         gr_line(fl2i(p1.sx),fl2i(p1.sy),fl2i(p2.sx),fl2i(p2.sy));
1191                                 } else {
1192                                         gr_pixel( fl2i(p2.sx),fl2i(p2.sy) );
1193                                 }
1194                         }
1195                 }
1196         }
1197
1198         last_stars_filled = 1;
1199
1200 #ifdef TIME_STAR_CODE
1201         xt2 = timer_get_fixed_seconds();
1202         mprintf(( "Stars: %d\n", xt2-xt1 ));
1203 #endif
1204         
1205
1206         if ( (Game_detail_flags & DETAIL_FLAG_MOTION) && (!Fred_running) && (supernova_active() < 3) )  {
1207
1208                 gr_set_color( 0, 0, 0 );
1209
1210                 // turn off fogging
1211                 if(The_mission.flags & MISSION_FLAG_FULLNEB){
1212                         gr_fog_set(GR_FOGMODE_NONE, 0, 0, 0);
1213                 }
1214
1215                 old_debris * d = odebris; 
1216                 for (i=0; i<MAX_DEBRIS; i++, d++ ) {
1217                         vertex p;
1218
1219                         if (!d->active) {
1220                                 d->pos.x = f2fl(myrand() - RAND_MAX/2);
1221                                 d->pos.y = f2fl(myrand() - RAND_MAX/2);
1222                                 d->pos.z = f2fl(myrand() - RAND_MAX/2);
1223
1224                                 vm_vec_normalize(&d->pos);
1225
1226                                 vm_vec_scale(&d->pos, MAX_DIST);
1227                                 vm_vec_add2(&d->pos, &Eye_position );
1228 //                              vm_vec_add2(&d->pos, &Player_obj->pos );
1229                                 d->active = 1;
1230                                 d->vclip = i % MAX_DEBRIS_VCLIPS;       //rand()
1231
1232                                 // if we're in full neb mode
1233                                 if((The_mission.flags & MISSION_FLAG_FULLNEB) && (Neb2_render_mode != NEB2_RENDER_NONE)){
1234                                         d->size = i2fl(myrand() % 4)*BASE_SIZE_NEB;
1235                                 } else {
1236                                         d->size = i2fl(myrand() % 4)*BASE_SIZE;
1237                                 }
1238
1239                                 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1240                         }
1241
1242                         if ( reload_old_debris )        {
1243                                 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1244                         }
1245                         
1246                         g3_rotate_vertex(&p, &d->pos);
1247
1248                         if (p.codes == 0) {
1249                                 int frame = Missiontime / (DEBRIS_ROT_MIN + (i % DEBRIS_ROT_RANGE) * DEBRIS_ROT_RANGE_SCALER);
1250                                 frame %= debris_vclips[d->vclip].nframes;
1251
1252                                 if((The_mission.flags & MISSION_FLAG_FULLNEB) && (Neb2_render_mode != NEB2_RENDER_NONE)){
1253                                         gr_set_bitmap( debris_vclips[d->vclip].bm + frame, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.3f);  
1254                                 } else {
1255                                         gr_set_bitmap( debris_vclips[d->vclip].bm + frame );                                            
1256                                 }
1257                                         
1258                                 vector tmp;
1259                                 vm_vec_add( &tmp, &d->last_pos, &Eye_position );
1260                                 g3_draw_laser( &d->pos,d->size,&tmp,d->size, TMAP_FLAG_TEXTURED|TMAP_FLAG_XPARENT, 25.0f );                                     
1261                         }
1262
1263                         vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1264
1265                         vdist = vm_vec_mag_quick(&d->last_pos);
1266
1267                         if (vdist > MAX_DIST_RANGE)
1268                                 d->active = 0;
1269                         else if (vdist < MIN_DIST_RANGE)
1270                                 d->active = 0;
1271
1272         //              vector tmp;
1273         //              vm_vec_sub( &tmp, &d->pos, &Player_obj->pos );
1274         //              vdist = vm_vec_dot( &tmp, &Player_obj->orient.fvec );
1275         //              if ( vdist < 0.0f )
1276         //                      d->active = 0;
1277
1278                 }
1279
1280                 reload_old_debris = 0;
1281         }
1282
1283
1284         stars_draw_sun( show_suns );    
1285         stars_draw_bitmaps( show_suns );
1286
1287         gr_zbuffer_set( gr_zbuffering_save );
1288 }
1289
1290 void stars_page_in()
1291 {
1292         int i, j;
1293
1294         // Initialize the subspace stuff
1295
1296         if ( Game_subspace_effect )     {
1297
1298                 Subspace_model_inner = model_load( "subspace_small.pof", 0, NULL );
1299                 Assert(Subspace_model_inner>-1);
1300                 Subspace_model_outer = model_load( "subspace_big.pof", 0, NULL );
1301                 Assert(Subspace_model_outer>-1);
1302
1303                 polymodel *pm;
1304                 
1305                 pm = model_get(Subspace_model_inner);
1306                 
1307                 nprintf(( "Paging", "Paging in textures for subspace effect.\n" ));
1308
1309                 for (j=0; j<pm->n_textures; j++ )       {
1310                         int bitmap_num = pm->original_textures[j];
1311
1312                         if ( bitmap_num > -1 )  {
1313                                 bm_page_in_texture( bitmap_num );
1314                         }
1315                 }
1316
1317                 pm = model_get(Subspace_model_outer);
1318                 
1319                 nprintf(( "Paging", "Paging in textures for subspace effect.\n" ));
1320
1321                 for (j=0; j<pm->n_textures; j++ )       {
1322                         int bitmap_num = pm->original_textures[j];
1323
1324                         if ( bitmap_num > -1 )  {
1325                                 bm_page_in_texture( bitmap_num );
1326                         }
1327                 }
1328         } else {
1329                 Subspace_model_inner = -1;
1330                 Subspace_model_outer = -1;
1331         }
1332
1333         Subspace_glow_bitmap = bm_load( NOX("SunGlow01"));
1334         bm_page_in_xparent_texture(Subspace_glow_bitmap);
1335
1336         // page in starfield bitmaps
1337         int idx;
1338         idx = 0;
1339         while((idx < MAX_STARFIELD_BITMAPS) && (Starfield_bitmaps[idx].bitmap != -1)){  
1340                 if(Starfield_bitmaps[idx].xparent){
1341                         bm_page_in_xparent_texture(Starfield_bitmaps[idx].bitmap);
1342                 } else { 
1343                         bm_page_in_texture(Starfield_bitmaps[idx].bitmap);
1344                 }
1345
1346                 // next;
1347                 idx++;
1348         }
1349
1350         // sun bitmaps and glows
1351         idx = 0;
1352         while((idx < MAX_STARFIELD_BITMAPS) && (Sun_bitmaps[idx].bitmap != -1) && (Sun_bitmaps[idx].glow_bitmap != -1)){
1353                 bm_page_in_texture(Sun_bitmaps[idx].bitmap);
1354                 bm_page_in_texture(Sun_bitmaps[idx].glow_bitmap);
1355
1356                 // next 
1357                 idx++;
1358         }
1359
1360         for (i=0; i<MAX_DEBRIS_VCLIPS; i++ )    {
1361                 for (j=0; j<debris_vclips[i].nframes; j++ )     {
1362                         bm_page_in_xparent_texture(debris_vclips[i].bm + j);
1363                 }
1364         }       
1365 }
1366
1367 // background nebula models and planets
1368 void stars_draw_background()
1369 {                               
1370         if((Nmodel_num < 0) || (Nmodel_bitmap < 0)){
1371                 return;
1372         }
1373
1374         // draw the model at the player's eye wif no z-buffering
1375         model_set_alpha(1.0f);
1376         model_set_forced_texture(Nmodel_bitmap);        
1377         model_render(Nmodel_num, &vmd_identity_matrix, &Eye_position, MR_NO_ZBUFFER | MR_NO_CULL | MR_ALL_XPARENT | MR_NO_LIGHTING | MR_FORCE_TEXTURE); 
1378         model_set_forced_texture(-1);
1379 }
1380
1381 // call this to set a specific model as the background model
1382 void stars_set_background_model(char *model_name, char *texture_name)
1383 {
1384         Nmodel_num = model_load(model_name, 0, NULL);
1385         Nmodel_bitmap = bm_load(texture_name);
1386 }
1387
1388 // lookup a starfield bitmap, return index or -1 on fail
1389 int stars_find_bitmap(char *name)
1390 {
1391         int idx;
1392
1393         // lookup
1394         for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
1395                 if(!strcmp(name, Starfield_bitmaps[idx].filename)){
1396                         return idx;
1397                 }
1398         }
1399
1400         // not found 
1401         return -1;
1402 }
1403
1404 // lookup a sun by bitmap filename, return index or -1 on fail
1405 int stars_find_sun(char *name)
1406 {
1407         int idx;
1408
1409         // lookup
1410         for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
1411                 if(!strcmp(name, Sun_bitmaps[idx].filename)){
1412                         return idx;
1413                 }
1414         }
1415
1416         // not found 
1417         return -1;
1418 }
1419