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