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