FS2_Open
Open source remastering of the Freespace 2 engine
3dsetup.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 
13 #include "graphics/2d.h" // Needed for w,h,aspect of canvas
14 #include "graphics/tmapper.h"
15 #include "lighting/lighting.h"
16 #include "render/3dinternal.h"
17 
18 
19 matrix View_matrix; // The matrix to convert local coordinates to screen
20 vec3d View_position; // The offset to convert local coordinates to screen
21 matrix Unscaled_matrix; // View_matrix before scaling
22 
23 matrix Light_matrix; // Used to rotate world points into current local coordinates
24 vec3d Light_base; // Used to rotate world points into current local coordinates
25 
26 matrix Eye_matrix; // Where the viewer's eye is pointing in World coordinates
27 vec3d Eye_position; // Where the viewer's eye is at in World coordinates
28 float Eye_fov; // What the viewer's FOV is
29 
30 float View_zoom; // The zoom factor
31 float Proj_fov; // The fov (for HTL projection matrix)
32 
33 vec3d Window_scale; // Scaling for window aspect
34 vec3d Matrix_scale; // How the matrix is scaled, window_scale * zoom
35 
36 int Canvas_width; // The actual width
37 int Canvas_height; // The actual height
38 
39 float Canv_w2; // Canvas_width / 2
40 float Canv_h2; // Canvas_height / 2
41 
43 matrix Object_matrix; // Where the opject is pointing in World coordinates
44 
45 
46 #define MAX_INSTANCE_DEPTH 10
47 
56 
58 
59 int G3_count = 0;
61 extern int Cmdline_nohtl;
62 
67 {
68  return G3_count;
69 }
70 
75 void g3_start_frame_func(int zbuffer_flag, const char *filename, int lineno)
76 {
77  float s;
78  int width, height;
79  float aspect;
80 
81  Assert( G3_count == 0 );
82  G3_count++;
83 
84  // Clear any user-defined clip planes
86 
87  // Get the values from the 2d...
88  width = gr_screen.clip_width;
89  height = gr_screen.clip_height;
90  aspect = gr_screen.aspect;
91 
92  //set int w,h & fixed-point w,h/2
94  Canv_w2 = (float)width / 2.0f;
96  Canv_h2 = (float)height / 2.0f;
97 
98  //compute aspect ratio for this canvas
99  s = aspect*(float)Canvas_height/(float)Canvas_width;
100 
101  if ( !Cmdline_nohtl || (s <= 0.0f) ) { //scale x
102  Window_scale.xyz.x = s;
103  Window_scale.xyz.y = 1.0f;
104  }
105  else {
106  Window_scale.xyz.y = 1.0f / s;
107  Window_scale.xyz.x = 1.0f;
108  }
109 
110  Window_scale.xyz.z = 1.0f; //always 1
111 
113 
114  if (zbuffer_flag) {
116  } else {
118  }
119 
120  G3_frame_count++;
121 }
122 
126 void g3_end_frame_func(const char *filename, int lineno)
127 {
128  G3_count--;
129  Assert( G3_count == 0 );
130 
131  free_point_num = 0;
132 }
133 
134 
135 void scale_matrix(void);
136 
137 void g3_set_view(camera *cam)
138 {
139  vec3d pos;
140  matrix ori;
141  cam->get_info(&pos, &ori);
142 
143  if(Sexp_fov <= 0.0f)
144  g3_set_view_matrix(&pos, &ori, cam->get_fov());
145  else
146  g3_set_view_matrix(&pos, &ori, Sexp_fov);
147 }
148 
152 void g3_set_view_matrix(const vec3d *view_pos, const matrix *view_matrix, float zoom)
153 {
154  Assert( G3_count == 1 );
155 
156  View_zoom = zoom;
157  View_position = *view_pos;
158 
159  View_matrix = *view_matrix;
160 
161  Proj_fov = 1.39626348f * View_zoom;
162 
163  Eye_matrix = View_matrix;
164  Eye_position = *view_pos;
165  Eye_fov = zoom;
166 
167  scale_matrix();
168 
169  Light_matrix = vmd_identity_matrix;
170  Light_base.xyz.x = 0.0f;
171  Light_base.xyz.y = 0.0f;
172  Light_base.xyz.z = 0.0f;
173 
174  vm_vec_zero(&Object_position);
175  Object_matrix = vmd_identity_matrix;
176 }
177 
178 
182 void g3_set_view_angles(const vec3d *view_pos, const angles *view_orient, float zoom)
183 {
184  matrix tmp;
185 
186  Assert( G3_count == 1 );
187 
188  vm_angles_2_matrix(&tmp,view_orient);
189  g3_set_view_matrix(view_pos,&tmp,zoom);
190 }
191 
192 
196 void scale_matrix(void)
197 {
198  Unscaled_matrix = View_matrix; //so we can use unscaled if we want
199 
200  Matrix_scale = Window_scale;
201 
202  float s = 1.0f;
203 
204  if (Cmdline_nohtl) {
205  if (View_zoom <= 1.0f) { //zoom in by scaling z
206  Matrix_scale.xyz.z = Matrix_scale.xyz.z*View_zoom;
207  } else { //zoom out by scaling x&y
208  s = 1.0f / View_zoom;
209 
210  Matrix_scale.xyz.x *= s;
211  Matrix_scale.xyz.y *= s;
212  }
213  } else {
214  s = 1.0f / tanf(Proj_fov * 0.5f);
215 
216  Matrix_scale.xyz.x *= s;
217  Matrix_scale.xyz.y *= s;
218  }
219 
220  //now scale matrix elements
221 
222  vm_vec_scale(&View_matrix.vec.rvec,Matrix_scale.xyz.x );
223  vm_vec_scale(&View_matrix.vec.uvec,Matrix_scale.xyz.y );
224  vm_vec_scale(&View_matrix.vec.fvec,Matrix_scale.xyz.z );
225 
226 }
227 
229 {
230  vec3d tempv;
231 
232  Assert( G3_count == 1 );
233 
234  Assert( instance_depth > 0 );
235 
236  vm_vec_sub(&tempv,src,&instance_stack[0].p);
237  vm_vec_rotate( &dest->world, &tempv, &instance_stack[0].m );
238  dest->flags = 0; //not projected
239  return g3_code_vertex(dest);
240 }
241 
242 
249 void g3_start_instance_matrix(const vec3d *pos, const matrix *orient, bool set_api)
250 {
251  vec3d tempv;
252  matrix tempm,tempm2;
253 
254  Assert( G3_count == 1 );
255 
257 
264  instance_depth++;
265 
266  // Make sure orient is valid
267  if (!orient) {
268  orient = &vmd_identity_matrix; // Assume no change in orient
269  }
270 
271  if ( pos ) {
272  //step 1: subtract object position from view position
273  vm_vec_sub2(&View_position,pos);
274 
275  //step 2: rotate view vector through object matrix
276  vm_vec_rotate(&tempv,&View_position,orient);
277  View_position = tempv;
278 
279  vm_vec_unrotate(&tempv,pos,&Object_matrix);
280  vm_vec_add2(&Object_position, &tempv);
281  } else {
282  // No movement, leave View_position alone
283  }
284 
285  //step 3: rotate object matrix through view_matrix (vm = ob * vm)
286  vm_copy_transpose(&tempm2,orient);
287 
288  vm_matrix_x_matrix(&tempm,&tempm2,&View_matrix);
289  View_matrix = tempm;
290 
291  vm_matrix_x_matrix(&Object_matrix,&instance_stack[instance_depth-1].om,orient);
292 
293  // Update the lighting matrix
294  matrix saved_orient = Light_matrix;
295  vec3d saved_base = Light_base;
296 
297  if ( pos ) {
298  vm_vec_unrotate(&Light_base,pos,&saved_orient );
299  vm_vec_add2(&Light_base, &saved_base );
300  } else {
301  // No movement, light_base doesn't change.
302  }
303 
304  vm_matrix_x_matrix(&Light_matrix,&saved_orient, orient);
305 
306  if(!Cmdline_nohtl && set_api)
307  gr_start_instance_matrix(pos, orient);
308 
309 }
310 
311 
318 {
319  matrix tm;
320 
321  Assert( G3_count == 1 );
322 
323  if (orient==NULL) {
324  g3_start_instance_matrix(pos,NULL);
325  return;
326  }
327 
328  vm_angles_2_matrix(&tm,orient);
329 
330  g3_start_instance_matrix(pos,&tm, false);
331 
332  if(!Cmdline_nohtl)
333  gr_start_angles_instance_matrix(pos, orient);
334 
335 }
336 
337 
341 void g3_done_instance(bool use_api)
342 {
343  Assert( G3_count == 1 );
344 
345  instance_depth--;
346 
347  Assert(instance_depth >= 0);
348 
349  View_position = instance_stack[instance_depth].p;
350  View_matrix = instance_stack[instance_depth].m;
351  Light_matrix = instance_stack[instance_depth].lm;
352  Light_base = instance_stack[instance_depth].lb;
353  Object_matrix = instance_stack[instance_depth].om;
354  Object_position = instance_stack[instance_depth].op;
355 
356  if (!Cmdline_nohtl && use_api)
358 }
359 
360 int G3_user_clip = 0;
363 
381 void g3_start_user_clip_plane(const vec3d *plane_point, const vec3d *plane_normal )
382 {
383  float mag = vm_vec_mag( plane_normal );
384  if ( (mag < 0.1f) || (mag > 1.5f ) ) {
385  // Invalid plane_normal passed in. Get Allender (since it is
386  // probably a ship warp in bug:) or John.
387  Int3();
388  return;
389  }
390 
391  G3_user_clip = 1;
392  if(!Cmdline_nohtl) {
393  G3_user_clip_normal = *plane_normal;
394  G3_user_clip_point = *plane_point;
395  gr_start_clip();
396  }
397  vm_vec_rotate(&G3_user_clip_normal, plane_normal, &Eye_matrix );
398  vm_vec_normalize(&G3_user_clip_normal);
399 
400  vec3d tempv;
401  vm_vec_sub(&tempv,plane_point,&Eye_position);
402  vm_vec_rotate(&G3_user_clip_point,&tempv,&Eye_matrix );
403 }
404 
409 {
410  G3_user_clip = 0;
411  if(!Cmdline_nohtl) {
412  gr_end_clip();
413  }
414 }
415 
420 {
421  if ( G3_user_clip ) {
422  vec3d tmp;
423  vm_vec_sub( &tmp, pnt, &G3_user_clip_point );
424  if ( vm_vec_dot( &tmp, &G3_user_clip_normal ) <= 0.0f ) {
425  return 1;
426  }
427  }
428 
429  return 0;
430 }
matrix Unscaled_matrix
Definition: 3dsetup.cpp:21
matrix * vm_matrix_x_matrix(matrix *dest, const matrix *src0, const matrix *src1)
Definition: vecmat.cpp:1006
vec3d Eye_position
Definition: 3dsetup.cpp:27
float vm_vec_mag(const vec3d *v)
Definition: vecmat.cpp:325
matrix * vm_angles_2_matrix(matrix *m, const angles *a)
Definition: vecmat.cpp:752
#define gr_start_instance_matrix
Definition: 2d.h:899
matrix View_matrix
Definition: 3dsetup.cpp:19
vec3d Light_base
Definition: 3dsetup.cpp:24
#define gr_start_angles_instance_matrix
Definition: 2d.h:900
Assert(pm!=NULL)
Definition: pstypes.h:88
matrix Light_matrix
Definition: 3dsetup.cpp:23
void init_free_points(void)
Definition: 3dclipper.cpp:21
struct vec3d::@225::@227 xyz
GLclampf f
Definition: Glext.h:7097
float Canv_w2
Definition: 3dsetup.cpp:39
#define TRUE
Definition: pstypes.h:399
matrix * vm_copy_transpose(matrix *dest, const matrix *src)
Definition: vecmat.cpp:984
vec3d * vm_vec_rotate(vec3d *dest, const vec3d *src, const matrix *m)
Definition: vecmat.cpp:933
ubyte g3_code_vertex(vertex *point)
Definition: 3dmath.cpp:54
#define gr_end_instance_matrix
Definition: 2d.h:901
hull_check orient
Definition: lua.cpp:5049
matrix Eye_matrix
Definition: 3dsetup.cpp:26
#define Int3()
Definition: pstypes.h:292
vec3d view_pos
Definition: fredrender.cpp:103
void scale_matrix(void)
Definition: 3dsetup.cpp:196
struct instance_context instance_stack[MAX_INSTANCE_DEPTH]
int clip_height
Definition: 2d.h:378
void get_info(vec3d *position, matrix *orientation)
Definition: camera.cpp:295
GLint GLsizei width
Definition: Gl.h:1505
vec3d Matrix_scale
Definition: 3dsetup.cpp:34
float Sexp_fov
Definition: camera.cpp:16
float Proj_fov
Definition: 3dsetup.cpp:31
void g3_stop_user_clip_plane()
Definition: 3dsetup.cpp:408
void vm_vec_add2(vec3d *dest, const vec3d *src)
Definition: vecmat.cpp:178
struct matrix::@228::@230 vec
int Cmdline_nohtl
Definition: cmdline.cpp:438
matrix view_orient
Definition: fredrender.cpp:112
void vm_vec_scale(vec3d *dest, float s)
Definition: vecmat.cpp:248
vec3d Window_scale
Definition: 3dsetup.cpp:33
int g3_point_behind_user_plane(const vec3d *pnt)
Definition: 3dsetup.cpp:419
int g3_in_frame()
Definition: 3dsetup.cpp:66
void g3_end_frame_func(const char *filename, int lineno)
Definition: 3dsetup.cpp:126
char * filename
#define MAX_INSTANCE_DEPTH
Definition: 3dsetup.cpp:46
void vm_vec_sub2(vec3d *dest, const vec3d *src)
Definition: vecmat.cpp:187
int G3_user_clip
Definition: 3dsetup.cpp:360
void g3_start_frame_func(int zbuffer_flag, const char *filename, int lineno)
Definition: 3dsetup.cpp:75
GLdouble s
Definition: Glext.h:5321
#define gr_start_clip
Definition: 2d.h:929
int Canvas_height
Definition: 3dsetup.cpp:37
vec3d * vm_vec_unrotate(vec3d *dest, const vec3d *src, const matrix *m)
Definition: vecmat.cpp:959
unsigned char ubyte
Definition: pstypes.h:62
#define vm_vec_zero(v)
Definition: vecmat.h:37
void g3_done_instance(bool use_api)
Definition: 3dsetup.cpp:341
ubyte flags
Definition: pstypes.h:178
void vm_vec_sub(vec3d *dest, const vec3d *src0, const vec3d *src1)
Definition: vecmat.cpp:168
typedef float(SCP_EXT_CALLCONV *SCPTRACKIR_PFFLOATVOID)()
vec3d G3_user_clip_point
Definition: 3dsetup.cpp:362
void g3_start_instance_matrix(const vec3d *pos, const matrix *orient, bool set_api)
Definition: 3dsetup.cpp:249
vec3d View_position
Definition: 3dsetup.cpp:20
GLint GLsizei GLsizei height
Definition: Gl.h:1505
void g3_start_instance_angles(const vec3d *pos, const angles *orient)
Definition: 3dsetup.cpp:317
int G3_count
Definition: 3dsetup.cpp:59
#define gr_end_clip
Definition: 2d.h:930
int free_point_num
Definition: 3dclipper.cpp:16
int clip_width
Definition: 2d.h:378
int G3_frame_count
Definition: 3dsetup.cpp:60
screen gr_screen
Definition: 2d.cpp:46
void g3_set_view(camera *cam)
Definition: 3dsetup.cpp:137
void g3_start_user_clip_plane(const vec3d *plane_point, const vec3d *plane_normal)
Definition: 3dsetup.cpp:381
int Canvas_width
Definition: 3dsetup.cpp:36
GLfloat GLfloat p
Definition: Glext.h:8373
ubyte g3_rotate_vertex_popped(vertex *dest, const vec3d *src)
Definition: 3dsetup.cpp:228
GLenum src
Definition: Glext.h:5917
int instance_depth
Definition: 3dsetup.cpp:57
vec3d G3_user_clip_normal
Definition: 3dsetup.cpp:361
matrix Object_matrix
Definition: 3dsetup.cpp:43
hull_check pos
Definition: lua.cpp:5050
float get_fov()
Definition: camera.cpp:284
float vm_vec_dot(const vec3d *v0, const vec3d *v1)
Definition: vecmat.cpp:312
vec3d world
Definition: pstypes.h:172
Definition: camera.h:18
vec3d Object_position
Definition: 3dsetup.cpp:42
float View_zoom
Definition: 3dsetup.cpp:30
float Canv_h2
Definition: 3dsetup.cpp:40
#define FALSE
Definition: pstypes.h:400
float Eye_fov
Definition: 3dsetup.cpp:28
matrix vmd_identity_matrix
Definition: vecmat.cpp:28
void g3_set_view_matrix(const vec3d *view_pos, const matrix *view_matrix, float zoom)
Definition: 3dsetup.cpp:152
float aspect
Definition: 2d.h:372
void g3_set_view_angles(const vec3d *view_pos, const angles *view_orient, float zoom)
Definition: 3dsetup.cpp:182
#define gr_zbuffer_clear
Definition: 2d.h:818
float vm_vec_normalize(vec3d *v)
Definition: vecmat.cpp:460