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.5 2002/06/17 06:33:11 relnev
20 * ryan's struct patch for gcc 2.95
22 * Revision 1.4 2002/06/09 04:41:27 relnev
23 * added copyright header
25 * Revision 1.3 2002/06/02 04:26:34 relnev
28 * Revision 1.2 2002/05/07 03:16:52 theoddone33
29 * The Great Newline Fix
31 * Revision 1.1.1.1 2002/05/03 03:28:10 root
35 * 34 9/07/99 4:01p Dave
36 * Fixed up a string.tbl paroblem (self destruct message). Make sure IPX
37 * does everything properly (setting up address when binding). Remove
38 * black rectangle background from UI_INPUTBOX.
40 * 33 9/01/99 10:14a Dave
43 * 32 8/30/99 5:01p Dave
44 * Made d3d do less state changing in the nebula. Use new chat server for
47 * 31 8/19/99 10:59a Dave
48 * Packet loss detection.
50 * 30 7/27/99 3:52p Dave
51 * Make star drawing a bit more robust to help lame D3D cards.
53 * 29 7/21/99 8:10p Dave
54 * First run of supernova effect.
56 * 28 7/13/99 2:01p Dave
57 * Don't draw background bitmaps in the nebula.
59 * 27 6/08/99 2:34p Jasenw
60 * Made perspective bitmaps render in Fred.
62 * 26 6/04/99 1:18p Dave
63 * Fixed briefing model rendering problems. Made show background option in
64 * fred toggle nebula rendering.
66 * 25 6/03/99 6:37p Dave
67 * More TNT fun. Made perspective bitmaps more flexible.
69 * 24 5/28/99 1:45p Dave
70 * Fixed up perspective bitmap drawing.
72 * 23 5/20/99 7:00p Dave
73 * Added alternate type names for ships. Changed swarm missile table
76 * 22 5/11/99 10:03a Dave
77 * Put a bunch of stuff into tables.
79 * 21 5/11/99 9:10a Dave
80 * Move default sun position.
82 * 20 5/09/99 6:00p Dave
83 * Lots of cool new effects. E3 build tweaks.
85 * 19 4/26/99 8:49p Dave
86 * Made all pof based nebula stuff full customizable through fred.
88 * 18 4/25/99 7:43p Dave
89 * Misc small bug fixes. Made sun draw properly.
91 * 17 4/23/99 5:53p Dave
92 * Started putting in new pof nebula support into Fred.
94 * 16 4/07/99 6:22p Dave
95 * Fred and Freespace support for multiple background bitmaps and suns.
96 * Fixed link errors on all subprojects. Moved encrypt_init() to
97 * cfile_init() and lcl_init(), since its safe to call twice.
99 * 15 3/31/99 8:24p Dave
100 * Beefed up all kinds of stuff, incluging beam weapons, nebula effects
101 * and background nebulae. Added per-ship non-dimming pixel colors.
103 * 14 3/20/99 5:09p Dave
104 * Fixed release build fred warnings and unhandled exception.
106 * 13 3/20/99 3:46p Dave
107 * Added support for model-based background nebulae. Added 3 new
110 * 12 3/20/99 2:04p Dave
111 * Removed unnecessary planet rendering.
113 * 11 3/19/99 9:51a Dave
114 * Checkin to repair massive source safe crash. Also added support for
115 * pof-style nebulae, and some new weapons code.
117 * 12 3/15/99 6:45p Daveb
118 * Put in rough nebula bitmap support.
120 * 11 3/11/99 5:53p Dave
121 * More network optimization. Spliced in Dell OEM planet bitmap crap.
123 * 10 2/03/99 11:44a Dave
124 * Fixed d3d transparent textures.
126 * 9 12/09/98 7:34p Dave
127 * Cleanup up nebula effect. Tweaked many values.
129 * 8 12/01/98 10:32a Johnson
130 * Fixed direct3d font problems. Fixed sun bitmap problem. Fixed direct3d
133 * 7 12/01/98 8:06a Dave
134 * Temporary checkin to fix some texture transparency problems in d3d.
136 * 6 11/14/98 5:33p Dave
137 * Lots of nebula work. Put in ship contrails.
139 * 5 11/05/98 5:55p Dave
140 * Big pass at reducing #includes
142 * 4 10/13/98 9:29a Dave
143 * Started neatening up freespace.h. Many variables renamed and
144 * reorganized. Added AlphaColors.[h,cpp]
146 * 3 10/07/98 11:16a Dave
149 * 2 10/07/98 10:54a Dave
152 * 1 10/07/98 10:51a Dave
154 * 106 5/23/98 4:14p John
155 * Added code to preload textures to video card for AGP. Added in code
156 * to page in some bitmaps that weren't getting paged in at level start.
158 * 105 5/13/98 2:53p John
159 * Made subspace effect work under software. Had to add new inner loop to
160 * tmapper. Added glows to end of subspace effect. Made subspace effect
161 * levels use gamepalette-subspace palette.
163 * 104 5/13/98 10:28a John
164 * made subpsace forward sliding 40% faster.
166 * 103 5/10/98 4:18p John
167 * Reversed the subspace effect direction
169 * 102 5/08/98 8:38p John
170 * Subspace tweaks. Made two layers rotate independentyl.
172 * 101 5/08/98 1:32p John
173 * Added code for using two layered subspace effects.
175 * 100 5/06/98 5:30p John
176 * Removed unused cfilearchiver. Removed/replaced some unused/little used
177 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
178 * DirectX header files and libs that fixed the Direct3D alpha blending
181 * 99 4/22/98 4:09p John
182 * String externalization
184 * 98 4/22/98 3:28p John
187 * 97 4/13/98 4:54p John
188 * Made uv rotate independently on subspace effect. Put in DCF function
189 * for setting subspace speeds.
191 * 96 4/12/98 5:55p John
192 * Made models work with subspace. Made subspace rotate also.
194 * 95 4/11/98 6:53p John
195 * Added first rev of subspace effect.
197 * 94 4/08/98 11:31a Dave
198 * AL: Fix syntax error for non-demo
200 * 93 4/08/98 10:46a Lawrance
201 * #ifdef out asteroid check for demo
203 * 92 4/08/98 9:25a John
204 * Made asteroid missions not show suns
206 * 91 4/07/98 4:17p John
207 * Made Fred be able to move suns. Made suns actually affect the lighting
210 * 90 4/07/98 11:19a Hoffoss
211 * Changed code to only use Sun01 for a bitmap by default, as John
218 #include "floating.h"
222 #include "starfield.h"
225 #include "freespace.h"
228 #include "linklist.h"
229 #include "lighting.h"
230 #include "asteroid.h"
231 #include "missionparse.h"
233 #include "alphacolors.h"
234 #include "supernova.h"
236 #define MAX_DEBRIS_VCLIPS 4
237 #define DEBRIS_ROT_MIN 10000
238 #define DEBRIS_ROT_RANGE 8
239 #define DEBRIS_ROT_RANGE_SCALER 10000
240 #define RND_MAX_MASK 0x3fff
241 #define HALF_RND_MAX 0x2000
243 typedef struct debris_vclip {
257 const int MAX_DEBRIS = 200;
258 const int MAX_STARS = 2000;
259 const float MAX_DIST = 50.0f;
260 const float MAX_DIST_RANGE = 60.0f;
261 const float MIN_DIST_RANGE = 14.0f;
262 const float BASE_SIZE = 0.12f;
263 float BASE_SIZE_NEB = 0.5f;
265 static int Subspace_model_inner = -1;
266 static int Subspace_model_outer = -1;
269 fix starfield_timestamp = 0;
271 // for drawing cool stuff on the background - comes from a table
272 starfield_bitmap Starfield_bitmaps[MAX_STARFIELD_BITMAPS];
273 starfield_bitmap_instance Starfield_bitmap_instance[MAX_STARFIELD_BITMAPS];
274 int Num_starfield_bitmaps = 0;
276 // sun bitmaps and sun glow bitmaps
277 starfield_bitmap Sun_bitmaps[MAX_STARFIELD_BITMAPS];
278 starfield_bitmap_instance Suns[MAX_STARFIELD_BITMAPS];
281 int last_stars_filled = 0;
282 color star_colors[8];
283 color star_aacolors[8];
285 typedef struct star {
287 vector last_star_pos;
290 star Stars[MAX_STARS];
292 old_debris odebris[MAX_DEBRIS];
295 debris_vclip debris_vclips_normal[MAX_DEBRIS_VCLIPS] = { { -1, -1, "debris01" }, { -1, -1, "debris02" }, { -1, -1, "debris03" }, { -1, -1, "debris04" } };
296 debris_vclip debris_vclips_nebula[MAX_DEBRIS_VCLIPS] = { { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" }, { -1, -1, "Neb01-64" } };
297 debris_vclip *debris_vclips = debris_vclips_normal;
300 int stars_debris_loaded = 0;
303 int Stars_background_inited = 0; // if we're inited
304 int Nmodel_num = -1; // model num
305 int Nmodel_bitmap = -1; // model texture
307 // given a starfield_bitmap_instance, return a pointer to its parent, for suns
308 starfield_bitmap *stars_lookup_sun(starfield_bitmap_instance *s)
318 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
319 if(!stricmp(Sun_bitmaps[idx].filename, s->filename)){
320 return &Sun_bitmaps[idx];
328 void stars_load_debris()
332 // if we're in nebula mode
333 if(The_mission.flags & MISSION_FLAG_FULLNEB){
334 debris_vclips = debris_vclips_nebula;
336 debris_vclips = debris_vclips_normal;
339 for (i=0; i<MAX_DEBRIS_VCLIPS; i++ ) {
340 debris_vclips[i].bm = bm_load_animation( debris_vclips[i].name, &debris_vclips[i].nframes, NULL, 1 );
341 if ( debris_vclips[i].bm < 0 ) {
342 // try loading it as a single bitmap
343 debris_vclips[i].bm = bm_load(debris_vclips[i].name);
344 debris_vclips[i].nframes = 1;
346 if(debris_vclips[i].bm <= 0){
347 Error( LOCATION, "Couldn't load animation/bitmap '%s'\n", debris_vclips[i].name );
351 stars_debris_loaded = 1;
354 // call on game startup
357 starfield_bitmap *bm;
359 char filename[MAX_FILENAME_LEN+1] = "";
360 char glow_filename[MAX_FILENAME_LEN+1] = "";
364 read_file_text("stars.tbl");
367 // make all bitmaps invalid
368 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
369 Starfield_bitmaps[idx].bitmap = -1;
370 Starfield_bitmaps[idx].glow_bitmap = -1;
371 strcpy(Starfield_bitmaps[idx].filename, "");
372 strcpy(Starfield_bitmaps[idx].glow_filename, "");
374 Sun_bitmaps[idx].bitmap = -1;
375 Sun_bitmaps[idx].glow_bitmap = -1;
376 strcpy(Sun_bitmaps[idx].filename, "");
377 strcpy(Sun_bitmaps[idx].glow_filename, "");
382 while(!optional_string("#end")){
383 // intensity alpha bitmap
384 if(optional_string("$Bitmap:")){
385 stuff_string(filename, F_NAME, NULL);
386 if(count < MAX_STARFIELD_BITMAPS){
387 bm = &Starfield_bitmaps[count++];
388 strcpy(bm->filename, filename);
390 bm->bitmap = bm_load(bm->filename);
391 Assert(bm->bitmap != -1);
393 // if fred is running we should lock the bitmap now
394 if(Fred_running && (bm->bitmap >= 0)){
395 bm_lock(bm->bitmap, 8, BMP_TEX_OTHER);
396 bm_unlock(bm->bitmap);
400 // green xparency bitmap
401 else if(optional_string("$BitmapX:")){
402 stuff_string(filename, F_NAME, NULL);
403 if(count < MAX_STARFIELD_BITMAPS){
404 bm = &Starfield_bitmaps[count++];
405 strcpy(bm->filename, filename);
407 bm->bitmap = bm_load(bm->filename);
408 Assert(bm->bitmap != -1);
410 // if fred is running we should lock as a 0, 255, 0 bitmap now
411 if(Fred_running && (bm->bitmap >= 0)){
412 bm_lock(bm->bitmap, 8, BMP_TEX_XPARENT);
413 bm_unlock(bm->bitmap);
421 while(!optional_string("#end")){
422 if(optional_string("$Sun:")){
423 stuff_string(filename, F_NAME, NULL);
426 required_string("$Sunglow:");
427 stuff_string(glow_filename, F_NAME, NULL);
429 // associated lighting values
430 required_string("$SunRGBI:");
436 if(count < MAX_STARFIELD_BITMAPS){
437 bm = &Sun_bitmaps[count++];
438 strcpy(bm->filename, filename);
439 strcpy(bm->glow_filename, glow_filename);
441 bm->bitmap = bm_load(bm->filename);
442 bm->glow_bitmap = bm_load(bm->glow_filename);
443 Assert(bm->bitmap != -1);
444 Assert(bm->glow_bitmap != -1);
450 // if fred is running we should lock the bitmap now
453 bm_lock(bm->bitmap, 8, BMP_TEX_OTHER);
454 bm_unlock(bm->bitmap);
456 if(bm->glow_bitmap >= 0){
457 bm_lock(bm->glow_bitmap, 8, BMP_TEX_OTHER);
458 bm_unlock(bm->glow_bitmap);
465 // normal debris pieces
467 while(!optional_string("#end")){
468 required_string("$Debris:");
469 stuff_string(filename, F_NAME, NULL);
471 if(count < MAX_DEBRIS_VCLIPS){
472 strcpy(debris_vclips_normal[count++].name, filename);
477 // nebula debris pieces
479 while(!optional_string("#end")){
480 required_string("$DebrisNeb:");
481 stuff_string(filename, F_NAME, NULL);
483 if(count < MAX_DEBRIS_VCLIPS){
484 strcpy(debris_vclips_nebula[count++].name, filename);
490 // call this in game_post_level_init() so we know whether we're running in full nebula mode or not
491 void stars_level_init()
495 float dist, dist_max;
497 // reset to -1 so we reload it each mission (if we need to)
499 if(Nmodel_bitmap != -1){
500 bm_unload(Nmodel_bitmap);
504 // if (!stars_debris_loaded){
508 // following code randomly distributes star points within a sphere volume, which
509 // avoids there being denser areas along the edges and in corners that we had in the
510 // old rectangular distribution scheme.
511 dist_max = (float) (HALF_RND_MAX * HALF_RND_MAX);
512 for (i=0; i<MAX_STARS; i++) {
514 while (dist >= dist_max) {
515 v.xyz.x = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
516 v.xyz.y = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
517 v.xyz.z = (float) ((myrand() & RND_MAX_MASK) - HALF_RND_MAX);
519 dist = v.xyz.x * v.xyz.x + v.xyz.y * v.xyz.y + v.xyz.z * v.xyz.z;
521 vm_vec_copy_normalize(&Stars[i].pos, &v);
524 for (i=0; i<MAX_DEBRIS; i++) {
525 odebris[i].active = 0;
528 for (i=0; i<8; i++ ) {
529 ubyte intensity = (ubyte)((i + 1) * 24);
530 gr_init_alphacolor(&star_aacolors[i], 255, 255, 255, intensity, AC_TYPE_BLEND );
531 gr_init_color(&star_colors[i], intensity, intensity, intensity );
534 last_stars_filled = 0;
536 // if we have no sun instances, create one
538 mprintf(("Adding default sun\n"));
541 strcpy(Suns[0].filename, Sun_bitmaps[0].filename);
542 Suns[0].scale_x = 1.0f;
543 Suns[0].scale_y = 1.0f;
546 memset(&Suns[0].ang, 0, sizeof(angles));
547 Suns[0].ang.h = fl_radian(60.0f);
556 extern object * Player_obj;
558 #define STAR_AMOUNT_DEFAULT 0.75f
559 #define STAR_DIM_DEFAULT 7800.0f
560 #define STAR_CAP_DEFAULT 75.0f
561 #define STAR_MAX_LENGTH_DEFAULT 0.04f // 312
563 float Star_amount = STAR_AMOUNT_DEFAULT;
564 float Star_dim = STAR_DIM_DEFAULT;
565 float Star_cap = STAR_CAP_DEFAULT;
566 float Star_max_length = STAR_MAX_LENGTH_DEFAULT;
568 #define STAR_FLAG_TAIL (1<<0) // Draw a tail when moving
569 #define STAR_FLAG_DIM (1<<1) // Dim as you move
570 #define STAR_FLAG_ANTIALIAS (1<<2) // Draw the star using antialiased lines
571 #define STAR_FLAG_DEFAULT (STAR_FLAG_TAIL | STAR_FLAG_DIM)
573 uint Star_flags = STAR_FLAG_DEFAULT;
576 DCF(stars,"Set parameters for starfield")
579 dc_get_arg(ARG_STRING);
580 if ( !strcmp( Dc_arg, "tail" )) {
581 dc_get_arg(ARG_FLOAT);
582 if ( (Dc_arg_float < 0.0f) || (Dc_arg_float > 1.0f) ) {
585 Star_amount = Dc_arg_float;
587 } else if ( !strcmp( Dc_arg, "len" )) {
588 dc_get_arg(ARG_FLOAT);
589 Star_max_length = Dc_arg_float;
590 } else if ( !strcmp( Dc_arg, "dim" )) {
591 dc_get_arg(ARG_FLOAT);
592 if ( Dc_arg_float < 0.0f ) {
595 Star_dim = Dc_arg_float;
597 } else if ( !strcmp( Dc_arg, "flag" )) {
598 dc_get_arg(ARG_STRING);
599 if ( !strcmp( Dc_arg, "tail" )) {
600 Star_flags ^= STAR_FLAG_TAIL;
601 } else if ( !strcmp( Dc_arg, "dim" )) {
602 Star_flags ^= STAR_FLAG_DIM;
603 } else if ( !strcmp( Dc_arg, "aa" )) {
604 Star_flags ^= STAR_FLAG_ANTIALIAS;
608 } else if ( !strcmp( Dc_arg, "cap" )) {
609 dc_get_arg(ARG_FLOAT);
610 if ( (Dc_arg_float < 0.0f) || (Dc_arg_float > 255.0f) ) {
613 Star_cap = Dc_arg_float;
615 } else if ( !strcmp( Dc_arg, "m0" ) ) {
620 Star_max_length = STAR_MAX_LENGTH_DEFAULT;
621 } else if ( !strcmp( Dc_arg, "m1" ) || !strcmp( Dc_arg, "default" )) {
622 Star_amount = STAR_AMOUNT_DEFAULT;
623 Star_dim = STAR_DIM_DEFAULT;
624 Star_cap = STAR_CAP_DEFAULT;
625 Star_flags = STAR_FLAG_DEFAULT;
626 Star_max_length = STAR_MAX_LENGTH_DEFAULT;
627 } else if ( !strcmp( Dc_arg, "m2" )) {
631 Star_flags = STAR_FLAG_TAIL|STAR_FLAG_DIM|STAR_FLAG_ANTIALIAS;
632 Star_max_length = STAR_MAX_LENGTH_DEFAULT;
633 } else if ( !strcmp( Dc_arg, "num" )) {
635 if ( (Dc_arg_int < 0) || (Dc_arg_int > MAX_STARS) ) {
638 Num_stars = Dc_arg_int;
641 // print usage, not stats
647 dc_printf( "Usage: stars keyword\nWhere keyword can be in the following forms:\n" );
648 dc_printf( "stars default Resets stars to all default values\n" );
649 dc_printf( "stars num X Sets number of stars to X. Between 0 and %d.\n", MAX_STARS );
650 dc_printf( "stars tail X Where X is the percent of 'tail' between 0 and 1.0\n" );
651 dc_printf( "stars dim X Where X is the amount stars dim between 0 and 255.0\n" );
652 dc_printf( "stars cap X Where X is the cap of dimming between 0 and 255.\n" );
653 dc_printf( "stars len X Where X is the cap of length.\n" );
654 dc_printf( "stars m0 Macro0. Old 'pixel type' crappy stars. flags=none\n" );
655 dc_printf( "stars m1 Macro1. (default) tail=.75, dim=20.0, cap=75.0, flags=dim,tail\n" );
656 dc_printf( "stars m2 Macro2. tail=.75, dim=20.0, cap=75.0, flags=dim,tail,aa\n" );
657 dc_printf( "stars flag X Toggles flag X, where X is tail or dim or aa (aa=antialias)\n" );
658 dc_printf( "\nHINT: set cap to 0 to get dim rate and tail down, then use\n" );
659 dc_printf( "cap to keep the lines from going away when moving too fast.\n" );
660 dc_printf( "\nUse '? stars' to see current values.\n" );
661 Dc_status = 0; // don't print status if help is printed. Too messy.
665 dc_printf( "Num_stars: %d\n", Num_stars );
666 dc_printf( "Tail: %.2f\n", Star_amount );
667 dc_printf( "Dim: %.2f\n", Star_dim );
668 dc_printf( "Cap: %.2f\n", Star_cap );
669 dc_printf( "Max length: %.2f\n", Star_max_length );
670 dc_printf( "Flags:\n" );
671 dc_printf( " Tail: %s\n", (Star_flags&STAR_FLAG_TAIL?"On":"Off") );
672 dc_printf( " Dim: %s\n", (Star_flags&STAR_FLAG_DIM?"On":"Off") );
673 dc_printf( " Antialias: %s\n", (Star_flags&STAR_FLAG_ANTIALIAS?"On":"Off") );
674 dc_printf( "\nTHESE AREN'T SAVED TO DISK, SO IF YOU TWEAK\n" );
675 dc_printf( "THESE AND LIKE THEM, WRITE THEM DOWN!!\n" );
680 int reload_old_debris = 1; // If set to one, then reload all the last_pos of the debris
682 // Call this if camera "cuts" or moves long distances
683 // so blur effect doesn't draw lines all over the screen.
684 void stars_camera_cut()
686 last_stars_filled = 0;
687 reload_old_debris = 1;
690 //#define TIME_STAR_CODE // enable to time star code
693 extern float Viewer_zoom;
695 // get the world coords of the sun pos on the unit sphere.
696 void stars_get_sun_pos(int sun_n, vector *pos)
702 Assert(sun_n < Num_suns);
703 if((sun_n >= Num_suns) || (sun_n < 0)){
707 // rotate the sun properly
708 temp = vmd_zero_vector;
712 vm_angles_2_matrix(&rot, &Suns[sun_n].ang);
713 vm_vec_rotate(pos, &temp, &rot);
717 void stars_draw_sun( int show_sun )
723 starfield_bitmap *bm;
724 float local_scale = 1.0f;
730 for(idx=0; idx<Num_suns; idx++){
732 bm = stars_lookup_sun(&Suns[idx]);
738 sun_pos = vmd_zero_vector;
739 sun_pos.xyz.y = 1.0f;
740 stars_get_sun_pos(idx, &sun_pos);
744 vm_vec_normalize(&sun_dir);
746 // add the light source corresponding to the sun
747 light_add_directional(&sun_dir, bm->i, bm->r, bm->g, bm->b);
750 if(supernova_active()){
751 local_scale = 1.0f + (SUPERNOVA_SUN_SCALE * supernova_pct_complete());
754 // draw the sun itself, keep track of how many we drew
755 gr_set_bitmap(bm->bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.999f);
756 g3_rotate_faraway_vertex(&sun_vex, &sun_pos);
757 if(!g3_draw_bitmap(&sun_vex, 0, 0.05f * Suns[idx].scale_x * local_scale, TMAP_FLAG_TEXTURED)){
763 // draw the corresponding glow for sun_n
764 void stars_draw_sun_glow(int sun_n)
766 starfield_bitmap *bm;
767 vector sun_pos, sun_dir;
769 float local_scale = 1.0f;
772 Assert(sun_n < Num_suns);
773 if((sun_n >= Num_suns) || (sun_n < 0)){
778 bm = stars_lookup_sun(&Suns[sun_n]);
784 sun_pos = vmd_zero_vector;
785 sun_pos.xyz.y = 1.0f;
786 stars_get_sun_pos(sun_n, &sun_pos);
790 vm_vec_normalize(&sun_dir);
793 if(supernova_active()){
794 local_scale = 1.0f + (SUPERNOVA_SUN_SCALE * supernova_pct_complete());
797 // draw the sun itself, keep track of how many we drew
798 gr_set_bitmap(bm->glow_bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.5f);
799 g3_rotate_faraway_vertex(&sun_vex, &sun_pos);
800 g3_draw_bitmap(&sun_vex, 0, 0.10f * Suns[sun_n].scale_x * local_scale, TMAP_FLAG_TEXTURED);
804 void stars_draw_bitmaps( int show_bitmaps )
809 // if we're in the nebula, don't render any backgrounds
810 if(The_mission.flags & MISSION_FLAG_FULLNEB){
815 if(!Detail.planets_suns){
819 // render all bitmaps
820 for(idx=0; idx<Num_starfield_bitmaps; idx++){
821 // lookup the info index
822 star_index = stars_find_bitmap(Starfield_bitmap_instance[idx].filename);
829 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.9999f);
830 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);
832 if(Starfield_bitmaps[star_index].xparent){
833 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap);
834 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);
836 gr_set_bitmap(Starfield_bitmaps[star_index].bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.9999f);
837 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);
844 void calculate_bitmap_matrix(starfield_bitmaps *bm, vector *v)
846 vm_vector_2_matrix(&bm->m, v, NULL, NULL);
847 vm_orthogonalize_matrix(&bm->m);
850 void calculate_bitmap_points(starfield_bitmaps *bm, float bank)
853 vector fvec, uvec, rvec, tmp;
856 vm_orthogonalize_matrix(&bm->m);
858 tangles.p = tangles.h = 0.0f;
860 vm_rotate_matrix_by_angles(&bm->m, &tangles);
864 vm_vec_scale(&fvec, bm->dist );
868 vm_vec_sub(&tmp, &fvec, &uvec);
869 vm_vec_sub(&bm->points[3], &tmp, &rvec);
871 vm_vec_sub(&tmp, &fvec, &uvec);
872 vm_vec_add(&bm->points[2], &tmp, &rvec);
874 vm_vec_add(&tmp, &fvec, &uvec);
875 vm_vec_add(&bm->points[1], &tmp, &rvec);
877 vm_vec_add(&tmp, &fvec, &uvec);
878 vm_vec_sub(&bm->points[0], &tmp, &rvec);
881 vm_vec_normalize(&bm->points[i]);
886 extern int Interp_subspace;
887 extern float Interp_subspace_offset_u;
888 extern float Interp_subspace_offset_u;
889 extern float Interp_subspace_offset_v;
891 float subspace_offset_u = 0.0f;
892 float subspace_offset_u_inner = 0.0f;
893 float subspace_offset_v = 0.0f;
895 float subspace_u_speed = 0.07f; // how fast u changes
896 float subspace_v_speed = 0.05f; // how fast v changes
898 int Subspace_glow_bitmap = -1;
900 float Subspace_glow_frame = 0.0f;
901 float Subspace_glow_rate = 1.0f;
905 DCF(subspace_set,"Set parameters for subspace effect")
908 dc_get_arg(ARG_STRING);
909 if ( !strcmp( Dc_arg, "u" )) {
910 dc_get_arg(ARG_FLOAT);
911 if ( Dc_arg_float < 0.0f ) {
914 subspace_u_speed = Dc_arg_float;
916 } else if ( !strcmp( Dc_arg, "v" )) {
917 dc_get_arg(ARG_FLOAT);
918 if ( Dc_arg_float < 0.0f ) {
921 subspace_v_speed = Dc_arg_float;
924 // print usage, not stats
930 dc_printf( "Usage: subspace keyword\nWhere keyword can be in the following forms:\n" );
931 dc_printf( "subspace u X Where X is how fast u moves.\n", MAX_STARS );
932 dc_printf( "subspace v X Where X is how fast v moves.\n" );
933 dc_printf( "\nUse '? subspace' to see current values.\n" );
934 Dc_status = 0; // don't print status if help is printed. Too messy.
938 dc_printf( "u: %.2f\n", subspace_u_speed );
939 dc_printf( "v: %.2f\n", subspace_v_speed );
944 void subspace_render()
946 if ( Subspace_model_inner == -1 ) {
947 Subspace_model_inner = model_load( "subspace_small.pof", 0, NULL );
948 Assert(Subspace_model_inner>-1);
951 if ( Subspace_model_outer == -1 ) {
952 Subspace_model_outer = model_load( "subspace_big.pof", 0, NULL );
953 Assert(Subspace_model_outer>-1);
956 if ( Subspace_glow_bitmap == -1 ) {
957 Subspace_glow_bitmap = bm_load( NOX("SunGlow01"));
958 Assert(Subspace_glow_bitmap>-1);
961 Subspace_glow_frame += flFrametime * 1.0f;
963 float total_time = i2fl(NOISE_NUM_FRAMES) / 15.0f;
966 if ( Subspace_glow_frame < 0.0f ) Subspace_glow_frame = 0.0f;
967 if ( Subspace_glow_frame > 100.0f ) Subspace_glow_frame = 0.0f;
969 while ( Subspace_glow_frame > total_time ) {
970 Subspace_glow_frame -= total_time;
972 int framenum = fl2i( (Subspace_glow_frame*NOISE_NUM_FRAMES) / total_time );
973 if ( framenum < 0 ) framenum = 0;
974 if ( framenum >= NOISE_NUM_FRAMES ) framenum = NOISE_NUM_FRAMES-1;
976 subspace_offset_u += flFrametime*subspace_u_speed;
977 if (subspace_offset_u > 1.0f ) {
978 subspace_offset_u -= 1.0f;
981 subspace_offset_u_inner += flFrametime*subspace_u_speed*3.0f;
982 if (subspace_offset_u > 1.0f ) {
983 subspace_offset_u -= 1.0f;
986 subspace_offset_v += flFrametime*subspace_v_speed;
987 if (subspace_offset_v > 1.0f ) {
988 subspace_offset_v -= 1.0f;
994 angles angs = { 0.0f, 0.0f, 0.0f };
995 angs.b = subspace_offset_v * PI2;
997 vm_angles_2_matrix(&tmp,&angs);
999 int saved_gr_zbuffering = gr_zbuffer_get();
1001 gr_zbuffer_set(GR_ZBUFF_NONE);
1003 if ( !D3D_enabled ) {
1005 int render_flags = MR_NO_LIGHTING | MR_ALWAYS_REDRAW;
1007 Interp_subspace = 1;
1008 Interp_subspace_offset_u = 1.0f - subspace_offset_u;
1009 Interp_subspace_offset_v = 0.0f;
1011 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1012 render_flags |= MR_SHOW_THRUSTERS;
1013 model_render( Subspace_model_outer, &tmp, &Eye_position, render_flags ); //MR_NO_CORRECT|MR_SHOW_OUTLINE
1017 int render_flags = MR_NO_LIGHTING | MR_ALWAYS_REDRAW;
1019 Interp_subspace = 1;
1020 Interp_subspace_offset_u = 1.0f - subspace_offset_u;
1021 Interp_subspace_offset_v = 0.0f;
1023 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1024 render_flags |= MR_SHOW_THRUSTERS;
1025 model_render( Subspace_model_outer, &tmp, &Eye_position, render_flags ); //MR_NO_CORRECT|MR_SHOW_OUTLINE
1027 Interp_subspace = 1;
1028 Interp_subspace_offset_u = 1.0f - subspace_offset_u_inner;
1029 Interp_subspace_offset_v = 0.0f;
1031 angs.b = -subspace_offset_v * PI2;
1033 vm_angles_2_matrix(&tmp,&angs);
1035 model_set_outline_color(255,255,255);
1037 model_set_thrust( Subspace_model_inner, 1.0f, -1, Subspace_glow_bitmap, Noise[framenum] );
1038 render_flags |= MR_SHOW_THRUSTERS;
1040 model_render( Subspace_model_inner, &tmp, &Eye_position, render_flags ); //MR_NO_CORRECT|MR_SHOW_OUTLINE
1043 Interp_subspace = 0;
1044 gr_zbuffer_set(saved_gr_zbuffering);
1047 void stars_draw( int show_stars, int show_suns, int show_nebulas, int show_subspace )
1053 int gr_zbuffering_save = gr_zbuffer_get();
1054 gr_zbuffer_set(GR_ZBUFF_NONE);
1056 if ( show_subspace ) {
1061 if (Num_stars >= MAX_STARS){
1062 Num_stars = MAX_STARS;
1065 #ifdef TIME_STAR_CODE
1067 xt1 = timer_get_fixed_seconds();
1070 if ( show_nebulas && (Game_detail_flags & DETAIL_FLAG_NEBULAS) && (Neb2_render_mode != NEB2_RENDER_POF) && (Neb2_render_mode != NEB2_RENDER_LAME)) {
1074 // draw background stuff
1075 if((Neb2_render_mode != NEB2_RENDER_POLY) && (Neb2_render_mode != NEB2_RENDER_LAME) && show_stars){
1076 // semi-hack, do we don't fog the background
1077 int neb_save = Neb2_render_mode;
1078 Neb2_render_mode = NEB2_RENDER_NONE;
1079 extern void stars_draw_background();
1080 stars_draw_background();
1081 Neb2_render_mode = neb_save;
1084 if (show_stars && ( Game_detail_flags & DETAIL_FLAG_STARS) && !(The_mission.flags & MISSION_FLAG_FULLNEB) && (supernova_active() < 3)) {
1089 if ( !last_stars_filled ) {
1090 for (sp=Stars,i=0; i<Num_stars; i++, sp++ ) {
1092 g3_rotate_faraway_vertex(&p2, &sp->pos);
1093 sp->last_star_pos.xyz.x = p2.x;
1094 sp->last_star_pos.xyz.y = p2.y;
1095 sp->last_star_pos.xyz.z = p2.z;
1101 tmp_num_stars = (Detail.num_stars*Num_stars)/MAX_DETAIL_LEVEL;
1102 if (tmp_num_stars < 0 ) {
1104 } else if ( tmp_num_stars > Num_stars ) {
1105 tmp_num_stars = Num_stars;
1108 for (sp=Stars,i=0; i<tmp_num_stars; i++, sp++ ) {
1112 memset(&p1, 0, sizeof(vertex));
1114 // This makes a star look "proper" by not translating the
1115 // point around the viewer's eye before rotation. In other
1116 // words, when the ship translates, the stars do not change.
1118 g3_rotate_faraway_vertex(&p2, &sp->pos);
1122 g3_project_vertex(&p2);
1123 if ( p2.flags & PF_OVERFLOW ) {
1130 if ( can_draw && (Star_flags & (STAR_FLAG_TAIL|STAR_FLAG_DIM)) ) {
1132 dist = vm_vec_dist_quick( &sp->last_star_pos, (vector *)&p2.x );
1135 if ( dist > Star_max_length ) {
1136 ratio = Star_max_length / dist;
1137 dist = Star_max_length;
1141 ratio *= Star_amount;
1143 p1.x = p2.x + (sp->last_star_pos.xyz.x-p2.x)*ratio;
1144 p1.y = p2.y + (sp->last_star_pos.xyz.y-p2.y)*ratio;
1145 p1.z = p2.z + (sp->last_star_pos.xyz.z-p2.z)*ratio;
1147 p1.flags = 0; // not projected
1148 g3_code_vertex( &p1 );
1153 g3_project_vertex(&p1);
1154 if ( p1.flags & PF_OVERFLOW ) {
1160 sp->last_star_pos.xyz.x = p2.x;
1161 sp->last_star_pos.xyz.y = p2.y;
1162 sp->last_star_pos.xyz.z = p2.z;
1164 if ( !can_draw ) continue;
1168 if ( Star_flags & STAR_FLAG_DIM ) {
1170 float colorf = 255.0f - dist*Star_dim;
1172 if ( colorf < Star_cap )
1175 color = (fl2i(colorf)*(i&7))/256;
1181 if ( (Star_flags & STAR_FLAG_ANTIALIAS) || (D3D_enabled) ) {
1182 gr_set_color_fast( &star_aacolors[color] );
1184 // if the two points are the same, fudge it, since some D3D cards (G200 and G400) are lame.
1185 if( (fl2i(p1.sx) == fl2i(p2.sx)) && (fl2i(p1.sy) == fl2i(p2.sy)) ){
1190 // use alphablended line so that dark stars don't look bad on top of nebulas
1191 gr_set_color_fast( &star_aacolors[color] );
1192 if ( Star_flags & STAR_FLAG_TAIL ) {
1193 gr_line(fl2i(p1.sx),fl2i(p1.sy),fl2i(p2.sx),fl2i(p2.sy));
1195 gr_pixel( fl2i(p2.sx),fl2i(p2.sy) );
1201 last_stars_filled = 1;
1203 #ifdef TIME_STAR_CODE
1204 xt2 = timer_get_fixed_seconds();
1205 mprintf(( "Stars: %d\n", xt2-xt1 ));
1209 if ( (Game_detail_flags & DETAIL_FLAG_MOTION) && (!Fred_running) && (supernova_active() < 3) ) {
1211 gr_set_color( 0, 0, 0 );
1214 if(The_mission.flags & MISSION_FLAG_FULLNEB){
1215 gr_fog_set(GR_FOGMODE_NONE, 0, 0, 0);
1218 old_debris * d = odebris;
1219 for (i=0; i<MAX_DEBRIS; i++, d++ ) {
1223 d->pos.xyz.x = f2fl(myrand() - RAND_MAX/2);
1224 d->pos.xyz.y = f2fl(myrand() - RAND_MAX/2);
1225 d->pos.xyz.z = f2fl(myrand() - RAND_MAX/2);
1227 vm_vec_normalize(&d->pos);
1229 vm_vec_scale(&d->pos, MAX_DIST);
1230 vm_vec_add2(&d->pos, &Eye_position );
1231 // vm_vec_add2(&d->pos, &Player_obj->pos );
1233 d->vclip = i % MAX_DEBRIS_VCLIPS; //rand()
1235 // if we're in full neb mode
1236 if((The_mission.flags & MISSION_FLAG_FULLNEB) && (Neb2_render_mode != NEB2_RENDER_NONE)){
1237 d->size = i2fl(myrand() % 4)*BASE_SIZE_NEB;
1239 d->size = i2fl(myrand() % 4)*BASE_SIZE;
1242 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1245 if ( reload_old_debris ) {
1246 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1249 g3_rotate_vertex(&p, &d->pos);
1252 int frame = Missiontime / (DEBRIS_ROT_MIN + (i % DEBRIS_ROT_RANGE) * DEBRIS_ROT_RANGE_SCALER);
1253 frame %= debris_vclips[d->vclip].nframes;
1255 if((The_mission.flags & MISSION_FLAG_FULLNEB) && (Neb2_render_mode != NEB2_RENDER_NONE)){
1256 gr_set_bitmap( debris_vclips[d->vclip].bm + frame, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.3f);
1258 gr_set_bitmap( debris_vclips[d->vclip].bm + frame );
1262 vm_vec_add( &tmp, &d->last_pos, &Eye_position );
1263 g3_draw_laser( &d->pos,d->size,&tmp,d->size, TMAP_FLAG_TEXTURED|TMAP_FLAG_XPARENT, 25.0f );
1266 vm_vec_sub( &d->last_pos, &d->pos, &Eye_position );
1268 vdist = vm_vec_mag_quick(&d->last_pos);
1270 if (vdist > MAX_DIST_RANGE)
1272 else if (vdist < MIN_DIST_RANGE)
1276 // vm_vec_sub( &tmp, &d->pos, &Player_obj->pos );
1277 // vdist = vm_vec_dot( &tmp, &Player_obj->orient.fvec );
1278 // if ( vdist < 0.0f )
1283 reload_old_debris = 0;
1287 stars_draw_sun( show_suns );
1288 stars_draw_bitmaps( show_suns );
1290 gr_zbuffer_set( gr_zbuffering_save );
1293 void stars_page_in()
1297 // Initialize the subspace stuff
1299 if ( Game_subspace_effect ) {
1301 Subspace_model_inner = model_load( "subspace_small.pof", 0, NULL );
1302 Assert(Subspace_model_inner>-1);
1303 Subspace_model_outer = model_load( "subspace_big.pof", 0, NULL );
1304 Assert(Subspace_model_outer>-1);
1308 pm = model_get(Subspace_model_inner);
1310 nprintf(( "Paging", "Paging in textures for subspace effect.\n" ));
1312 for (j=0; j<pm->n_textures; j++ ) {
1313 int bitmap_num = pm->original_textures[j];
1315 if ( bitmap_num > -1 ) {
1316 bm_page_in_texture( bitmap_num );
1320 pm = model_get(Subspace_model_outer);
1322 nprintf(( "Paging", "Paging in textures for subspace effect.\n" ));
1324 for (j=0; j<pm->n_textures; j++ ) {
1325 int bitmap_num = pm->original_textures[j];
1327 if ( bitmap_num > -1 ) {
1328 bm_page_in_texture( bitmap_num );
1332 Subspace_model_inner = -1;
1333 Subspace_model_outer = -1;
1336 Subspace_glow_bitmap = bm_load( NOX("SunGlow01"));
1337 bm_page_in_xparent_texture(Subspace_glow_bitmap);
1339 // page in starfield bitmaps
1342 while((idx < MAX_STARFIELD_BITMAPS) && (Starfield_bitmaps[idx].bitmap != -1)){
1343 if(Starfield_bitmaps[idx].xparent){
1344 bm_page_in_xparent_texture(Starfield_bitmaps[idx].bitmap);
1346 bm_page_in_texture(Starfield_bitmaps[idx].bitmap);
1353 // sun bitmaps and glows
1355 while((idx < MAX_STARFIELD_BITMAPS) && (Sun_bitmaps[idx].bitmap != -1) && (Sun_bitmaps[idx].glow_bitmap != -1)){
1356 bm_page_in_texture(Sun_bitmaps[idx].bitmap);
1357 bm_page_in_texture(Sun_bitmaps[idx].glow_bitmap);
1363 for (i=0; i<MAX_DEBRIS_VCLIPS; i++ ) {
1364 for (j=0; j<debris_vclips[i].nframes; j++ ) {
1365 bm_page_in_xparent_texture(debris_vclips[i].bm + j);
1370 // background nebula models and planets
1371 void stars_draw_background()
1373 if((Nmodel_num < 0) || (Nmodel_bitmap < 0)){
1377 // draw the model at the player's eye wif no z-buffering
1378 model_set_alpha(1.0f);
1379 model_set_forced_texture(Nmodel_bitmap);
1380 model_render(Nmodel_num, &vmd_identity_matrix, &Eye_position, MR_NO_ZBUFFER | MR_NO_CULL | MR_ALL_XPARENT | MR_NO_LIGHTING | MR_FORCE_TEXTURE);
1381 model_set_forced_texture(-1);
1384 // call this to set a specific model as the background model
1385 void stars_set_background_model(char *model_name, char *texture_name)
1387 Nmodel_num = model_load(model_name, 0, NULL);
1388 Nmodel_bitmap = bm_load(texture_name);
1391 // lookup a starfield bitmap, return index or -1 on fail
1392 int stars_find_bitmap(char *name)
1397 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
1398 if(!strcmp(name, Starfield_bitmaps[idx].filename)){
1407 // lookup a sun by bitmap filename, return index or -1 on fail
1408 int stars_find_sun(char *name)
1413 for(idx=0; idx<MAX_STARFIELD_BITMAPS; idx++){
1414 if(!strcmp(name, Sun_bitmaps[idx].filename)){