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