]> icculus.org git repositories - taylor/freespace2.git/blob - src/starfield/nebula.cpp
Initial revision
[taylor/freespace2.git] / src / starfield / nebula.cpp
1 /*
2  * $Logfile: /Freespace2/code/Starfield/Nebula.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * Code to load & display nebulas
8  *
9  * $Log$
10  * Revision 1.1  2002/05/03 03:28:10  root
11  * Initial revision
12  *
13  * 
14  * 14    9/01/99 11:26p Dave
15  * Fixed release build warnings.
16  * 
17  * 13    8/30/99 5:01p Dave
18  * Made d3d do less state changing in the nebula. Use new chat server for
19  * PXO.
20  * 
21  * 12    7/29/99 10:47p Dave
22  * Standardized D3D fogging using vertex fog. Shook out Savage 4 bugs.
23  * 
24  * 11    7/14/99 9:42a Dave
25  * Put in clear_color debug function. Put in base for 3dnow stuff / P3
26  * stuff
27  * 
28  * 10    6/22/99 7:03p Dave
29  * New detail options screen.
30  * 
31  * 9     3/31/99 8:24p Dave
32  * Beefed up all kinds of stuff, incluging beam weapons, nebula effects
33  * and background nebulae. Added per-ship non-dimming pixel colors.
34  * 
35  * 8     12/09/98 7:34p Dave
36  * Cleanup up nebula effect. Tweaked many values.
37  * 
38  * 7     12/08/98 9:36a Dave
39  * Almost done nebula effect for D3D. Looks 85% as good as Glide.
40  * 
41  * 6     12/07/98 5:51p Dave
42  * Finally got d3d fog working! Now we just need to tweak values.
43  * 
44  * 5     12/06/98 2:36p Dave
45  * Drastically improved nebula fogging.
46  * 
47  * 4     11/14/98 5:33p Dave
48  * Lots of nebula work. Put in ship contrails.
49  * 
50  * 3     11/11/98 5:37p Dave
51  * Checkin for multiplayer testing.
52  * 
53  * 2     10/07/98 10:54a Dave
54  * Initial checkin.
55  * 
56  * 1     10/07/98 10:51a Dave
57  * 
58  * 15    5/13/98 2:53p John
59  * Made subspace effect work under software.  Had to add new inner loop to
60  * tmapper.  Added glows to end of subspace effect.  Made subspace effect
61  * levels use gamepalette-subspace palette.
62  * 
63  * 14    4/10/98 5:20p John
64  * Changed RGB in lighting structure to be ubytes.  Removed old
65  * not-necessary 24 bpp software stuff.
66  * 
67  * 13    3/22/98 11:02a John
68  * Made a bunch of the detail levels actually do something
69  * 
70  * 12    2/22/98 12:19p John
71  * Externalized some strings
72  * 
73  * 11    1/23/98 5:08p John
74  * Took L out of vertex structure used B (blue) instead.   Took all small
75  * fireballs out of fireball types and used particles instead.  Fixed some
76  * debris explosion things.  Restructured fireball code.   Restructured
77  * some lighting code.   Made dynamic lighting on by default. Made groups
78  * of lasers only cast one light.  Made fireballs not cast light.
79  * 
80  * 10    1/10/98 1:14p John
81  * Added explanation to debug console commands
82  * 
83  * 9     12/21/97 4:33p John
84  * Made debug console functions a class that registers itself
85  * automatically, so you don't need to add the function to
86  * debugfunctions.cpp.  
87  * 
88  * 8     11/25/97 11:40a Hoffoss
89  * Added support for nebula placement editing.
90  * 
91  * 7     11/24/97 12:04p John
92  * 
93  * 6     11/21/97 2:55p Hoffoss
94  * Added Nebula support to Fred.  Implemented loading and saving nebula
95  * info to/from mission files.
96  * 
97  * 5     11/21/97 11:32a John
98  * Added nebulas.   Fixed some warpout bugs.
99  * 
100  * 4     11/19/97 10:47p Adam
101  * upped MAX_TRIS to 200.  Whaddya think this is, I-War?
102  * 
103  * 3     11/16/97 2:41p John
104  * added a debug function to load a nebula into the game.
105  * 
106  * 2     11/16/97 2:29p John
107  * added versioning to nebulas; put nebula code into freespace.
108  * 
109  * 1     11/16/97 1:14p John
110  *
111  * $NoKeywords: $
112  */
113
114 #include "pstypes.h"
115 #include "floating.h"
116 #include "vecmat.h"
117 #include "3d.h"
118 #include "2d.h"
119 #include "starfield.h"
120 #include "bmpman.h"
121 #include "key.h"
122 #include "freespace.h"  
123 #include "timer.h"
124 #include "nebula.h"
125 #include "palman.h"
126 #include "cfile.h"
127 #include "linklist.h"
128 #include "2d.h"
129 #include "missionparse.h"
130 #include "neb.h"
131
132 #define MAX_TRIS 200
133 #define MAX_POINTS 300
134
135 static int neb_w = 0, neb_h = 0;
136
137 static int nebula_inited = 0;
138 static int num_pts = 0;
139
140 static vector nebula_vecs[MAX_POINTS];
141 static vertex nebula_verts[MAX_POINTS];
142
143 static float scale_factor = 1.0f;
144
145 static int num_tris = 0;
146 static int tri[MAX_TRIS][3];
147
148 static int Nebula_loaded = 0;
149 static angles Nebula_pbh;
150 static matrix Nebula_orient;
151
152 int Nebula_pitch;
153 int Nebula_bank;
154 int Nebula_heading;
155
156 void nebula_close()
157 {
158         if (!Nebula_loaded) return;
159
160         Nebula_loaded = 0;
161 }
162
163 #define NEBULA_FILE_ID NOX("NEBU")
164 #define NEBULA_MAJOR_VERSION 1          // Can be 1-?
165 #define NEBULA_MINOR_VERSION 0          // Can be 0-99
166
167 // given:
168 // u,v in range 0-1
169
170 void project_2d_onto_sphere( vector *pnt, float u, float v )
171 {
172         float a,x,y,z,s;
173
174         a = PI * (2.0f * u - 1.0f );
175         z = 2.0f * v - 1.0f;    
176         s = scale_factor * fl_sqrt( 1.0f - z*z );
177         x = s * (float)cos(a);
178         y = s * (float)sin(a);
179         pnt->x = x;
180         pnt->y = y;
181         pnt->z = z;
182 }
183
184 // Version 199 mean major version=1, minor=99.
185 // Changing major means no longer compatible.
186 // Revision history:
187 // 1.00 - initial version
188
189 // returns 0 if failed
190 int load_nebula_sub(char *filename)
191 {
192         CFILE *fp;
193         char id[16];
194         int version, major, minor;
195
196         fp = cfopen(filename, "rb");
197
198         if ( !fp )      {
199                 return 0;
200         }
201
202         // ID of NEBU
203         cfread( id, 4, 1, fp ); 
204         if ( strncmp( id, NEBULA_FILE_ID, 4))   {
205                 mprintf(( "Not a valid nebula file.\n" ));
206                 return 0;
207         } 
208         cfread( &version, sizeof(int), 1, fp );
209         major = version / 100;
210         minor = version % 100;
211
212         if ( major != NEBULA_MAJOR_VERSION )    {
213                 mprintf(( "An out of date nebula file.\n" ));
214                 return 0;
215         }       
216
217         cfread( &num_pts, sizeof(int), 1, fp );
218         Assert( num_pts < MAX_POINTS );
219         cfread( &num_tris, sizeof(int), 1, fp );
220         Assert( num_tris < MAX_TRIS );
221
222         for (int i=0; i<num_pts; i++ )  {
223                 float xf, yf;
224                 int l;
225
226                 cfread( &xf, sizeof(float), 1, fp );
227                 cfread( &yf, sizeof(float), 1, fp );
228                 cfread( &l, sizeof(int), 1, fp );
229                 project_2d_onto_sphere( &nebula_vecs[i], 1.0f - xf, yf );
230                 vm_vec_scale( &nebula_vecs[i], 10.0f );
231                 nebula_verts[i].b = ubyte((l*255)/31);
232
233                 // throw in some randomness to the nebula vertices depth
234         }
235
236         for (i=0; i<num_tris; i++ )     {
237                 cfread( &tri[i][0], sizeof(int), 1, fp );
238                 cfread( &tri[i][1], sizeof(int), 1, fp );
239                 cfread( &tri[i][2], sizeof(int), 1, fp );
240         }
241
242         cfclose(fp);
243
244         return 1;
245 }
246
247 void nebula_init( char *filename, int pitch, int bank, int heading )
248 {
249         angles a;
250
251         a.p = ANG_TO_RAD((float) pitch);
252         a.b = ANG_TO_RAD((float) bank);
253         a.h = ANG_TO_RAD((float) heading);
254         nebula_init(filename, &a);
255 }
256
257 void nebula_init( char *filename, angles * pbh )
258 {
259         if ( Nebula_loaded )    {
260                 nebula_close();
261         }
262
263         if ( load_nebula_sub( cf_add_ext(filename, NOX(".neb")) ) ) {
264                 Nebula_loaded = 1;
265         }
266
267         if ( pbh ) {
268                 Nebula_pbh = *pbh;
269                 vm_angles_2_matrix(&Nebula_orient, &Nebula_pbh );
270
271         } else {
272                 Nebula_pbh.p = 0.0f;
273                 Nebula_pbh.b = 0.0f;
274                 Nebula_pbh.h = 0.0f;
275                 Nebula_orient = vmd_identity_matrix;
276         }
277 }
278
279 void nebula_render()
280 {
281         int i;
282         // int r, g, b;
283
284         // no nebula for you!
285         return;
286
287         if ( !Nebula_loaded ) {
288                 return;
289         }
290
291         if ( !Detail.planets_suns )     {
292                 return;
293         }       
294
295         // Rotate the nebula.
296         g3_start_instance_matrix( NULL, &Nebula_orient );
297
298         for (i=0; i<num_pts; i++ )      {
299                 g3_rotate_faraway_vertex( &nebula_verts[i], &nebula_vecs[i] );
300                 g3_project_vertex( &nebula_verts[i] );
301         }
302
303         int saved_gr_zbuffering =       gr_zbuffer_get();
304
305         gr_zbuffer_set(GR_ZBUFF_NONE);
306
307         for (i=0; i<num_tris; i++ ) {
308
309                 vertex * verts[3];
310
311                 verts[0] = &nebula_verts[tri[i][0]];
312                 verts[1] = &nebula_verts[tri[i][1]];
313                 verts[2] = &nebula_verts[tri[i][2]];
314
315                 g3_draw_poly(3, verts, TMAP_FLAG_RAMP | TMAP_FLAG_GOURAUD | TMAP_FLAG_NEBULA );
316         }               
317
318         g3_done_instance();
319
320         gr_zbuffer_set(saved_gr_zbuffering);
321
322         // always switch off fogging for good measure
323         if((The_mission.flags & MISSION_FLAG_FULLNEB) && (Neb2_render_mode == NEB2_RENDER_NONE)){
324                 gr_fog_set(GR_FOGMODE_NONE, 0, 0, 0);
325         }
326 }
327
328 DCF(nebula,"Loads a different nebula")
329 {
330         if ( Dc_command )       {
331                 dc_get_arg(ARG_STRING|ARG_NONE);
332                 if ( Dc_arg_type == ARG_NONE )  {
333                         nebula_close();
334                 } else {
335                         nebula_init( Dc_arg );
336                 }
337         }
338         if ( Dc_help )  {
339                 dc_printf( "Usage: nebula filename\nLoads the nebula file. No filename takes away nebula\n" );
340         }       
341 }
342
343
344