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