FS2_Open
Open source remastering of the Freespace 2 engine
missiongrid.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 "io/key.h"
14 #include "math/fvi.h"
15 #include "mission/missiongrid.h"
16 #include "render/3d.h"
17 
18 
22 
23 void grid_read_camera_controls( control_info * ci, float frametime )
24 {
25  float kh;
26 
27  {
28  float temp = ci->heading;
29  float temp1 = ci->pitch;
30  memset( ci, 0, sizeof(control_info) );
31  ci->heading = temp;
32  ci->pitch = temp1;
33  }
34 
35  // From keyboard...
37  if (kh == 0.0f)
38  ci->heading = 0.0f;
39  else if (kh > 0.0f) {
40  if (ci->heading < 0.0f)
41  ci->heading = 0.0f;
42  } else // kh < 0
43  if (ci->heading > 0.0f)
44  ci->heading = 0.0f;
45  ci->heading += kh;
46 
48  if (kh == 0.0f)
49  ci->pitch = 0.0f;
50  else if (kh > 0.0f) {
51  if (ci->pitch < 0.0f)
52  ci->pitch = 0.0f;
53  } else // kh < 0
54  if (ci->pitch > 0.0f)
55  ci->pitch = 0.0f;
56  ci->pitch += kh;
57 
62 }
63 
64 // Project the viewer's position onto the grid plane. If more than threshold distance
65 // from grid center, move grid center.
66 void maybe_create_new_grid(grid* gridp, vec3d *pos, matrix *orient, int force)
67 {
68  int roundoff;
69  plane tplane;
70  vec3d gpos, tmp, c;
71  float dist_to_plane;
72  float square_size, ux, uy, uz;
73 
74  ux = tplane.A = gridp->gmatrix.vec.uvec.xyz.x;
75  uy = tplane.B = gridp->gmatrix.vec.uvec.xyz.y;
76  uz = tplane.C = gridp->gmatrix.vec.uvec.xyz.z;
77  tplane.D = gridp->planeD;
78 
79  compute_point_on_plane(&c, &tplane, pos);
80  dist_to_plane = fl_abs(vm_dist_to_plane(pos, &gridp->gmatrix.vec.uvec, &c));
81  square_size = 1.0f;
82  while (dist_to_plane >= 25.0f)
83  {
84  square_size *= 10.0f;
85  dist_to_plane /= 10.0f;
86  }
87 
88  if (fvi_ray_plane(&gpos, &gridp->center, &gridp->gmatrix.vec.uvec, pos, &orient->vec.fvec, 0.0f)<0.0f) {
89  vec3d p;
90  vm_vec_scale_add(&p,pos,&orient->vec.fvec, 100.0f );
91  compute_point_on_plane(&gpos, &tplane, &p );
92  }
93 
94  if (vm_vec_dist(&gpos, &c) > 50.0f * square_size)
95  {
96  vm_vec_sub(&tmp, &gpos, &c);
97  vm_vec_normalize(&tmp);
98  vm_vec_scale_add(&gpos, &c, &tmp, 50.0f * square_size);
99  }
100 
101  roundoff = (int) square_size * 10;
102  if (!ux)
103  gpos.xyz.x = fl_roundoff(gpos.xyz.x, roundoff);
104  if (!uy)
105  gpos.xyz.y = fl_roundoff(gpos.xyz.y, roundoff);
106  if (!uz)
107  gpos.xyz.z = fl_roundoff(gpos.xyz.z, roundoff);
108 
109  if ((square_size != gridp->square_size) ||
110  (gpos.xyz.x != gridp->center.xyz.x) ||
111  (gpos.xyz.y != gridp->center.xyz.y) ||
112  (gpos.xyz.z != gridp->center.xyz.z) || force)
113  {
114  gridp->square_size = square_size;
115  gridp->center = gpos;
116  modify_grid(gridp);
117  }
118 }
119 
120 // Create a grid
121 // *forward is vector pointing forward
122 // *right is vector pointing right
123 // *center is center point of grid
124 // length is length of grid
125 // width is width of grid
126 // square_size is size of a grid square
127 // For example:
128 // *forward = (0.0, 0.0, 1.0)
129 // *right = (1.0, 0.0, 0.0)
130 // *center = (0.0, 0.0, 0.0)
131 // nrows = 10
132 // ncols = 50.0
133 // square_size = 10.0
134 // will generate a grid of squares 10 long by 5 wide.
135 // Each grid square will be 10.0 x 10.0 units.
136 // The center of the grid will be at the global origin.
137 // The grid will be parallel to the xz plane (because the normal is 0,1,0).
138 // (In fact, it will be the xz plane because it is centered on the origin.)
139 //
140 // Stuffs grid in *gridp. If gridp == NULL, mallocs and returns a grid.
141 grid *create_grid(grid *gridp, vec3d *forward, vec3d *right, vec3d *center, int nrows, int ncols, float square_size)
142 {
143  int i, ncols2, nrows2, d = 1;
144  vec3d dfvec, drvec, cur, cur2, tvec, uvec, save, save2;
145 
146  Assert(square_size > 0.0);
148  d = 2;
149 
150  if (gridp == NULL)
151  gridp = (grid *) vm_malloc(sizeof(grid));
152 
153  Assert(gridp);
154 
155  gridp->center = *center;
156  gridp->square_size = square_size;
157 
158  // Create the plane equation.
159  Assert(!IS_VEC_NULL(forward));
160  Assert(!IS_VEC_NULL(right));
161 
162  vm_vec_copy_normalize(&dfvec, forward);
163  vm_vec_copy_normalize(&drvec, right);
164 
165  vm_vec_cross(&uvec, &dfvec, &drvec);
166 
167  Assert(!IS_VEC_NULL(&uvec));
168 
169  gridp->gmatrix.vec.uvec = uvec;
170 
171  gridp->planeD = -(center->xyz.x * uvec.xyz.x + center->xyz.y * uvec.xyz.y + center->xyz.z * uvec.xyz.z);
172  Assert(!_isnan(gridp->planeD));
173 
174  gridp->gmatrix.vec.fvec = dfvec;
175  gridp->gmatrix.vec.rvec = drvec;
176 
177  vm_vec_scale(&dfvec, square_size);
178  vm_vec_scale(&drvec, square_size);
179 
180  vm_vec_scale_add(&cur, center, &dfvec, (float) -nrows * d / 2);
181  vm_vec_scale_add2(&cur, &drvec, (float) -ncols * d / 2);
182  vm_vec_scale_add(&cur2, center, &dfvec, (float) -nrows * 5 / 2);
183  vm_vec_scale_add2(&cur2, &drvec, (float) -ncols * 5 / 2);
184  save = cur;
185  save2 = cur2;
186 
187  gridp->ncols = ncols;
188  gridp->nrows = nrows;
189  ncols2 = ncols / 2;
190  nrows2 = nrows / 2;
191  Assert(ncols < MAX_GRIDLINE_POINTS && nrows < MAX_GRIDLINE_POINTS);
192 
193  // Create the points along the edges of the grid, so we can just draw lines
194  // between them to form the grid.
195  for (i=0; i<=ncols*d; i++) {
196  gridp->gpoints1[i] = cur; // small, dark gridline points
197  vm_vec_scale_add(&tvec, &cur, &dfvec, (float) nrows * d);
198  gridp->gpoints2[i] = tvec;
199  vm_vec_add2(&cur, &drvec);
200  }
201 
202  for (i=0; i<=ncols2; i++) {
203  gridp->gpoints5[i] = cur2; // large, brighter gridline points
204  vm_vec_scale_add(&tvec, &cur2, &dfvec, (float) nrows2 * 10);
205  gridp->gpoints6[i] = tvec;
206  vm_vec_scale_add2(&cur2, &drvec, 10.0f);
207  }
208 
209  cur = save;
210  cur2 = save2;
211  for (i=0; i<=nrows*d; i++) {
212  gridp->gpoints3[i] = cur; // small, dark gridline points
213  vm_vec_scale_add(&tvec, &cur, &drvec, (float) ncols * d);
214  gridp->gpoints4[i] = tvec;
215  vm_vec_add2(&cur, &dfvec);
216  }
217 
218  for (i=0; i<=nrows2; i++) {
219  gridp->gpoints7[i] = cur2; // large, brighter gridline points
220  vm_vec_scale_add(&tvec, &cur2, &drvec, (float) ncols2 * 10);
221  gridp->gpoints8[i] = tvec;
222  vm_vec_scale_add2(&cur2, &dfvec, 10.0f);
223  }
224 
225  return gridp;
226 }
227 
228 // Create a nice grid -- centered at origin, 10x10, 10.0 size squares, in xz plane.
230 {
231  grid *rgrid;
232  vec3d fvec, rvec, cvec;
233 
234  vm_vec_make(&fvec, 0.0f, 0.0f, 1.0f);
235  vm_vec_make(&rvec, 1.0f, 0.0f, 0.0f);
236  vm_vec_make(&cvec, 0.0f, 0.0f, 0.0f);
237 
238  rgrid = create_grid(&Global_grid, &fvec, &rvec, &cvec, 100, 100, 5.0f);
239 
240  physics_init(&rgrid->physics);
241  return rgrid;
242 }
243 
244 // Rotate and project points and draw a line.
246 {
247  vertex tv0, tv1;
248 
249  g3_rotate_vertex(&tv0, v0);
250  g3_rotate_vertex(&tv1, v1);
251  g3_draw_line(&tv0, &tv1);
252 }
253 
254 
255 void modify_grid(grid *gridp)
256 {
257  create_grid(gridp, &gridp->gmatrix.vec.fvec, &gridp->gmatrix.vec.rvec, &gridp->center,
258  gridp->nrows, gridp->ncols, gridp->square_size);
259 }
260 
262 {
263  vec3d gpos; // Location of point on grid.
264  vec3d tpos;
265  float dxz;
266  plane tplane;
267  vec3d *gv;
268 
269  tplane.A = gridp->gmatrix.vec.uvec.xyz.x;
270  tplane.B = gridp->gmatrix.vec.uvec.xyz.y;
271  tplane.C = gridp->gmatrix.vec.uvec.xyz.z;
272  tplane.D = gridp->planeD;
273 
274  compute_point_on_plane(&gpos, &tplane, pos);
275 
276  dxz = vm_vec_dist(pos, &gpos)/8.0f;
277 
278  gv = &gridp->gmatrix.vec.uvec;
279  if (gv->xyz.x * pos->xyz.x + gv->xyz.y * pos->xyz.y + gv->xyz.z * pos->xyz.z < -gridp->planeD)
280  gr_set_color(127, 127, 127);
281  else
282  gr_set_color(255, 255, 255); // white
283 
284  rpd_line(&gpos, pos); // Line from grid to object center.
285 
286  tpos = gpos;
287 
288  vm_vec_scale_add2(&gpos, &gridp->gmatrix.vec.rvec, -dxz/2);
289  vm_vec_scale_add2(&gpos, &gridp->gmatrix.vec.fvec, -dxz/2);
290 
291  vm_vec_scale_add2(&tpos, &gridp->gmatrix.vec.rvec, dxz/2);
292  vm_vec_scale_add2(&tpos, &gridp->gmatrix.vec.fvec, dxz/2);
293 
294  rpd_line(&gpos, &tpos);
295 
296  vm_vec_scale_add2(&gpos, &gridp->gmatrix.vec.rvec, dxz);
297  vm_vec_scale_add2(&tpos, &gridp->gmatrix.vec.rvec, -dxz);
298 
299  rpd_line(&gpos, &tpos);
300 }
#define KEY_PAD9
Definition: key.h:165
int i
Definition: multi_pxo.cpp:466
#define _isnan(f)
Definition: config.h:280
float square_size
Definition: missiongrid.h:26
grid * create_grid(grid *gridp, vec3d *forward, vec3d *right, vec3d *center, int nrows, int ncols, float square_size)
void modify_grid(grid *gridp)
void vm_vec_scale_add(vec3d *dest, const vec3d *src1, const vec3d *src2, float k)
Definition: vecmat.cpp:266
float key_down_timef(uint scancode)
Definition: key.cpp:568
void grid_render_elevation_line(vec3d *pos, grid *gridp)
#define KEY_PAD8
Definition: key.h:164
#define KEY_Z
Definition: key.h:107
grid * The_grid
Definition: missiongrid.cpp:20
Assert(pm!=NULL)
Definition: pstypes.h:88
#define KEY_PAD1
Definition: key.h:157
void maybe_create_new_grid(grid *gridp, vec3d *pos, matrix *orient, int force)
Definition: missiongrid.cpp:66
#define KEY_PAD4
Definition: key.h:160
struct vec3d::@225::@227 xyz
GLclampf f
Definition: Glext.h:7097
void vm_vec_scale_add2(vec3d *dest, const vec3d *src, float k)
Definition: vecmat.cpp:284
float A
Definition: vecmat.h:73
int g3_draw_line(vertex *p0, vertex *p1)
Definition: 3ddraw.cpp:112
hull_check orient
Definition: lua.cpp:5049
grid * create_default_grid(void)
#define IS_VEC_NULL(v)
Definition: vecmat.h:28
int nrows
Definition: missiongrid.h:22
float B
Definition: vecmat.h:73
int ncols
Definition: missiongrid.h:22
#define KEY_PAD7
Definition: key.h:163
void gr_set_color(int r, int g, int b)
Definition: 2d.cpp:1188
#define MAX_GRIDLINE_POINTS
Definition: missiongrid.h:18
#define KEY_A
Definition: key.h:82
typedef int(SCP_EXT_CALLCONV *SCPDLL_PFVERSION)(SCPDLL_Version *)
float D
Definition: vecmat.h:73
void vm_vec_add2(vec3d *dest, const vec3d *src)
Definition: vecmat.cpp:178
void grid_read_camera_controls(control_info *ci, float frametime)
Definition: missiongrid.cpp:23
float pitch
Definition: physics.h:100
struct matrix::@228::@230 vec
void vm_vec_scale(vec3d *dest, float s)
Definition: vecmat.cpp:248
matrix gmatrix
Definition: missiongrid.h:24
#define KEY_PAD3
Definition: key.h:159
vec3d center
Definition: missiongrid.h:23
GLfloat v0
Definition: Glext.h:5638
vec3d gpoints4[MAX_GRIDLINE_POINTS]
Definition: missiongrid.h:31
vec3d gpoints7[MAX_GRIDLINE_POINTS]
Definition: missiongrid.h:34
float forward
Definition: physics.h:105
ubyte g3_rotate_vertex(vertex *dest, const vec3d *src)
Definition: 3dmath.cpp:97
void physics_init(physics_info *pi)
Definition: physics.cpp:49
#define fl_abs(fl)
Definition: floating.h:31
vec3d gpoints5[MAX_GRIDLINE_POINTS]
Definition: missiongrid.h:32
float vm_vec_dist(const vec3d *v0, const vec3d *v1)
Definition: vecmat.cpp:355
void rpd_line(vec3d *v0, vec3d *v1)
float vm_dist_to_plane(const vec3d *checkp, const vec3d *norm, const vec3d *planep)
Definition: vecmat.cpp:1128
float C
Definition: vecmat.h:73
float fl_roundoff(float x, int multiple)
Rounds off a floating point number to a multiple of some number.
Definition: floating.cpp:21
vec3d gpoints6[MAX_GRIDLINE_POINTS]
Definition: missiongrid.h:33
#define KEY_PADMINUS
Definition: key.h:166
#define vm_vec_make(v, _x, _y, _z)
Definition: vecmat.h:48
vec3d gpoints2[MAX_GRIDLINE_POINTS]
Definition: missiongrid.h:29
GLfloat GLfloat v1
Definition: Glext.h:5639
float bank
Definition: physics.h:104
#define vm_malloc(size)
Definition: pstypes.h:547
vec3d gpoints1[MAX_GRIDLINE_POINTS]
Definition: missiongrid.h:28
GLdouble GLdouble right
Definition: Glext.h:10330
void vm_vec_sub(vec3d *dest, const vec3d *src0, const vec3d *src1)
Definition: vecmat.cpp:168
grid Global_grid
Definition: missiongrid.cpp:19
float heading
Definition: physics.h:102
int double_fine_gridlines
Definition: missiongrid.cpp:21
float planeD
Definition: missiongrid.h:27
float vm_vec_copy_normalize(vec3d *dest, const vec3d *src)
Definition: vecmat.cpp:427
GLfloat GLfloat p
Definition: Glext.h:8373
float vertical
Definition: physics.h:101
hull_check pos
Definition: lua.cpp:5050
float sideways
Definition: physics.h:103
int temp
Definition: lua.cpp:4996
#define KEY_PAD6
Definition: key.h:162
vec3d gpoints3[MAX_GRIDLINE_POINTS]
Definition: missiongrid.h:30
float fvi_ray_plane(vec3d *new_pnt, const vec3d *plane_pnt, const vec3d *plane_norm, const vec3d *ray_origin, const vec3d *ray_direction, float rad)
Definition: fvi.cpp:118
void compute_point_on_plane(vec3d *q, const plane *planep, const vec3d *p)
Definition: vecmat.cpp:1363
vec3d * vm_vec_cross(vec3d *dest, const vec3d *src0, const vec3d *src1)
Definition: vecmat.cpp:645
vec3d gpoints8[MAX_GRIDLINE_POINTS]
Definition: missiongrid.h:35
Definition: vecmat.h:72
physics_info physics
Definition: missiongrid.h:25
#define KEY_PAD2
Definition: key.h:158
#define KEY_PADPLUS
Definition: key.h:167
const GLubyte * c
Definition: Glext.h:8376
float vm_vec_normalize(vec3d *v)
Definition: vecmat.cpp:460