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