Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
camera.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2001-2002 Daniel Horn & Alan Shieh
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 #include "camera.h"
22 #include "cmd/unit_generic.h"
23 #include "matrix.h"
24 
25 //Remove GL specific stuff here
26 
27 #include "vs_globals.h"
28 #include "audiolib.h"
29 #include "lin_time.h"
30 
31 #include <assert.h> //needed for assert() calls
32 //#include "planetary_transform.h" commented out by chuck_starchaser; --never used
33 
34 //const float PI=3.1415926536;
35 
36 Camera::Camera( ProjectionType proj ) : projectionType( proj )
37  , myPhysics( 0.1, 0.075, &Coord, &P, &Q, &R )
38 {
39  ResetVectors( P, Q, R );
40  R = -R;
41  Coord.i = 0;
42  Coord.j = 0;
43  Coord.k = -1;
44  velocity = angular_velocity = Vector( 0, 0, 0 );
45  lastpos.Set( 0, 0, 0 );
46  cockpit_offset = 0;
48  changed = GFXTRUE;
49  //SetPosition();
50  //SetOrientation();
51  Yaw( PI );
52  x = y = 0;
53  xsize = ysize = 1.0;
54  zoom = 1.0;
55  fov = g_game.fov;
56 
57  lastGFXUpdate.clip = GFXTRUE;
58  lastGFXUpdate.updateFrustum = GFXTRUE;
59  lastGFXUpdate.centerCamera = GFXFALSE;
60  lastGFXUpdate.overrideZFrustum = GFXFALSE;
61  lastGFXUpdate.overrideZNear = 0;
62  lastGFXUpdate.overrideZFar = 1000000;
63 }
64 
65 void Camera::GetPQR( Vector &p1, Vector &q1, Vector &r1 )
66 {
67  p1.i = P.i;
68  p1.j = P.j;
69  p1.k = P.k;
70  q1.i = Q.i;
71  q1.j = Q.j;
72  q1.k = Q.k;
73  r1.i = R.i;
74  r1.j = R.j;
75  r1.k = R.k;
76 }
77 
79  GFXBOOL updateFrustum,
80  GFXBOOL centerCamera,
81  GFXBOOL overrideZFrustum,
82  float overrideZNear,
83  float overrideZFar )
84 {
85  lastGFXUpdate.clip = clip;
86  lastGFXUpdate.updateFrustum = updateFrustum;
87  lastGFXUpdate.centerCamera = centerCamera;
88  lastGFXUpdate.overrideZFrustum = overrideZFrustum;
89  lastGFXUpdate.overrideZNear = overrideZNear;
90  lastGFXUpdate.overrideZFar = overrideZFar;
91 
92  const float ZFARCONST = 1000000;
93  float xmin, xmax, ymin, ymax, znear, zfar;
94  if (1 || changed) {
95  myPhysics.Update();
97  //FIXMEGFXLoadIdentity(VIEW);
98  switch (projectionType)
99  {
100  case Camera::PERSPECTIVE:
101  znear = (overrideZFrustum ? overrideZNear : g_game.znear);
102  zfar = ( overrideZFrustum ? overrideZFar : g_game.zfar*(clip ? 1 : ZFARCONST) );
103 
104  GFXPerspective( zoom*fov, g_game.aspect, znear, zfar, cockpit_offset ); //set perspective to 78 degree FOV
105  break;
106  case Camera::PARALLEL:
107  ymax = g_game.znear*tanf( zoom*fov*PI/( (float) 360.0 ) );
108 
109  ymin = -ymax; //-4.7046
110 
111  xmin = ymin*g_game.aspect; //-6.2571
112  xmax = ymax*g_game.aspect; //6.2571
113 
114  znear = ( overrideZFrustum ? overrideZNear : -g_game.zfar*(clip ? 1 : ZFARCONST) );
115  zfar = ( overrideZFrustum ? overrideZFar : g_game.zfar*(clip ? 1 : ZFARCONST) );
116 
117  //GFXParallel(xmin,xmax,ymin,ymax,-znear,zfar);
118  GFXParallel( g_game.aspect* -zoom, g_game.aspect*zoom, -zoom, zoom, znear, zfar );
119  break;
120  }
121  GFXLookAt( -R, centerCamera ? QVector( 0, 0, 0 ) : Coord, Q );
122  if (updateFrustum) GFXCalculateFrustum();
123 #ifdef PERFRAMESOUND
124  Vector lastpos( view[12], view[13], view[14] );
125  AUDListener( Coord, (Coord-lastpos)/GetElapsedTime() ); //this pos-last pos / elapsed time
126 #endif
127  //GFXGetMatrix(VIEW,view);
128  GFXSubwindow( x, y, xsize, ysize );
129 #ifdef PERFRAMESOUND
131 #endif
132  }
133 }
134 
136 {
137 #ifndef PERFRAMESOUND
138  AUDListener( Coord, GetVelocity() );
140 #endif
141 }
142 
143 #ifdef OLDUPDATEPLANEGFX
144 
161 #endif
162 
164 {
165  GFXGetMatrixView( vw );
166 }
167 
169 {
170  nebula.SetUnit( (Unit*) neb );
171 }
172 
173 Nebula * Camera::GetNebula() //this function can't be const, as it must return a non-const ptr
174 {
175  return reinterpret_cast<Nebula*>( nebula.GetUnit() ); //changed by chuck from (Nebula*) cast
176 }
177 
179 {
180  Identity( planetview );
181 }
182 
183 void Camera::RestoreViewPort( float xoffset, float yoffset )
184 {
185  GFXSubwindow( x+xoffset, y+yoffset, xsize, ysize );
186 }
187 
189 {
190 #define ITISDEPRECATED 0
191  assert( ITISDEPRECATED );
192 #undef ITISDEPRECATED
193 //static float rotfactor = 0;
194  //glMatrixMode(GL_PROJECTION);
195  float xmin, xmax, ymin, ymax;
196  if (changed) {
199  //updating the center should always use a perspective
200  switch (Camera::PERSPECTIVE)
201  {
202  case Camera::PERSPECTIVE:
203  GFXPerspective( zoom*fov, g_game.aspect, g_game.znear, g_game.zfar, cockpit_offset ); //set perspective to 78 degree FOV
204  break;
205  case Camera::PARALLEL:
206  ymax = g_game.znear*tanf( zoom*fov*PI/( (float) 360.0 ) ); //78.0 --> 4.7046
207 
208  ymin = -ymax; //-4.7046
209 
210  xmin = ymin*g_game.aspect; //-6.2571
211  xmax = ymax*g_game.aspect; //6.2571
212 
213  //GFXParallel(xmin,xmax,ymin,ymax,-znear,zfar);
214  GFXParallel( g_game.aspect* -zoom, g_game.aspect*zoom, -zoom, zoom, -g_game.znear, g_game.zfar );
215  break;
216  }
217  RestoreViewPort( 0, 0 );
218 
219  GFXLookAt( -R, QVector( 0, 0, 0 ), Q );
220  //changed = GFXFALSE;
221  }
222  //glMultMatrixf(view);
223 }
224 
225 void Camera::SetPosition( const QVector &origin, const Vector &vel, const Vector &angvel, const Vector &acceleration )
226 {
227  if ( FINITE( origin.i ) && FINITE( origin.j ) && FINITE( origin.k ) ) {
228  velocity = vel;
229  angular_velocity = angvel;
230  accel = acceleration;
231  Coord = origin;
232  changed = GFXTRUE;
233  } else {
234  VSFileSystem::vs_fprintf( stderr, "fatal error in camera" );
235  }
236 }
237 
242 {
243  return &planetview;
244  //CopyMatrix (x,view);
245 }
246 
247 void Camera::LookDirection( const Vector &myR, const Vector &up )
248 {
249  P = (myR);
250  P.Normalize();
251  Q = up;
252  Q.Normalize();
253  CrossProduct( P, Q, R );
254  changed = GFXTRUE;
255 }
256 
257 void Camera::SetOrientation( const Vector &p, const Vector &q, const Vector &r )
258 {
259  P = p;
260  Q = q;
261  R = r;
262  myPhysics.SetAngularVelocity( Vector( 0, 0, 0 ) );
263  changed = GFXTRUE;
264 }
265 
266 void Camera::SetSubwindow( float x, float y, float xsize, float ysize )
267 {
268  this->x = x;
269  this->y = y;
270  this->xsize = xsize;
271  this->ysize = ysize;
272  changed = GFXTRUE;
273 }
274 
276 {
277  projectionType = t;
278 }
279 
280 void Camera::SetZoom( float z )
281 {
282  zoom = z;
283 }
284 
285 float Camera::GetZoom() const
286 {
287  return zoom;
288 }
289 
290 void Camera::SetFov( float f )
291 {
292  fov = f;
293 }
294 
295 float Camera::GetFov() const
296 {
297  return fov;
298 }
299 
300 void Camera::Yaw( float rad )
301 {
302  ::Yaw( rad, P, Q, R );
303  changed = GFXTRUE;
304 }
305 
306 void Camera::Pitch( float rad )
307 {
308  ::Pitch( rad, P, Q, R );
309  changed = GFXTRUE;
310 }
311 
312 void Camera::Roll( float rad )
313 {
314  ::Roll( rad, P, Q, R );
315  changed = GFXTRUE;
316 }
317 
318 void Camera::XSlide( float factor )
319 {
320  Coord += (P*factor).Cast();
321  changed = GFXTRUE;
322 }
323 
324 void Camera::YSlide( float factor )
325 {
326  Coord += (Q*factor).Cast();
327  changed = GFXTRUE;
328 }
329 
330 void Camera::ZSlide( float factor )
331 {
332  Coord += (R*factor).Cast();
333  changed = GFXTRUE;
334 }
335