FS2_Open
Open source remastering of the Freespace 2 engine
3dmath.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 "graphics/2d.h"
13 #include "hud/hud.h" //For HUD_offset_*
14 #include "render/3dinternal.h"
15 
16 #define MIN_Z 0.0f
17 
22 {
23  ubyte cc=0;
24 
25  if (p->xyz.x > p->xyz.z)
26  cc |= CC_OFF_RIGHT;
27 
28  if (p->xyz.y > p->xyz.z)
29  cc |= CC_OFF_TOP;
30 
31  if (p->xyz.x < -p->xyz.z)
32  cc |= CC_OFF_LEFT;
33 
34  if (p->xyz.y < -p->xyz.z)
35  cc |= CC_OFF_BOT;
36 
37  if (p->xyz.z < MIN_Z )
38  cc |= CC_BEHIND;
39 
40  if ( G3_user_clip ) {
41  // Check if behind user plane
43  cc |= CC_OFF_USER;
44  }
45  }
46 
47  return cc;
48 }
49 
50 
55 {
56  ubyte cc=0;
57 
58  if (p->world.xyz.x > p->world.xyz.z)
59  cc |= CC_OFF_RIGHT;
60 
61  if (p->world.xyz.y > p->world.xyz.z)
62  cc |= CC_OFF_TOP;
63 
64  if (p->world.xyz.x < -p->world.xyz.z)
65  cc |= CC_OFF_LEFT;
66 
67  if (p->world.xyz.y < -p->world.xyz.z)
68  cc |= CC_OFF_BOT;
69 
70  if (p->world.xyz.z < MIN_Z )
71  cc |= CC_BEHIND;
72 
73  if ( G3_user_clip ) {
74  // Check if behind user plane
76  cc |= CC_OFF_USER;
77  }
78  }
79 
80  return p->codes = cc;
81 
82 }
83 
85 {
86  dest->world = *src;
87 
88  dest->codes = 0;
89  dest->flags = 0;
90 
91  return 0;
92 }
93 
94 
95 MONITOR( NumRotations )
96 
98 {
99 #if 0
100  vec3d tempv;
101  Assert( G3_count == 1 );
102  vm_vec_sub(&tempv,src,&View_position);
103  vm_vec_rotate( (vec3d *)&dest->x, &tempv, &View_matrix );
104  dest->flags = 0; //not projected
105  return g3_code_vertex(dest);
106 #else
107  float tx, ty, tz, x,y,z;
108  ubyte codes;
109 
110  MONITOR_INC( NumRotations, 1 );
111 
112  tx = src->xyz.x - View_position.xyz.x;
113  ty = src->xyz.y - View_position.xyz.y;
114  tz = src->xyz.z - View_position.xyz.z;
115 
116  x = tx * View_matrix.vec.rvec.xyz.x;
117  x += ty * View_matrix.vec.rvec.xyz.y;
118  x += tz * View_matrix.vec.rvec.xyz.z;
119 
120  y = tx * View_matrix.vec.uvec.xyz.x;
121  y += ty * View_matrix.vec.uvec.xyz.y;
122  y += tz * View_matrix.vec.uvec.xyz.z; //-V537
123 
124  z = tx * View_matrix.vec.fvec.xyz.x;
125  z += ty * View_matrix.vec.fvec.xyz.y;
126  z += tz * View_matrix.vec.fvec.xyz.z;
127 
128  codes = 0;
129 
130  if (x > z) codes |= CC_OFF_RIGHT;
131  if (x < -z) codes |= CC_OFF_LEFT;
132  if (y > z) codes |= CC_OFF_TOP;
133  if (y < -z) codes |= CC_OFF_BOT;
134  if (z < MIN_Z ) codes |= CC_BEHIND;
135 
136  dest->world.xyz.x = x;
137  dest->world.xyz.y = y;
138  dest->world.xyz.z = z;
139 
140  if ( G3_user_clip ) {
141  // Check if behind user plane
142  if ( g3_point_behind_user_plane(&dest->world)) {
143  codes |= CC_OFF_USER;
144  }
145  }
146 
147  dest->codes = codes;
148 
149  dest->flags = 0; // not projected
150 
151  return codes;
152 #endif
153 }
154 
155 
157 {
158  Assert( G3_count == 1 );
159 
160  MONITOR_INC( NumRotations, 1 );
161 
162  vm_vec_rotate( &dest->world, src, &View_matrix );
163  dest->flags = 0; //not projected
164  return g3_code_vertex(dest);
165 }
166 
167 
172 {
173  vec3d tempv;
174 
175  Assert( G3_count == 1 );
176 
177  MONITOR_INC( NumRotations, 1 );
178 
179  vm_vec_sub(&tempv,src,&View_position);
180  vm_vec_rotate(dest,&tempv,&View_matrix);
181  return g3_code_vector(dest);
182 }
183 
184 ubyte g3_project_vector(const vec3d *p, float *sx, float *sy )
185 {
186  float w;
187 
188  Assert( G3_count == 1 );
189 
190  if ( p->xyz.z <= MIN_Z ) return PF_OVERFLOW;
191 
192  w=1.0f / p->xyz.z;
193 
194  *sx = (Canvas_width + (p->xyz.x*Canvas_width*w))*0.5f;
195  *sy = (Canvas_height - (p->xyz.y*Canvas_height*w))*0.5f;
196  return PF_PROJECTED;
197 }
198 
203 {
204  float w;
205 
206  Assert( G3_count == 1 );
207 
208  if ( p->flags & PF_PROJECTED )
209  return p->flags;
210 
211  if ( p->world.xyz.z <= MIN_Z ) {
212  p->flags |= PF_OVERFLOW;
213  } else {
214  w = 1.0f / p->world.xyz.z;
215  p->screen.xyw.x = (Canvas_width + (p->world.xyz.x*Canvas_width*w))*0.5f;
216  p->screen.xyw.y = (Canvas_height - (p->world.xyz.y*Canvas_height*w))*0.5f;
217 
218  if ( w > 1.0f ) w = 1.0f;
219 
220  p->screen.xyw.w = w;
221  p->flags |= PF_PROJECTED;
222  }
223 
224  return p->flags;
225 }
226 
227 
231 void g3_point_to_vec(vec3d *v, int sx, int sy)
232 {
233  vec3d tempv;
234 
235  Assert( G3_count == 1 );
236 
237  tempv.xyz.x = ((float)sx - Canv_w2) / Canv_w2;
238  tempv.xyz.y = -((float)sy - Canv_h2) / Canv_h2;
239  tempv.xyz.z = 1.0f;
240 
241  tempv.xyz.x = tempv.xyz.x * Matrix_scale.xyz.z / Matrix_scale.xyz.x;
242  tempv.xyz.y = tempv.xyz.y * Matrix_scale.xyz.z / Matrix_scale.xyz.y;
243 
244  vm_vec_normalize(&tempv);
245  vm_vec_unrotate(v, &tempv, &Unscaled_matrix);
246 }
247 
254 void g3_point_to_vec_delayed(vec3d *v, int sx, int sy)
255 {
256  vec3d tempv;
257 
258  tempv.xyz.x = ((float)sx - Canv_w2) / Canv_w2;
259  tempv.xyz.y = -((float)sy - Canv_h2) / Canv_h2;
260  tempv.xyz.z = 1.0f;
261 
262  tempv.xyz.x = tempv.xyz.x * Matrix_scale.xyz.z / Matrix_scale.xyz.x;
263  tempv.xyz.y = tempv.xyz.y * Matrix_scale.xyz.z / Matrix_scale.xyz.y;
264 
265  vm_vec_normalize(&tempv);
266  vm_vec_unrotate(v, &tempv, &Unscaled_matrix);
267 }
268 
270 {
271  Assert( G3_count == 1 );
272  return vm_vec_rotate(dest,src,&View_matrix);
273 }
274 
278 float g3_calc_point_depth(const vec3d *pnt)
279 {
280  float q;
281 
282  q = (pnt->xyz.x - View_position.xyz.x) * View_matrix.vec.fvec.xyz.x;
283  q += (pnt->xyz.y - View_position.xyz.y) * View_matrix.vec.fvec.xyz.y;
284  q += (pnt->xyz.z - View_position.xyz.z) * View_matrix.vec.fvec.xyz.z;
285 
286  return q;
287 }
#define CC_OFF_BOT
Definition: 3d.h:28
struct screen3d::@234::@236 xyw
float g3_calc_point_depth(const vec3d *pnt)
Definition: 3dmath.cpp:278
void g3_point_to_vec_delayed(vec3d *v, int sx, int sy)
Definition: 3dmath.cpp:254
vec3d View_position
Definition: 3dsetup.cpp:20
ubyte g3_rotate_vector(vec3d *dest, const vec3d *src)
Definition: 3dmath.cpp:171
ubyte g3_rotate_faraway_vertex(vertex *dest, const vec3d *src)
Definition: 3dmath.cpp:156
float Canv_h2
Definition: 3dsetup.cpp:40
Assert(pm!=NULL)
Definition: pstypes.h:88
matrix Unscaled_matrix
Definition: 3dsetup.cpp:21
struct vec3d::@225::@227 xyz
GLclampf f
Definition: Glext.h:7097
ubyte g3_code_vertex(vertex *p)
Definition: 3dmath.cpp:54
ubyte g3_transfer_vertex(vertex *dest, const vec3d *src)
Definition: 3dmath.cpp:84
vec3d * vm_vec_rotate(vec3d *dest, const vec3d *src, const matrix *m)
Definition: vecmat.cpp:933
int g3_point_behind_user_plane(const vec3d *pnt)
Definition: 3dsetup.cpp:419
ubyte g3_code_vector(const vec3d *p)
Definition: 3dmath.cpp:21
#define CC_OFF_USER
Definition: 3d.h:30
int G3_user_clip
Definition: 3dsetup.cpp:360
#define PF_OVERFLOW
Definition: 3d.h:22
struct matrix::@228::@230 vec
screen3d screen
Definition: pstypes.h:173
#define MIN_Z
Definition: 3dmath.cpp:16
#define w(p)
Definition: modelsinc.h:68
vec3d * g3_rotate_delta_vec(vec3d *dest, const vec3d *src)
Definition: 3dmath.cpp:269
GLdouble GLdouble z
Definition: Glext.h:5451
int Canvas_width
Definition: 3dsetup.cpp:36
#define MONITOR(function_name)
Definition: pstypes.h:454
#define CC_OFF_RIGHT
Definition: 3d.h:27
vec3d Matrix_scale
Definition: 3dsetup.cpp:34
vec3d * vm_vec_unrotate(vec3d *dest, const vec3d *src, const matrix *m)
Definition: vecmat.cpp:959
GLint GLint GLint GLint GLint x
Definition: Glext.h:5182
unsigned char ubyte
Definition: pstypes.h:62
#define MONITOR_INC(function_name, inc)
Definition: pstypes.h:457
ubyte flags
Definition: pstypes.h:178
void vm_vec_sub(vec3d *dest, const vec3d *src0, const vec3d *src1)
Definition: vecmat.cpp:168
#define CC_BEHIND
Definition: 3d.h:31
typedef float(SCP_EXT_CALLCONV *SCPTRACKIR_PFFLOATVOID)()
GLdouble GLdouble GLdouble GLdouble q
Definition: Glext.h:5345
#define CC_OFF_TOP
Definition: 3d.h:29
ubyte g3_project_vector(const vec3d *p, float *sx, float *sy)
Definition: 3dmath.cpp:184
GLfloat GLfloat p
Definition: Glext.h:8373
GLenum src
Definition: Glext.h:5917
ubyte g3_rotate_vertex(vertex *dest, const vec3d *src)
Definition: 3dmath.cpp:97
#define PF_PROJECTED
Definition: 3d.h:21
matrix View_matrix
Definition: 3dsetup.cpp:19
vec3d world
Definition: pstypes.h:172
float Canv_w2
Definition: 3dsetup.cpp:39
int G3_count
Definition: 3dsetup.cpp:59
ubyte codes
Definition: pstypes.h:177
int g3_project_vertex(vertex *p)
Definition: 3dmath.cpp:202
void g3_point_to_vec(vec3d *v, int sx, int sy)
Definition: 3dmath.cpp:231
int Canvas_height
Definition: 3dsetup.cpp:37
const GLdouble * v
Definition: Glext.h:5322
GLbyte GLbyte tz
Definition: Glext.h:8227
GLbyte ty
Definition: Glext.h:8227
#define CC_OFF_LEFT
Definition: 3d.h:26
GLint y
Definition: Gl.h:1505
float vm_vec_normalize(vec3d *v)
Definition: vecmat.cpp:460