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