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
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 "gfxlib.h"
22 #include "gfx/vec.h"
23 #include <stdio.h>
24 //typedef float GLdouble;
25 #include <math.h>
26 #include <string.h>
27 #include <assert.h>
28 #ifdef WIN32
29 #ifndef NOMINMAX
30 #define NOMINMAX
31 #endif //tells VCC not to generate min/max macros
32 #include <windows.h>
33 #ifndef M_PI
34 # define M_PI 3.14159265358979323846 /* pi */
35 #endif
36 #endif
37 #include "gl_matrix.h"
38 
39 #include "vs_globals.h"
40 
41 //#include <GL/glu.h>
42 
43 inline void Zero( float matrix[] )
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 }
65 
66 inline void Identity( float matrix[] )
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 }
88 
89 float Magnitude( Vector v )
90 {
91  return sqrtf( v.i*v.i+v.j*v.j+v.k*v.k );
92 }
93 
94 /*
95  * Vector operator*(float left, const Vector &right)
96  * {
97  * return Vector(right.i*left, right.j*left, right.k*left);
98  * }*/
99 
101 {
102  return a.Dot( b );
103 }
104 
105 inline void MultMatrix( float dest[], const float m1[], const float m2[] )
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 }
133 
134 inline void CopyMatrix( Matrix dest, const Matrix source )
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 }
158 
159 using namespace GFXMatrices; //causes problems with g_game
160 
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 }
182 
184 
185 void getInverseProjection( float* &inv )
186 {
187  inv = invprojection;
188 }
189 
200 float GFXGetZPerspective( const float z )
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 }
217 
219 {
220  return /*invprojection[11]* */ invprojection[0]; //invprojection[15];//should be?? c/d == invproj[15]
221 }
222 
224 {
225  return /*invprojection[11]* */ invprojection[5]; //invprojection[15];//should be?? c/d == invproj[15]
226 }
227 
228 void /*GFXDRVAPI*/ GFXTranslate( const MATRIXMODE mode, const Vector &a )
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 }
263 
264 void /*GFXDRVAPI*/ GFXMultMatrix( const MATRIXMODE mode, const Matrix matrix )
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 }
302 
303 void /*GFXDRVAPI*/ GFXLoadMatrix( const MATRIXMODE mode, const Matrix matrix )
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 }
347 
348 void GFXViewPort( int minx, int miny, int maxx, int maxy )
349 {
350  glViewport( minx, miny, maxx, maxy );
351 }
352 
353 void GFXHudMode( const bool Enter )
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 }
369 
370 void /*GFXDRVAPI*/ GFXLoadIdentity( const MATRIXMODE mode )
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 }
399 
400 void /*GFXDRVAPI*/ GFXGetMatrix( const MATRIXMODE mode, Matrix matrix )
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 }
421 
422 static void gl_Frustum( float left, float right, float bottom, float top, float nearval, float farval )
423 {
424  GFXGetFrustumVars( false, &left, &right, &bottom, &top, &nearval, &farval );
425  GFXFrustum( projection, invprojection, left, right, bottom, top, nearval, farval );
426 }
427 
428 void GFXGetFrustumVars( bool retr, float *l, float *r, float *b, float *t, float *n, float *f )
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 }
448 
449 void GFXFrustum( float *m, float *i, float left, float right, float bottom, float top, float nearval, float farval )
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 }
501 
502 void /*GFXDRVAPI*/ GFXPerspective( float fov, float aspect, float znear, float zfar, float cockpit_offset )
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 }
515 
516 void /*GFXDRVAPI*/ GFXParallel( float left, float right, float bottom, float top, float nearval, float farval )
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 }
546 
547 static void LookAtHelper( float eyex,
548  float eyey,
549  float eyez,
550  float centerx,
551  float centery,
552  float centerz,
553  float upx,
554  float upy,
555  float upz )
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 }
685 
686 void /*GFXDRVAPI*/ GFXLookAt( Vector eye, Vector center, Vector up )
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 }
699 
700 float frust[6][4];
701 
702 float /*GFXDRVAPI*/ GFXSphereInFrustum( const Vector &Cnt, float radius )
703 {
704  return GFXSphereInFrustum( frust, Cnt, radius );
705 }
706 
707 float /*GFXDRVAPI*/ GFXSphereInFrustum( float f[6][4], const Vector &Cnt, float radius )
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 }
719 
720 void /*GFXDRVAPI*/ GFXGetFrustum( float f[6][4] )
721 {
722  f = frust;
723 }
724 
725 void /*GFXDRVAPI*/ GFXCalculateFrustum()
726 {
728 }
729 
730 void /*GFXDRVAPI*/ GFXCalculateFrustum( float frustum[6][4], float *modl, float *proj )
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 }
842