2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
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
10 * $Logfile: /Freespace2/code/Starfield/StarField.cpp $
15 * Code to handle and draw starfields, background space image bitmaps, floating
19 * Revision 1.8 2004/07/04 11:40:26 taylor
20 * only load those background bitmaps that we are going to use this mission
22 * Revision 1.7 2003/05/25 02:30:44 taylor
25 * Revision 1.6 2002/06/22 23:57:39 relnev
26 * remove writable strings.
28 * fix compile for intel compiler.
30 * Revision 1.5 2002/06/17 06:33:11 relnev
31 * ryan's struct patch for gcc 2.95
33 * Revision 1.4 2002/06/09 04:41:27 relnev
34 * added copyright header
36 * Revision 1.3 2002/06/02 04:26:34 relnev
39 * Revision 1.2 2002/05/07 03:16:52 theoddone33
40 * The Great Newline Fix
42 * Revision 1.1.1.1 2002/05/03 03:28:10 root
46 * 34 9/07/99 4:01p Dave
47 * Fixed up a string.tbl paroblem (self destruct message). Make sure IPX
48 * does everything properly (setting up address when binding). Remove
49 * black rectangle background from UI_INPUTBOX.
51 * 33 9/01/99 10:14a Dave
54 * 32 8/30/99 5:01p Dave
55 * Made d3d do less state changing in the nebula. Use new chat server for
58 * 31 8/19/99 10:59a Dave
59 * Packet loss detection.
61 * 30 7/27/99 3:52p Dave
62 * Make star drawing a bit more robust to help lame D3D cards.
64 * 29 7/21/99 8:10p Dave
65 * First run of supernova effect.
67 * 28 7/13/99 2:01p Dave
68 * Don't draw background bitmaps in the nebula.
70 * 27 6/08/99 2:34p Jasenw
71 * Made perspective bitmaps render in Fred.
73 * 26 6/04/99 1:18p Dave
74 * Fixed briefing model rendering problems. Made show background option in
75 * fred toggle nebula rendering.
77 * 25 6/03/99 6:37p Dave
78 * More TNT fun. Made perspective bitmaps more flexible.
80 * 24 5/28/99 1:45p Dave
81 * Fixed up perspective bitmap drawing.
83 * 23 5/20/99 7:00p Dave
84 * Added alternate type names for ships. Changed swarm missile table
87 * 22 5/11/99 10:03a Dave
88 * Put a bunch of stuff into tables.
90 * 21 5/11/99 9:10a Dave
91 * Move default sun position.
93 * 20 5/09/99 6:00p Dave
94 * Lots of cool new effects. E3 build tweaks.
96 * 19 4/26/99 8:49p Dave
97 * Made all pof based nebula stuff full customizable through fred.
99 * 18 4/25/99 7:43p Dave
100 * Misc small bug fixes. Made sun draw properly.
102 * 17 4/23/99 5:53p Dave
103 * Started putting in new pof nebula support into Fred.
105 * 16 4/07/99 6:22p Dave
106 * Fred and Freespace support for multiple background bitmaps and suns.
107 * Fixed link errors on all subprojects. Moved encrypt_init() to
108 * cfile_init() and lcl_init(), since its safe to call twice.
110 * 15 3/31/99 8:24p Dave
111 * Beefed up all kinds of stuff, incluging beam weapons, nebula effects
112 * and background nebulae. Added per-ship non-dimming pixel colors.
114 * 14 3/20/99 5:09p Dave
115 * Fixed release build fred warnings and unhandled exception.
117 * 13 3/20/99 3:46p Dave
118 * Added support for model-based background nebulae. Added 3 new
121 * 12 3/20/99 2:04p Dave
122 * Removed unnecessary planet rendering.
124 * 11 3/19/99 9:51a Dave
125 * Checkin to repair massive source safe crash. Also added support for
126 * pof-style nebulae, and some new weapons code.
128 * 12 3/15/99 6:45p Daveb
129 * Put in rough nebula bitmap support.
131 * 11 3/11/99 5:53p Dave
132 * More network optimization. Spliced in Dell OEM planet bitmap crap.
134 * 10 2/03/99 11:44a Dave
135 * Fixed d3d transparent textures.
137 * 9 12/09/98 7:34p Dave
138 * Cleanup up nebula effect. Tweaked many values.
140 * 8 12/01/98 10:32a Johnson
141 * Fixed direct3d font problems. Fixed sun bitmap problem. Fixed direct3d
144 * 7 12/01/98 8:06a Dave
145 * Temporary checkin to fix some texture transparency problems in d3d.
147 * 6 11/14/98 5:33p Dave
148 * Lots of nebula work. Put in ship contrails.
150 * 5 11/05/98 5:55p Dave
151 * Big pass at reducing #includes
153 * 4 10/13/98 9:29a Dave
154 * Started neatening up freespace.h. Many variables renamed and
155 * reorganized. Added AlphaColors.[h,cpp]
157 * 3 10/07/98 11:16a Dave
160 * 2 10/07/98 10:54a Dave
163 * 1 10/07/98 10:51a Dave
165 * 106 5/23/98 4:14p John
166 * Added code to preload textures to video card for AGP. Added in code
167 * to page in some bitmaps that weren't getting paged in at level start.
169 * 105 5/13/98 2:53p John
170 * Made subspace effect work under software. Had to add new inner loop to
171 * tmapper. Added glows to end of subspace effect. Made subspace effect
172 * levels use gamepalette-subspace palette.
174 * 104 5/13/98 10:28a John
175 * made subpsace forward sliding 40% faster.
177 * 103 5/10/98 4:18p John
178 * Reversed the subspace effect direction
180 * 102 5/08/98 8:38p John
181 * Subspace tweaks. Made two layers rotate independentyl.
183 * 101 5/08/98 1:32p John
184 * Added code for using two layered subspace effects.
186 * 100 5/06/98 5:30p John
187 * Removed unused cfilearchiver. Removed/replaced some unused/little used
188 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
189 * DirectX header files and libs that fixed the Direct3D alpha blending
192 * 99 4/22/98 4:09p John
193 * String externalization
195 * 98 4/22/98 3:28p John
198 * 97 4/13/98 4:54p John
199 * Made uv rotate independently on subspace effect. Put in DCF function
200 * for setting subspace speeds.
202 * 96 4/12/98 5:55p John
203 * Made models work with subspace. Made subspace rotate also.
205 * 95 4/11/98 6:53p John
206 * Added first rev of subspace effect.
208 * 94 4/08/98 11:31a Dave
209 * AL: Fix syntax error for non-demo
211 * 93 4/08/98 10:46a Lawrance
212 * #ifdef out asteroid check for demo
214 * 92 4/08/98 9:25a John
215 * Made asteroid missions not show suns
217 * 91 4/07/98 4:17p John
218 * Made Fred be able to move suns. Made suns actually affect the lighting
221 * 90 4/07/98 11:19a Hoffoss
222 * Changed code to only use Sun01 for a bitmap by default, as John
229 #include "floating.h"
233 #include "starfield.h"
236 #include "freespace.h"
239 #include "linklist.h"
240 #include "lighting.h"
241 #include "asteroid.h"
242 #include "missionparse.h"
244 #include "alphacolors.h"
245 #include "supernova.h"
247 #define MAX_DEBRIS_VCLIPS 4
248 #define DEBRIS_ROT_MIN 10000
249 #define DEBRIS_ROT_RANGE 8
250 #define DEBRIS_ROT_RANGE_SCALER 10000
251 #define RND_MAX_MASK 0x3fff
252 #define HALF_RND_MAX 0x2000
254 typedef struct debris_vclip {
268 const int MAX_DEBRIS = 200;
269 const int MAX_STARS = 2000;
270 const float MAX_DIST = 50.0f;
271 const float MAX_DIST_RANGE = 60.0f;
272 const float MIN_DIST_RANGE = 14.0f;
273 const float BASE_SIZE = 0.12f;
274 float BASE_SIZE_NEB = 0.5f;
276 static int Subspace_model_inner = -1;
277 static int Subspace_model_outer = -1;
280 fix starfield_timestamp = 0;
282 // for drawing cool stuff on the background - comes from a table
283 starfield_bitmap Starfield_bitmaps[MAX_STARFIELD_BITMAPS];
284 starfield_bitmap_instance Starfield_bitmap_instance[MAX_STARFIELD_BITMAPS];
285 int Num_starfield_bitmaps = 0;
287 // sun bitmaps and sun glow bitmaps
288 starfield_bitmap Sun_bitmaps[MAX_STARFIELD_BITMAPS];
289 starfield_bitmap_instance Suns[MAX_STARFIELD_BITMAPS];
292 int last_stars_filled = 0;
293 color star_colors[8];
294 color star_aacolors[8];
296 typedef struct star {
298 vector last_star_pos;
301 star Stars[MAX_STARS];
303 old_debris odebris[MAX_DEBRIS];
306 debris_vclip debris_vclips_normal[MAX_DEBRIS_VCLIPS] = { { -1, -1, "debris01" }, { -1, -1, "debris02" }, { -1, -1, "debris03" }, { -1, -1, "debris04" } };
307 debris_vclip debris_vclips_nebula[MAX_DEBRIS_VCLIPS] = { { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" } };
308 debris_vclip *debris_vclips = debris_vclips_normal;
311 int stars_debris_loaded = 0;
314 int Stars_background_inited = 0; // if we're inited
315 int Nmodel_num = -1; // model num
316 int Nmodel_bitmap = -1; // model texture
318 // given a starfield_bitmap_instance, return a pointer to its parent, for suns
319 starfield_bitmap *stars_lookup_sun(starfield_bitmap_instance *s)
329 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
330 if(!stricmp(Sun_bitmaps[idx].filename, s->filename)){
331 return &Sun_bitmaps[idx];
339 void stars_load_debris()
343 // if we're in nebula mode
344 if(The_mission.flags & MISSION_FLAG_FULLNEB){
345 debris_vclips = debris_vclips_nebula;
347 debris_vclips = debris_vclips_normal;
350 for (i=0; i<MAX_DEBRIS_VCLIPS; i++ ) {
351 debris_vclips[i].bm = bm_load_animation( debris_vclips[i].name, &debris_vclips[i].nframes, NULL, 1 );
352 if ( debris_vclips[i].bm < 0 ) {
353 // try loading it as a single bitmap
354 debris_vclips[i].bm = bm_load(debris_vclips[i].name);
355 debris_vclips[i].nframes = 1;
357 if(debris_vclips[i].bm <= 0){
358 Error( LOCATION, "Couldn't load animation/bitmap '%s'\n", debris_vclips[i].name );
362 stars_debris_loaded = 1;
365 // call on game startup
368 starfield_bitmap *bm;
370 char filename[MAX_FILENAME_LEN+1] = "";
371 char glow_filename[MAX_FILENAME_LEN+1] = "";
375 read_file_text("stars.tbl");
378 // make all bitmaps invalid
379 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
380 Starfield_bitmaps[idx].bitmap = -1;
381 Starfield_bitmaps[idx].glow_bitmap = -1;
382 strcpy(Starfield_bitmaps[idx].filename, "");
383 strcpy(Starfield_bitmaps[idx].glow_filename, "");
385 Sun_bitmaps[idx].bitmap = -1;
386 Sun_bitmaps[idx].glow_bitmap = -1;
387 strcpy(Sun_bitmaps[idx].filename, "");
388 strcpy(Sun_bitmaps[idx].glow_filename, "");
393 while(!optional_string("#end")){
394 // intensity alpha bitmap
395 if(optional_string("$Bitmap:")){
396 stuff_string(filename, F_NAME, NULL);
397 if(count < MAX_STARFIELD_BITMAPS){
398 bm = &Starfield_bitmaps[count++];
399 strcpy(bm->filename, filename);
401 bm->bitmap = bm_load(bm->filename);
402 Assert(bm->bitmap != -1);
404 // if fred is running we should lock the bitmap now
405 if(Fred_running && (bm->bitmap >= 0)){
406 bm_lock(bm->bitmap, 8, BMP_TEX_OTHER);
407 bm_unlock(bm->bitmap);
411 // green xparency bitmap
412 else if(optional_string("$BitmapX:")){
413 stuff_string(filename, F_NAME, NULL);
414 if(count < MAX_STARFIELD_BITMAPS){
415 bm = &Starfield_bitmaps[count++];
416 strcpy(bm->filename, filename);
418 bm->bitmap = bm_load(bm->filename);
419 Assert(bm->bitmap != -1);
421 // if fred is running we should lock as a 0, 255, 0 bitmap now
422 if(Fred_running && (bm->bitmap >= 0)){
423 bm_lock(bm->bitmap, 8, BMP_TEX_XPARENT);
424 bm_unlock(bm->bitmap);
432 while(!optional_string("#end")){
433 if(optional_string("$Sun:")){
434 stuff_string(filename, F_NAME, NULL);
437 required_string("$Sunglow:");
438 stuff_string(glow_filename, F_NAME, NULL);
440 // associated lighting values
441 required_string("$SunRGBI:");
447 if(count < MAX_STARFIELD_BITMAPS){
448 bm = &Sun_bitmaps[count++];
449 strcpy(bm->filename, filename);
450 strcpy(bm->glow_filename, glow_filename);
452 bm->bitmap = bm_load(bm->filename);
453 bm->glow_bitmap = bm_load(bm->glow_filename);
454 Assert(bm->bitmap != -1);
455 Assert(bm->glow_bitmap != -1);
461 // if fred is running we should lock the bitmap now
464 bm_lock(bm->bitmap, 8, BMP_TEX_OTHER);
465 bm_unlock(bm->bitmap);
467 if(bm->glow_bitmap >= 0){
468 bm_lock(bm->glow_bitmap, 8, BMP_TEX_OTHER);
469 bm_unlock(bm->glow_bitmap);
476 // normal debris pieces
478 while(!optional_string("#end")){
479 required_string("$Debris:");
480 stuff_string(filename, F_NAME, NULL);
482 if(count < MAX_DEBRIS_VCLIPS){
483 strcpy(debris_vclips_normal[count++].name, filename);
488 // nebula debris pieces
490 while(!optional_string("#end")){
491 required_string("$DebrisNeb:");
492 stuff_string(filename, F_NAME, NULL);
494 if(count < MAX_DEBRIS_VCLIPS){
495 strcpy(debris_vclips_nebula[count++].name, filename);
498 #ifndef MAKE_FS1 // string not used in FS1
503 // call this in game_post_level_init() so we know whether we're running in full nebula mode or not
504 void stars_level_init()
508 float dist, dist_max;
510 // reset to -1 so we reload it each mission (if we need to)
512 if(Nmodel_bitmap != -1){
513 bm_unload(Nmodel_bitmap);
517 // if (!stars_debris_loaded){
521 // following code randomly distributes star points within a sphere volume, which
522 // avoids there being denser areas along the edges and in corners that we had in the
523 // old rectangular distribution scheme.
524 dist_max = (float) (HALF_RND_MAX * HALF_RND_MAX);
525 for (i=0; i<MAX_STARS; i++) {
527 while (dist >= dist_max) {
528 v.xyz.x = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
529 v.xyz.y = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
530 v.xyz.z = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
532 dist = v.xyz.x * v.xyz.x + v.xyz.y * v.xyz.y + v.xyz.z * v.xyz.z;
534 vm_vec_copy_normalize(&Stars[i].pos, &v);
537 for (i=0; i<MAX_DEBRIS; i++) {
538 odebris[i].active = 0;
541 for (i=0; i<8; i++ ) {
542 ubyte intensity = (ubyte)((i + 1) * 24);
543 gr_init_alphacolor(&star_aacolors[i], 255, 255, 255, intensity, AC_TYPE_BLEND );
544 gr_init_color(&star_colors[i], intensity, intensity, intensity );
547 last_stars_filled = 0;
549 #ifndef MAKE_FS1 // some FS1 missions don't have a sun
550 // if we have no sun instances, create one
552 mprintf(("Adding default sun\n"));
555 strcpy(Suns[0].filename, Sun_bitmaps[0].filename);
556 Suns[0].scale_x = 1.0f;
557 Suns[0].scale_y = 1.0f;
560 memset(&Suns[0].ang, 0, sizeof(angles));
561 Suns[0].ang.h = fl_radian(60.0f);
571 extern object * Player_obj;
573 #define STAR_AMOUNT_DEFAULT 0.75f
574 #define STAR_DIM_DEFAULT 7800.0f
575 #define STAR_CAP_DEFAULT 75.0f
576 #define STAR_MAX_LENGTH_DEFAULT 0.04f // 312
578 float Star_amount = STAR_AMOUNT_DEFAULT;
579 float Star_dim = STAR_DIM_DEFAULT;
580 float Star_cap = STAR_CAP_DEFAULT;
581 float Star_max_length = STAR_MAX_LENGTH_DEFAULT;
583 #define STAR_FLAG_TAIL (1<<0) // Draw a tail when moving
584 #define STAR_FLAG_DIM (1<<1) // Dim as you move
585 #define STAR_FLAG_ANTIALIAS (1<<2) // Draw the star using antialiased lines
586 #define STAR_FLAG_DEFAULT (STAR_FLAG_TAIL | STAR_FLAG_DIM)
588 uint Star_flags = STAR_FLAG_DEFAULT;
591 DCF(stars,"Set parameters for starfield")
594 dc_get_arg(ARG_STRING);
595 if ( !strcmp( Dc_arg, "tail" )) {
596 dc_get_arg(ARG_FLOAT);
597 if ( (Dc_arg_float < 0.0f) || (Dc_arg_float > 1.0f) ) {
600 Star_amount = Dc_arg_float;
602 } else if ( !strcmp( Dc_arg, "len" )) {
603 dc_get_arg(ARG_FLOAT);
604 Star_max_length = Dc_arg_float;
605 } else if ( !strcmp( Dc_arg, "dim" )) {
606 dc_get_arg(ARG_FLOAT);
607 if ( Dc_arg_float < 0.0f ) {
610 Star_dim = Dc_arg_float;
612 } else if ( !strcmp( Dc_arg, "flag" )) {
613 dc_get_arg(ARG_STRING);
614 if ( !strcmp( Dc_arg, "tail" )) {
615 Star_flags ^= STAR_FLAG_TAIL;
616 } else if ( !strcmp( Dc_arg, "dim" )) {
617 Star_flags ^= STAR_FLAG_DIM;
618 } else if ( !strcmp( Dc_arg, "aa" )) {
619 Star_flags ^= STAR_FLAG_ANTIALIAS;
623 } else if ( !strcmp( Dc_arg, "cap" )) {
624 dc_get_arg(ARG_FLOAT);
625 if ( (Dc_arg_float < 0.0f) || (Dc_arg_float > 255.0f) ) {
628 Star_cap = Dc_arg_float;
630 } else if ( !strcmp( Dc_arg, "m0" ) ) {
635 Star_max_length = STAR_MAX_LENGTH_DEFAULT;
636 } else if ( !strcmp( Dc_arg, "m1" ) || !strcmp( Dc_arg, "default" )) {
637 Star_amount = STAR_AMOUNT_DEFAULT;
638 Star_dim = STAR_DIM_DEFAULT;
639 Star_cap = STAR_CAP_DEFAULT;
640 Star_flags = STAR_FLAG_DEFAULT;
641 Star_max_length = STAR_MAX_LENGTH_DEFAULT;
642 } else if ( !strcmp( Dc_arg, "m2" )) {
646 Star_flags = STAR_FLAG_TAIL|STAR_FLAG_DIM|STAR_FLAG_ANTIALIAS;
647 Star_max_length = STAR_MAX_LENGTH_DEFAULT;
648 } else if ( !strcmp( Dc_arg, "num" )) {
650 if ( (Dc_arg_int < 0) || (Dc_arg_int > MAX_STARS) ) {
653 Num_stars = Dc_arg_int;
656 // print usage, not stats
662 dc_printf( "Usage: stars keyword\nWhere keyword can be in the following forms:\n" );
663 dc_printf( "stars default Resets stars to all default values\n" );
664 dc_printf( "stars num X Sets number of stars to X. Between 0 and %d.\n", MAX_STARS );
665 dc_printf( "stars tail X Where X is the percent of 'tail' between 0 and 1.0\n" );
666 dc_printf( "stars dim X Where X is the amount stars dim between 0 and 255.0\n" );
667 dc_printf( "stars cap X Where X is the cap of dimming between 0 and 255.\n" );
668 dc_printf( "stars len X Where X is the cap of length.\n" );
669 dc_printf( "stars m0 Macro0. Old 'pixel type' crappy stars. flags=none\n" );
670 dc_printf( "stars m1 Macro1. (default) tail=.75, dim=20.0, cap=75.0, flags=dim,tail\n" );
671 dc_printf( "stars m2 Macro2. tail=.75, dim=20.0, cap=75.0, flags=dim,tail,aa\n" );
672 dc_printf( "stars flag X Toggles flag X, where X is tail or dim or aa (aa=antialias)\n" );
673 dc_printf( "\nHINT: set cap to 0 to get dim rate and tail down, then use\n" );
674 dc_printf( "cap to keep the lines from going away when moving too fast.\n" );
675 dc_printf( "\nUse '? stars' to see current values.\n" );
676 Dc_status = 0; // don't print status if help is printed. Too messy.
680 dc_printf( "Num_stars: %d\n", Num_stars );
681 dc_printf( "Tail: %.2f\n", Star_amount );
682 dc_printf( "Dim: %.2f\n", Star_dim );
683 dc_printf( "Cap: %.2f\n", Star_cap );
684 dc_printf( "Max length: %.2f\n", Star_max_length );
685 dc_printf( "Flags:\n" );
686 dc_printf( " Tail: %s\n", (Star_flags&STAR_FLAG_TAIL?"On":"Off") );
687 dc_printf( " Dim: %s\n", (Star_flags&STAR_FLAG_DIM?"On":"Off") );
688 dc_printf( " Antialias: %s\n", (Star_flags&STAR_FLAG_ANTIALIAS?"On":"Off") );
689 dc_printf( "\nTHESE AREN'T SAVED TO DISK, SO IF YOU TWEAK\n" );
690 dc_printf( "THESE AND LIKE THEM, WRITE THEM DOWN!!\n" );
695 int reload_old_debris = 1; // If set to one, then reload all the last_pos of the debris
697 // Call this if camera "cuts" or moves long distances
698 // so blur effect doesn't draw lines all over the screen.
699 void stars_camera_cut()
701 last_stars_filled = 0;
702 reload_old_debris = 1;
705 //#define TIME_STAR_CODE // enable to time star code
708 extern float Viewer_zoom;
710 // get the world coords of the sun pos on the unit sphere.
711 void stars_get_sun_pos(int sun_n, vector *pos)
719 Assert(sun_n < Num_suns);
720 if((sun_n >= Num_suns) || (sun_n < 0)){
724 // rotate the sun properly
725 temp = vmd_zero_vector;
730 // we aleady know what the matrix is so just rotate
731 vm_vec_rotate(pos, &temp, &Suns[sun_n].m);
733 vm_angles_2_matrix(&rot, &Suns[sun_n].ang);
734 vm_vec_rotate(pos, &temp, &rot);
739 void stars_draw_sun( int show_sun )
745 starfield_bitmap *bm;
746 float local_scale = 1.0f;
752 for(idx=0; idx<Num_suns; idx++){
754 bm = stars_lookup_sun(&Suns[idx]);
760 sun_pos = vmd_zero_vector;
761 sun_pos.xyz.y = 1.0f;
762 stars_get_sun_pos(idx, &sun_pos);
766 vm_vec_normalize(&sun_dir);
768 // add the light source corresponding to the sun
769 light_add_directional(&sun_dir, bm->i, bm->r, bm->g, bm->b);
772 if(supernova_active()){
773 local_scale = 1.0f + (SUPERNOVA_SUN_SCALE * supernova_pct_complete());
776 // draw the sun itself, keep track of how many we drew
777 gr_set_bitmap(bm->bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.999f);
778 g3_rotate_faraway_vertex(&sun_vex, &sun_pos);
780 // divide by 10 to make normal size
781 if(!g3_draw_bitmap(&sun_vex, 0, 0.05f * (Suns[idx].scale_x / 10) * local_scale, TMAP_FLAG_TEXTURED)){
783 if(!g3_draw_bitmap(&sun_vex, 0, 0.05f * Suns[idx].scale_x * local_scale, TMAP_FLAG_TEXTURED)){
790 // draw the corresponding glow for sun_n
791 void stars_draw_sun_glow(int sun_n)
793 starfield_bitmap *bm;
794 vector sun_pos, sun_dir;
796 float local_scale = 1.0f;
799 Assert(sun_n < Num_suns);
800 if((sun_n >= Num_suns) || (sun_n < 0)){
805 bm = stars_lookup_sun(&Suns[sun_n]);
811 sun_pos = vmd_zero_vector;
812 sun_pos.xyz.y = 1.0f;
813 stars_get_sun_pos(sun_n, &sun_pos);
817 vm_vec_normalize(&sun_dir);
820 if(supernova_active()){
821 local_scale = 1.0f + (SUPERNOVA_SUN_SCALE * supernova_pct_complete());
824 // draw the sun itself, keep track of how many we drew
825 gr_set_bitmap(bm->glow_bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.5f);
826 g3_rotate_faraway_vertex(&sun_vex, &sun_pos);
828 // divide by 10 to make normal size
829 g3_draw_bitmap(&sun_vex, 0, 0.10f * (Suns[sun_n].scale_x / 10) * local_scale, TMAP_FLAG_TEXTURED);
831 g3_draw_bitmap(&sun_vex, 0, 0.10f * Suns[sun_n].scale_x * local_scale, TMAP_FLAG_TEXTURED);
836 void stars_draw_bitmaps( int show_bitmaps )
841 // if we're in the nebula, don't render any backgrounds
842 if(The_mission.flags & MISSION_FLAG_FULLNEB){
847 if(!Detail.planets_suns){
851 // render all bitmaps
852 for(idx=0; idx<Num_starfield_bitmaps; idx++){
853 // lookup the info index
854 star_index = stars_find_bitmap(Starfield_bitmap_instance[idx].filename);
861 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.9999f);
862 g3_draw_perspective_bitmap(&Starfield_bitmap_instance[idx].ang, Starfield_bitmap_instance[idx].scale_x, Starfield_bitmap_instance[idx].scale_y, Starfield_bitmap_instance[idx].div_x, Starfield_bitmap_instance[idx].div_y, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
864 if(Starfield_bitmaps[star_index].xparent){
865 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap);
866 g3_draw_perspective_bitmap(&Starfield_bitmap_instance[idx].ang, Starfield_bitmap_instance[idx].scale_x, Starfield_bitmap_instance[idx].scale_y, Starfield_bitmap_instance[idx].div_x, Starfield_bitmap_instance[idx].div_y, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT | TMAP_FLAG_XPARENT);
868 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.9999f);
869 g3_draw_perspective_bitmap(&Starfield_bitmap_instance[idx].ang, Starfield_bitmap_instance[idx].scale_x, Starfield_bitmap_instance[idx].scale_y, Starfield_bitmap_instance[idx].div_x, Starfield_bitmap_instance[idx].div_y, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
876 void calculate_bitmap_matrix(starfield_bitmaps *bm, vector *v)
878 vm_vector_2_matrix(&bm->m, v, NULL, NULL);
879 vm_orthogonalize_matrix(&bm->m);
882 void calculate_bitmap_points(starfield_bitmaps *bm, float bank)
885 vector fvec, uvec, rvec, tmp;
888 vm_orthogonalize_matrix(&bm->m);
890 tangles.p = tangles.h = 0.0f;
892 vm_rotate_matrix_by_angles(&bm->m, &tangles);
896 vm_vec_scale(&fvec, bm->dist );
900 vm_vec_sub(&tmp, &fvec, &uvec);
901 vm_vec_sub(&bm->points[3], &tmp, &rvec);
903 vm_vec_sub(&tmp, &fvec, &uvec);
904 vm_vec_add(&bm->points[2], &tmp, &rvec);
906 vm_vec_add(&tmp, &fvec, &uvec);
907 vm_vec_add(&bm->points[1], &tmp, &rvec);
909 vm_vec_add(&tmp, &fvec, &uvec);
910 vm_vec_sub(&bm->points[0], &tmp, &rvec);
913 vm_vec_normalize(&bm->points[i]);
918 extern int Interp_subspace;
919 extern float Interp_subspace_offset_u;
920 extern float Interp_subspace_offset_u;
921 extern float Interp_subspace_offset_v;
923 float subspace_offset_u = 0.0f;
924 float subspace_offset_u_inner = 0.0f;
925 float subspace_offset_v = 0.0f;
927 float subspace_u_speed = 0.07f; // how fast u changes
928 float subspace_v_speed = 0.05f; // how fast v changes
930 int Subspace_glow_bitmap = -1;
932 float Subspace_glow_frame = 0.0f;
933 float Subspace_glow_rate = 1.0f;
937 DCF(subspace_set,"Set parameters for subspace effect")
940 dc_get_arg(ARG_STRING);
941 if ( !strcmp( Dc_arg, "u" )) {
942 dc_get_arg(ARG_FLOAT);
943 if ( Dc_arg_float < 0.0f ) {
946 subspace_u_speed = Dc_arg_float;
948 } else if ( !strcmp( Dc_arg, "v" )) {
949 dc_get_arg(ARG_FLOAT);
950 if ( Dc_arg_float < 0.0f ) {
953 subspace_v_speed = Dc_arg_float;
956 // print usage, not stats
962 dc_printf( "Usage: subspace keyword\nWhere keyword can be in the following forms:\n" );
963 dc_printf( "subspace u X Where X is how fast u moves.\n", MAX_STARS );
964 dc_printf( "subspace v X Where X is how fast v moves.\n" );
965 dc_printf( "\nUse '? subspace' to see current values.\n" );
966 Dc_status = 0; // don't print status if help is printed. Too messy.
970 dc_printf( "u: %.2f\n", subspace_u_speed );
971 dc_printf( "v: %.2f\n", subspace_v_speed );
976 void subspace_render()
978 if ( Subspace_model_inner == -1 ) {
979 Subspace_model_inner = model_load( "subspace_small.pof", 0, NULL );
980 Assert(Subspace_model_inner>-1);
983 if ( Subspace_model_outer == -1 ) {
984 Subspace_model_outer = model_load( "subspace_big.pof", 0, NULL );
985 Assert(Subspace_model_outer>-1);
988 if ( Subspace_glow_bitmap == -1 ) {
989 Subspace_glow_bitmap = bm_load( NOX("SunGlow01"));
990 Assert(Subspace_glow_bitmap>-1);
993 Subspace_glow_frame += flFrametime * 1.0f;
995 float total_time = i2fl(NOISE_NUM_FRAMES) / 15.0f;
998 if ( Subspace_glow_frame < 0.0f ) Subspace_glow_frame = 0.0f;
999 if ( Subspace_glow_frame > 100.0f ) Subspace_glow_frame = 0.0f;
1001 while ( Subspace_glow_frame > total_time ) {
1002 Subspace_glow_frame -= total_time;
1004 int framenum = fl2i( (Subspace_glow_frame*NOISE_NUM_FRAMES) / total_time );
1005 if ( framenum < 0 ) framenum = 0;
1006 if ( framenum >= NOISE_NUM_FRAMES ) framenum = NOISE_NUM_FRAMES-1;
1008 subspace_offset_u += flFrametime*subspace_u_speed;
1009 if (subspace_offset_u > 1.0f ) {
1010 subspace_offset_u -= 1.0f;
1013 subspace_offset_u_inner += flFrametime*subspace_u_speed*3.0f;
1014 if (subspace_offset_u > 1.0f ) {
1015 subspace_offset_u -= 1.0f;
1018 subspace_offset_v += flFrametime*subspace_v_speed;
1019 if (subspace_offset_v > 1.0f ) {
1020 subspace_offset_v -= 1.0f;
1026 angles angs = { 0.0f, 0.0f, 0.0f };
1027 angs.b = subspace_offset_v * PI2;
1029 vm_angles_2_matrix(&tmp,&angs);
1031 int saved_gr_zbuffering = gr_zbuffer_get();
1033 gr_zbuffer_set(GR_ZBUFF_NONE);
1035 if ( !D3D_enabled ) {
1037 int render_flags = MR_NO_LIGHTING | MR_ALWAYS_REDRAW;
1039 Interp_subspace = 1;
1040 Interp_subspace_offset_u = 1.0f - subspace_offset_u;
1041 Interp_subspace_offset_v = 0.0f;
1043 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1044 render_flags |= MR_SHOW_THRUSTERS;
1045 model_render( Subspace_model_outer, &tmp, &Eye_position, render_flags ); //MR_NO_CORRECT|MR_SHOW_OUTLINE
1049 int render_flags = MR_NO_LIGHTING | MR_ALWAYS_REDRAW;
1051 Interp_subspace = 1;
1052 Interp_subspace_offset_u = 1.0f - subspace_offset_u;
1053 Interp_subspace_offset_v = 0.0f;
1055 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1056 render_flags |= MR_SHOW_THRUSTERS;
1057 model_render( Subspace_model_outer, &tmp, &Eye_position, render_flags ); //MR_NO_CORRECT|MR_SHOW_OUTLINE
1059 Interp_subspace = 1;
1060 Interp_subspace_offset_u = 1.0f - subspace_offset_u_inner;
1061 Interp_subspace_offset_v = 0.0f;
1063 angs.b = -subspace_offset_v * PI2;
1065 vm_angles_2_matrix(&tmp,&angs);
1067 model_set_outline_color(255,255,255);
1069 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1070 render_flags |= MR_SHOW_THRUSTERS;
1072 model_render( Subspace_model_inner, &tmp, &Eye_position, render_flags ); //MR_NO_CORRECT|MR_SHOW_OUTLINE
1075 Interp_subspace = 0;
1076 gr_zbuffer_set(saved_gr_zbuffering);
1079 void stars_draw( int show_stars, int show_suns, int show_nebulas, int show_subspace )
1085 int gr_zbuffering_save = gr_zbuffer_get();
1086 gr_zbuffer_set(GR_ZBUFF_NONE);
1088 if ( show_subspace ) {
1093 if (Num_stars >= MAX_STARS){
1094 Num_stars = MAX_STARS;
1097 #ifdef TIME_STAR_CODE
1099 xt1 = timer_get_fixed_seconds();
1102 if ( show_nebulas && (Game_detail_flags & DETAIL_FLAG_NEBULAS) && (Neb2_render_mode != NEB2_RENDER_POF) && (Neb2_render_mode != NEB2_RENDER_LAME)) {
1106 // draw background stuff
1107 if((Neb2_render_mode != NEB2_RENDER_POLY) && (Neb2_render_mode != NEB2_RENDER_LAME) && show_stars){
1108 // semi-hack, do we don't fog the background
1109 int neb_save = Neb2_render_mode;
1110 Neb2_render_mode = NEB2_RENDER_NONE;
1111 extern void stars_draw_background();
1112 stars_draw_background();
1113 Neb2_render_mode = neb_save;
1116 if (show_stars && ( Game_detail_flags & DETAIL_FLAG_STARS) && !(The_mission.flags & MISSION_FLAG_FULLNEB) && (supernova_active() < 3)) {
1121 if ( !last_stars_filled ) {
1122 for (sp=Stars,i=0; i<Num_stars; i++, sp++ ) {
1124 g3_rotate_faraway_vertex(&p2, &sp->pos);
1125 sp->last_star_pos.xyz.x = p2.x;
1126 sp->last_star_pos.xyz.y = p2.y;
1127 sp->last_star_pos.xyz.z = p2.z;
1133 tmp_num_stars = (Detail.num_stars*Num_stars)/MAX_DETAIL_LEVEL;
1134 if (tmp_num_stars < 0 ) {
1136 } else if ( tmp_num_stars > Num_stars ) {
1137 tmp_num_stars = Num_stars;
1140 for (sp=Stars,i=0; i<tmp_num_stars; i++, sp++ ) {
1144 memset(&p1, 0, sizeof(vertex));
1146 // This makes a star look "proper" by not translating the
1147 // point around the viewer's eye before rotation. In other
1148 // words, when the ship translates, the stars do not change.
1150 g3_rotate_faraway_vertex(&p2, &sp->pos);
1154 g3_project_vertex(&p2);
1155 if ( p2.flags & PF_OVERFLOW ) {
1162 if ( can_draw && (Star_flags & (STAR_FLAG_TAIL|STAR_FLAG_DIM)) ) {
1164 dist = vm_vec_dist_quick( &sp->last_star_pos, (vector *)&p2.x );
1167 if ( dist > Star_max_length ) {
1168 ratio = Star_max_length / dist;
1169 dist = Star_max_length;
1173 ratio *= Star_amount;
1175 p1.x = p2.x + (sp->last_star_pos.xyz.x-p2.x)*ratio;
1176 p1.y = p2.y + (sp->last_star_pos.xyz.y-p2.y)*ratio;
1177 p1.z = p2.z + (sp->last_star_pos.xyz.z-p2.z)*ratio;
1179 p1.flags = 0; // not projected
1180 g3_code_vertex( &p1 );
1185 g3_project_vertex(&p1);
1186 if ( p1.flags & PF_OVERFLOW ) {
1192 sp->last_star_pos.xyz.x = p2.x;
1193 sp->last_star_pos.xyz.y = p2.y;
1194 sp->last_star_pos.xyz.z = p2.z;
1196 if ( !can_draw ) continue;
1200 if ( Star_flags & STAR_FLAG_DIM ) {
1202 float colorf = 255.0f - dist*Star_dim;
1204 if ( colorf < Star_cap )
1207 color = (fl2i(colorf)*(i&7))/256;
1213 if ( (Star_flags & STAR_FLAG_ANTIALIAS) || (D3D_enabled) ) {
1214 gr_set_color_fast( &star_aacolors[color] );
1216 // if the two points are the same, fudge it, since some D3D cards (G200 and G400) are lame.
1217 if( (fl2i(p1.sx) == fl2i(p2.sx)) && (fl2i(p1.sy) == fl2i(p2.sy)) ){
1222 // use alphablended line so that dark stars don't look bad on top of nebulas
1223 gr_set_color_fast( &star_aacolors[color] );
1224 if ( Star_flags & STAR_FLAG_TAIL ) {
1225 gr_line(fl2i(p1.sx),fl2i(p1.sy),fl2i(p2.sx),fl2i(p2.sy));
1227 gr_pixel( fl2i(p2.sx),fl2i(p2.sy) );
1233 last_stars_filled = 1;
1235 #ifdef TIME_STAR_CODE
1236 xt2 = timer_get_fixed_seconds();
1237 mprintf(( "Stars: %d\n", xt2-xt1 ));
1241 if ( (Game_detail_flags & DETAIL_FLAG_MOTION) && (!Fred_running) && (supernova_active() < 3) ) {
1243 gr_set_color( 0, 0, 0 );
1246 if(The_mission.flags & MISSION_FLAG_FULLNEB){
1247 gr_fog_set(GR_FOGMODE_NONE, 0, 0, 0);
1250 old_debris * d = odebris;
1251 for (i=0; i<MAX_DEBRIS; i++, d++ ) {
1255 d->pos.xyz.x = f2fl(myrand() - RAND_MAX/2);
1256 d->pos.xyz.y = f2fl(myrand() - RAND_MAX/2);
1257 d->pos.xyz.z = f2fl(myrand() - RAND_MAX/2);
1259 vm_vec_normalize(&d->pos);
1261 vm_vec_scale(&d->pos, MAX_DIST);
1262 vm_vec_add2(&d->pos, &Eye_position );
1263 // vm_vec_add2(&d->pos, &Player_obj->pos );
1265 d->vclip = i % MAX_DEBRIS_VCLIPS; //rand()
1267 // if we're in full neb mode
1268 if((The_mission.flags & MISSION_FLAG_FULLNEB) && (Neb2_render_mode != NEB2_RENDER_NONE)){
1269 d->size = i2fl(myrand() % 4)*BASE_SIZE_NEB;
1271 d->size = i2fl(myrand() % 4)*BASE_SIZE;
1274 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1277 if ( reload_old_debris ) {
1278 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1281 g3_rotate_vertex(&p, &d->pos);
1284 int frame = Missiontime / (DEBRIS_ROT_MIN + (i % DEBRIS_ROT_RANGE) * DEBRIS_ROT_RANGE_SCALER);
1285 frame %= debris_vclips[d->vclip].nframes;
1287 if((The_mission.flags & MISSION_FLAG_FULLNEB) && (Neb2_render_mode != NEB2_RENDER_NONE)){
1288 gr_set_bitmap( debris_vclips[d->vclip].bm + frame, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.3f);
1290 gr_set_bitmap( debris_vclips[d->vclip].bm + frame );
1294 vm_vec_add( &tmp, &d->last_pos, &Eye_position );
1295 g3_draw_laser( &d->pos,d->size,&tmp,d->size, TMAP_FLAG_TEXTURED|TMAP_FLAG_XPARENT, 25.0f );
1298 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1300 vdist = vm_vec_mag_quick(&d->last_pos);
1302 if (vdist > MAX_DIST_RANGE)
1304 else if (vdist < MIN_DIST_RANGE)
1308 // vm_vec_sub( &tmp, &d->pos, &Player_obj->pos );
1309 // vdist = vm_vec_dot( &tmp, &Player_obj->orient.fvec );
1310 // if ( vdist < 0.0f )
1315 reload_old_debris = 0;
1319 stars_draw_sun( show_suns );
1320 stars_draw_bitmaps( show_suns );
1322 gr_zbuffer_set( gr_zbuffering_save );
1325 void stars_page_in()
1329 // Initialize the subspace stuff
1331 if ( Game_subspace_effect ) {
1333 Subspace_model_inner = model_load( "subspace_small.pof", 0, NULL );
1334 Assert(Subspace_model_inner>-1);
1335 Subspace_model_outer = model_load( "subspace_big.pof", 0, NULL );
1336 Assert(Subspace_model_outer>-1);
1340 pm = model_get(Subspace_model_inner);
1342 nprintf(( "Paging", "Paging in textures for subspace effect.\n" ));
1344 for (j=0; j<pm->n_textures; j++ ) {
1345 int bitmap_num = pm->original_textures[j];
1347 if ( bitmap_num > -1 ) {
1348 bm_page_in_texture( bitmap_num );
1352 pm = model_get(Subspace_model_outer);
1354 nprintf(( "Paging", "Paging in textures for subspace effect.\n" ));
1356 for (j=0; j<pm->n_textures; j++ ) {
1357 int bitmap_num = pm->original_textures[j];
1359 if ( bitmap_num > -1 ) {
1360 bm_page_in_texture( bitmap_num );
1364 Subspace_model_inner = -1;
1365 Subspace_model_outer = -1;
1368 Subspace_glow_bitmap = bm_load( NOX("SunGlow01"));
1369 bm_page_in_xparent_texture(Subspace_glow_bitmap);
1371 // page in starfield bitmaps
1374 while((idx < MAX_STARFIELD_BITMAPS) && (Starfield_bitmaps[idx].bitmap != -1)){
1375 // make sure it's used in this mission before loading
1376 for (t=0; t<Num_starfield_bitmaps; t++) {
1377 if (!stricmp(Starfield_bitmaps[idx].filename, Starfield_bitmap_instance[t].filename)) {
1378 if(Starfield_bitmaps[idx].xparent){
1379 bm_page_in_xparent_texture(Starfield_bitmaps[idx].bitmap);
1381 bm_page_in_texture(Starfield_bitmaps[idx].bitmap);
1390 // sun bitmaps and glows
1392 while((idx < MAX_STARFIELD_BITMAPS) && (Sun_bitmaps[idx].bitmap != -1) && (Sun_bitmaps[idx].glow_bitmap != -1)){
1393 // make sure it's used in this mission before loading
1394 for (t=0; t<Num_suns; t++) {
1395 if (!stricmp(Sun_bitmaps[idx].filename, Suns[t].filename)) {
1396 bm_page_in_texture(Sun_bitmaps[idx].bitmap);
1397 bm_page_in_texture(Sun_bitmaps[idx].glow_bitmap);
1405 for (i=0; i<MAX_DEBRIS_VCLIPS; i++ ) {
1406 for (j=0; j<debris_vclips[i].nframes; j++ ) {
1407 bm_page_in_xparent_texture(debris_vclips[i].bm + j);
1412 // background nebula models and planets
1413 void stars_draw_background()
1415 if((Nmodel_num < 0) || (Nmodel_bitmap < 0)){
1419 // draw the model at the player's eye wif no z-buffering
1420 model_set_alpha(1.0f);
1421 model_set_forced_texture(Nmodel_bitmap);
1422 model_render(Nmodel_num, &vmd_identity_matrix, &Eye_position, MR_NO_ZBUFFER | MR_NO_CULL | MR_ALL_XPARENT | MR_NO_LIGHTING | MR_FORCE_TEXTURE);
1423 model_set_forced_texture(-1);
1426 // call this to set a specific model as the background model
1427 void stars_set_background_model(char *model_name, char *texture_name)
1429 Nmodel_num = model_load(model_name, 0, NULL);
1430 Nmodel_bitmap = bm_load(texture_name);
1433 // lookup a starfield bitmap, return index or -1 on fail
1434 int stars_find_bitmap(char *name)
1439 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
1440 if(!strcmp(name, Starfield_bitmaps[idx].filename)){
1449 // lookup a sun by bitmap filename, return index or -1 on fail
1450 int stars_find_sun(char *name)
1455 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
1456 if(!strcmp(name, Sun_bitmaps[idx].filename)){