FS2_Open
Open source remastering of the Freespace 2 engine
nebula.cpp
Go to the documentation of this file.
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 the
6  * source.
7  *
8 */
9 
10 
11 
12 #include "cfile/cfile.h"
13 #include "debugconsole/console.h"
14 #include "math/vecmat.h"
15 #include "mission/missionparse.h"
16 #include "nebula/neb.h"
17 #include "render/3d.h"
18 #include "starfield/nebula.h"
19 
20 
21 #define MAX_TRIS 200
22 #define MAX_POINTS 300
23 
24 static int num_pts = 0;
25 
26 static vec3d nebula_vecs[MAX_POINTS];
27 static vertex nebula_verts[MAX_POINTS];
28 
29 static float scale_factor = 1.0f;
30 
31 static int num_tris = 0;
32 static int tri[MAX_TRIS][3];
33 
34 static int Nebula_loaded = 0;
35 static angles Nebula_pbh;
36 static matrix Nebula_orient;
37 
41 
43 {
44  if (!Nebula_loaded) return;
45 
46  Nebula_loaded = 0;
47 }
48 
49 #define NEBULA_FILE_ID NOX("NEBU")
50 #define NEBULA_MAJOR_VERSION 1 // Can be 1-?
51 #define NEBULA_MINOR_VERSION 0 // Can be 0-99
52 
53 // given:
54 // u,v in range 0-1
55 
56 void project_2d_onto_sphere( vec3d *pnt, float u, float v )
57 {
58  float a,x,y,z,s;
59 
60  a = PI * (2.0f * u - 1.0f );
61  z = 2.0f * v - 1.0f;
62  s = scale_factor * fl_sqrt( 1.0f - z*z );
63  x = s * cosf(a);
64  y = s * sinf(a);
65  pnt->xyz.x = x;
66  pnt->xyz.y = y;
67  pnt->xyz.z = z;
68 }
69 
70 // Version 199 mean major version=1, minor=99.
71 // Changing major means no longer compatible.
72 // Revision history:
73 // 1.00 - initial version
74 
75 // returns 0 if failed
77 {
78  CFILE *fp;
79  char id[16];
80  int version, major;
81 
82  fp = cfopen(filename, "rb");
83 
84  if ( !fp ) {
85  return 0;
86  }
87 
88  // ID of NEBU
89  cfread( id, 4, 1, fp );
90  if ( strncmp( id, NEBULA_FILE_ID, 4)) {
91  mprintf(( "Not a valid nebula file.\n" ));
92  return 0;
93  }
94  cfread( &version, sizeof(int), 1, fp );
95  major = version / 100;
96 
97  if ( major != NEBULA_MAJOR_VERSION ) {
98  mprintf(( "An out of date nebula file.\n" ));
99  return 0;
100  }
101 
102  cfread( &num_pts, sizeof(int), 1, fp );
103  Assert( num_pts < MAX_POINTS );
104  cfread( &num_tris, sizeof(int), 1, fp );
105  Assert( num_tris < MAX_TRIS );
106 
107  int i;
108  for (i=0; i<num_pts; i++ ) {
109  float xf, yf;
110  int l;
111 
112  cfread( &xf, sizeof(float), 1, fp );
113  cfread( &yf, sizeof(float), 1, fp );
114  cfread( &l, sizeof(int), 1, fp );
115  project_2d_onto_sphere( &nebula_vecs[i], 1.0f - xf, yf );
116  vm_vec_scale( &nebula_vecs[i], 10.0f );
117  nebula_verts[i].b = ubyte((l*255)/31);
118 
119  // throw in some randomness to the nebula vertices depth
120  }
121 
122  for (i=0; i<num_tris; i++ ) {
123  cfread( &tri[i][0], sizeof(int), 1, fp );
124  cfread( &tri[i][1], sizeof(int), 1, fp );
125  cfread( &tri[i][2], sizeof(int), 1, fp );
126  }
127 
128  cfclose(fp);
129 
130  return 1;
131 }
132 
133 void nebula_init( const char *filename, int pitch, int bank, int heading )
134 {
135  angles a;
136 
137  a.p = fl_radians(pitch);
138  a.b = fl_radians(bank);
139  a.h = fl_radians(heading);
140  nebula_init(filename, &a);
141 }
142 
143 void nebula_init( const char *filename, angles * pbh )
144 {
145  if ( Nebula_loaded ) {
146  nebula_close();
147  }
148 
149  if ( load_nebula_sub( cf_add_ext(filename, NOX(".neb")) ) ) {
150  Nebula_loaded = 1;
151  }
152 
153  if ( pbh ) {
154  Nebula_pbh = *pbh;
155  vm_angles_2_matrix(&Nebula_orient, &Nebula_pbh );
156 
157  } else {
158  Nebula_pbh.p = 0.0f;
159  Nebula_pbh.b = 0.0f;
160  Nebula_pbh.h = 0.0f;
161  Nebula_orient = vmd_identity_matrix;
162  }
163 }
164 
166 {
167  int i;
168  // int r, g, b;
169 
170  if (Fred_running) {
171  // no nebula for you!
172  return;
173  }
174 
175  if ( !Nebula_loaded ) {
176  return;
177  }
178 
179  if ( !Detail.planets_suns ) {
180  return;
181  }
182 
183  // Rotate the nebula.
184  g3_start_instance_matrix( NULL, &Nebula_orient, false);
185 
186  for (i=0; i<num_pts; i++ ) {
187  g3_rotate_faraway_vertex( &nebula_verts[i], &nebula_vecs[i] );
188  g3_project_vertex( &nebula_verts[i] );
189  }
190 
191  int saved_gr_zbuffering = gr_zbuffer_get();
192 
194 
195  for (i=0; i<num_tris; i++ ) {
196 
197  vertex * verts[3];
198 
199  verts[0] = &nebula_verts[tri[i][0]];
200  verts[1] = &nebula_verts[tri[i][1]];
201  verts[2] = &nebula_verts[tri[i][2]];
202 
204  }
205 
206  g3_done_instance(false);
207 
208  gr_zbuffer_set(saved_gr_zbuffering);
209 
210  // always switch off fogging for good measure
212  gr_fog_set(GR_FOGMODE_NONE, 0, 0, 0);
213  }
214 }
215 
216 DCF(nebula,"Loads a different nebula")
217 {
219 
220  if (dc_optional_string_either("help", "--help")) {
221  dc_printf("Usage: nebula [filename]\n");
222  dc_printf("Loads the nebula file (without filename extension). No filename takes away nebula\n" );
223  return;
224  }
225 
226  if (dc_maybe_stuff_string_white(filename)) {
227  nebula_init(filename.c_str());
228  } else {
229  nebula_close();
230  }
231 }
232 
233 
234 
#define gr_zbuffer_set
Definition: 2d.h:817
int i
Definition: multi_pxo.cpp:466
#define MAX_TRIS
Definition: nebula.cpp:21
int Nebula_heading
Definition: nebula.cpp:40
float p
Definition: pstypes.h:111
int Neb2_render_mode
Definition: neb.cpp:57
#define MISSION_FLAG_FULLNEB
Definition: missionparse.h:70
void nebula_init(const char *filename, int pitch, int bank, int heading)
Definition: nebula.cpp:133
void nebula_close()
Definition: nebula.cpp:42
int cfread(void *buf, int elsize, int nelem, CFILE *fp)
int Fred_running
Definition: fred.cpp:44
matrix * vm_angles_2_matrix(matrix *m, const angles *a)
Definition: vecmat.cpp:752
Assert(pm!=NULL)
Definition: pstypes.h:88
#define mprintf(args)
Definition: pstypes.h:238
#define NEBULA_FILE_ID
Definition: nebula.cpp:49
struct vec3d::@225::@227 xyz
GLclampf f
Definition: Glext.h:7097
#define GR_ZBUFF_NONE
Definition: 2d.h:672
Definition: cfile.h:28
enum_h * u
Definition: lua.cpp:12649
#define MAX_POINTS
Definition: nebula.cpp:22
std::basic_string< char, std::char_traits< char >, std::allocator< char > > SCP_string
Definition: vmallocator.h:21
#define TMAP_FLAG_NEBULA
Definition: tmapper.h:46
void g3_done_instance(bool set_api=false)
Definition: 3dsetup.cpp:341
#define NEBULA_MAJOR_VERSION
Definition: nebula.cpp:50
cfp version
Definition: cfile.cpp:1063
#define cfopen(...)
Definition: cfile.h:134
void vm_vec_scale(vec3d *dest, float s)
Definition: vecmat.cpp:248
#define TMAP_FLAG_GOURAUD
Definition: tmapper.h:40
detail_levels Detail
Definition: systemvars.cpp:478
GLboolean GLboolean GLboolean GLboolean a
Definition: Glext.h:5781
char * filename
GLdouble GLdouble z
Definition: Glext.h:5451
int Nebula_bank
Definition: nebula.cpp:39
int load_nebula_sub(char *filename)
Definition: nebula.cpp:76
GLdouble s
Definition: Glext.h:5321
bool dc_optional_string_either(const char *str1, const char *str2)
Searches for an optional string and it's alias.
int g3_project_vertex(vertex *point)
Definition: 3dmath.cpp:202
cfbp fp
Definition: cfile.cpp:1065
GLint GLint GLint GLint GLint x
Definition: Glext.h:5182
unsigned char ubyte
Definition: pstypes.h:62
#define NOX(s)
Definition: pstypes.h:473
ubyte b
Definition: pstypes.h:175
char * cf_add_ext(const char *filename, const char *ext)
Definition: cfile.cpp:458
__inline void gr_fog_set(int fog_mode, int r, int g, int b, float fog_near=-1.0f, float fog_far=-1.0f)
Definition: 2d.h:833
void project_2d_onto_sphere(vec3d *pnt, float u, float v)
Definition: nebula.cpp:56
ubyte g3_rotate_faraway_vertex(vertex *dest, const vec3d *src)
Definition: 3dmath.cpp:156
#define fl_sqrt(fl)
Definition: floating.h:29
An overhauled/updated debug console to allow monitoring, testing, and general debugging of new featur...
void g3_start_instance_matrix(const vec3d *pos, const matrix *orient, bool set_api=true)
Definition: 3dsetup.cpp:249
#define PI
Definition: pstypes.h:303
#define TMAP_FLAG_RAMP
Definition: tmapper.h:38
DCF(nebula,"Loads a different nebula")
Definition: nebula.cpp:216
void dc_printf(const char *format,...)
Prints the given char string to the debug console.
Definition: console.cpp:358
#define NEB2_RENDER_NONE
Definition: neb.h:31
void nebula_render()
Definition: nebula.cpp:165
#define gr_zbuffer_get
Definition: 2d.h:816
mission The_mission
int g3_draw_poly(int nv, vertex **pointlist, uint tmap_flags)
Definition: 3ddraw.cpp:207
int cfclose(CFILE *cfile)
Definition: cfile.cpp:895
float h
Definition: pstypes.h:111
const GLdouble * v
Definition: Glext.h:5322
matrix vmd_identity_matrix
Definition: vecmat.cpp:28
float b
Definition: pstypes.h:111
GLint y
Definition: Gl.h:1505
bool dc_maybe_stuff_string_white(char *str, size_t len)
Tries to stuff a whitespace delimited string to out_str from the command line, stopping at the end of...
#define fl_radians(fl)
Definition: floating.h:42
#define GR_FOGMODE_NONE
Definition: 2d.h:355
int Nebula_pitch
Definition: nebula.cpp:38