]> icculus.org git repositories - taylor/freespace2.git/blob - src/nebula/neb.cpp
safer strings using SDL string functions
[taylor/freespace2.git] / src / nebula / neb.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Nebula/Neb.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Nebula effect
16  *
17  * $Log$
18  * Revision 1.9  2004/09/20 01:31:44  theoddone33
19  * GCC 3.4 fixes.
20  *
21  * Revision 1.8  2003/05/25 02:30:43  taylor
22  * Freespace 1 support
23  *
24  * Revision 1.7  2002/06/17 06:33:09  relnev
25  * ryan's struct patch for gcc 2.95
26  *
27  * Revision 1.6  2002/06/09 04:41:23  relnev
28  * added copyright header
29  *
30  * Revision 1.5  2002/06/01 03:32:00  relnev
31  * fix texture loading mistake.
32  *
33  * enable some d3d stuff for opengl also
34  *
35  * Revision 1.4  2002/05/26 20:22:48  theoddone33
36  * Most of network/ works
37  *
38  * Revision 1.3  2002/05/07 03:16:47  theoddone33
39  * The Great Newline Fix
40  *
41  * Revision 1.2  2002/05/04 04:36:56  theoddone33
42  * More changes, took out a lot of the sound stuff which will bite later but
43  * I don't care.
44  *
45  * Revision 1.1.1.1  2002/05/03 03:28:10  root
46  * Initial import.
47  * 
48  * 
49  * 50    8/30/99 5:01p Dave
50  * Made d3d do less state changing in the nebula. Use new chat server for
51  * PXO.
52  * 
53  * 49    8/10/99 6:54p Dave
54  * Mad optimizations. Added paging to the nebula effect.
55  * 
56  * 48    8/05/99 2:05a Dave
57  * Whee.
58  * 
59  * 47    7/30/99 10:55a Anoop
60  * Hmm. Fixed release build problem again, with area-rotated bitmaps.
61  * 
62  * 46    7/29/99 10:47p Dave
63  * Standardized D3D fogging using vertex fog. Shook out Savage 4 bugs.
64  * 
65  * 44    7/29/99 12:05a Dave
66  * Nebula speed optimizations.
67  * 
68  * 43    7/19/99 7:20p Dave
69  * Beam tooling. Specialized player-killed-self messages. Fixed d3d nebula
70  * pre-rendering.
71  * 
72  * 42    7/18/99 5:20p Dave
73  * Jump node icon. Fixed debris fogging. Framerate warning stuff.
74  * 
75  * 41    7/13/99 1:15p Dave
76  * 32 bit support. Whee!
77  * 
78  * 40    7/09/99 5:54p Dave
79  * Seperated cruiser types into individual types. Added tons of new
80  * briefing icons. Campaign screen.
81  * 
82  * 39    7/07/99 10:44a Jamesa
83  * Make sure the nebula regens properly after loading a mission.
84  * 
85  * 38    6/11/99 2:32p Dave
86  * Toned down nebula brightness a bit.
87  * 
88  * 37    5/26/99 3:39p Dave
89  * Fixed nebula regeneration problem. Removed optimizations from
90  * neblightning.cpp
91  * 
92  * 36    5/26/99 11:46a Dave
93  * Added ship-blasting lighting and made the randomization of lighting
94  * much more customizable.
95  * 
96  * 35    5/24/99 5:45p Dave
97  * Added detail levels to the nebula, with a decent speedup. Split nebula
98  * lightning into its own section.
99  * 
100  * $NoKeywords: $
101  */
102
103 #include "neb.h"
104 #include "vecmat.h"
105 #include "3d.h"
106 #include "bmpman.h"
107 #include "2d.h"
108 #include "object.h"
109 #include "timer.h"
110 #include "multi.h"
111 #include "freespace.h"
112 #include "key.h"
113 #include "nebula.h"
114 #include "starfield.h"
115 #include "parselo.h"
116 #include "beam.h"
117 #include "sound.h"
118 #include "gamesnd.h"
119 #include "grinternal.h"
120
121 #include "alphacolors.h"
122
123 // --------------------------------------------------------------------------------------------------------
124 // NEBULA DEFINES/VARS
125 //
126
127 // #define NEB2_THUMBNAIL
128
129 /*
130 3D CARDS THAT FOG PROPERLY
131 Voodoo1
132 Voodoo2
133 G200
134 TNT
135
136 3D CARDS THAT DON'T FOG PROPERLY
137 Permedia2
138 AccelStar II
139 */
140
141 // if nebula rendering is active (DCF stuff - not mission specific)
142 int Neb2_render_mode = NEB2_RENDER_NONE;
143
144 // array of neb2 poofs
145 char Neb2_poof_filenames[MAX_NEB2_POOFS][MAX_FILENAME_LEN] = {
146         "", "", "", "", "", ""
147 };
148 int Neb2_poofs[MAX_NEB2_POOFS] = { -1, -1, -1, -1, -1, -1 };
149 int Neb2_poof_flags = ( (1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<4) | (1<<5) );
150 int Neb2_poof_count = 0;
151
152 // array of neb2 bitmaps
153 char Neb2_bitmap_filenames[MAX_NEB2_BITMAPS][MAX_FILENAME_LEN] = {
154         "", "", "", "", "", ""
155 };
156 int Neb2_bitmap[MAX_NEB2_BITMAPS] = { -1, -1, -1, -1, -1, -1 };
157 int Neb2_bitmap_count = 0;
158
159 // texture to use for this level
160 char Neb2_texture_name[MAX_FILENAME_LEN] = "";
161
162 // nebula flags
163 #define NF_USED                                         (1<<0)          // if this nebula slot is used
164
165 float max_rotation = 3.75f;
166 float neb2_flash_fade = 0.3f;
167
168 // fog values for different ship types
169 float Neb_ship_fog_vals[MAX_SHIP_TYPE_COUNTS][2] = {
170         {0.0f, 0.0f},                           // SHIP_TYPE_NONE
171         {10.0f, 500.0f},                        // SHIP_TYPE_CARGO
172         {10.0f, 500.0f},                        // SHIP_TYPE_FIGHTER_BOMBER
173         {10.0f, 600.0f},                        // SHIP_TYPE_CRUISER
174         {10.0f, 600.0f},                        // SHIP_TYPE_FREIGHTER
175         {10.0f, 750.0f},                        // SHIP_TYPE_CAPITAL
176         {10.0f, 500.0f},                        // SHIP_TYPE_TRANSPORT
177         {10.0f, 500.0f},                        // SHIP_TYPE_REPAIR_REARM
178         {10.0f, 500.0f},                        // SHIP_TYPE_NAVBUOY
179         {10.0f, 500.0f},                        // SHIP_TYPE_SENTRYGUN
180         {10.0f, 600.0f},                        // SHIP_TYPE_ESCAPEPOD
181         {10.0f, 1000.0f},                       // SHIP_TYPE_SUPERCAP
182         {10.0f, 500.0f},                        // SHIP_TYPE_STEALTH
183         {10.0f, 500.0f},                        // SHIP_TYPE_FIGHTER
184         {10.0f, 500.0f},                        // SHIP_TYPE_BOMBER
185         {10.0f, 750.0f},                        // SHIP_TYPE_DRYDOCK
186         {10.0f, 600.0f},                        // SHIP_TYPE_AWACS
187         {10.0f, 600.0f},                        // SHIP_TYPE_GAS_MINER
188         {10.0f, 600.0f},                        // SHIP_TYPE_CORVETTE
189         {10.0f, 1000.0f},                       // SHIP_TYPE_KNOSSOS_DEVICE
190 };
191
192 // fog near and far values for rendering the background nebula
193 #define NEB_BACKG_FOG_NEAR                              4.5f
194 #define NEB_BACKG_FOG_FAR                                       10.0f
195 float Neb_backg_fog_near = NEB_BACKG_FOG_NEAR;
196 float Neb_backg_fog_far = NEB_BACKG_FOG_FAR;
197
198 // stats
199 int pneb_tried = 0;                             // total pnebs tried to render
200 int pneb_tossed_alpha = 0;              // pnebs tossed because of alpha optimization
201 int pneb_tossed_dot = 0;                // pnebs tossed because of dot product
202 int pneb_tossed_off = 0;                // pnebs tossed because of being offscree
203 int neb_tried = 0;                              // total nebs tried
204 int neb_tossed_alpha = 0;               // nebs tossed because of alpha
205 int neb_tossed_dot = 0;                 // nebs tossed because of dot product
206 int neb_tossed_count = 0;               // nebs tossed because of max render count 
207
208 // the AWACS suppresion level for the nebula
209 float Neb2_awacs = -1.0f;
210
211 // how many "slices" are in the current player nebuls
212 int Neb2_slices = 5;
213
214 cube_poof Neb2_cubes[MAX_CPTS][MAX_CPTS][MAX_CPTS];
215
216 // nebula detail level
217 typedef struct neb2_detail {
218         float max_alpha_glide;                                  // max alpha for this detail level in Glide
219         float max_alpha_d3d;                                            // max alpha for this detail level in D3d
220         float break_alpha;                                              // break alpha (below which, poofs don't draw). this affects the speed and visual quality a lot
221         float break_x, break_y;                                 // x and y alpha fade/break values. adjust alpha on the polys as they move offscreen 
222         float cube_dim;                                                 // total dimension of player poof cube
223         float cube_inner;                                                       // inner radius of the player poof cube
224         float cube_outer;                                                       // outer radius of the player pood cube
225         float prad;                                                                     // radius of the poofs
226         float wj, hj, dj;                                                       // width, height, depth jittering. best left at 1.0     
227 } neb2_detail;
228 neb2_detail     Neb2_detail[MAX_DETAIL_LEVEL] = {
229         { // lowest detail level
230                 0.575f,                                                                         // max alpha for this detail level in Glide
231                 0.71f,                                                                  // max alpha for this detail level in D3d
232                 0.13f,                                                                  // break alpha (below which, poofs don't draw). this affects the speed and visual quality a lot
233                 150.0f, 150.0f / 1.3333f,                       // x and y alpha fade/break values. adjust alpha on the polys as they move offscreen 
234                 510.0f,                                                                 // total dimension of player poof cube
235                 50.0f,                                                                  // inner radius of the player poof cube
236                 250.0f,                                                                 // outer radius of the player pood cube
237                 120.0f,                                                                 // radius of the poofs
238                 1.0f, 1.0f, 1.0f                                                // width, height, depth jittering. best left at 1.0     
239         },      
240         { // 2nd lowest detail level
241                 0.575f,                                                                         // max alpha for this detail level in Glide
242                 0.71f,                                                                  // max alpha for this detail level in D3d
243                 0.125f,                                                                 // break alpha (below which, poofs don't draw). this affects the speed and visual quality a lot
244                 300.0f, 300.0f / 1.3333f,                       // x and y alpha fade/break values. adjust alpha on the polys as they move offscreen 
245                 550.0f,                                                                 // total dimension of player poof cube
246                 100.0f,                                                                 // inner radius of the player poof cube
247                 250.0f,                                                                 // outer radius of the player pood cube
248                 125.0f,                                                                 // radius of the poofs
249                 1.0f, 1.0f, 1.0f                                                // width, height, depth jittering. best left at 1.0     
250         },
251         { // 2nd highest detail level
252                 0.575f,                                                                         // max alpha for this detail level in Glide
253                 0.71f,                                                                  // max alpha for this detail level in D3d
254                 0.1f,                                                                           // break alpha (below which, poofs don't draw). this affects the speed and visual quality a lot
255                 300.0f, 300.0f / 1.3333f,                       // x and y alpha fade/break values. adjust alpha on the polys as they move offscreen 
256                 550.0f,                                                                 // total dimension of player poof cube
257                 150.0f,                                                                 // inner radius of the player poof cube
258                 250.0f,                                                                 // outer radius of the player pood cube
259                 125.0f,                                                                 // radius of the poofs
260                 1.0f, 1.0f, 1.0f                                                // width, height, depth jittering. best left at 1.0     
261         },
262         { // higest detail level
263                 0.475f,                                                                 // max alpha for this detail level in Glide
264                 0.575f,                                                                 // max alpha for this detail level in D3d
265                 0.05f,                                                                  // break alpha (below which, poofs don't draw). this affects the speed and visual quality a lot
266                 200.0f, 200.0f / 1.3333f,                       // x and y alpha fade/break values. adjust alpha on the polys as they move offscreen 
267                 750.0f,                                                                 // total dimension of player poof cube
268                 200.0f,                                                                 // inner radius of the player poof cube
269                 360.0f,                                                                 // outer radius of the player pood cube
270                 150.0f,                                                                 // radius of the poofs
271                 1.0f, 1.0f, 1.0f                                                // width, height, depth jittering. best left at 1.0     
272         },              
273 };
274 neb2_detail *Nd = &Neb2_detail[MAX_DETAIL_LEVEL - 2];
275
276 int Neb2_background_color[3] = {0, 0, 255};                     // rgb background color (used for lame rendering)
277
278 int Neb2_regen = 0;
279
280 // --------------------------------------------------------------------------------------------------------
281 // NEBULA FORWARD DECLARATIONS
282 //
283
284 // return the alpha the passed poof should be rendered with, for a 2 shell nebula
285 float neb2_get_alpha_2shell(float inner_radius, float outer_radius, float magic_num, vector *v);
286
287 // return an alpha value for a bitmap offscreen based upon "break" value
288 float neb2_get_alpha_offscreen(float sx, float sy, float incoming_alpha);
289
290 // do a pre-render of the background nebula
291 void neb2_pre_render(vector *eye_pos, matrix *eye_orient);
292
293 // fill in the position of the eye for this frame
294 void neb2_get_eye_pos(vector *eye);
295
296 // fill in the eye orient for this frame
297 void neb2_get_eye_orient(matrix *eye);
298
299 // get a (semi) random bitmap to use for a poof
300 int neb2_get_bitmap();
301
302 // regenerate the player nebula
303 void neb2_regen();
304
305
306 // --------------------------------------------------------------------------------------------------------
307 // NEBULA FUNCTIONS
308 //
309
310 // initialize neb2 stuff at game startup
311 void neb2_init()
312 {       
313 #ifndef MAKE_FS1
314         char name[255] = "";
315
316         // read in the nebula.tbl
317         read_file_text("nebula.tbl");
318         reset_parse();
319
320         // background bitmaps
321         Neb2_bitmap_count = 0;
322         while(!optional_string("#end")){
323                 // nebula
324                 required_string("+Nebula:");
325                 stuff_string(name, F_NAME, NULL);
326
327                 if(Neb2_bitmap_count < MAX_NEB2_BITMAPS){
328                         SDL_strlcpy(Neb2_bitmap_filenames[Neb2_bitmap_count++], name, sizeof(Neb2_bitmap_filenames[0]));
329                 }
330         }
331
332         // poofs
333         Neb2_poof_count = 0;
334         while(!optional_string("#end")){
335                 // nebula
336                 required_string("+Poof:");
337                 stuff_string(name, F_NAME, NULL);
338
339                 if(Neb2_poof_count < MAX_NEB2_POOFS){
340                         SDL_strlcpy(Neb2_poof_filenames[Neb2_poof_count++], name, sizeof(Neb2_poof_filenames[0]));
341                 }
342         }
343
344         // should always have 6 neb poofs
345         SDL_assert(Neb2_poof_count == 6);
346 #endif
347 }
348
349 // set detail level
350 void neb2_set_detail_level(int level)
351 {
352         // sanity
353         if(level < 0){
354                 Nd = &Neb2_detail[0];
355                 return;
356         }
357         if(level >= MAX_DETAIL_LEVEL){
358                 Nd = &Neb2_detail[MAX_DETAIL_LEVEL-1];
359                 return;
360         }
361
362         Nd = &Neb2_detail[level];
363
364         // regen the player neb
365         Neb2_regen = 1;
366 }
367
368 // initialize nebula stuff - call from game_post_level_init(), so the mission has been loaded
369 void neb2_level_init()
370 {
371         int idx;                
372
373         // standalone servers can bail here
374         if(Game_mode & GM_STANDALONE_SERVER){
375                 return;
376         }
377
378         // if the mission is not a fullneb mission, skip
379         if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
380                 Neb2_render_mode = NEB2_RENDER_NONE;
381                 Neb2_awacs = -1.0f;
382                 return;
383         }
384
385         /*
386         if(gr_screen.mode == GR_DIRECT3D){
387                 max_alpha_player = NEB2_MAX_ALPHA_D3D;
388         } else {
389                 max_alpha_player = NEB2_MAX_ALPHA_GLIDE;
390         }
391         */
392
393         // by default we'll use pof rendering
394         Neb2_render_mode = NEB2_RENDER_POF;
395         stars_set_background_model(BACKGROUND_MODEL_FILENAME, Neb2_texture_name);
396
397         // load in all nebula bitmaps
398         for(idx=0; idx<Neb2_poof_count; idx++){
399                 if(Neb2_poofs[idx] < 0){
400                         Neb2_poofs[idx] = bm_load(Neb2_poof_filenames[idx]);
401                 }
402         }
403
404         pneb_tried = 0;         
405         pneb_tossed_alpha = 0;          
406         pneb_tossed_dot = 0;
407         neb_tried = 0;          
408         neb_tossed_alpha = 0;           
409         neb_tossed_dot = 0;
410         neb_tossed_count = 0;
411
412         // setup proper fogging values
413         Neb_backg_fog_near = NEB_BACKG_FOG_NEAR;
414         Neb_backg_fog_far = NEB_BACKG_FOG_FAR;
415
416         // regen the nebula
417         neb2_eye_changed();
418 }
419
420 // shutdown nebula stuff
421 void neb2_level_close()
422 {
423         int idx;
424         
425         // standalone servers can bail here
426         if(Game_mode & GM_STANDALONE_SERVER){
427                 return;
428         }
429
430         // if the mission is not a fullneb mission, skip
431         if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
432                 return;
433         }
434
435         // unload all nebula bitmaps
436         for(idx=0; idx<Neb2_poof_count; idx++){
437                 if(Neb2_poofs[idx] >= 0){
438                         bm_unload(Neb2_poofs[idx]);
439                         Neb2_poofs[idx] = -1;
440                 }
441         }       
442
443         // unflag the mission as being fullneb so stuff doesn't fog in the techdata room :D
444         The_mission.flags &= ~MISSION_FLAG_FULLNEB;
445 }
446
447 // call before beginning all rendering
448 void neb2_render_setup(vector *eye_pos, matrix *eye_orient)
449 {
450         // standalone servers can bail here
451         if(Game_mode & GM_STANDALONE_SERVER){
452                 return;
453         }
454
455         // if the mission is not a fullneb mission, skip
456         if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){                
457                 return;
458         }
459
460         // pre-render the real background nebula
461         neb2_pre_render(eye_pos, eye_orient);           
462 }
463
464 // level paging code
465 void neb2_page_in()
466 {
467         int idx;
468
469         // load in all nebula bitmaps
470         for(idx=0; idx<Neb2_poof_count; idx++){
471                 if((Neb2_poofs[idx] >= 0) && (Neb2_poof_flags & (1<<idx))){
472                         bm_page_in_texture(Neb2_poofs[idx]);
473                 }
474         }
475 }
476
477 // should we not render this object because its obscured by the nebula?
478 int neb_skip_opt = 1;
479 DCF(neb_skip, "")
480 {
481         neb_skip_opt = !neb_skip_opt;
482         if(neb_skip_opt){
483                 dc_printf("Using neb object skipping!\n");
484         } else {
485                 dc_printf("Not using neb object skipping!\n");
486         }
487 }
488 int neb2_skip_render(object *objp, float z_depth)
489 {
490         float fog_near, fog_far;                
491
492         // if we're never skipping
493         if(!neb_skip_opt){
494                 return 0;
495         }
496
497         // lame rendering
498         if(Neb2_render_mode == NEB2_RENDER_LAME){
499                 return 0;
500         }
501
502         // get near and far fog values based upon object type and rendering mode
503         neb2_get_fog_values(&fog_near, &fog_far, objp);
504
505         // by object type
506         switch( objp->type )    {
507         // some objects we always render
508         case OBJ_SHOCKWAVE:
509         case OBJ_JUMP_NODE:
510         case OBJ_NONE:
511         case OBJ_GHOST:
512         case OBJ_BEAM:
513                 return 0;                       
514                 
515         // any weapon over 500 meters away 
516         case OBJ_WEAPON:                
517                 if(z_depth >= 500.0f){
518                         return 1;
519                 }
520                 break;
521
522         // any small ship over the fog limit, or any cruiser 50% further than the fog limit
523         case OBJ_SHIP:  
524                 ship_info *sip;
525                 if((objp->instance >= 0) && (Ships[objp->instance].ship_info_index >= 0)){
526                         sip = &Ship_info[Ships[objp->instance].ship_info_index];
527                 } else {
528                         return 0;
529                 }
530
531                 // small ships over the fog limit by a small factor
532                 if((sip->flags & SIF_SMALL_SHIP) && (z_depth >= (fog_far * 1.3f))){
533                         return 1;
534                 }
535
536                 // big ships
537                 if((sip->flags & SIF_BIG_SHIP) && (z_depth >= (fog_far * 2.0f))){
538                         return 1;
539                 }
540
541                 // huge ships
542                 if((sip->flags & SIF_HUGE_SHIP) && (z_depth >= (fog_far * 3.0f))){
543                         return 1;
544                 }
545                 break;
546
547         // any fireball over the fog limit for small ships
548         case OBJ_FIREBALL:              
549                 /*
550                 if(z_depth >= fog_far){
551                         return 1;
552                 }
553                 */
554                 return 0;
555                 break;  
556
557         // any debris over the fog limit for small ships
558         case OBJ_DEBRIS:                
559                 /*
560                 if(z_depth >= fog_far){
561                         return 1;
562                 }
563                 */
564                 return 0;
565                 break;
566
567         // any asteroids 50% farther than the fog limit for small ships
568         case OBJ_ASTEROID:              
569                 if(z_depth >= (fog_far * 1.5f)){
570                         return 1;
571                 }
572                 break;
573
574         // any countermeasures over 100 meters away
575         case OBJ_CMEASURE:              
576                 if(z_depth >= 100.0f){
577                         return 1;
578                 }
579                 break;  
580
581         // hmmm. unknown object type - should probably let it through
582         default:
583                 return 0;
584         }
585
586         return 0;
587 }
588
589 // extend LOD 
590 float neb2_get_lod_scale(int objnum)
591 {       
592         ship *shipp;
593         ship_info *sip;
594
595         // bogus
596         if((objnum < 0) || (objnum >= MAX_OBJECTS) || (Objects[objnum].type != OBJ_SHIP) || (Objects[objnum].instance < 0) || (Objects[objnum].instance >= MAX_SHIPS)){
597                 return 1.0f;
598         }
599         shipp = &Ships[Objects[objnum].instance];
600         sip = &Ship_info[shipp->ship_info_index];
601
602         // small ship?
603         if(sip->flags & SIF_SMALL_SHIP){
604                 return 1.8f;
605         } else if(sip->flags & SIF_BIG_SHIP){
606                 return 1.4f;
607         }       
608
609         // hmm
610         return 1.0f;
611 }
612
613
614 // --------------------------------------------------------------------------------------------------------
615 // NEBULA FORWARD DEFINITIONS
616 //
617
618 // return the alpha the passed poof should be rendered with, for a 2 shell nebula
619 float neb2_get_alpha_2shell(float inner_radius, float outer_radius, float magic_num, vector *v)
620 {                       
621         float dist;
622         float alpha;
623         vector eye_pos;
624
625         // get the eye position
626         neb2_get_eye_pos(&eye_pos);
627         
628         // determine what alpha to draw this bitmap with
629         // higher alpha the closer the bitmap gets to the eye
630         dist = vm_vec_dist_quick(&eye_pos, v);  
631
632         // if the point is inside the inner radius, alpha is based on distance to the player's eye, 
633         // becoming more transparent as it gets close
634         if(dist <= inner_radius){
635                 // alpha per meter between the magic # and the inner radius
636                 alpha = Nd->max_alpha_glide / (inner_radius - magic_num);
637
638                 // above value times the # of meters away we are
639                 alpha *= (dist - magic_num);
640                 return alpha < 0.0f ? 0.0f : alpha;
641         }
642         // if the point is outside the inner radius, it starts out as completely transparent at max     
643         // outer radius, and becomes more opaque as it moves towards inner radius       
644         else if(dist <= outer_radius){                          
645                 // alpha per meter between the outer radius and the inner radius
646                 alpha = Nd->max_alpha_glide / (outer_radius - inner_radius);
647
648                 // above value times the range between the outer radius and the poof
649                 return alpha < 0.0f ? 0.0f : alpha * (outer_radius - dist);
650         }
651
652         // otherwise transparent
653         return 0.0f;    
654 }
655
656 // return an alpha value for a bitmap offscreen based upon "break" value
657 float neb2_get_alpha_offscreen(float sx, float sy, float incoming_alpha)
658 {       
659         float alpha = 0.0f;
660         float per_pixel_x = incoming_alpha / (float)Nd->break_x;
661         float per_pixel_y = incoming_alpha / (float)Nd->break_y;
662         int off_x = ((sx < 0.0f) || (sx > (float)gr_screen.max_w));     
663         int off_y = ((sy < 0.0f) || (sy > (float)gr_screen.max_h));
664         float off_x_amount = 0.0f;
665         float off_y_amount = 0.0f;
666
667         // determine how many pixels outside we are
668         if(off_x){
669                 if(sx < 0.0f){
670                         off_x_amount = fl_abs(sx);
671                 } else {
672                         off_x_amount = sx - (float)gr_screen.max_w;
673                 }
674         }
675         if(off_y){
676                 if(sy < 0.0f){
677                         off_y_amount = fl_abs(sy);
678                 } else {
679                         off_y_amount = sy - (float)gr_screen.max_h;
680                 }
681         }
682
683         // if offscreen X
684         if(off_x){
685                 // offscreen X and Y - and Y is greater
686                 if(off_y && (off_y_amount > off_x_amount)){                     
687                         alpha = incoming_alpha - (off_y_amount * per_pixel_y);
688                 } else {
689                         alpha = incoming_alpha - (off_x_amount * per_pixel_x);
690                 }
691         }
692         // offscreen y
693         else if(off_y){
694                 alpha = incoming_alpha - (off_y_amount * per_pixel_y);
695         }
696         // should never get here
697         else {          
698                 Int3();
699         }
700
701         return alpha < 0.0f ? 0.0f : alpha;                     
702 }
703
704 // -------------------------------------------------------------------------------------------------
705 // WACKY LOCAL PLAYER NEBULA STUFF
706 //
707
708 vector cube_cen;
709
710 int crossed_border()
711 {
712         vector eye_pos;
713         float ws = Nd->cube_dim / (float)Neb2_slices;
714         float hs = Nd->cube_dim / (float)Neb2_slices;
715         float ds = Nd->cube_dim / (float)Neb2_slices;
716
717         // get the eye position
718         neb2_get_eye_pos(&eye_pos);
719
720         // check left, right (0, and 1, x and -x)
721         if(cube_cen.xyz.x - eye_pos.xyz.x > ws){
722                 // -x
723                 return 0;
724         } else if(eye_pos.xyz.x - cube_cen.xyz.x > ws){
725                 // +x
726                 return 1;
727         }
728
729         // check up, down (2, and 3, y and -y)
730         if(cube_cen.xyz.y - eye_pos.xyz.y > hs){
731                 // -y
732                 return 2;
733         } else if(eye_pos.xyz.y - cube_cen.xyz.y > hs){
734                 // +y
735                 return 3;
736         }
737
738         // check front, back (4, and 5, z and -z)
739         if(cube_cen.xyz.z - eye_pos.xyz.z > ds){
740                 // -z
741                 return 4;
742         } else if(eye_pos.xyz.z - cube_cen.xyz.z > ds){
743                 // +z
744                 return 5;
745         }
746
747         // nothing
748         return -1;
749 }
750
751 void neb2_copy(int xyz, int src, int dest)
752 {
753         int idx1, idx2;
754
755         switch(xyz){
756         case 0:
757                 for(idx1=0; idx1<Neb2_slices; idx1++){
758                         for(idx2=0; idx2<Neb2_slices; idx2++){
759                                 Neb2_cubes[dest][idx1][idx2] = Neb2_cubes[src][idx1][idx2];                             
760                         }
761                 }
762                 break;
763         case 1:
764                 for(idx1=0; idx1<Neb2_slices; idx1++){
765                         for(idx2=0; idx2<Neb2_slices; idx2++){                          
766                                 Neb2_cubes[idx1][dest][idx2] = Neb2_cubes[idx1][src][idx2];                             
767                         }
768                 }
769                 break;
770         case 2:
771                 for(idx1=0; idx1<Neb2_slices; idx1++){
772                         for(idx2=0; idx2<Neb2_slices; idx2++){
773                                 Neb2_cubes[idx1][idx2][dest] = Neb2_cubes[idx1][idx2][src];                             
774                         }
775                 }
776                 break;
777         default:
778                 Int3();
779                 break;
780         }
781 }
782
783 void neb2_gen_slice(int xyz, int src, vector *cube_center)
784 {
785         int idx1, idx2; 
786         float h_incw, h_inch, h_incd;
787         float ws, hs, ds;
788         vector cube_corner;     
789         vector *v;
790
791         ws = Nd->cube_dim / (float)Neb2_slices;
792         h_incw = ws / 2.0f;
793         hs = Nd->cube_dim / (float)Neb2_slices;
794         h_inch = hs / 2.0f;
795         ds = Nd->cube_dim / (float)Neb2_slices; 
796         h_incd = ds / 2.0f;
797         cube_corner = *cube_center;             
798         cube_corner.xyz.x -= (Nd->cube_dim / 2.0f);                     
799         cube_corner.xyz.y -= (Nd->cube_dim / 2.0f);     
800         cube_corner.xyz.z -= (Nd->cube_dim / 2.0f);     
801         switch(xyz){
802         case 0:
803                 for(idx1=0; idx1<Neb2_slices; idx1++){
804                         for(idx2=0; idx2<Neb2_slices; idx2++){
805                                 v = &Neb2_cubes[src][idx1][idx2].pt;
806
807                                 v->xyz.x = h_incw + (ws * (float)src) + frand_range(-Nd->wj, Nd->wj);
808                                 v->xyz.y = h_inch + (hs * (float)idx1) + frand_range(-Nd->hj, Nd->hj);
809                                 v->xyz.z = h_incd + (ds * (float)idx2) + frand_range(-Nd->dj, Nd->dj);
810                                 vm_vec_add2(v, &cube_corner);
811
812                                 // set the bitmap
813                                 Neb2_cubes[src][idx1][idx2].bmap = neb2_get_bitmap();
814
815                                 // set the rotation speed
816                                 Neb2_cubes[src][idx1][idx2].rot = 0.0f;
817                                 Neb2_cubes[src][idx1][idx2].rot_speed = frand_range(-max_rotation, max_rotation);
818                                 Neb2_cubes[src][idx1][idx2].flash = 0.0f;
819                         }
820                 }
821                 break;
822         case 1:
823                 for(idx1=0; idx1<Neb2_slices; idx1++){
824                         for(idx2=0; idx2<Neb2_slices; idx2++){
825                                 v = &Neb2_cubes[idx1][src][idx2].pt;
826                                 
827                                 v->xyz.x = h_incw + (ws * (float)idx1) + frand_range(-Nd->wj, Nd->wj);
828                                 v->xyz.y = h_inch + (hs * (float)src) + frand_range(-Nd->hj, Nd->hj);
829                                 v->xyz.z = h_incd + (ds * (float)idx2) + frand_range(-Nd->dj, Nd->dj);
830                                 vm_vec_add2(v, &cube_corner);
831
832                                 // set the bitmap
833                                 Neb2_cubes[idx1][src][idx2].bmap = neb2_get_bitmap();
834
835                                 // set the rotation speed
836                                 Neb2_cubes[idx1][src][idx2].rot = 0.0f;
837                                 Neb2_cubes[idx1][src][idx2].rot_speed = frand_range(-max_rotation, max_rotation);
838                                 Neb2_cubes[src][idx1][idx2].flash = 0.0f;
839                         }
840                 }
841                 break;
842         case 2:
843                 for(idx1=0; idx1<Neb2_slices; idx1++){
844                         for(idx2=0; idx2<Neb2_slices; idx2++){
845                                 v = &Neb2_cubes[idx1][idx2][src].pt;
846
847                                 v->xyz.x = h_incw + (ws * (float)idx1) + frand_range(-Nd->wj, Nd->wj);
848                                 v->xyz.y = h_inch + (hs * (float)idx2) + frand_range(-Nd->hj, Nd->hj);
849                                 v->xyz.z = h_incd + (ds * (float)src) + frand_range(-Nd->dj, Nd->dj);
850                                 vm_vec_add2(v, &cube_corner);
851                                 
852                                 // set the bitmap
853                                 Neb2_cubes[idx1][idx2][src].bmap = neb2_get_bitmap();
854
855                                 // set the rotation speed
856                                 Neb2_cubes[idx1][idx2][src].rot = 0.0f;
857                                 Neb2_cubes[idx1][idx2][src].rot_speed = frand_range(-max_rotation, max_rotation);
858                                 Neb2_cubes[src][idx1][idx2].flash = 0.0f;
859                         }
860                 }
861                 break;
862         default:
863                 Int3();
864                 break;
865         }
866 }
867
868 // regenerate the player nebula
869 void neb2_regen()
870 {
871         int idx;
872         vector eye_pos; 
873         matrix eye_orient;
874
875         mprintf(("Regenerating local nebula!\n"));
876
877         // get eye position and orientation
878         neb2_get_eye_pos(&eye_pos);
879         neb2_get_eye_orient(&eye_orient);       
880
881         // determine the corner of the cube
882         cube_cen = eye_pos;
883                 
884         // generate slices of the cube
885         for(idx=0; idx<Neb2_slices; idx++){
886                 neb2_gen_slice(0, idx, &cube_cen);
887         }
888 }
889
890 float max_area = 100000000.0f;
891 DCF(max_area, "")
892 {
893         dc_get_arg(ARG_FLOAT);
894         max_area = Dc_arg_float;
895 }
896
897 float g3_draw_rotated_bitmap_area(vertex *pnt, float angle, float rad, uint tmap_flags, float area);
898 int neb_mode = 1;
899 int frames_total = 0;
900 int frame_count = 0;
901 float frame_avg;
902 void neb2_render_player()
903 {       
904         vertex p, ptemp;
905         int idx1, idx2, idx3;
906         float alpha;
907         int frame_rendered;     
908         vector eye_pos;
909         matrix eye_orient;
910
911 #ifndef NDEBUG
912         float this_area;
913         float frame_area = max_area;
914         float total_area = 0.0f;
915 #endif
916
917         // standalone servers can bail here
918         if(Game_mode & GM_STANDALONE_SERVER){
919                 return;
920         }
921
922         // if the mission is not a fullneb mission, skip
923         if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
924                 return;
925         }               
926
927         if(Neb2_regen){
928                 neb2_regen();
929                 Neb2_regen = 0;
930         }
931
932         // don't render in lame mode
933         if((Neb2_render_mode == NEB2_RENDER_LAME) || (Neb2_render_mode == NEB2_RENDER_NONE)){
934                 return;
935         }
936
937         // get eye position and orientation
938         neb2_get_eye_pos(&eye_pos);
939         neb2_get_eye_orient(&eye_orient);
940         
941         // maybe swap stuff around if the player crossed a "border"     
942         for(idx2=0; idx2<3; idx2++){
943                 switch(crossed_border()){
944                 case -1:
945                         break;
946                 // -x
947                 case 0 :
948                         cube_cen.xyz.x -= Nd->cube_dim / (float)Neb2_slices;
949                         for(idx1=Neb2_slices-1; idx1>0; idx1--){
950                                 neb2_copy(0, idx1-1, idx1);
951                         }
952                         neb2_gen_slice(0, 0, &cube_cen);                                
953                         break;
954                 // x
955                 case 1 :
956                         cube_cen.xyz.x += Nd->cube_dim / (float)Neb2_slices;
957                         for(idx1=0; idx1<Neb2_slices-1; idx1++){
958                                 neb2_copy(0, idx1+1, idx1);
959                         }                               
960                         neb2_gen_slice(0, Neb2_slices - 1, &cube_cen);                          
961                         break;
962                 // -y
963                 case 2 :                        
964                         cube_cen.xyz.y -= Nd->cube_dim / (float)Neb2_slices;
965                         for(idx1=Neb2_slices-1; idx1>0; idx1--){
966                                 neb2_copy(1, idx1-1, idx1);
967                         }                               
968                         neb2_gen_slice(1, 0, &cube_cen);                                
969                         break;
970                 // y
971                 case 3 :                                                
972                         cube_cen.xyz.y += Nd->cube_dim / (float)Neb2_slices;
973                         for(idx1=0; idx1<Neb2_slices-1; idx1++){
974                                 neb2_copy(1, idx1+1, idx1);
975                         }                               
976                         neb2_gen_slice(1, Neb2_slices - 1, &cube_cen);                          
977                         break;
978                 // -z
979                 case 4 :                        
980                         cube_cen.xyz.z -= Nd->cube_dim / (float)Neb2_slices;
981                         for(idx1=Neb2_slices-1; idx1>0; idx1--){
982                                 neb2_copy(2, idx1-1, idx1);
983                         }                                                               
984                         neb2_gen_slice(2, 0, &cube_cen);                                
985                         break;
986                 // z
987                 case 5 :                                                
988                         cube_cen.xyz.z += Nd->cube_dim / (float)Neb2_slices;
989                         for(idx1=0; idx1<Neb2_slices-1; idx1++){
990                                 neb2_copy(2, idx1+1, idx1);
991                         }                                                                                               
992                         neb2_gen_slice(2, Neb2_slices - 1, &cube_cen);                          
993                         break;
994                 }       
995         }       
996
997         // if we've switched nebula rendering off
998         if(Neb2_render_mode == NEB2_RENDER_NONE){
999                 return;
1000         }       
1001         
1002         frame_rendered = 0;
1003         // render the nebula
1004         for(idx1=0; idx1<Neb2_slices; idx1++){
1005                 for(idx2=0; idx2<Neb2_slices; idx2++){
1006                         for(idx3=0; idx3<Neb2_slices; idx3++){
1007                                 pneb_tried++;                           
1008
1009                                 // rotate the poof
1010                                 Neb2_cubes[idx1][idx2][idx3].rot += (Neb2_cubes[idx1][idx2][idx3].rot_speed * flFrametime);
1011                                 if(Neb2_cubes[idx1][idx2][idx3].rot >= 360.0f){
1012                                         Neb2_cubes[idx1][idx2][idx3].rot = 0.0f;
1013                                 }                               
1014                                 
1015                                 // optimization 1 - don't draw backfacing poly's
1016                                 // useless
1017                                 if(vm_vec_dot_to_point(&eye_orient.v.fvec, &eye_pos, &Neb2_cubes[idx1][idx2][idx3].pt) <= 0.0f){
1018                                         pneb_tossed_dot++;
1019                                         continue;
1020                                 }
1021
1022                                 // rotate and project the vertex into viewspace
1023                                 g3_rotate_vertex(&p, &Neb2_cubes[idx1][idx2][idx3].pt);
1024                                 ptemp = p;
1025                                 g3_project_vertex(&ptemp);
1026
1027                                 // get the proper alpha value                           
1028                                 alpha = neb2_get_alpha_2shell(Nd->cube_inner, Nd->cube_outer, Nd->prad/4.0f, &Neb2_cubes[idx1][idx2][idx3].pt);
1029
1030                                 // optimization 2 - don't draw 0.0f or less poly's
1031                                 // this amounts to big savings
1032                                 if(alpha <= Nd->break_alpha){
1033                                         pneb_tossed_alpha++;
1034                                         continue;
1035                                 }
1036
1037                                 // drop poly's which are offscreen at all                               
1038                                 // if the poly's are offscreen                                          
1039                                 if((ptemp.sx < 0.0f) || (ptemp.sx > (float)gr_screen.max_w) || (ptemp.sy < 0.0f) || (ptemp.sy > (float)gr_screen.max_h) ){
1040                                         alpha = neb2_get_alpha_offscreen(ptemp.sx, ptemp.sy, alpha);
1041                                 }                               
1042
1043                                 // optimization 2 - don't draw 0.0f or less poly's
1044                                 // this amounts to big savings
1045                                 if(alpha <= Nd->break_alpha){
1046                                         pneb_tossed_alpha++;
1047                                         continue;
1048                                 }
1049         
1050                                 // set the bitmap and render                            
1051                                 gr_set_bitmap(Neb2_cubes[idx1][idx2][idx3].bmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, alpha + Neb2_cubes[idx1][idx2][idx3].flash, -1, -1);
1052
1053 #ifndef NDEBUG
1054                                 this_area = g3_draw_rotated_bitmap_area(&p, fl_radian(Neb2_cubes[idx1][idx2][idx3].rot), Nd->prad, TMAP_FLAG_TEXTURED, max_area);                               
1055                                 total_area += this_area;
1056                                 frame_area -= this_area;
1057                                 frame_rendered++;                       
1058 #else 
1059                                 g3_draw_rotated_bitmap(&p, fl_radian(Neb2_cubes[idx1][idx2][idx3].rot), Nd->prad, TMAP_FLAG_TEXTURED);
1060 #endif
1061                         }
1062                 }
1063         }       
1064
1065         frames_total += frame_rendered;
1066         frame_count++;
1067         frame_avg = (float)frames_total / (float)frame_count;   
1068
1069         // gr_set_color_fast(&Color_bright_red);
1070         // gr_printf(30, 100, "Area %.3f", total_area);
1071 #ifdef NEB2_THUMBNAIL
1072         extern int tbmap;
1073         if(tbmap != -1){
1074                 gr_set_bitmap(tbmap);
1075                 gr_bitmap(0, 0);
1076         }
1077 #endif
1078 }       
1079
1080 // call this when the player's viewpoint has changed, this will cause the code to properly reset
1081 // the eye's local poofs
1082 void neb2_eye_changed()
1083 {
1084         Neb2_regen = 1;
1085 }
1086
1087 // get near and far fog values based upon object type and rendering mode
1088 void neb2_get_fog_values(float *fnear, float *ffar, object *objp)
1089 {
1090         int fog_index;
1091
1092         // default values in case something truly nasty happens
1093         *fnear = 10.0f;
1094         *ffar = 1000.0f;
1095
1096         // determine what fog index to use
1097         if(objp->type == OBJ_SHIP){
1098                 SDL_assert((objp->instance >= 0) && (objp->instance < MAX_SHIPS));
1099                 if((objp->instance < 0) || (objp->instance >= MAX_SHIPS)){
1100                         fog_index = SHIP_TYPE_FIGHTER_BOMBER;
1101                 } else {
1102                         fog_index = ship_query_general_type(objp->instance);
1103                         SDL_assert(fog_index >= 0);
1104                         if(fog_index < 0){
1105                                 fog_index = SHIP_TYPE_FIGHTER_BOMBER;
1106                         }
1107                 }
1108         }
1109         // fog everything else like a fighter
1110         else {
1111                 fog_index = SHIP_TYPE_FIGHTER_BOMBER;
1112         }
1113
1114         // get the values
1115         *fnear = Neb_ship_fog_vals[fog_index][0];
1116         *ffar = Neb_ship_fog_vals[fog_index][1];
1117 }
1118
1119 // given a position in space, return a value from 0.0 to 1.0 representing the fog level 
1120 float neb2_get_fog_intensity(object *obj)
1121 {
1122         float f_near, f_far, pct;
1123
1124         // get near and far fog values based upon object type and rendering mode
1125         neb2_get_fog_values(&f_near, &f_far, obj);
1126
1127         // get the fog pct
1128         pct = vm_vec_dist_quick(&Eye_position, &obj->pos) / (f_far - f_near);
1129         if(pct < 0.0f){
1130                 return 0.0f;
1131         } else if(pct > 1.0f){
1132                 return 1.0f;
1133         }
1134
1135         return pct;
1136 }
1137
1138 // fogging stuff --------------------------------------------------------------------
1139
1140 // do a pre-render of the background nebula
1141 #define ESIZE                                   32
1142 ubyte tpixels[ESIZE * ESIZE * 4];               // for 32 bits
1143 int last_esize = -1;
1144 int this_esize = ESIZE;
1145 extern float Viewer_zoom;
1146 float ex_scale, ey_scale;
1147 int tbmap = -1;
1148 void neb2_pre_render(vector *eye_pos, matrix *eye_orient)
1149 {       
1150         // bail early in lame and poly modes
1151         if(Neb2_render_mode != NEB2_RENDER_POF){
1152                 return;
1153         }
1154
1155         // set the view clip
1156         gr_screen.clip_width = this_esize;
1157         gr_screen.clip_height = this_esize;
1158         g3_start_frame(1);                                                      // Turn on zbuffering
1159         g3_set_view_matrix(eye_pos, eye_orient, Viewer_zoom);
1160         gr_set_clip(0, 0, this_esize, this_esize);              
1161
1162         // render the background properly
1163         // hack - turn off nebula stuff
1164         int neb_save = Neb2_render_mode;
1165         Neb2_render_mode = NEB2_RENDER_NONE;
1166
1167         // draw background stuff nebula                 
1168         extern void stars_draw_background();
1169         stars_draw_background();                
1170
1171         Neb2_render_mode = neb_save;    
1172
1173         // grab the region
1174         gr_get_region(0, this_esize, this_esize, (ubyte*)tpixels);      
1175
1176 #ifdef NEB2_THUMBNAIL
1177         if(tbmap == -1){
1178                 tbmap = bm_create(16, this_esize, this_esize, tpixels, 0);
1179                 bm_lock(tbmap, 16, 0);
1180                 bm_unlock(tbmap);
1181         }
1182 #endif
1183
1184         // maybe do some swizzling
1185         
1186         // end the frame
1187         g3_end_frame();
1188         
1189         gr_clear();     
1190
1191         // if the size has changed between frames, make a new bitmap
1192         if(this_esize != last_esize){
1193                 last_esize = this_esize;                                                
1194                 
1195                 // recalculate ex_scale and ey_scale values for looking up color values
1196                 ex_scale = (float)this_esize / (float)gr_screen.max_w;
1197                 ey_scale = (float)this_esize / (float)gr_screen.max_h;
1198         }       
1199                 
1200         // restore the game clip stuff
1201         extern void game_set_view_clip();
1202         game_set_view_clip();   
1203 }
1204
1205 // wacky scheme for smoothing colors
1206 int wacky_scheme = 3;
1207
1208 // get the color of the pixel in the small pre-rendered background nebula
1209 #define PIXEL_INDEX_SMALL(xv, yv)       ( (this_esize * (yv) * gr_screen.bytes_per_pixel) + ((xv) * gr_screen.bytes_per_pixel) )
1210 void neb2_get_pixel(int x, int y, int *r, int *g, int *b)
1211 {       
1212         int ra, ga, ba;
1213         ubyte rv, gv, bv;       
1214         int avg_count;
1215         int xs, ys;
1216
1217         // if we're in lame rendering mode, return a constant value
1218         if(Neb2_render_mode == NEB2_RENDER_LAME){
1219                 *r = Neb2_background_color[0];
1220                 *g = Neb2_background_color[1];
1221                 *b = Neb2_background_color[2];
1222
1223                 return;
1224         }               
1225         
1226         // get the proper pixel index to be looking up
1227         rv = gv = bv = 0;       
1228         
1229         // select screen format
1230         BM_SELECT_SCREEN_FORMAT();
1231
1232         // pixel plus all immediate neighbors (on the small screen - should be more effective than 2 or 1)      
1233         xs = (int)(ex_scale * x);
1234         ys = (int)(ey_scale * y);               
1235
1236         // sometimes goes over by 1 in direct3d
1237         if(ys >= (this_esize - 1)){
1238                 ys--;
1239         }
1240
1241         avg_count = 0;
1242         bm_get_components(&tpixels[PIXEL_INDEX_SMALL(xs, ys)], &rv, &gv, &bv, NULL);
1243         ra = rv;
1244         ga = gv;
1245         ba = bv;
1246         avg_count++;
1247         if(xs > 0){                     
1248                 bm_get_components(&tpixels[PIXEL_INDEX_SMALL(xs - 1, ys)], &rv, &gv, &bv, NULL);        // left
1249                 ra += rv;
1250                 ba += bv;
1251                 ga += gv;
1252                 avg_count++;
1253         }
1254         if(xs < this_esize - 1){                        
1255                 bm_get_components(&tpixels[PIXEL_INDEX_SMALL(xs + 1, ys)], &rv, &gv, &bv, NULL);        // right
1256                 ra += rv;
1257                 ba += bv;
1258                 ga += gv;
1259                 avg_count++;
1260         }
1261         if(ys > 0){                     
1262                 bm_get_components(&tpixels[PIXEL_INDEX_SMALL(xs, ys - 1)], &rv, &gv, &bv, NULL);        // top
1263                 ra += rv;
1264                 ba += bv;
1265                 ga += gv;
1266                 avg_count++;
1267         }                               
1268         if(ys < this_esize - 2){                        
1269                 bm_get_components(&tpixels[PIXEL_INDEX_SMALL(xs, ys + 1)], &rv, &gv, &bv, NULL);        // bottom
1270                 ra += rv;
1271                 ba += bv;
1272                 ga += gv;
1273                 avg_count++;
1274         }
1275                 
1276         if((xs > 0) && (ys > 0)){                       
1277                 bm_get_components(&tpixels[PIXEL_INDEX_SMALL(xs - 1, ys - 1)], &rv, &gv, &bv, NULL);    // upper left
1278                 ra += rv;
1279                 ba += bv;
1280                 ga += gv;
1281                 avg_count++;
1282         }
1283         if((xs < this_esize - 1) && (ys < this_esize - 1)){                     
1284                 bm_get_components(&tpixels[PIXEL_INDEX_SMALL(xs + 1, ys + 1)], &rv, &gv, &bv, NULL);    // lower right
1285                 ra += rv;
1286                 ba += bv;
1287                 ga += gv;
1288                 avg_count++;
1289         }
1290         if((ys > 0) && (xs < this_esize - 1)){                  
1291                 bm_get_components(&tpixels[PIXEL_INDEX_SMALL(xs + 1, ys - 1)], &rv, &gv, &bv, NULL);    // lower left
1292                 ra += rv;
1293                 ba += bv;
1294                 ga += gv;
1295                 avg_count++;
1296         }
1297         if((ys < this_esize - 1) && (xs > 0)){                  
1298                 bm_get_components(&tpixels[PIXEL_INDEX_SMALL(xs - 1, ys + 1)], &rv, &gv, &bv, NULL);    // upper right
1299                 ra += rv;
1300                 ba += bv;
1301                 ga += gv;
1302                 avg_count++;
1303         }               
1304
1305         rv = (ubyte) (ra / avg_count);
1306         gv = (ubyte) (ga / avg_count);
1307         bv = (ubyte) (ba / avg_count);  
1308
1309         // return values
1310         *r = (int)rv;
1311         *g = (int)gv;
1312         *b = (int)bv;
1313 }
1314
1315 // get the color to fog the background color to
1316 void neb2_get_backg_color(int *r, int *g, int *b)
1317 {
1318         *r = Neb2_background_color[0];
1319         *g = Neb2_background_color[1];
1320         *b = Neb2_background_color[2];
1321 }
1322
1323 // set the background color
1324 void neb2_set_backg_color(int r, int g, int b)
1325 {
1326         Neb2_background_color[0] = r;
1327         Neb2_background_color[1] = g;
1328         Neb2_background_color[2] = b;
1329 }
1330
1331 // fill in the position of the eye for this frame
1332 void neb2_get_eye_pos(vector *eye)
1333 {
1334         *eye = Eye_position;
1335 }
1336
1337 // fill in the eye orient for this frame
1338 void neb2_get_eye_orient(matrix *eye)
1339 {
1340         *eye = Eye_matrix;
1341 }
1342
1343 // get a (semi) random bitmap to use for a poof
1344 int neb2_get_bitmap()
1345 {
1346         int count = 0;
1347         int huh;
1348         static int neb2_choose = 0;
1349
1350         // get a random count
1351         count = (int)frand_range(1.0f, 5.0f);
1352
1353         // very ad-hoc
1354         while(count > 0){
1355                 // don't cycle too many times
1356                 huh = 0;                
1357                 do {
1358                         if(neb2_choose == MAX_NEB2_POOFS - 1){
1359                                 neb2_choose = 0;
1360                         } else {
1361                                 neb2_choose++;
1362                         }
1363
1364                         huh++;
1365                 } while(!(Neb2_poof_flags & (1<<neb2_choose)) && (huh < 10));
1366
1367                 count--;
1368         }
1369
1370         // bitmap 0
1371         if(Neb2_poofs[neb2_choose] < 0){        
1372                 return Neb2_poofs[0];
1373         } 
1374         return Neb2_poofs[neb2_choose];
1375 }
1376
1377 // nebula DCF functions ------------------------------------------------------
1378
1379 DCF(neb2, "list nebula console commands")
1380 {               
1381         dc_printf("neb2_fog <X> <float> <float>  : set near and far fog planes for ship type X\n");
1382         dc_printf("where X is an integer from 1 - 11\n");
1383         dc_printf("1 = cargo containers, 2 = fighters/bombers, 3 = cruisers\n");
1384         dc_printf("4 = freighters, 5 = capital ships, 6 = transports, 7 = support ships\n");
1385         dc_printf("8 = navbuoys, 9 = sentryguns, 10 = escape pods, 11 = background nebula polygons\n\n");
1386         
1387         dc_printf("neb2_max_alpha   : max alpha value (0.0 to 1.0) for cloud poofs. 0.0 is completely transparent\n");
1388         dc_printf("neb2_break_alpha : alpha value (0.0 to 1.0) at which faded polygons are not drawn. higher values generally equals higher framerate, with more visual cloud popping\n");
1389         dc_printf("neb2_break_off   : how many pixels offscreen (left, right, top, bottom) when a cloud poof becomes fully transparent. Lower values cause quicker fading\n");
1390         dc_printf("neb2_smooth      : magic fog smoothing modes (0 - 3)\n");
1391         dc_printf("neb2_select      : <int> <int>  where the first # is the bitmap to be adjusting (0 through 5), and the second int is a 0 or 1, to turn off and on\n");
1392         dc_printf("neb2_rot         : set max rotation speed for poofs\n");
1393         dc_printf("neb2_prad        : set cloud poof radius\n");
1394         dc_printf("neb2_cdim        : poof cube dimension\n");
1395         dc_printf("neb2_cinner      : poof cube inner dimension\n");
1396         dc_printf("neb2_couter      : poof cube outer dimension\n");
1397         dc_printf("neb2_jitter      : poof jitter\n");
1398         dc_printf("neb2_mode        : switch between no nebula, polygon background, amd pof background (0, 1 and 2 respectively)\n\n"); 
1399         dc_printf("neb2_ff          : flash fade/sec\n");
1400         dc_printf("neb2_background       : rgb background color\n");
1401
1402         dc_printf("neb2_fog_vals    : display all the current settings for all above values\n");        
1403 }
1404
1405 DCF(neb2_prad, "")
1406 {
1407         dc_get_arg(ARG_FLOAT);
1408         Nd->prad = Dc_arg_float;
1409 }
1410 DCF(neb2_cdim, "")
1411 {
1412         dc_get_arg(ARG_FLOAT);
1413         Nd->cube_dim = Dc_arg_float;
1414 }
1415
1416 DCF(neb2_cinner, "")
1417 {
1418         dc_get_arg(ARG_FLOAT);
1419         Nd->cube_inner = Dc_arg_float;
1420 }
1421
1422 DCF(neb2_couter, "")
1423 {
1424         dc_get_arg(ARG_FLOAT);
1425         Nd->cube_outer = Dc_arg_float;
1426 }
1427
1428 DCF(neb2_jitter, "")
1429 {
1430         dc_get_arg(ARG_FLOAT);
1431         Nd->hj = Nd->dj = Nd->wj = Dc_arg_float;
1432 }
1433
1434 DCF(neb2_fog, "")
1435 {
1436         int index;
1437         float fnear, ffar;
1438         dc_get_arg(ARG_INT);
1439         index = Dc_arg_int;
1440         dc_get_arg(ARG_FLOAT);
1441         fnear = Dc_arg_float;
1442         dc_get_arg(ARG_FLOAT);
1443         ffar = Dc_arg_float;
1444
1445         if((index >= 1) && (index <= 11) && (fnear >= 0.0f) && (ffar >= 0.0f) && (ffar > fnear)){
1446                 if(index == 11){
1447                         Neb_backg_fog_near = fnear;
1448                         Neb_backg_fog_far = ffar;
1449                 } else {
1450                         Neb_ship_fog_vals[index][0] = fnear;
1451                         Neb_ship_fog_vals[index][1] = ffar;
1452                 }
1453         }
1454 }
1455
1456 DCF(neb2_max_alpha, "")
1457 {
1458         dc_get_arg(ARG_FLOAT);
1459         Nd->max_alpha_glide = Dc_arg_float;
1460 }
1461
1462 DCF(neb2_break_alpha, "")
1463 {
1464         dc_get_arg(ARG_FLOAT);
1465         Nd->break_alpha = Dc_arg_float;
1466 }
1467
1468 DCF(neb2_break_off, "")
1469 {
1470         dc_get_arg(ARG_INT);
1471         Nd->break_y = (float)Dc_arg_int;
1472         Nd->break_x = Nd->break_y * 1.3333f;
1473 }
1474
1475 DCF(neb2_smooth, "")
1476 {
1477         int index;
1478         dc_get_arg(ARG_INT);
1479         index = Dc_arg_int;
1480         if((index >= 0) && (index <= 3)){
1481                 wacky_scheme = index;
1482         }
1483 }
1484
1485 DCF(neb2_select, "")
1486 {
1487         dc_get_arg(ARG_INT);    
1488         int bmap = Dc_arg_int;
1489         if((bmap >= 0) && (bmap < Neb2_poof_count)){
1490                 dc_get_arg(ARG_INT);
1491
1492                 if(Dc_arg_int){
1493                         Neb2_poof_flags |= (1<<bmap);
1494                 } else {
1495                         Neb2_poof_flags &= ~(1<<bmap);
1496                 }
1497         }
1498 }
1499
1500 DCF(neb2_rot, "")
1501 {
1502         dc_get_arg(ARG_FLOAT);
1503         max_rotation = Dc_arg_float;
1504 }
1505
1506 DCF(neb2_ff, "")
1507 {
1508         dc_get_arg(ARG_FLOAT);
1509         neb2_flash_fade = Dc_arg_float;
1510 }
1511
1512 DCF(neb2_mode, "")
1513 {
1514         dc_get_arg(ARG_INT);
1515
1516         switch(Dc_arg_int){
1517         case NEB2_RENDER_NONE:
1518                 Neb2_render_mode = NEB2_RENDER_NONE;
1519                 break;
1520
1521         case NEB2_RENDER_POLY:
1522                 Neb2_render_mode = NEB2_RENDER_POLY;
1523                 break;
1524
1525         case NEB2_RENDER_POF:
1526                 Neb2_render_mode = NEB2_RENDER_POF;
1527                 stars_set_background_model(BACKGROUND_MODEL_FILENAME, "Eraseme3");
1528                 break;
1529
1530         case NEB2_RENDER_LAME:
1531                 Neb2_render_mode = NEB2_RENDER_LAME;
1532                 break;
1533         }
1534 }
1535
1536 DCF(neb2_slices, "")
1537 {
1538         dc_get_arg(ARG_INT);
1539         Neb2_slices = Dc_arg_int;
1540         Neb2_regen = 1;
1541 }
1542
1543 DCF(neb2_background, "")
1544 {
1545         int r, g, b;
1546
1547         dc_get_arg(ARG_INT);
1548         r = Dc_arg_int;
1549         dc_get_arg(ARG_INT);
1550         g = Dc_arg_int;
1551         dc_get_arg(ARG_INT);
1552         b = Dc_arg_int;
1553
1554         Neb2_background_color[0] = r;
1555         Neb2_background_color[1] = g;
1556         Neb2_background_color[2] = b;
1557 }
1558
1559 DCF(neb2_fog_vals, "")
1560 {
1561         dc_printf("neb2_fog : \n");
1562
1563         dc_printf("(1)cargo containers : %f, %f\n", Neb_ship_fog_vals[1][0], Neb_ship_fog_vals[1][1]);
1564         dc_printf("(2)fighters/bombers : %f, %f\n", Neb_ship_fog_vals[2][0], Neb_ship_fog_vals[2][1]);
1565         dc_printf("(3)cruisers : %f, %f\n", Neb_ship_fog_vals[3][0], Neb_ship_fog_vals[3][1]);
1566         dc_printf("(4)freighters : %f, %f\n", Neb_ship_fog_vals[4][0], Neb_ship_fog_vals[4][1]);
1567         dc_printf("(5)cap ships : %f, %f\n", Neb_ship_fog_vals[5][0], Neb_ship_fog_vals[5][1]);
1568         dc_printf("(6)transports : %f, %f\n", Neb_ship_fog_vals[6][0], Neb_ship_fog_vals[6][1]);
1569         dc_printf("(7)support ships : %f, %f\n", Neb_ship_fog_vals[7][0], Neb_ship_fog_vals[7][1]);
1570         dc_printf("(8)navbuoys : %f, %f\n", Neb_ship_fog_vals[8][0], Neb_ship_fog_vals[8][1]);
1571         dc_printf("(9)sentry guns : %f, %f\n", Neb_ship_fog_vals[9][0], Neb_ship_fog_vals[9][1]);
1572         dc_printf("(10)escape pods : %f, %f\n", Neb_ship_fog_vals[10][0], Neb_ship_fog_vals[10][1]);
1573         dc_printf("(11)background polys : %f, %f\n\n", Neb_backg_fog_near, Neb_backg_fog_far);
1574
1575         dc_printf("neb2_max_alpha   : %f\n", Nd->max_alpha_glide);
1576         dc_printf("neb2_break_alpha : %f\n", Nd->break_alpha);
1577         dc_printf("neb2_break_off   : %d\n", (int)Nd->break_y);
1578         dc_printf("neb2_smooth      : %d\n", wacky_scheme);
1579         dc_printf("neb2_toggle      : %s\n", Neb2_render_mode ? "on" : "off");
1580         dc_printf("neb2_rot                      : %f\n", max_rotation);
1581         dc_printf("neb2_prad                     : %f\n", Nd->prad);
1582         dc_printf("neb2_cdim                     : %f\n", Nd->cube_dim);
1583         dc_printf("neb2_couter      : %f\n", Nd->cube_outer);
1584         dc_printf("neb2_cinner           : %f\n", Nd->cube_inner);
1585         dc_printf("neb2_jitter           : %f\n", Nd->wj);
1586         dc_printf("neb2_ff                       : %f\n", neb2_flash_fade);
1587         dc_printf("neb2_background       : %d %d %d\n", Neb2_background_color[0], Neb2_background_color[1], Neb2_background_color[2]);
1588 }
1589
1590 /* Obsolete !?
1591 DCF(neb2_create, "create a basic nebula")
1592 {
1593         int points = 0;
1594         float rad1 = 0.0f;
1595         float rad2 = 0.0f;
1596         
1597         dc_get_arg(ARG_INT);
1598         if(Dc_arg_type & ARG_INT){
1599                 points = Dc_arg_int;
1600         }
1601         dc_get_arg(ARG_FLOAT);
1602         if(Dc_arg_type & ARG_FLOAT){
1603                 rad1 = Dc_arg_float;
1604         }
1605         dc_get_arg(ARG_FLOAT);
1606         if(Dc_arg_type & ARG_FLOAT){
1607                 rad2 = Dc_arg_float;
1608         }
1609         neb2_create(&vmd_zero_vector, points, rad1, -1.0f, rad2);
1610 }
1611
1612 DCF(neb2_del, "delete existing nebulae")
1613 {
1614         for(int idx=0; idx<MAX_OBJECTS; idx++){
1615                 if(Objects[idx].type == OBJ_NEB2){
1616                         obj_delete(idx);                        
1617                 }
1618         }
1619 }
1620
1621 int magic0 = 15;
1622 float magic1 = 1000.0f;
1623 float magic2 = -1.0f;
1624 float magic3 = 700.0f;
1625 DCF(neb2_def, "create a default nebula")
1626 {
1627         vector a,b,c,d,e,f;
1628         vm_vec_make(&a, 0.0f, 0.0f, 0.0f);
1629         vm_vec_make(&b, 3600.0f, 700.0f, 0.0f);
1630         vm_vec_make(&c, -3000.0f, 20.0f, 480.0f);
1631         vm_vec_make(&d, -4000.0f, 100.0f, 100.0f);
1632         vm_vec_make(&e, 0.0f, 3000.0f, -400.0f);
1633         vm_vec_make(&f, 670.0f, -2500.0f, -1600.0f);
1634
1635         neb2_create(&a, magic0, magic1, magic2, magic3);
1636         neb2_create(&b, magic0, magic1, magic2, magic3);
1637         neb2_create(&c, magic0, magic1, magic2, magic3);
1638         neb2_create(&d, magic0, magic1, magic2, magic3);
1639         neb2_create(&e, magic0, magic1, magic2, magic3);
1640         neb2_create(&f, magic0, magic1, magic2, magic3);
1641 }
1642
1643 DCF(neb2_plr, "regenerate the player's nebula")
1644 {
1645         Neb2_regen = 0;
1646 }
1647
1648 DCF(neb2_stats, "display info about the nebula rendering")
1649 {
1650         dc_printf("Player poofs tried : %d\n", pneb_tried);
1651         dc_printf("Player poofs tossed (alpha): %d\n", pneb_tossed_alpha);
1652         dc_printf("Player poofs tossed (dot): %d\n", pneb_tossed_dot);
1653         dc_printf("Player poofs tossed (off): %d\n", pneb_tossed_off);
1654
1655         dc_printf("Poofs tried : %d\n", neb_tried);
1656         dc_printf("Poofs tossed (alpha): %d\n", neb_tossed_alpha);
1657         dc_printf("Poofs tossed (dot): %d\n", neb_tossed_dot);
1658         dc_printf("Poofs tossed (count): %d\n", neb_tossed_count);
1659
1660         dc_printf("Avg poofs/frame: %f\n", frame_avg);
1661 }
1662
1663 // create a nebula object, return objnum of the nebula or -1 on fail
1664 // NOTE : in most cases you will want to pass -1.0f for outer_radius. Trust me on this
1665 int neb2_create(vector *offset, int num_poofs, float inner_radius, float outer_radius, float max_poof_radius)
1666 {       
1667         Int3();
1668         return -1;
1669 }
1670
1671 // delete a nebula object
1672 void neb2_delete(object *objp)
1673 {       
1674         Int3();
1675 }
1676
1677 // renders a nebula object
1678 void neb2_render(object *objp)
1679 {       
1680         Int3();
1681 }
1682
1683 // preprocess the nebula object before simulation
1684 void neb2_process_pre(object *objp)
1685 {
1686         Int3();
1687 }
1688
1689 // process the nebula object after simulating, but before rendering
1690 void neb2_process_post(object *objp)
1691 {       
1692         Int3();
1693 }
1694 */
1695
1696 /*
1697 // add N poofs to the inner shell of the nebula
1698 // if orient and ang are specified, generate the poofs so that they are "visible" around
1699 // the orient v.fvec in a cone of ang degrees
1700 void neb2_add_inner(neb2 *neb, int num_poofs, matrix *orient, float ang)
1701 {       
1702         int idx;
1703         vector pt, pt2, pt3;
1704         int final_index = (neb->num_poofs + num_poofs) > neb->max_poofs ? neb->max_poofs : (neb->num_poofs + num_poofs);
1705
1706         // add the points a pick a random bitmap
1707         for(idx=neb->num_poofs; idx<final_index; idx++){
1708                 if(orient != NULL){
1709                         // put a point directly in front of the player, between 0 and inner_radius distance away
1710                         vm_vec_copy_scale(&pt, &orient->v.fvec, frand_range(neb->magic_num, neb->inner_radius));
1711
1712                         // rotate the point by -ang <-> ang around the up vector
1713                         vm_rot_point_around_line(&pt2, &pt, fl_radian(frand_range(-ang, ang)), &vmd_zero_vector, &orient->uvec);
1714
1715                         // rotate the point by -ang <-> ang around the right vector
1716                         vm_rot_point_around_line(&pt3, &pt2, fl_radian(frand_range(-ang, ang)), &vmd_zero_vector, &orient->rvec);
1717
1718                         // now add in the center of the nebula so its placed properly (ie, not around the origin)
1719                         vm_vec_add(&neb->pts[idx], &pt3, &Objects[neb->objnum].pos);
1720                 } else {
1721                         neb->pts[idx].xyz.x = frand_range(-1.0f * neb->inner_radius, neb->inner_radius) + Objects[neb->objnum].pos.xyz.x;
1722                         neb->pts[idx].xyz.y = frand_range(-1.0f * neb->inner_radius, neb->inner_radius) + Objects[neb->objnum].pos.xyz.y;
1723                         neb->pts[idx].xyz.z = frand_range(-1.0f * neb->inner_radius, neb->inner_radius) + Objects[neb->objnum].pos.xyz.z;
1724                 }
1725
1726                 neb->bmaps[idx] = (int)frand_range(0.0f, (float)2);
1727                 neb->num_poofs++;
1728         }
1729 }
1730
1731 // add N poofs to the outer shell of the nebula
1732 // if orient and ang are specified, generate the poofs so that they are "visible" around
1733 // the orient v.fvec in a cone of ang degrees
1734 void neb2_add_outer(neb2 *neb, int num_poofs, matrix *orient, float ang)
1735 {
1736         int idx;
1737         float phi, theta;
1738         vector pt, pt2, pt3;
1739         int final_index = (neb->num_poofs + num_poofs) > neb->max_poofs ? neb->max_poofs : (neb->num_poofs + num_poofs);
1740
1741         // add the points a pick a random bitmap
1742         for(idx=neb->num_poofs; idx<final_index; idx++){
1743                 if(orient != NULL){
1744                         // put a point directly in front of the player, at outer_radius distance away
1745                         vm_vec_copy_scale(&pt, &orient->v.fvec, neb->outer_radius);
1746
1747                         // rotate the point by -ang <-> ang around the up vector
1748                         vm_rot_point_around_line(&pt2, &pt, fl_radian(frand_range(-ang, ang)), &vmd_zero_vector, &orient->uvec);
1749
1750                         // rotate the point by -ang <-> ang around the right vector
1751                         vm_rot_point_around_line(&pt3, &pt2, fl_radian(frand_range(-ang, ang)), &vmd_zero_vector, &orient->rvec);
1752
1753                         // now add in the center of the nebula so its placed properly (ie, not around the origin)
1754                         vm_vec_add(&neb->pts[idx], &pt, &Objects[neb->objnum].pos);
1755                 } else {
1756                         // get a random point on the very outer radius, using spherical coords
1757                         phi = fl_radian(frand_range(0.0f, 360.0f));
1758                         theta = fl_radian(frand_range(0.0f, 360.f));
1759         
1760                         neb->pts[idx].xyz.x = neb->outer_radius * (float)sin(phi) * (float)cos(theta);
1761                         neb->pts[idx].xyz.y = neb->outer_radius * (float)sin(phi) * (float)sin(theta);
1762                         neb->pts[idx].xyz.z = neb->outer_radius * (float)cos(phi);                      
1763                 }
1764
1765                 // pick a random bitmap and increment the # of poofs
1766                 neb->bmaps[idx] = (int)frand_range(0.0f, (float)2);
1767                 neb->num_poofs++;
1768         }
1769 }
1770
1771 // return the alpha the passed poof should be rendered with, for a 1 shell nebula
1772 float neb2_get_alpha_1shell(neb2 *neb, int poof_index)
1773 {
1774         float dist;
1775         float alpha;
1776         vector eye_pos;
1777
1778         // get the eye position
1779         neb2_get_eye_pos(&eye_pos);
1780         
1781         // determine what alpha to draw this bitmap with
1782         // higher alpha the closer the bitmap gets to the eye
1783         dist = vm_vec_dist_quick(&eye_pos, &neb->pts[poof_index]);
1784         
1785         // at poof radius or greater, alpha should be 1.0
1786         // scale from 0.0 to 1.0 between radius and magic
1787         if(dist >= neb->max_poof_radius){
1788                 return max_alpha - 0.0001f;
1789         } else if(dist > neb->magic_num){
1790                 // alpha per meter between the magic # and the max radius
1791                 alpha = max_alpha / (neb->max_poof_radius - neb->magic_num);
1792
1793                 // above value times the # of meters away we are
1794                 return alpha * (dist - neb->magic_num);
1795         }       
1796         
1797         // otherwise transparent
1798         return 0.0f;
1799 }
1800 */
1801