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
gl_matrix_hack.cpp File Reference
#include "gfxlib.h"
#include "gfx/vec.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <assert.h>
#include "gl_matrix.h"
#include "vs_globals.h"

Go to the source code of this file.

Macros

#define M(row, col)   rotview[col*4+row]
 
#define M(row, col)   m[col*4+row]
 
#define M(row, col)   i[col*4+row]
 
#define M(row, col)   m[col*4+row]
 
#define M(row, col)   m[col*4+row]
 
#define M(row, col)   tm[col*4+row]
 

Functions

void Zero (float matrix[])
 
void Identity (float matrix[])
 
float Magnitude (Vector v)
 
float DotProduct (Vector &a, Vector &b)
 
void MultMatrix (float dest[], const float m1[], const float m2[])
 
void CopyMatrix (Matrix dest, const Matrix source)
 
void evaluateViews ()
 
void getInverseProjection (float *&inv)
 
float GFXGetZPerspective (const float z)
 Given the current projection matrix, how much will the model be divided by. More...
 
float GFXGetXInvPerspective ()
 Screen to eye. More...
 
float GFXGetYInvPerspective ()
 
void GFXTranslate (const MATRIXMODE mode, const Vector &a)
 
void GFXMultMatrix (const MATRIXMODE mode, const Matrix matrix)
 
void GFXLoadMatrix (const MATRIXMODE mode, const Matrix matrix)
 
void GFXViewPort (int minx, int miny, int maxx, int maxy)
 Sets the final translation to screen coordinates, Also adjusts range of clearing. More...
 
void GFXHudMode (const bool Enter)
 
void GFXLoadIdentity (const MATRIXMODE mode)
 Loads the identity matrix for the given mode. More...
 
void GFXGetMatrix (const MATRIXMODE mode, Matrix matrix)
 
static void gl_Frustum (float left, float right, float bottom, float top, float nearval, float farval)
 
void GFXGetFrustumVars (bool retr, float *l, float *r, float *b, float *t, float *n, float *f)
 Saves and restores last used left,right,bot,top,near,far vals (internal use) More...
 
void GFXFrustum (float *m, float *i, float left, float right, float bottom, float top, float nearval, float farval)
 Given matrices, calculates the matrix and inverse matrix of a projection matrix to go from screen to 3-space coordinates. More...
 
void GFXPerspective (float fov, float aspect, float znear, float zfar, float cockpit_offset)
 Sets the Projection matrix to have fov and aspect as follows (fov is field of view in radians, aspect is width/height znear and zfar are clip planes. More...
 
void GFXParallel (float left, float right, float bottom, float top, float nearval, float farval)
 Sets the Projection matrix to a parallel view with given paramters. More...
 
static void LookAtHelper (float eyex, float eyey, float eyez, float centerx, float centery, float centerz, float upx, float upy, float upz)
 
void GFXLookAt (Vector eye, Vector center, Vector up)
 
float GFXSphereInFrustum (const Vector &Cnt, float radius)
 
float GFXSphereInFrustum (float f[6][4], const Vector &Cnt, float radius)
 
void GFXGetFrustum (float f[6][4])
 
void GFXCalculateFrustum ()
 Calculates frustum matrix (internal use) More...
 
void GFXCalculateFrustum (float frustum[6][4], float *modl, float *proj)
 

Variables

float centerx
 
float centery
 
float centerz
 
Vector eye
 
Vector center
 
Vector up
 
float frust [6][4]
 

Macro Definition Documentation

#define M (   row,
  col 
)    rotview[col*4+row]
#define M (   row,
  col 
)    m[col*4+row]
#define M (   row,
  col 
)    i[col*4+row]
#define M (   row,
  col 
)    m[col*4+row]
#define M (   row,
  col 
)    m[col*4+row]
#define M (   row,
  col 
)    tm[col*4+row]

Function Documentation

void CopyMatrix ( Matrix  dest,
const Matrix  source 
)
inline

Definition at line 134 of file gl_matrix_hack.cpp.

135 {
136  dest[0] = source[0];
137  dest[1] = source[1];
138  dest[2] = source[2];
139  dest[3] = source[3];
140  dest[4] = source[4];
141  dest[5] = source[5];
142  dest[6] = source[6];
143  dest[7] = source[7];
144  dest[8] = source[8];
145  dest[9] = source[9];
146  dest[10] = source[10];
147  dest[11] = source[11];
148  dest[12] = source[12];
149  dest[13] = source[13];
150  dest[14] = source[14];
151  dest[15] = source[15];
152  //memcpy(dest, source, sizeof(Matrix));
153 
154  /* for(int matindex = 0; matindex<16; matindex++)
155  * dest[matindex] = source[matindex];
156  */
157 }
float DotProduct ( Vector a,
Vector b 
)

Definition at line 100 of file gl_matrix_hack.cpp.

Referenced by Camera::GetZDist(), ZoneMgr::isVisible(), linear_interpolate_uncapped(), and Quaternion::operator*().

101 {
102  return a.Dot( b );
103 }
void evaluateViews ( )

Definition at line 162 of file gl_matrix_hack.cpp.

References Identity(), GFXMatrices::rotview, and GFXMatrices::view.

Referenced by GFXLoadMatrix(), and GFXMultMatrix().

163 {
164  //Identity(transview);
165  Identity( rotview );
166 #define M( row, col ) rotview[col*4+row]
167  rotview[0] = view[0];
168  rotview[1] = view[1];
169  rotview[2] = view[2];
170  //transview[3]=view[3];
171  rotview[4] = view[4];
172  rotview[5] = view[5];
173  rotview[6] = view[6];
174  //transview[7]=view[7];
175  rotview[8] = view[8];
176  rotview[9] = view[9];
177  rotview[10] = view[10];
178  //transview[11]=view[11];
179  //VSFileSystem::Fprintf (stderr,"trans %f,%f,%f",transview[3],transview[7],transview[11]);
180 #undef M
181 }
void getInverseProjection ( float *&  inv)

Definition at line 185 of file gl_matrix_hack.cpp.

References GFXMatrices::invprojection.

186 {
187  inv = invprojection;
188 }
void GFXCalculateFrustum ( )

Calculates frustum matrix (internal use)

Definition at line 725 of file gl_matrix_hack.cpp.

References frust, GFXCalculateFrustum(), GFXMatrices::projection, and GFXMatrices::view.

Referenced by GFXBoxInFrustumModel(), GFXCalculateFrustum(), ClickList::requestIterator(), and Camera::UpdateGFX().

726 {
728 }
void GFXCalculateFrustum ( float  frustum[6][4],
float modl,
float proj 
)

Definition at line 730 of file gl_matrix_hack.cpp.

731 {
734  float clip[16];
735  float t;
736 
737  /* Get the current PROJECTION matrix from OpenGL */
738  //glGetFloatv( GL_PROJECTION_MATRIX, proj );
739 
740  /* Get the current MODELVIEW matrix from OpenGL */
741  //glGetFloatv( GL_MODELVIEW_MATRIX, modl );
742 
743  /* Combine the two matrices (multiply projection by modelview) */
744  clip[0] = modl[0]*proj[0]+modl[1]*proj[4]+modl[2]*proj[8]+modl[3]*proj[12];
745  clip[1] = modl[0]*proj[1]+modl[1]*proj[5]+modl[2]*proj[9]+modl[3]*proj[13];
746  clip[2] = modl[0]*proj[2]+modl[1]*proj[6]+modl[2]*proj[10]+modl[3]*proj[14];
747  clip[3] = modl[0]*proj[3]+modl[1]*proj[7]+modl[2]*proj[11]+modl[3]*proj[15];
748 
749  clip[4] = modl[4]*proj[0]+modl[5]*proj[4]+modl[6]*proj[8]+modl[7]*proj[12];
750  clip[5] = modl[4]*proj[1]+modl[5]*proj[5]+modl[6]*proj[9]+modl[7]*proj[13];
751  clip[6] = modl[4]*proj[2]+modl[5]*proj[6]+modl[6]*proj[10]+modl[7]*proj[14];
752  clip[7] = modl[4]*proj[3]+modl[5]*proj[7]+modl[6]*proj[11]+modl[7]*proj[15];
753 
754  clip[8] = modl[8]*proj[0]+modl[9]*proj[4]+modl[10]*proj[8]+modl[11]*proj[12];
755  clip[9] = modl[8]*proj[1]+modl[9]*proj[5]+modl[10]*proj[9]+modl[11]*proj[13];
756  clip[10] = modl[8]*proj[2]+modl[9]*proj[6]+modl[10]*proj[10]+modl[11]*proj[14];
757  clip[11] = modl[8]*proj[3]+modl[9]*proj[7]+modl[10]*proj[11]+modl[11]*proj[15];
758 
759  clip[12] = modl[12]*proj[0]+modl[13]*proj[4]+modl[14]*proj[8]+modl[15]*proj[12];
760  clip[13] = modl[12]*proj[1]+modl[13]*proj[5]+modl[14]*proj[9]+modl[15]*proj[13];
761  clip[14] = modl[12]*proj[2]+modl[13]*proj[6]+modl[14]*proj[10]+modl[15]*proj[14];
762  clip[15] = modl[12]*proj[3]+modl[13]*proj[7]+modl[14]*proj[11]+modl[15]*proj[15];
763 
764  /* Extract the numbers for the RIGHT plane */
765  frustum[0][0] = clip[3]-clip[0];
766  frustum[0][1] = clip[7]-clip[4];
767  frustum[0][2] = clip[11]-clip[8];
768  frustum[0][3] = clip[15]-clip[12];
769 
770  /* Normalize the result */
771  t = sqrtf( frustum[0][0]*frustum[0][0]+frustum[0][1]*frustum[0][1]+frustum[0][2]*frustum[0][2] );
772  frustum[0][0] /= t;
773  frustum[0][1] /= t;
774  frustum[0][2] /= t;
775  frustum[0][3] /= t;
776 
777  /* Extract the numbers for the LEFT plane */
778  frustum[1][0] = clip[3]+clip[0];
779  frustum[1][1] = clip[7]+clip[4];
780  frustum[1][2] = clip[11]+clip[8];
781  frustum[1][3] = clip[15]+clip[12];
782 
783  /* Normalize the result */
784  t = sqrtf( frustum[1][0]*frustum[1][0]+frustum[1][1]*frustum[1][1]+frustum[1][2]*frustum[1][2] );
785  frustum[1][0] /= t;
786  frustum[1][1] /= t;
787  frustum[1][2] /= t;
788  frustum[1][3] /= t;
789 
790  /* Extract the BOTTOM plane */
791  frustum[2][0] = clip[3]+clip[1];
792  frustum[2][1] = clip[7]+clip[5];
793  frustum[2][2] = clip[11]+clip[9];
794  frustum[2][3] = clip[15]+clip[13];
795 
796  /* Normalize the result */
797  t = sqrtf( frustum[2][0]*frustum[2][0]+frustum[2][1]*frustum[2][1]+frustum[2][2]*frustum[2][2] );
798  frustum[2][0] /= t;
799  frustum[2][1] /= t;
800  frustum[2][2] /= t;
801  frustum[2][3] /= t;
802 
803  /* Extract the TOP plane */
804  frustum[3][0] = clip[3]-clip[1];
805  frustum[3][1] = clip[7]-clip[5];
806  frustum[3][2] = clip[11]-clip[9];
807  frustum[3][3] = clip[15]-clip[13];
808 
809  /* Normalize the result */
810  t = sqrtf( frustum[3][0]*frustum[3][0]+frustum[3][1]*frustum[3][1]+frustum[3][2]*frustum[3][2] );
811  frustum[3][0] /= t;
812  frustum[3][1] /= t;
813  frustum[3][2] /= t;
814  frustum[3][3] /= t;
815 
816  /* Extract the FAR plane */
817  frustum[5][0] = clip[3]-clip[2];
818  frustum[5][1] = clip[7]-clip[6];
819  frustum[5][2] = clip[11]-clip[10];
820  frustum[5][3] = clip[15]-clip[14];
821 
822  /* Normalize the result */
823  t = sqrtf( frustum[5][0]*frustum[5][0]+frustum[5][1]*frustum[5][1]+frustum[5][2]*frustum[5][2] );
824  frustum[5][0] /= t;
825  frustum[5][1] /= t;
826  frustum[5][2] /= t;
827  frustum[5][3] /= t;
828 
829  /* Extract the NEAR plane */
830  frustum[4][0] = clip[3]+clip[2];
831  frustum[4][1] = clip[7]+clip[6];
832  frustum[4][2] = clip[11]+clip[10];
833  frustum[4][3] = clip[15]+clip[14];
834 
835  /* Normalize the result */
836  t = sqrtf( frustum[4][0]*frustum[4][0]+frustum[4][1]*frustum[4][1]+frustum[4][2]*frustum[4][2] );
837  frustum[4][0] /= t;
838  frustum[4][1] /= t;
839  frustum[4][2] /= t;
840  frustum[4][3] /= t;
841 }
void GFXFrustum ( float m,
float i,
float  left,
float  right,
float  bottom,
float  top,
float  nearval,
float  farval 
)

Given matrices, calculates the matrix and inverse matrix of a projection matrix to go from screen to 3-space coordinates.

Definition at line 449 of file gl_matrix_hack.cpp.

References a, b, c, d, float, M, x, and y.

Referenced by gl_Frustum(), and ClickList::requestIterator().

450 {
451  GLfloat x, y, a, b, c, d;
452  x = ( ( (float) 2.0 )*nearval )/(right-left);
453  y = ( ( (float) 2.0 )*nearval )/(top-bottom);
454  a = (right+left)/(right-left);
455  b = (top+bottom)/(top-bottom);
456  //If farval == 0, we'll build an infinite-farplane projection matrix.
457  if (farval == 0) {
458  c = -1.0;
459  d = -1.99*nearval; //-2*nearval, but using exactly -2 might create artifacts
460  } else {
461  c = -(farval+nearval)/(farval-nearval);
462  d = -( ( (float) 2.0 )*farval*nearval )/(farval-nearval);
463  }
464 #define M( row, col ) m[col*4+row]
465  M( 0, 0 ) = x;
466  M( 0, 1 ) = 0.0F;
467  M( 0, 2 ) = a;
468  M( 0, 3 ) = 0.0F;
469  M( 1, 0 ) = 0.0F;
470  M( 1, 1 ) = y;
471  M( 1, 2 ) = b;
472  M( 1, 3 ) = 0.0F;
473  M( 2, 0 ) = 0.0F;
474  M( 2, 1 ) = 0.0F;
475  M( 2, 2 ) = c;
476  M( 2, 3 ) = d;
477  M( 3, 0 ) = 0.0F;
478  M( 3, 1 ) = 0.0F;
479  M( 3, 2 ) = -1.0F;
480  M( 3, 3 ) = 0.0F;
481 #undef M
482 #define M( row, col ) i[col*4+row]
483  M( 0, 0 ) = 1./x;
484  M( 0, 1 ) = 0.0F;
485  M( 0, 2 ) = 0.0F;
486  M( 0, 3 ) = a/x;
487  M( 1, 0 ) = 0.0F;
488  M( 1, 1 ) = 1./y;
489  M( 1, 2 ) = 0.0F;
490  M( 1, 3 ) = b/y;
491  M( 2, 0 ) = 0.0F;
492  M( 2, 1 ) = 0.0F;
493  M( 2, 2 ) = 0.0F;
494  M( 2, 3 ) = -1.0F;
495  M( 3, 0 ) = 0.0F;
496  M( 3, 1 ) = 0.0F;
497  M( 3, 2 ) = 1.F/d;
498  M( 3, 3 ) = (float) c/d;
499 #undef M
500 }
void GFXGetFrustum ( float  f[6][4])

Definition at line 720 of file gl_matrix_hack.cpp.

References frust.

721 {
722  f = frust;
723 }
void GFXGetFrustumVars ( bool  retr,
float l,
float r,
float b,
float t,
float n,
float f 
)

Saves and restores last used left,right,bot,top,near,far vals (internal use)

Definition at line 428 of file gl_matrix_hack.cpp.

References b.

Referenced by GFXBeginPick(), GFXDeviceToEye(), GFXGetZPerspective(), GFXParallel(), gl_Frustum(), ClickList::requestIterator(), and CoordinateSelect::UpdateMouse().

429 {
430  static float nnear, ffar, left, right, bot, top; //Visual C++ reserves near and far
431  if (!retr) {
432  nnear = *n;
433  ffar = *f;
434  left = *l;
435  right = *r;
436  bot = *b;
437  top = *t;
438  } else {
439  *l = left;
440  *r = right;
441  *b = bot;
442  *t = top;
443  *n = nnear;
444  *f = ffar;
445  }
446  //VSFileSystem::Fprintf (stderr,"<FUN%f,%f,%f,%f,%f,%f>>",near,far,left,right,bot,top);
447 }
void GFXGetMatrix ( const MATRIXMODE  mode,
Matrix  matrix 
)

Definition at line 400 of file gl_matrix_hack.cpp.

References centerx, centery, centerz, CopyMatrix(), Identity(), MODEL, MultMatrix(), GFXMatrices::projection, PROJECTION, GFXMatrices::rotview, and VIEW.

401 {
402  Matrix translation;
403  switch (mode)
404  {
405  case MODEL:
406  assert( 0 );
407  break;
408  case VIEW:
409  Identity( translation );
410  translation[12] = -centerx;
411  translation[13] = -centery;
412  translation[14] = -centerz;
413  MultMatrix( matrix, rotview, translation );
414  //CopyMatrix(matrix, view);
415  break;
416  case PROJECTION:
417  CopyMatrix( matrix, projection );
418  break;
419  }
420 }
float GFXGetXInvPerspective ( )

Screen to eye.

Definition at line 218 of file gl_matrix_hack.cpp.

References GFXMatrices::invprojection.

Referenced by LocationSelect::Draw().

219 {
220  return /*invprojection[11]* */ invprojection[0]; //invprojection[15];//should be?? c/d == invproj[15]
221 }
float GFXGetYInvPerspective ( )

Definition at line 223 of file gl_matrix_hack.cpp.

References GFXMatrices::invprojection.

Referenced by LocationSelect::Draw().

224 {
225  return /*invprojection[11]* */ invprojection[5]; //invprojection[15];//should be?? c/d == invproj[15]
226 }
float GFXGetZPerspective ( const float  z)

Given the current projection matrix, how much will the model be divided by.

GFXGetXPerspective () returns the number that x/z is multiplied by to land a pixel on the screen. | xs 0 a 0 |[x] [xs + az] [1/xs 0 0 a/xs][x] [x/xs+ aw/xs] | 0 ys b 0 |[y] = [ys + bz] ^-1 [ 0 1/ys 0 b/ys][y] = [y/ys+ bw/ys] | 0 0 c d |[z] [cz + dw] [ 0 0 0 -1 ][z] [0 ] | 0 0 -1 0 |[w] [-z ] [ 0 0 1/d c/d ][w] [z/d + cw/d ] therefore return 1/(xs *d) and 1/(ys * d) I'm not good with matrix math...tell me if I should ret 1/xs+c/d instead for test cases I can think of, it doesn't matter—

Definition at line 200 of file gl_matrix_hack.cpp.

References a, and GFXGetFrustumVars().

Referenced by perspectiveFactor().

201 {
202  float left, right, bottom, top, nearval, farval;
203  GFXGetFrustumVars( true, &left, &right, &bottom, &top, &nearval, &farval );
204 
205  printf( "nearval: %f, left: %f, right: %f, z: %f\n", nearval, left, right, z );
206 
207  float xs = 2*nearval/(right-left);
208  float a = (right+left)/(right-left);
209 
210  //Compute homogeneus x,w for (1,0,z,0)
211  float hx = xs+z*a;
212  float hw = -z;
213 
214  //Translate into euclidean coordinates and return euclidean x
215  return hx/hw;
216 }
void GFXHudMode ( const bool  Enter)

Hud Mode saves the current matrices and sets projection and view matrices to identity. Caution: use of other matrix functions in HudMode could alter state.

Definition at line 353 of file gl_matrix_hack.cpp.

Referenced by bootstrap_draw(), WindowManager::draw(), GameCockpit::Draw(), BaseInterface::Room::BaseShip::Draw(), NavigationSystem::Draw(), EndGUIFrame(), and StartGUIFrame().

354 {
355  if (Enter) {
356  glMatrixMode( GL_MODELVIEW );
357  glPushMatrix();
358  glLoadIdentity();
359  glMatrixMode( GL_PROJECTION );
360  glPushMatrix();
361  glLoadIdentity();
362  } else {
363  glMatrixMode( GL_PROJECTION );
364  glPopMatrix();
365  glMatrixMode( GL_MODELVIEW );
366  glPopMatrix();
367  }
368 }
void GFXLoadIdentity ( const MATRIXMODE  mode)

Loads the identity matrix for the given mode.

Definition at line 370 of file gl_matrix_hack.cpp.

References Identity(), GFXMatrices::model, MODEL, GFXMatrices::projection, PROJECTION, GFXMatrices::rotview, GFXMatrices::view, and VIEW.

Referenced by bootstrap_draw(), CoordinateSelect::Draw(), LocationSelect::Draw(), GamePlanet::Draw(), Stars::Draw(), GameCockpit::Draw(), ParticleTrail::DrawAndUpdate(), Mesh::DrawNow(), GamePlanet::DrawTerrain(), GFXBeginScene(), GFXCenterCamera(), GFXUploadLightState(), Halo::ProcessDrawQueue(), Camera::UpdateGFX(), and Camera::UpdateGLCenter().

371 {
372  switch (mode)
373  {
374  case MODEL:
375  Identity( model );
376  glMatrixMode( GL_MODELVIEW );
377  //glLoadMatrixf(transview);
378  glPopMatrix();
379  glPushMatrix();
380  break;
381  case VIEW:
382  Identity( view );
383  Identity( rotview );
384  //Identity (transview);
385  glMatrixMode( GL_MODELVIEW );
386  glPopMatrix();
387  glLoadMatrixf( model );
388  glPushMatrix();
389  glMatrixMode( GL_PROJECTION );
390  glLoadMatrixf( projection );
391  break;
392  case PROJECTION:
393  Identity( projection );
394  glMatrixMode( GL_PROJECTION );
395  glLoadMatrixf( rotview );
396  break;
397  }
398 }
void GFXLoadMatrix ( const MATRIXMODE  mode,
const Matrix  matrix 
)

Definition at line 303 of file gl_matrix_hack.cpp.

References centerx, centery, centerz, CopyMatrix(), evaluateViews(), GFXMatrices::model, MODEL, MultMatrix(), GFXMatrices::projection, PROJECTION, GFXMatrices::rotview, GFXMatrices::view, and VIEW.

Referenced by GFXLookAt(), and GFXParallel().

304 {
305  Matrix t;
306  switch (mode)
307  {
308  case MODEL:
309  CopyMatrix( model, matrix );
310  model[12] -= centerx;
311  model[13] -= centery;
312  model[14] -= centerz;
313  glMatrixMode( GL_MODELVIEW );
314  //glPopMatrix();
315  //glPushMatrix();
316  glLoadMatrixf( model );
317  model[12] = matrix[12];
318  model[13] = matrix[13];
319  model[14] = matrix[14];
320 
321  break;
322  case VIEW:
323  CopyMatrix( view, matrix );
324  evaluateViews();
325  //MultMatrix(t, transview, model);
326  /* FIXME1233
327  * glMatrixMode(GL_MODELVIEW);
328  * glPopMatrix();
329  * glLoadIdentity();
330  * glTranslatef(-centerx,-centery,-centerz);
331  * glPushMatrix();
332  *
333  * glMultMatrixf(model);
334  */
335  glMatrixMode( GL_PROJECTION );
337  glLoadMatrixf( t );
338  break;
339  case PROJECTION:
340  CopyMatrix( projection, matrix );
341  glMatrixMode( GL_PROJECTION );
342  glLoadMatrixf( projection );
343  glMultMatrixf( rotview );
344  break;
345  }
346 }
void GFXLookAt ( Vector  eye,
Vector  center,
Vector  up 
)

Definition at line 686 of file gl_matrix_hack.cpp.

References centerx, centery, centerz, GFXLoadMatrix(), LookAtHelper(), GFXMatrices::view, and VIEW.

687 {
688  LookAtHelper( eye.i, eye.j, eye.k, center.i, center.j, center.k, up.i, up.j, up.k );
689 
690  //Identity(transview);
691  //transview[3]=center.i;
692  //transview[7]=center.j;
693  //transview[11]=center.k;
694  centerx = center.i;
695  centery = center.j;
696  centerz = center.k;
697  GFXLoadMatrix( VIEW, view );
698 }
void GFXMultMatrix ( const MATRIXMODE  mode,
const Matrix  matrix 
)

Definition at line 264 of file gl_matrix_hack.cpp.

References CopyMatrix(), evaluateViews(), GFXMatrices::model, MODEL, MultMatrix(), GFXMatrices::projection, PROJECTION, GFXMatrices::rotview, GFXMatrices::view, and VIEW.

265 {
266  Matrix t;
267  switch (mode)
268  {
269  case MODEL:
270  MultMatrix( t, model, matrix );
271  CopyMatrix( model, t );
272  glMatrixMode( GL_MODELVIEW );
273  //glPopMatrix();
274  //glPushMatrix();
275  glMultMatrixf( matrix );
276  break;
277  case VIEW:
278  MultMatrix( t, view, matrix );
279  CopyMatrix( view, t );
280  evaluateViews();
282  glMatrixMode( GL_PROJECTION );
283  glLoadMatrixf( t );
284  /*FIXME1233
285  * glMatrixMode(GL_MODELVIEW);
286  * glPopMatrix();
287  * glLoadIdentity();
288  * glTranslatef(-centerx,-centery,-centerz);
289  * glPushMatrix();
290  * glMultMatrixf(model);
291  */
292  break;
293  case PROJECTION:
294  MultMatrix( t, projection, matrix );
295  CopyMatrix( projection, t );
297  glMatrixMode( GL_PROJECTION );
298  glLoadMatrixf( t );
299  break;
300  }
301 }
void GFXParallel ( float  left,
float  right,
float  bottom,
float  top,
float  nearval,
float  farval 
)

Sets the Projection matrix to a parallel view with given paramters.

Definition at line 516 of file gl_matrix_hack.cpp.

References GFXGetFrustumVars(), GFXLoadMatrix(), M, GFXMatrices::projection, PROJECTION, x, y, and z.

Referenced by Camera::UpdateGFX(), and Camera::UpdateGLCenter().

517 {
518  float *m = projection, x, y, z, tx, ty, tz;
519  x = 2.0/(right-left);
520  y = 2.0/(top-bottom);
521  z = -2.0/(farval-nearval);
522  tx = -(right+left)/(right-left);
523  ty = -(top+bottom)/(top-bottom);
524  tz = -(farval+nearval)/(farval-nearval);
525 #define M( row, col ) m[col*4+row]
526  M( 0, 0 ) = x;
527  M( 0, 1 ) = 0.0F;
528  M( 0, 2 ) = 0.0F;
529  M( 0, 3 ) = tx;
530  M( 1, 0 ) = 0.0F;
531  M( 1, 1 ) = y;
532  M( 1, 2 ) = 0.0F;
533  M( 1, 3 ) = ty;
534  M( 2, 0 ) = 0.0F;
535  M( 2, 1 ) = 0.0F;
536  M( 2, 2 ) = z;
537  M( 2, 3 ) = tz;
538  M( 3, 0 ) = 0.0F;
539  M( 3, 1 ) = 0.0F;
540  M( 3, 2 ) = 0.0F;
541  M( 3, 3 ) = 1.0F;
542 #undef M
544  GFXGetFrustumVars( false, &left, &right, &bottom, &top, &nearval, &farval );
545 }
void GFXPerspective ( float  fov,
float  aspect,
float  znear,
float  zfar,
float  cockpit_offset 
)

Sets the Projection matrix to have fov and aspect as follows (fov is field of view in radians, aspect is width/height znear and zfar are clip planes.

Definition at line 502 of file gl_matrix_hack.cpp.

References gl_Frustum(), M_PI, and GFXMatrices::projection.

Referenced by Camera::UpdateGFX(), and Camera::UpdateGLCenter().

503 {
504  //gluPerspective (fov,aspect,znear,zfar);
505  float xmin, xmax, ymin, ymax;
506  ymax = znear*tanf( fov*M_PI/( (float) 360.0 ) ); //78.0 --> 4.7046
507  ymin = -ymax; //-4.7046
508  xmin = (ymin-cockpit_offset/2)*aspect; //-6.2571
509  xmax = (ymax+cockpit_offset/2)*aspect; //6.2571
510  ymin -= cockpit_offset;
511  gl_Frustum( xmin, xmax, ymin, ymax, znear, zfar );
512  glMatrixMode( GL_PROJECTION );
513  glLoadMatrixf( projection );
514 }
float GFXSphereInFrustum ( const Vector Cnt,
float  radius 
)

Definition at line 702 of file gl_matrix_hack.cpp.

References frust, and GFXSphereInFrustum().

703 {
704  return GFXSphereInFrustum( frust, Cnt, radius );
705 }
float GFXSphereInFrustum ( float  f[6][4],
const Vector Cnt,
float  radius 
)

Definition at line 707 of file gl_matrix_hack.cpp.

References d.

708 {
709  int p;
710  float d;
711  for (p = 0; p < 5; p++) {
712  //does not evaluate for yon
713  d = f[p][0]*Cnt.i+f[p][1]*Cnt.j+f[p][2]*Cnt.k+f[p][3];
714  if (d <= -radius)
715  return 0;
716  }
717  return d;
718 }
void GFXTranslate ( const MATRIXMODE  mode,
const Vector a 
)

Definition at line 228 of file gl_matrix_hack.cpp.

References GFXMatrices::model, MODEL, MultMatrix(), GFXMatrices::projection, PROJECTION, GFXMatrices::rotview, GFXMatrices::view, and VIEW.

229 {
230  switch (mode)
231  {
232  case MODEL:
233  model[12] += a.i*model[0]+a.j*model[4]+a.k*model[8];
234  model[13] += a.i*model[1]+a.j*model[5]+a.k*model[9];
235  model[14] += a.i*model[2]+a.j*model[6]+a.k*model[10];
236  glMatrixMode( GL_MODELVIEW );
237  glTranslatef( a.i, a.j, a.k );
238  break;
239  case VIEW:
240  view[12] += a.i*view[0]+a.j*view[4]+a.k*view[8];
241  view[13] += a.i*view[1]+a.j*view[5]+a.k*view[9];
242  view[14] += a.i*view[2]+a.j*view[6]+a.k*view[10];
243  glMatrixMode( GL_MODELVIEW );
244  glPopMatrix();
245  glLoadIdentity();
246  glTranslatef( -view[12], -view[13], -view[14] );
247  glPushMatrix();
248  glMultMatrixf( model );
249  break;
250  case PROJECTION:
251  projection[12] += a.i*projection[0]+a.j*projection[4]+a.k*projection[8];
252  projection[13] += a.i*projection[1]+a.j*projection[5]+a.k*projection[9];
253  projection[14] += a.i*projection[2]+a.j*projection[6]+a.k*projection[10];
254  {
255  Matrix t;
257  glMatrixMode( GL_PROJECTION );
258  glLoadMatrixf( t );
259  }
260  break;
261  }
262 }
void GFXViewPort ( int  minx,
int  miny,
int  maxx,
int  maxy 
)

Sets the final translation to screen coordinates, Also adjusts range of clearing.

Definition at line 348 of file gl_matrix_hack.cpp.

Referenced by GameCockpit::SetupViewPort().

349 {
350  glViewport( minx, miny, maxx, maxy );
351 }
static void gl_Frustum ( float  left,
float  right,
float  bottom,
float  top,
float  nearval,
float  farval 
)
static

Definition at line 422 of file gl_matrix_hack.cpp.

References GFXFrustum(), GFXGetFrustumVars(), GFXMatrices::invprojection, and GFXMatrices::projection.

Referenced by GFXPerspective().

423 {
424  GFXGetFrustumVars( false, &left, &right, &bottom, &top, &nearval, &farval );
425  GFXFrustum( projection, invprojection, left, right, bottom, top, nearval, farval );
426 }
void Identity ( float  matrix[])
inline

Definition at line 66 of file gl_matrix_hack.cpp.

67 {
68  matrix[0] = 1;
69  matrix[1] = 0;
70  matrix[2] = 0;
71  matrix[3] = 0;
72 
73  matrix[4] = 0;
74  matrix[5] = 1;
75  matrix[6] = 0;
76  matrix[7] = 0;
77 
78  matrix[8] = 0;
79  matrix[9] = 0;
80  matrix[10] = 1;
81  matrix[11] = 0;
82 
83  matrix[12] = 0;
84  matrix[13] = 0;
85  matrix[14] = 0;
86  matrix[15] = 1;
87 }
static void LookAtHelper ( float  eyex,
float  eyey,
float  eyez,
float  centerx,
float  centery,
float  centerz,
float  upx,
float  upy,
float  upz 
)
static

Definition at line 547 of file gl_matrix_hack.cpp.

References centerx, centery, centerz, GFXActiveTexture(), M, MultMatrix(), Vector, GFXMatrices::view, x, y, z, and ZeroMemory().

Referenced by GFXLookAt().

556 {
557  float m[16];
558  float x[3], y[3], z[3];
559  float mag;
560  /* Make rotation matrix */
561  /* Z vector */
562  z[0] = eyex-centerx;
563  z[1] = eyey-centery;
564  z[2] = eyez-centerz;
565  mag = sqrtf( z[0]*z[0]+z[1]*z[1]+z[2]*z[2] );
566  if (mag) {
567  /* mpichler, 19950515 */
568  z[0] /= mag;
569  z[1] /= mag;
570  z[2] /= mag;
571  }
572  /* Y vector */
573  y[0] = upx;
574  y[1] = upy;
575  y[2] = upz;
576  /* X vector = Y cross Z */
577  //x[0] = z[1]*y[2] - z[2]*y[1];
578  //x[1] = -z[0]*y[2] + z[2]*y[0];
579  //x[2] = z[0]*y[1] - z[1]*y[0];
580  x[0] = y[1]*z[2]-y[2]*z[1];
581  x[1] = -y[0]*z[2]+y[2]*z[0];
582  x[2] = y[0]*z[1]-y[1]*z[0];
583  /* Recompute Y = Z cross X */
584  //y[0] = x[1]*z[2] - x[2]*z[1];
585  //y[1] = -x[0]*z[2] + x[2]*z[0];
586  //y[2] = x[0]*z[1] - x[1]*z[0];
587  y[0] = z[1]*x[2]-z[2]*x[1];
588  y[1] = -z[0]*x[2]+z[2]*x[0];
589  y[2] = z[0]*x[1]-z[1]*x[0];
590  /* mpichler, 19950515 */
591  /* cross product gives area of parallelogram, which is < 1.0 for
592  * non-perpendicular unit-length vectors; so normalize x, y here
593  */
594  mag = sqrtf( x[0]*x[0]+x[1]*x[1]+x[2]*x[2] );
595  if (mag) {
596  x[0] /= mag;
597  x[1] /= mag;
598  x[2] /= mag;
599  }
600  mag = sqrtf( y[0]*y[0]+y[1]*y[1]+y[2]*y[2] );
601  if (mag) {
602  y[0] /= mag;
603  y[1] /= mag;
604  y[2] /= mag;
605  }
606 #define M( row, col ) m[col*4+row]
607  M( 0, 0 ) = x[0];
608  M( 0, 1 ) = x[1];
609  M( 0, 2 ) = x[2];
610  M( 0, 3 ) = 0.0;
611  M( 1, 0 ) = y[0];
612  M( 1, 1 ) = y[1];
613  M( 1, 2 ) = y[2];
614  M( 1, 3 ) = 0.0;
615  M( 2, 0 ) = z[0];
616  M( 2, 1 ) = z[1];
617  M( 2, 2 ) = z[2];
618  M( 2, 3 ) = 0.0;
619  M( 3, 0 ) = 0.0;
620  M( 3, 1 ) = 0.0;
621  M( 3, 2 ) = 0.0;
622  M( 3, 3 ) = 1.0;
623  float tm[16];
624 #ifdef WIN32
625  ZeroMemory( tm, sizeof (tm) );
626 #else
627  bzero( tm, sizeof (tm) );
628 #endif
629 #undef M
630 #define M( row, col ) tm[col*4+row]
631  M( 0, 0 ) = 1.0;
632  M( 0, 3 ) = -eyex;
633  M( 1, 1 ) = 1.0;
634  M( 1, 3 ) = -eyey;
635  M( 2, 2 ) = 1.0;
636  M( 2, 3 ) = -eyez;
637  M( 3, 3 ) = 1.0;
638 #undef M
639  MultMatrix( view, m, tm );
640 /***
641  * float dis = sqrtf(upx*upx+upy*upy);
642  * Identity (tm);
643  * if (eyez-centerz > 0) {
644  * upx = -upx;
645  * }
646  * #define M(row,col) tm[col*4+row]
647  * M(0,0) = upy/dis;
648  * M(0,1) = -upx/dis;
649  * M(1,1) = upy/dis;
650  * M(1,0) = upx/dis;
651  * M(2,2) = 1.0;
652  * M(3,3) = 1.0;
653  * #undef M
654  ***/ //old hack to twiddle the texture in the xy plane
655 #ifdef NV_CUBE_MAP
656  //FIXME--ADD CAMERA MATRICES
657  //the texture matrix must be used to rotate the texgen-computed
658  //reflection or normal vector texture coordinates to match the orinetation
659  //of the cube map. Teh rotation can be computed based on two vectors
660  //1) the direction vector from the cube map center to the eye position
661  //and 2 the cube map orientation in world coordinates.
662  //the axis is the cross product of these two vectors...teh angle is arcsin
663  //of the dot of these two vectors
664  GFXActiveTexture( 1 );
665  glMatrixMode( GL_TEXTURE );
666  glLoadIdentity();
667  //Vector (centerx,centery,centerz).Cross (Vector (1,0,0)); DID NOT TRANSFORM THE ORIENTATION VECTOR TO REVERSE CAMERASPACE
668  Vector axis( centerx, centery, centerz );
669  Vector cubemapincamspace( eyex, eyey, eyez );
670  cubemapincamspace.Normalize();
671  axis.Normalize();
672  //float theta = arcsinf (Vector (centerx,centery,centerz).Normalize().Dot (Vector (1,0,0))); DID NOT TRANSFORM THE ORIENTATION VECTOR TO REVERSE CAMERASPACE
673  float theta = asinf( axis.Dot( cubemapincamspace ) );
674  axis = axis.Cross( axis.Cross( cubemapincamspace ) );
675  glRotatef( theta, axis.i, axis.j, axis.k );
676  //ok do matrix math to rotate by theta on axis those ..
677  GFXActiveTexture( 0 );
678 #else
679  /* glTranslatef(.5f,.5f,.4994f);
680  * glMultMatrixf(tm);
681  * glTranslatef(-.5f,-.5f,-.4994f);
682  */
683 #endif
684 }
void MultMatrix ( float  dest[],
const float  m1[],
const float  m2[] 
)
inline

Definition at line 105 of file gl_matrix_hack.cpp.

106 {
107  dest[0] = m1[0]*m2[0]+m1[4]*m2[1]+m1[8]*m2[2]+m1[12]*m2[3];
108  dest[1] = m1[1]*m2[0]+m1[5]*m2[1]+m1[9]*m2[2]+m1[13]*m2[3];
109  dest[2] = m1[2]*m2[0]+m1[6]*m2[1]+m1[10]*m2[2]+m1[14]*m2[3];
110  dest[3] = m1[3]*m2[0]+m1[7]*m2[1]+m1[11]*m2[2]+m1[15]*m2[3];
111 
112  dest[4] = m1[0]*m2[4]+m1[4]*m2[5]+m1[8]*m2[6]+m1[12]*m2[7];
113  dest[5] = m1[1]*m2[4]+m1[5]*m2[5]+m1[9]*m2[6]+m1[13]*m2[7];
114  dest[6] = m1[2]*m2[4]+m1[6]*m2[5]+m1[10]*m2[6]+m1[14]*m2[7];
115  dest[7] = m1[3]*m2[4]+m1[7]*m2[5]+m1[11]*m2[6]+m1[15]*m2[7];
116 
117  dest[8] = m1[0]*m2[8]+m1[4]*m2[9]+m1[8]*m2[10]+m1[12]*m2[11];
118  dest[9] = m1[1]*m2[8]+m1[5]*m2[9]+m1[9]*m2[10]+m1[13]*m2[11];
119  dest[10] = m1[2]*m2[8]+m1[6]*m2[9]+m1[10]*m2[10]+m1[14]*m2[11];
120  dest[11] = m1[3]*m2[8]+m1[7]*m2[9]+m1[11]*m2[10]+m1[15]*m2[11];
121 
122  dest[12] = m1[0]*m2[12]+m1[4]*m2[13]+m1[8]*m2[14]+m1[12]*m2[15];
123  dest[13] = m1[1]*m2[12]+m1[5]*m2[13]+m1[9]*m2[14]+m1[13]*m2[15];
124  dest[14] = m1[2]*m2[12]+m1[6]*m2[13]+m1[10]*m2[14]+m1[14]*m2[15];
125  dest[15] = m1[3]*m2[12]+m1[7]*m2[13]+m1[11]*m2[14]+m1[15]*m2[15];
126  /* Zero(dest);
127  * for(int rowcount = 0; rowcount<4; rowcount++)
128  * for(int colcount = 0; colcount<4; colcount++)
129  * for(int mcount = 0; mcount <4; mcount ++)
130  * dest[colcount*4+rowcount] += m1[mcount*4+rowcount]*m2[colcount*4+mcount];
131  */
132 }
void Zero ( float  matrix[])
inline

Definition at line 43 of file gl_matrix_hack.cpp.

44 {
45  matrix[0] = 0;
46  matrix[1] = 0;
47  matrix[2] = 0;
48  matrix[3] = 0;
49 
50  matrix[4] = 0;
51  matrix[5] = 0;
52  matrix[6] = 0;
53  matrix[7] = 0;
54 
55  matrix[8] = 0;
56  matrix[9] = 0;
57  matrix[10] = 0;
58  matrix[11] = 0;
59 
60  matrix[12] = 0;
61  matrix[13] = 0;
62  matrix[14] = 0;
63  matrix[15] = 0;
64 }

Variable Documentation

float centerx

Definition at line 161 of file gl_matrix_hack.cpp.

Referenced by GFXGetMatrix(), GFXLoadMatrix(), GFXLookAt(), and LookAtHelper().

float centery

Definition at line 161 of file gl_matrix_hack.cpp.

Referenced by GFXGetMatrix(), GFXLoadMatrix(), GFXLookAt(), and LookAtHelper().

float centerz

Definition at line 161 of file gl_matrix_hack.cpp.

Referenced by GFXGetMatrix(), GFXLoadMatrix(), GFXLookAt(), and LookAtHelper().

Vector eye

Definition at line 183 of file gl_matrix_hack.cpp.

float frust[6][4]

Definition at line 700 of file gl_matrix_hack.cpp.