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