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.7 2003/05/25 02:30:44 taylor
22 * Revision 1.6 2002/06/22 23:57:39 relnev
23 * remove writable strings.
25 * fix compile for intel compiler.
27 * Revision 1.5 2002/06/17 06:33:11 relnev
28 * ryan's struct patch for gcc 2.95
30 * Revision 1.4 2002/06/09 04:41:27 relnev
31 * added copyright header
33 * Revision 1.3 2002/06/02 04:26:34 relnev
36 * Revision 1.2 2002/05/07 03:16:52 theoddone33
37 * The Great Newline Fix
39 * Revision 1.1.1.1 2002/05/03 03:28:10 root
43 * 34 9/07/99 4:01p Dave
44 * Fixed up a string.tbl paroblem (self destruct message). Make sure IPX
45 * does everything properly (setting up address when binding). Remove
46 * black rectangle background from UI_INPUTBOX.
48 * 33 9/01/99 10:14a Dave
51 * 32 8/30/99 5:01p Dave
52 * Made d3d do less state changing in the nebula. Use new chat server for
55 * 31 8/19/99 10:59a Dave
56 * Packet loss detection.
58 * 30 7/27/99 3:52p Dave
59 * Make star drawing a bit more robust to help lame D3D cards.
61 * 29 7/21/99 8:10p Dave
62 * First run of supernova effect.
64 * 28 7/13/99 2:01p Dave
65 * Don't draw background bitmaps in the nebula.
67 * 27 6/08/99 2:34p Jasenw
68 * Made perspective bitmaps render in Fred.
70 * 26 6/04/99 1:18p Dave
71 * Fixed briefing model rendering problems. Made show background option in
72 * fred toggle nebula rendering.
74 * 25 6/03/99 6:37p Dave
75 * More TNT fun. Made perspective bitmaps more flexible.
77 * 24 5/28/99 1:45p Dave
78 * Fixed up perspective bitmap drawing.
80 * 23 5/20/99 7:00p Dave
81 * Added alternate type names for ships. Changed swarm missile table
84 * 22 5/11/99 10:03a Dave
85 * Put a bunch of stuff into tables.
87 * 21 5/11/99 9:10a Dave
88 * Move default sun position.
90 * 20 5/09/99 6:00p Dave
91 * Lots of cool new effects. E3 build tweaks.
93 * 19 4/26/99 8:49p Dave
94 * Made all pof based nebula stuff full customizable through fred.
96 * 18 4/25/99 7:43p Dave
97 * Misc small bug fixes. Made sun draw properly.
99 * 17 4/23/99 5:53p Dave
100 * Started putting in new pof nebula support into Fred.
102 * 16 4/07/99 6:22p Dave
103 * Fred and Freespace support for multiple background bitmaps and suns.
104 * Fixed link errors on all subprojects. Moved encrypt_init() to
105 * cfile_init() and lcl_init(), since its safe to call twice.
107 * 15 3/31/99 8:24p Dave
108 * Beefed up all kinds of stuff, incluging beam weapons, nebula effects
109 * and background nebulae. Added per-ship non-dimming pixel colors.
111 * 14 3/20/99 5:09p Dave
112 * Fixed release build fred warnings and unhandled exception.
114 * 13 3/20/99 3:46p Dave
115 * Added support for model-based background nebulae. Added 3 new
118 * 12 3/20/99 2:04p Dave
119 * Removed unnecessary planet rendering.
121 * 11 3/19/99 9:51a Dave
122 * Checkin to repair massive source safe crash. Also added support for
123 * pof-style nebulae, and some new weapons code.
125 * 12 3/15/99 6:45p Daveb
126 * Put in rough nebula bitmap support.
128 * 11 3/11/99 5:53p Dave
129 * More network optimization. Spliced in Dell OEM planet bitmap crap.
131 * 10 2/03/99 11:44a Dave
132 * Fixed d3d transparent textures.
134 * 9 12/09/98 7:34p Dave
135 * Cleanup up nebula effect. Tweaked many values.
137 * 8 12/01/98 10:32a Johnson
138 * Fixed direct3d font problems. Fixed sun bitmap problem. Fixed direct3d
141 * 7 12/01/98 8:06a Dave
142 * Temporary checkin to fix some texture transparency problems in d3d.
144 * 6 11/14/98 5:33p Dave
145 * Lots of nebula work. Put in ship contrails.
147 * 5 11/05/98 5:55p Dave
148 * Big pass at reducing #includes
150 * 4 10/13/98 9:29a Dave
151 * Started neatening up freespace.h. Many variables renamed and
152 * reorganized. Added AlphaColors.[h,cpp]
154 * 3 10/07/98 11:16a Dave
157 * 2 10/07/98 10:54a Dave
160 * 1 10/07/98 10:51a Dave
162 * 106 5/23/98 4:14p John
163 * Added code to preload textures to video card for AGP. Added in code
164 * to page in some bitmaps that weren't getting paged in at level start.
166 * 105 5/13/98 2:53p John
167 * Made subspace effect work under software. Had to add new inner loop to
168 * tmapper. Added glows to end of subspace effect. Made subspace effect
169 * levels use gamepalette-subspace palette.
171 * 104 5/13/98 10:28a John
172 * made subpsace forward sliding 40% faster.
174 * 103 5/10/98 4:18p John
175 * Reversed the subspace effect direction
177 * 102 5/08/98 8:38p John
178 * Subspace tweaks. Made two layers rotate independentyl.
180 * 101 5/08/98 1:32p John
181 * Added code for using two layered subspace effects.
183 * 100 5/06/98 5:30p John
184 * Removed unused cfilearchiver. Removed/replaced some unused/little used
185 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
186 * DirectX header files and libs that fixed the Direct3D alpha blending
189 * 99 4/22/98 4:09p John
190 * String externalization
192 * 98 4/22/98 3:28p John
195 * 97 4/13/98 4:54p John
196 * Made uv rotate independently on subspace effect. Put in DCF function
197 * for setting subspace speeds.
199 * 96 4/12/98 5:55p John
200 * Made models work with subspace. Made subspace rotate also.
202 * 95 4/11/98 6:53p John
203 * Added first rev of subspace effect.
205 * 94 4/08/98 11:31a Dave
206 * AL: Fix syntax error for non-demo
208 * 93 4/08/98 10:46a Lawrance
209 * #ifdef out asteroid check for demo
211 * 92 4/08/98 9:25a John
212 * Made asteroid missions not show suns
214 * 91 4/07/98 4:17p John
215 * Made Fred be able to move suns. Made suns actually affect the lighting
218 * 90 4/07/98 11:19a Hoffoss
219 * Changed code to only use Sun01 for a bitmap by default, as John
226 #include "floating.h"
230 #include "starfield.h"
233 #include "freespace.h"
236 #include "linklist.h"
237 #include "lighting.h"
238 #include "asteroid.h"
239 #include "missionparse.h"
241 #include "alphacolors.h"
242 #include "supernova.h"
244 #define MAX_DEBRIS_VCLIPS 4
245 #define DEBRIS_ROT_MIN 10000
246 #define DEBRIS_ROT_RANGE 8
247 #define DEBRIS_ROT_RANGE_SCALER 10000
248 #define RND_MAX_MASK 0x3fff
249 #define HALF_RND_MAX 0x2000
251 typedef struct debris_vclip {
265 const int MAX_DEBRIS = 200;
266 const int MAX_STARS = 2000;
267 const float MAX_DIST = 50.0f;
268 const float MAX_DIST_RANGE = 60.0f;
269 const float MIN_DIST_RANGE = 14.0f;
270 const float BASE_SIZE = 0.12f;
271 float BASE_SIZE_NEB = 0.5f;
273 static int Subspace_model_inner = -1;
274 static int Subspace_model_outer = -1;
277 fix starfield_timestamp = 0;
279 // for drawing cool stuff on the background - comes from a table
280 starfield_bitmap Starfield_bitmaps[MAX_STARFIELD_BITMAPS];
281 starfield_bitmap_instance Starfield_bitmap_instance[MAX_STARFIELD_BITMAPS];
282 int Num_starfield_bitmaps = 0;
284 // sun bitmaps and sun glow bitmaps
285 starfield_bitmap Sun_bitmaps[MAX_STARFIELD_BITMAPS];
286 starfield_bitmap_instance Suns[MAX_STARFIELD_BITMAPS];
289 int last_stars_filled = 0;
290 color star_colors[8];
291 color star_aacolors[8];
293 typedef struct star {
295 vector last_star_pos;
298 star Stars[MAX_STARS];
300 old_debris odebris[MAX_DEBRIS];
303 debris_vclip debris_vclips_normal[MAX_DEBRIS_VCLIPS] = { { -1, -1, "debris01" }, { -1, -1, "debris02" }, { -1, -1, "debris03" }, { -1, -1, "debris04" } };
304 debris_vclip debris_vclips_nebula[MAX_DEBRIS_VCLIPS] = { { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" } };
305 debris_vclip *debris_vclips = debris_vclips_normal;
308 int stars_debris_loaded = 0;
311 int Stars_background_inited = 0; // if we're inited
312 int Nmodel_num = -1; // model num
313 int Nmodel_bitmap = -1; // model texture
315 // given a starfield_bitmap_instance, return a pointer to its parent, for suns
316 starfield_bitmap *stars_lookup_sun(starfield_bitmap_instance *s)
326 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
327 if(!stricmp(Sun_bitmaps[idx].filename, s->filename)){
328 return &Sun_bitmaps[idx];
336 void stars_load_debris()
340 // if we're in nebula mode
341 if(The_mission.flags & MISSION_FLAG_FULLNEB){
342 debris_vclips = debris_vclips_nebula;
344 debris_vclips = debris_vclips_normal;
347 for (i=0; i<MAX_DEBRIS_VCLIPS; i++ ) {
348 debris_vclips[i].bm = bm_load_animation( debris_vclips[i].name, &debris_vclips[i].nframes, NULL, 1 );
349 if ( debris_vclips[i].bm < 0 ) {
350 // try loading it as a single bitmap
351 debris_vclips[i].bm = bm_load(debris_vclips[i].name);
352 debris_vclips[i].nframes = 1;
354 if(debris_vclips[i].bm <= 0){
355 Error( LOCATION, "Couldn't load animation/bitmap '%s'\n", debris_vclips[i].name );
359 stars_debris_loaded = 1;
362 // call on game startup
365 starfield_bitmap *bm;
367 char filename[MAX_FILENAME_LEN+1] = "";
368 char glow_filename[MAX_FILENAME_LEN+1] = "";
372 read_file_text("stars.tbl");
375 // make all bitmaps invalid
376 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
377 Starfield_bitmaps[idx].bitmap = -1;
378 Starfield_bitmaps[idx].glow_bitmap = -1;
379 strcpy(Starfield_bitmaps[idx].filename, "");
380 strcpy(Starfield_bitmaps[idx].glow_filename, "");
382 Sun_bitmaps[idx].bitmap = -1;
383 Sun_bitmaps[idx].glow_bitmap = -1;
384 strcpy(Sun_bitmaps[idx].filename, "");
385 strcpy(Sun_bitmaps[idx].glow_filename, "");
390 while(!optional_string("#end")){
391 // intensity alpha bitmap
392 if(optional_string("$Bitmap:")){
393 stuff_string(filename, F_NAME, NULL);
394 if(count < MAX_STARFIELD_BITMAPS){
395 bm = &Starfield_bitmaps[count++];
396 strcpy(bm->filename, filename);
398 bm->bitmap = bm_load(bm->filename);
399 Assert(bm->bitmap != -1);
401 // if fred is running we should lock the bitmap now
402 if(Fred_running && (bm->bitmap >= 0)){
403 bm_lock(bm->bitmap, 8, BMP_TEX_OTHER);
404 bm_unlock(bm->bitmap);
408 // green xparency bitmap
409 else if(optional_string("$BitmapX:")){
410 stuff_string(filename, F_NAME, NULL);
411 if(count < MAX_STARFIELD_BITMAPS){
412 bm = &Starfield_bitmaps[count++];
413 strcpy(bm->filename, filename);
415 bm->bitmap = bm_load(bm->filename);
416 Assert(bm->bitmap != -1);
418 // if fred is running we should lock as a 0, 255, 0 bitmap now
419 if(Fred_running && (bm->bitmap >= 0)){
420 bm_lock(bm->bitmap, 8, BMP_TEX_XPARENT);
421 bm_unlock(bm->bitmap);
429 while(!optional_string("#end")){
430 if(optional_string("$Sun:")){
431 stuff_string(filename, F_NAME, NULL);
434 required_string("$Sunglow:");
435 stuff_string(glow_filename, F_NAME, NULL);
437 // associated lighting values
438 required_string("$SunRGBI:");
444 if(count < MAX_STARFIELD_BITMAPS){
445 bm = &Sun_bitmaps[count++];
446 strcpy(bm->filename, filename);
447 strcpy(bm->glow_filename, glow_filename);
449 bm->bitmap = bm_load(bm->filename);
450 bm->glow_bitmap = bm_load(bm->glow_filename);
451 Assert(bm->bitmap != -1);
452 Assert(bm->glow_bitmap != -1);
458 // if fred is running we should lock the bitmap now
461 bm_lock(bm->bitmap, 8, BMP_TEX_OTHER);
462 bm_unlock(bm->bitmap);
464 if(bm->glow_bitmap >= 0){
465 bm_lock(bm->glow_bitmap, 8, BMP_TEX_OTHER);
466 bm_unlock(bm->glow_bitmap);
473 // normal debris pieces
475 while(!optional_string("#end")){
476 required_string("$Debris:");
477 stuff_string(filename, F_NAME, NULL);
479 if(count < MAX_DEBRIS_VCLIPS){
480 strcpy(debris_vclips_normal[count++].name, filename);
485 // nebula debris pieces
487 while(!optional_string("#end")){
488 required_string("$DebrisNeb:");
489 stuff_string(filename, F_NAME, NULL);
491 if(count < MAX_DEBRIS_VCLIPS){
492 strcpy(debris_vclips_nebula[count++].name, filename);
495 #ifndef MAKE_FS1 // string not used in FS1
500 // call this in game_post_level_init() so we know whether we're running in full nebula mode or not
501 void stars_level_init()
505 float dist, dist_max;
507 // reset to -1 so we reload it each mission (if we need to)
509 if(Nmodel_bitmap != -1){
510 bm_unload(Nmodel_bitmap);
514 // if (!stars_debris_loaded){
518 // following code randomly distributes star points within a sphere volume, which
519 // avoids there being denser areas along the edges and in corners that we had in the
520 // old rectangular distribution scheme.
521 dist_max = (float) (HALF_RND_MAX * HALF_RND_MAX);
522 for (i=0; i<MAX_STARS; i++) {
524 while (dist >= dist_max) {
525 v.xyz.x = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
526 v.xyz.y = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
527 v.xyz.z = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
529 dist = v.xyz.x * v.xyz.x + v.xyz.y * v.xyz.y + v.xyz.z * v.xyz.z;
531 vm_vec_copy_normalize(&Stars[i].pos, &v);
534 for (i=0; i<MAX_DEBRIS; i++) {
535 odebris[i].active = 0;
538 for (i=0; i<8; i++ ) {
539 ubyte intensity = (ubyte)((i + 1) * 24);
540 gr_init_alphacolor(&star_aacolors[i], 255, 255, 255, intensity, AC_TYPE_BLEND );
541 gr_init_color(&star_colors[i], intensity, intensity, intensity );
544 last_stars_filled = 0;
546 #ifndef MAKE_FS1 // some FS1 missions don't have a sun
547 // if we have no sun instances, create one
549 mprintf(("Adding default sun\n"));
552 strcpy(Suns[0].filename, Sun_bitmaps[0].filename);
553 Suns[0].scale_x = 1.0f;
554 Suns[0].scale_y = 1.0f;
557 memset(&Suns[0].ang, 0, sizeof(angles));
558 Suns[0].ang.h = fl_radian(60.0f);
568 extern object * Player_obj;
570 #define STAR_AMOUNT_DEFAULT 0.75f
571 #define STAR_DIM_DEFAULT 7800.0f
572 #define STAR_CAP_DEFAULT 75.0f
573 #define STAR_MAX_LENGTH_DEFAULT 0.04f // 312
575 float Star_amount = STAR_AMOUNT_DEFAULT;
576 float Star_dim = STAR_DIM_DEFAULT;
577 float Star_cap = STAR_CAP_DEFAULT;
578 float Star_max_length = STAR_MAX_LENGTH_DEFAULT;
580 #define STAR_FLAG_TAIL (1<<0) // Draw a tail when moving
581 #define STAR_FLAG_DIM (1<<1) // Dim as you move
582 #define STAR_FLAG_ANTIALIAS (1<<2) // Draw the star using antialiased lines
583 #define STAR_FLAG_DEFAULT (STAR_FLAG_TAIL | STAR_FLAG_DIM)
585 uint Star_flags = STAR_FLAG_DEFAULT;
588 DCF(stars,"Set parameters for starfield")
591 dc_get_arg(ARG_STRING);
592 if ( !strcmp( Dc_arg, "tail" )) {
593 dc_get_arg(ARG_FLOAT);
594 if ( (Dc_arg_float < 0.0f) || (Dc_arg_float > 1.0f) ) {
597 Star_amount = Dc_arg_float;
599 } else if ( !strcmp( Dc_arg, "len" )) {
600 dc_get_arg(ARG_FLOAT);
601 Star_max_length = Dc_arg_float;
602 } else if ( !strcmp( Dc_arg, "dim" )) {
603 dc_get_arg(ARG_FLOAT);
604 if ( Dc_arg_float < 0.0f ) {
607 Star_dim = Dc_arg_float;
609 } else if ( !strcmp( Dc_arg, "flag" )) {
610 dc_get_arg(ARG_STRING);
611 if ( !strcmp( Dc_arg, "tail" )) {
612 Star_flags ^= STAR_FLAG_TAIL;
613 } else if ( !strcmp( Dc_arg, "dim" )) {
614 Star_flags ^= STAR_FLAG_DIM;
615 } else if ( !strcmp( Dc_arg, "aa" )) {
616 Star_flags ^= STAR_FLAG_ANTIALIAS;
620 } else if ( !strcmp( Dc_arg, "cap" )) {
621 dc_get_arg(ARG_FLOAT);
622 if ( (Dc_arg_float < 0.0f) || (Dc_arg_float > 255.0f) ) {
625 Star_cap = Dc_arg_float;
627 } else if ( !strcmp( Dc_arg, "m0" ) ) {
632 Star_max_length = STAR_MAX_LENGTH_DEFAULT;
633 } else if ( !strcmp( Dc_arg, "m1" ) || !strcmp( Dc_arg, "default" )) {
634 Star_amount = STAR_AMOUNT_DEFAULT;
635 Star_dim = STAR_DIM_DEFAULT;
636 Star_cap = STAR_CAP_DEFAULT;
637 Star_flags = STAR_FLAG_DEFAULT;
638 Star_max_length = STAR_MAX_LENGTH_DEFAULT;
639 } else if ( !strcmp( Dc_arg, "m2" )) {
643 Star_flags = STAR_FLAG_TAIL|STAR_FLAG_DIM|STAR_FLAG_ANTIALIAS;
644 Star_max_length = STAR_MAX_LENGTH_DEFAULT;
645 } else if ( !strcmp( Dc_arg, "num" )) {
647 if ( (Dc_arg_int < 0) || (Dc_arg_int > MAX_STARS) ) {
650 Num_stars = Dc_arg_int;
653 // print usage, not stats
659 dc_printf( "Usage: stars keyword\nWhere keyword can be in the following forms:\n" );
660 dc_printf( "stars default Resets stars to all default values\n" );
661 dc_printf( "stars num X Sets number of stars to X. Between 0 and %d.\n", MAX_STARS );
662 dc_printf( "stars tail X Where X is the percent of 'tail' between 0 and 1.0\n" );
663 dc_printf( "stars dim X Where X is the amount stars dim between 0 and 255.0\n" );
664 dc_printf( "stars cap X Where X is the cap of dimming between 0 and 255.\n" );
665 dc_printf( "stars len X Where X is the cap of length.\n" );
666 dc_printf( "stars m0 Macro0. Old 'pixel type' crappy stars. flags=none\n" );
667 dc_printf( "stars m1 Macro1. (default) tail=.75, dim=20.0, cap=75.0, flags=dim,tail\n" );
668 dc_printf( "stars m2 Macro2. tail=.75, dim=20.0, cap=75.0, flags=dim,tail,aa\n" );
669 dc_printf( "stars flag X Toggles flag X, where X is tail or dim or aa (aa=antialias)\n" );
670 dc_printf( "\nHINT: set cap to 0 to get dim rate and tail down, then use\n" );
671 dc_printf( "cap to keep the lines from going away when moving too fast.\n" );
672 dc_printf( "\nUse '? stars' to see current values.\n" );
673 Dc_status = 0; // don't print status if help is printed. Too messy.
677 dc_printf( "Num_stars: %d\n", Num_stars );
678 dc_printf( "Tail: %.2f\n", Star_amount );
679 dc_printf( "Dim: %.2f\n", Star_dim );
680 dc_printf( "Cap: %.2f\n", Star_cap );
681 dc_printf( "Max length: %.2f\n", Star_max_length );
682 dc_printf( "Flags:\n" );
683 dc_printf( " Tail: %s\n", (Star_flags&STAR_FLAG_TAIL?"On":"Off") );
684 dc_printf( " Dim: %s\n", (Star_flags&STAR_FLAG_DIM?"On":"Off") );
685 dc_printf( " Antialias: %s\n", (Star_flags&STAR_FLAG_ANTIALIAS?"On":"Off") );
686 dc_printf( "\nTHESE AREN'T SAVED TO DISK, SO IF YOU TWEAK\n" );
687 dc_printf( "THESE AND LIKE THEM, WRITE THEM DOWN!!\n" );
692 int reload_old_debris = 1; // If set to one, then reload all the last_pos of the debris
694 // Call this if camera "cuts" or moves long distances
695 // so blur effect doesn't draw lines all over the screen.
696 void stars_camera_cut()
698 last_stars_filled = 0;
699 reload_old_debris = 1;
702 //#define TIME_STAR_CODE // enable to time star code
705 extern float Viewer_zoom;
707 // get the world coords of the sun pos on the unit sphere.
708 void stars_get_sun_pos(int sun_n, vector *pos)
716 Assert(sun_n < Num_suns);
717 if((sun_n >= Num_suns) || (sun_n < 0)){
721 // rotate the sun properly
722 temp = vmd_zero_vector;
727 // we aleady know what the matrix is so just rotate
728 vm_vec_rotate(pos, &temp, &Suns[sun_n].m);
730 vm_angles_2_matrix(&rot, &Suns[sun_n].ang);
731 vm_vec_rotate(pos, &temp, &rot);
736 void stars_draw_sun( int show_sun )
742 starfield_bitmap *bm;
743 float local_scale = 1.0f;
749 for(idx=0; idx<Num_suns; idx++){
751 bm = stars_lookup_sun(&Suns[idx]);
757 sun_pos = vmd_zero_vector;
758 sun_pos.xyz.y = 1.0f;
759 stars_get_sun_pos(idx, &sun_pos);
763 vm_vec_normalize(&sun_dir);
765 // add the light source corresponding to the sun
766 light_add_directional(&sun_dir, bm->i, bm->r, bm->g, bm->b);
769 if(supernova_active()){
770 local_scale = 1.0f + (SUPERNOVA_SUN_SCALE * supernova_pct_complete());
773 // draw the sun itself, keep track of how many we drew
774 gr_set_bitmap(bm->bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.999f);
775 g3_rotate_faraway_vertex(&sun_vex, &sun_pos);
777 // divide by 10 to make normal size
778 if(!g3_draw_bitmap(&sun_vex, 0, 0.05f * (Suns[idx].scale_x / 10) * local_scale, TMAP_FLAG_TEXTURED)){
780 if(!g3_draw_bitmap(&sun_vex, 0, 0.05f * Suns[idx].scale_x * local_scale, TMAP_FLAG_TEXTURED)){
787 // draw the corresponding glow for sun_n
788 void stars_draw_sun_glow(int sun_n)
790 starfield_bitmap *bm;
791 vector sun_pos, sun_dir;
793 float local_scale = 1.0f;
796 Assert(sun_n < Num_suns);
797 if((sun_n >= Num_suns) || (sun_n < 0)){
802 bm = stars_lookup_sun(&Suns[sun_n]);
808 sun_pos = vmd_zero_vector;
809 sun_pos.xyz.y = 1.0f;
810 stars_get_sun_pos(sun_n, &sun_pos);
814 vm_vec_normalize(&sun_dir);
817 if(supernova_active()){
818 local_scale = 1.0f + (SUPERNOVA_SUN_SCALE * supernova_pct_complete());
821 // draw the sun itself, keep track of how many we drew
822 gr_set_bitmap(bm->glow_bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.5f);
823 g3_rotate_faraway_vertex(&sun_vex, &sun_pos);
825 // divide by 10 to make normal size
826 g3_draw_bitmap(&sun_vex, 0, 0.10f * (Suns[sun_n].scale_x / 10) * local_scale, TMAP_FLAG_TEXTURED);
828 g3_draw_bitmap(&sun_vex, 0, 0.10f * Suns[sun_n].scale_x * local_scale, TMAP_FLAG_TEXTURED);
833 void stars_draw_bitmaps( int show_bitmaps )
838 // if we're in the nebula, don't render any backgrounds
839 if(The_mission.flags & MISSION_FLAG_FULLNEB){
844 if(!Detail.planets_suns){
848 // render all bitmaps
849 for(idx=0; idx<Num_starfield_bitmaps; idx++){
850 // lookup the info index
851 star_index = stars_find_bitmap(Starfield_bitmap_instance[idx].filename);
858 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.9999f);
859 g3_draw_perspective_bitmap(&Starfield_bitmap_instance[idx].ang, Starfield_bitmap_instance[idx].scale_x, Starfield_bitmap_instance[idx].scale_y, Starfield_bitmap_instance[idx].div_x, Starfield_bitmap_instance[idx].div_y, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
861 if(Starfield_bitmaps[star_index].xparent){
862 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap);
863 g3_draw_perspective_bitmap(&Starfield_bitmap_instance[idx].ang, Starfield_bitmap_instance[idx].scale_x, Starfield_bitmap_instance[idx].scale_y, Starfield_bitmap_instance[idx].div_x, Starfield_bitmap_instance[idx].div_y, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT | TMAP_FLAG_XPARENT);
865 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.9999f);
866 g3_draw_perspective_bitmap(&Starfield_bitmap_instance[idx].ang, Starfield_bitmap_instance[idx].scale_x, Starfield_bitmap_instance[idx].scale_y, Starfield_bitmap_instance[idx].div_x, Starfield_bitmap_instance[idx].div_y, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
873 void calculate_bitmap_matrix(starfield_bitmaps *bm, vector *v)
875 vm_vector_2_matrix(&bm->m, v, NULL, NULL);
876 vm_orthogonalize_matrix(&bm->m);
879 void calculate_bitmap_points(starfield_bitmaps *bm, float bank)
882 vector fvec, uvec, rvec, tmp;
885 vm_orthogonalize_matrix(&bm->m);
887 tangles.p = tangles.h = 0.0f;
889 vm_rotate_matrix_by_angles(&bm->m, &tangles);
893 vm_vec_scale(&fvec, bm->dist );
897 vm_vec_sub(&tmp, &fvec, &uvec);
898 vm_vec_sub(&bm->points[3], &tmp, &rvec);
900 vm_vec_sub(&tmp, &fvec, &uvec);
901 vm_vec_add(&bm->points[2], &tmp, &rvec);
903 vm_vec_add(&tmp, &fvec, &uvec);
904 vm_vec_add(&bm->points[1], &tmp, &rvec);
906 vm_vec_add(&tmp, &fvec, &uvec);
907 vm_vec_sub(&bm->points[0], &tmp, &rvec);
910 vm_vec_normalize(&bm->points[i]);
915 extern int Interp_subspace;
916 extern float Interp_subspace_offset_u;
917 extern float Interp_subspace_offset_u;
918 extern float Interp_subspace_offset_v;
920 float subspace_offset_u = 0.0f;
921 float subspace_offset_u_inner = 0.0f;
922 float subspace_offset_v = 0.0f;
924 float subspace_u_speed = 0.07f; // how fast u changes
925 float subspace_v_speed = 0.05f; // how fast v changes
927 int Subspace_glow_bitmap = -1;
929 float Subspace_glow_frame = 0.0f;
930 float Subspace_glow_rate = 1.0f;
934 DCF(subspace_set,"Set parameters for subspace effect")
937 dc_get_arg(ARG_STRING);
938 if ( !strcmp( Dc_arg, "u" )) {
939 dc_get_arg(ARG_FLOAT);
940 if ( Dc_arg_float < 0.0f ) {
943 subspace_u_speed = Dc_arg_float;
945 } else if ( !strcmp( Dc_arg, "v" )) {
946 dc_get_arg(ARG_FLOAT);
947 if ( Dc_arg_float < 0.0f ) {
950 subspace_v_speed = Dc_arg_float;
953 // print usage, not stats
959 dc_printf( "Usage: subspace keyword\nWhere keyword can be in the following forms:\n" );
960 dc_printf( "subspace u X Where X is how fast u moves.\n", MAX_STARS );
961 dc_printf( "subspace v X Where X is how fast v moves.\n" );
962 dc_printf( "\nUse '? subspace' to see current values.\n" );
963 Dc_status = 0; // don't print status if help is printed. Too messy.
967 dc_printf( "u: %.2f\n", subspace_u_speed );
968 dc_printf( "v: %.2f\n", subspace_v_speed );
973 void subspace_render()
975 if ( Subspace_model_inner == -1 ) {
976 Subspace_model_inner = model_load( "subspace_small.pof", 0, NULL );
977 Assert(Subspace_model_inner>-1);
980 if ( Subspace_model_outer == -1 ) {
981 Subspace_model_outer = model_load( "subspace_big.pof", 0, NULL );
982 Assert(Subspace_model_outer>-1);
985 if ( Subspace_glow_bitmap == -1 ) {
986 Subspace_glow_bitmap = bm_load( NOX("SunGlow01"));
987 Assert(Subspace_glow_bitmap>-1);
990 Subspace_glow_frame += flFrametime * 1.0f;
992 float total_time = i2fl(NOISE_NUM_FRAMES) / 15.0f;
995 if ( Subspace_glow_frame < 0.0f ) Subspace_glow_frame = 0.0f;
996 if ( Subspace_glow_frame > 100.0f ) Subspace_glow_frame = 0.0f;
998 while ( Subspace_glow_frame > total_time ) {
999 Subspace_glow_frame -= total_time;
1001 int framenum = fl2i( (Subspace_glow_frame*NOISE_NUM_FRAMES) / total_time );
1002 if ( framenum < 0 ) framenum = 0;
1003 if ( framenum >= NOISE_NUM_FRAMES ) framenum = NOISE_NUM_FRAMES-1;
1005 subspace_offset_u += flFrametime*subspace_u_speed;
1006 if (subspace_offset_u > 1.0f ) {
1007 subspace_offset_u -= 1.0f;
1010 subspace_offset_u_inner += flFrametime*subspace_u_speed*3.0f;
1011 if (subspace_offset_u > 1.0f ) {
1012 subspace_offset_u -= 1.0f;
1015 subspace_offset_v += flFrametime*subspace_v_speed;
1016 if (subspace_offset_v > 1.0f ) {
1017 subspace_offset_v -= 1.0f;
1023 angles angs = { 0.0f, 0.0f, 0.0f };
1024 angs.b = subspace_offset_v * PI2;
1026 vm_angles_2_matrix(&tmp,&angs);
1028 int saved_gr_zbuffering = gr_zbuffer_get();
1030 gr_zbuffer_set(GR_ZBUFF_NONE);
1032 if ( !D3D_enabled ) {
1034 int render_flags = MR_NO_LIGHTING | MR_ALWAYS_REDRAW;
1036 Interp_subspace = 1;
1037 Interp_subspace_offset_u = 1.0f - subspace_offset_u;
1038 Interp_subspace_offset_v = 0.0f;
1040 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1041 render_flags |= MR_SHOW_THRUSTERS;
1042 model_render( Subspace_model_outer, &tmp, &Eye_position, render_flags ); //MR_NO_CORRECT|MR_SHOW_OUTLINE
1046 int render_flags = MR_NO_LIGHTING | MR_ALWAYS_REDRAW;
1048 Interp_subspace = 1;
1049 Interp_subspace_offset_u = 1.0f - subspace_offset_u;
1050 Interp_subspace_offset_v = 0.0f;
1052 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1053 render_flags |= MR_SHOW_THRUSTERS;
1054 model_render( Subspace_model_outer, &tmp, &Eye_position, render_flags ); //MR_NO_CORRECT|MR_SHOW_OUTLINE
1056 Interp_subspace = 1;
1057 Interp_subspace_offset_u = 1.0f - subspace_offset_u_inner;
1058 Interp_subspace_offset_v = 0.0f;
1060 angs.b = -subspace_offset_v * PI2;
1062 vm_angles_2_matrix(&tmp,&angs);
1064 model_set_outline_color(255,255,255);
1066 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1067 render_flags |= MR_SHOW_THRUSTERS;
1069 model_render( Subspace_model_inner, &tmp, &Eye_position, render_flags ); //MR_NO_CORRECT|MR_SHOW_OUTLINE
1072 Interp_subspace = 0;
1073 gr_zbuffer_set(saved_gr_zbuffering);
1076 void stars_draw( int show_stars, int show_suns, int show_nebulas, int show_subspace )
1082 int gr_zbuffering_save = gr_zbuffer_get();
1083 gr_zbuffer_set(GR_ZBUFF_NONE);
1085 if ( show_subspace ) {
1090 if (Num_stars >= MAX_STARS){
1091 Num_stars = MAX_STARS;
1094 #ifdef TIME_STAR_CODE
1096 xt1 = timer_get_fixed_seconds();
1099 if ( show_nebulas && (Game_detail_flags & DETAIL_FLAG_NEBULAS) && (Neb2_render_mode != NEB2_RENDER_POF) && (Neb2_render_mode != NEB2_RENDER_LAME)) {
1103 // draw background stuff
1104 if((Neb2_render_mode != NEB2_RENDER_POLY) && (Neb2_render_mode != NEB2_RENDER_LAME) && show_stars){
1105 // semi-hack, do we don't fog the background
1106 int neb_save = Neb2_render_mode;
1107 Neb2_render_mode = NEB2_RENDER_NONE;
1108 extern void stars_draw_background();
1109 stars_draw_background();
1110 Neb2_render_mode = neb_save;
1113 if (show_stars && ( Game_detail_flags & DETAIL_FLAG_STARS) && !(The_mission.flags & MISSION_FLAG_FULLNEB) && (supernova_active() < 3)) {
1118 if ( !last_stars_filled ) {
1119 for (sp=Stars,i=0; i<Num_stars; i++, sp++ ) {
1121 g3_rotate_faraway_vertex(&p2, &sp->pos);
1122 sp->last_star_pos.xyz.x = p2.x;
1123 sp->last_star_pos.xyz.y = p2.y;
1124 sp->last_star_pos.xyz.z = p2.z;
1130 tmp_num_stars = (Detail.num_stars*Num_stars)/MAX_DETAIL_LEVEL;
1131 if (tmp_num_stars < 0 ) {
1133 } else if ( tmp_num_stars > Num_stars ) {
1134 tmp_num_stars = Num_stars;
1137 for (sp=Stars,i=0; i<tmp_num_stars; i++, sp++ ) {
1141 memset(&p1, 0, sizeof(vertex));
1143 // This makes a star look "proper" by not translating the
1144 // point around the viewer's eye before rotation. In other
1145 // words, when the ship translates, the stars do not change.
1147 g3_rotate_faraway_vertex(&p2, &sp->pos);
1151 g3_project_vertex(&p2);
1152 if ( p2.flags & PF_OVERFLOW ) {
1159 if ( can_draw && (Star_flags & (STAR_FLAG_TAIL|STAR_FLAG_DIM)) ) {
1161 dist = vm_vec_dist_quick( &sp->last_star_pos, (vector *)&p2.x );
1164 if ( dist > Star_max_length ) {
1165 ratio = Star_max_length / dist;
1166 dist = Star_max_length;
1170 ratio *= Star_amount;
1172 p1.x = p2.x + (sp->last_star_pos.xyz.x-p2.x)*ratio;
1173 p1.y = p2.y + (sp->last_star_pos.xyz.y-p2.y)*ratio;
1174 p1.z = p2.z + (sp->last_star_pos.xyz.z-p2.z)*ratio;
1176 p1.flags = 0; // not projected
1177 g3_code_vertex( &p1 );
1182 g3_project_vertex(&p1);
1183 if ( p1.flags & PF_OVERFLOW ) {
1189 sp->last_star_pos.xyz.x = p2.x;
1190 sp->last_star_pos.xyz.y = p2.y;
1191 sp->last_star_pos.xyz.z = p2.z;
1193 if ( !can_draw ) continue;
1197 if ( Star_flags & STAR_FLAG_DIM ) {
1199 float colorf = 255.0f - dist*Star_dim;
1201 if ( colorf < Star_cap )
1204 color = (fl2i(colorf)*(i&7))/256;
1210 if ( (Star_flags & STAR_FLAG_ANTIALIAS) || (D3D_enabled) ) {
1211 gr_set_color_fast( &star_aacolors[color] );
1213 // if the two points are the same, fudge it, since some D3D cards (G200 and G400) are lame.
1214 if( (fl2i(p1.sx) == fl2i(p2.sx)) && (fl2i(p1.sy) == fl2i(p2.sy)) ){
1219 // use alphablended line so that dark stars don't look bad on top of nebulas
1220 gr_set_color_fast( &star_aacolors[color] );
1221 if ( Star_flags & STAR_FLAG_TAIL ) {
1222 gr_line(fl2i(p1.sx),fl2i(p1.sy),fl2i(p2.sx),fl2i(p2.sy));
1224 gr_pixel( fl2i(p2.sx),fl2i(p2.sy) );
1230 last_stars_filled = 1;
1232 #ifdef TIME_STAR_CODE
1233 xt2 = timer_get_fixed_seconds();
1234 mprintf(( "Stars: %d\n", xt2-xt1 ));
1238 if ( (Game_detail_flags & DETAIL_FLAG_MOTION) && (!Fred_running) && (supernova_active() < 3) ) {
1240 gr_set_color( 0, 0, 0 );
1243 if(The_mission.flags & MISSION_FLAG_FULLNEB){
1244 gr_fog_set(GR_FOGMODE_NONE, 0, 0, 0);
1247 old_debris * d = odebris;
1248 for (i=0; i<MAX_DEBRIS; i++, d++ ) {
1252 d->pos.xyz.x = f2fl(myrand() - RAND_MAX/2);
1253 d->pos.xyz.y = f2fl(myrand() - RAND_MAX/2);
1254 d->pos.xyz.z = f2fl(myrand() - RAND_MAX/2);
1256 vm_vec_normalize(&d->pos);
1258 vm_vec_scale(&d->pos, MAX_DIST);
1259 vm_vec_add2(&d->pos, &Eye_position );
1260 // vm_vec_add2(&d->pos, &Player_obj->pos );
1262 d->vclip = i % MAX_DEBRIS_VCLIPS; //rand()
1264 // if we're in full neb mode
1265 if((The_mission.flags & MISSION_FLAG_FULLNEB) && (Neb2_render_mode != NEB2_RENDER_NONE)){
1266 d->size = i2fl(myrand() % 4)*BASE_SIZE_NEB;
1268 d->size = i2fl(myrand() % 4)*BASE_SIZE;
1271 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1274 if ( reload_old_debris ) {
1275 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1278 g3_rotate_vertex(&p, &d->pos);
1281 int frame = Missiontime / (DEBRIS_ROT_MIN + (i % DEBRIS_ROT_RANGE) * DEBRIS_ROT_RANGE_SCALER);
1282 frame %= debris_vclips[d->vclip].nframes;
1284 if((The_mission.flags & MISSION_FLAG_FULLNEB) && (Neb2_render_mode != NEB2_RENDER_NONE)){
1285 gr_set_bitmap( debris_vclips[d->vclip].bm + frame, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.3f);
1287 gr_set_bitmap( debris_vclips[d->vclip].bm + frame );
1291 vm_vec_add( &tmp, &d->last_pos, &Eye_position );
1292 g3_draw_laser( &d->pos,d->size,&tmp,d->size, TMAP_FLAG_TEXTURED|TMAP_FLAG_XPARENT, 25.0f );
1295 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1297 vdist = vm_vec_mag_quick(&d->last_pos);
1299 if (vdist > MAX_DIST_RANGE)
1301 else if (vdist < MIN_DIST_RANGE)
1305 // vm_vec_sub( &tmp, &d->pos, &Player_obj->pos );
1306 // vdist = vm_vec_dot( &tmp, &Player_obj->orient.fvec );
1307 // if ( vdist < 0.0f )
1312 reload_old_debris = 0;
1316 stars_draw_sun( show_suns );
1317 stars_draw_bitmaps( show_suns );
1319 gr_zbuffer_set( gr_zbuffering_save );
1322 void stars_page_in()
1326 // Initialize the subspace stuff
1328 if ( Game_subspace_effect ) {
1330 Subspace_model_inner = model_load( "subspace_small.pof", 0, NULL );
1331 Assert(Subspace_model_inner>-1);
1332 Subspace_model_outer = model_load( "subspace_big.pof", 0, NULL );
1333 Assert(Subspace_model_outer>-1);
1337 pm = model_get(Subspace_model_inner);
1339 nprintf(( "Paging", "Paging in textures for subspace effect.\n" ));
1341 for (j=0; j<pm->n_textures; j++ ) {
1342 int bitmap_num = pm->original_textures[j];
1344 if ( bitmap_num > -1 ) {
1345 bm_page_in_texture( bitmap_num );
1349 pm = model_get(Subspace_model_outer);
1351 nprintf(( "Paging", "Paging in textures for subspace effect.\n" ));
1353 for (j=0; j<pm->n_textures; j++ ) {
1354 int bitmap_num = pm->original_textures[j];
1356 if ( bitmap_num > -1 ) {
1357 bm_page_in_texture( bitmap_num );
1361 Subspace_model_inner = -1;
1362 Subspace_model_outer = -1;
1365 Subspace_glow_bitmap = bm_load( NOX("SunGlow01"));
1366 bm_page_in_xparent_texture(Subspace_glow_bitmap);
1368 // page in starfield bitmaps
1371 while((idx < MAX_STARFIELD_BITMAPS) && (Starfield_bitmaps[idx].bitmap != -1)){
1372 if(Starfield_bitmaps[idx].xparent){
1373 bm_page_in_xparent_texture(Starfield_bitmaps[idx].bitmap);
1375 bm_page_in_texture(Starfield_bitmaps[idx].bitmap);
1382 // sun bitmaps and glows
1384 while((idx < MAX_STARFIELD_BITMAPS) && (Sun_bitmaps[idx].bitmap != -1) && (Sun_bitmaps[idx].glow_bitmap != -1)){
1385 bm_page_in_texture(Sun_bitmaps[idx].bitmap);
1386 bm_page_in_texture(Sun_bitmaps[idx].glow_bitmap);
1392 for (i=0; i<MAX_DEBRIS_VCLIPS; i++ ) {
1393 for (j=0; j<debris_vclips[i].nframes; j++ ) {
1394 bm_page_in_xparent_texture(debris_vclips[i].bm + j);
1399 // background nebula models and planets
1400 void stars_draw_background()
1402 if((Nmodel_num < 0) || (Nmodel_bitmap < 0)){
1406 // draw the model at the player's eye wif no z-buffering
1407 model_set_alpha(1.0f);
1408 model_set_forced_texture(Nmodel_bitmap);
1409 model_render(Nmodel_num, &vmd_identity_matrix, &Eye_position, MR_NO_ZBUFFER | MR_NO_CULL | MR_ALL_XPARENT | MR_NO_LIGHTING | MR_FORCE_TEXTURE);
1410 model_set_forced_texture(-1);
1413 // call this to set a specific model as the background model
1414 void stars_set_background_model(char *model_name, char *texture_name)
1416 Nmodel_num = model_load(model_name, 0, NULL);
1417 Nmodel_bitmap = bm_load(texture_name);
1420 // lookup a starfield bitmap, return index or -1 on fail
1421 int stars_find_bitmap(char *name)
1426 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
1427 if(!strcmp(name, Starfield_bitmaps[idx].filename)){
1436 // lookup a sun by bitmap filename, return index or -1 on fail
1437 int stars_find_sun(char *name)
1442 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
1443 if(!strcmp(name, Sun_bitmaps[idx].filename)){