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