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