TAPs 0.7.7.3
TAPsOpenGLCameraModel.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsOpenGLCameraModel.hpp
00003 
00004 Camera Model for OpenGL
00005 
00006 SUKITTI PUNAK   (05/13/2006)
00007 UPDATE          (12/14/2010)
00008 ******************************************************************************/
00009 #include "TAPsOpenGLCameraModel.hpp"
00010 // Using Inclusion Model (i.e. definitions are included in declarations)
00011 //                       (this name.cpp is included in name.hpp)
00012 // Each friend is defined directly inside its declaration.
00013 
00014 BEGIN_NAMESPACE_TAPs__OpenGL
00015 //=============================================================================
00016 template <typename T> GLuint    OpenGLCameraModel<T>::g_uiGLDisplayList = 0;
00017 template <typename T> int       OpenGLCameraModel<T>::g_iTotalCameras = 0;
00018 //=============================================================================
00019 // Constructor(s) and Destructor
00020 //-----------------------------------------------------------------------------
00021 template <typename T>
00022 OpenGLCameraModel<T>::OpenGLCameraModel ( 
00023     Enum::CameraType    cameraType          // Orthographic or Perspective
00024 )
00025 {
00026     SetDefaultValues( cameraType );
00027     ++g_iTotalCameras;
00028 }
00029 //-----------------------------------------------------------------------------
00030 template <typename T>
00031 OpenGLCameraModel<T>::OpenGLCameraModel (
00032     Enum::CameraType    cameraType,         // Orthographic or Perspective
00033     T positionX, T positionY, T positionZ,          // position
00034     T centerX, T centerY, T centerZ,                // center
00035     T orientationX, T orientationY, T orientationZ, // orientation
00036     T tLeft,   T tRight,    // left & right projections
00037     T tBottom, T tTop,      // bottom & top projections
00038     T tNear,   T tFar       // near & far   projections
00039 )
00040 {
00041     //-------------------------------------------
00042     m_eCameraType = cameraType;
00043     //------------------------------------
00044     m_v3Position.SetXYZ( positionX, positionY, positionZ );
00045     m_v3Center.SetXYZ( centerX, centerY, centerZ );
00046     m_v3Orientation.SetXYZ( orientationX, orientationY, orientationZ );
00047     //------------------------------------
00048     m_tLeft   = tLeft;
00049     m_tRight  = tRight;
00050     m_tBottom = tBottom;
00051     m_tTop    = tTop;
00052     m_tNear   = tNear;
00053     m_tFar    = tFar;
00054     //------------------------------------
00055     SetProjectionMatrix();
00056     //-------------------------------------------
00057     ++g_iTotalCameras;
00058 }
00059 //-----------------------------------------------------------------------------
00060 template <typename T>
00061 OpenGLCameraModel<T>::OpenGLCameraModel (
00062     Enum::CameraType    cameraType,         // Orthographic or Perspective
00063     T positionX, T positionY, T positionZ,          // position
00064     T centerX, T centerY, T centerZ,                // center
00065     T orientationX, T orientationY, T orientationZ, // orientation
00066     T tFovy,    // angle of the field of view in the yz-plane [0,180]
00067     T tAspect,  // aspect ratio (width/height of the view port)
00068     T tNear,    // near clipping plane
00069     T tFar      // far  clipping plane
00070 )
00071 {
00072     //-------------------------------------------
00073     m_eCameraType = cameraType;
00074     //------------------------------------
00075     m_v3Position.SetXYZ( positionX, positionY, positionZ );
00076     m_v3Center.SetXYZ( centerX, centerY, centerZ );
00077     m_v3Orientation.SetXYZ( orientationX, orientationY, orientationZ );
00078     //------------------------------------
00079     // fovy, aspect, near, far ???
00080     //------------------------------------
00081     SetProjectionMatrix();
00082     //-------------------------------------------
00083     ++g_iTotalCameras;
00084 }
00085 //-----------------------------------------------------------------------------
00086 template <typename T>
00087 OpenGLCameraModel<T>::~OpenGLCameraModel ()
00088 {
00089     //Disable();
00090     --g_iTotalCameras;
00091 }
00092 //-----------------------------------------------------------------------------
00093 //=============================================================================
00094 
00095 //=============================================================================
00096 // Get/Set Function(s)
00097 //-----------------------------------------------------------------------------
00098 // Set <--> Camera starts shooting
00099 //-----------------------------------------------------------------------------
00100 template <typename T>
00101 void OpenGLCameraModel<T>::Set ()
00102 {
00103     //SetViewVolume();
00104     //---------------------------------------------------------------
00105     //if ( T type double
00106     GLfloat M[16];
00107 
00108     M[ 0] = static_cast<GLfloat>( m_M4x4ProjectionMatrix[ 0] );
00109     M[ 1] = static_cast<GLfloat>( m_M4x4ProjectionMatrix[ 4] );
00110     M[ 2] = static_cast<GLfloat>( m_M4x4ProjectionMatrix[ 8] );
00111     M[ 3] = static_cast<GLfloat>( m_M4x4ProjectionMatrix[12] );
00112     M[ 4] = static_cast<GLfloat>( m_M4x4ProjectionMatrix[ 1] );
00113     M[ 5] = static_cast<GLfloat>( m_M4x4ProjectionMatrix[ 5] );
00114     M[ 6] = static_cast<GLfloat>( m_M4x4ProjectionMatrix[ 9] );
00115     M[ 7] = static_cast<GLfloat>( m_M4x4ProjectionMatrix[13] );
00116     M[ 8] = static_cast<GLfloat>( m_M4x4ProjectionMatrix[ 2] );
00117     M[ 9] = static_cast<GLfloat>( m_M4x4ProjectionMatrix[ 6] );
00118     M[10] = static_cast<GLfloat>( m_M4x4ProjectionMatrix[10] );
00119     M[11] = static_cast<GLfloat>( m_M4x4ProjectionMatrix[14] );
00120     M[12] = static_cast<GLfloat>( m_M4x4ProjectionMatrix[ 3] );
00121     M[13] = static_cast<GLfloat>( m_M4x4ProjectionMatrix[ 7] );
00122     M[14] = static_cast<GLfloat>( m_M4x4ProjectionMatrix[11] );
00123     M[15] = static_cast<GLfloat>( m_M4x4ProjectionMatrix[15] );
00124 
00125     glPushAttrib( GL_TRANSFORM_BIT );
00126     //glMatrixMode( GL_MODELVIEW );
00127     glMatrixMode( GL_PROJECTION );
00128     glLoadIdentity();
00129     glMultMatrixf( M );
00130     glMatrixMode( GL_MODELVIEW );
00131     glTranslatef( -m_v3Position[0], -m_v3Position[1], -m_v3Position[2] );
00132     glPopAttrib();
00133 }
00134 //-----------------------------------------------------------------------------
00135 // Set View Volume (Camera's view range/volume)
00136 //-----------------------------------------------------------------------------
00137 template <typename T>
00138 void OpenGLCameraModel<T>::SetViewVolume ()
00139 {
00140     if ( m_eCameraType == Enum::PERSPECTIVE ) {
00141         SetPerspectiveViewVolume();
00142     }
00143     else {
00144         SetOrthographicViewVolume();
00145     }
00146 }
00147 //-----------------------------------------------------------------------------
00148 // Camera Type
00149 //-----------------------------------------------------------------------------
00150 template <typename T>
00151 void OpenGLCameraModel<T>::SetCameraType ( Enum::CameraType cameraType )
00152 {
00153     if ( cameraType == m_eCameraType )  return;
00154     //-----------------------------------------------------
00155     // Switch Camera Type
00156     m_eCameraType = cameraType;
00157     //-----------------------------------------------------
00158     //SetViewVolume();
00159 }
00160 //-----------------------------------------------------------------------------
00161 template <typename T>
00162 Enum::CameraType OpenGLCameraModel<T>::GetCameraType () const
00163 {
00164     return m_eCameraType;
00165 }
00166 //-----------------------------------------------------------------------------
00167 /*
00168 //-----------------------------------------------------------------------------
00169 // Enable/Disable <==> On/Off
00170 //-----------------------------------------------------------------------------
00171 template <typename T>
00172 void OpenGLCameraModel<T>::Enable ()
00173 {
00174     m_bOn = true;
00175     //-----------------------------------------------------
00176     glCamerafv( m_eGLCameraNumber, GL_AMBIENT, &m_v4Ambient.GetDataFloat() );
00177     glCamerafv( m_eGLCameraNumber, GL_DIFFUSE, &m_v4Diffuse.GetDataFloat() );
00178     glCamerafv( m_eGLCameraNumber, GL_SPECULAR, &m_v4Specular.GetDataFloat() );
00179     glCamerafv( m_eGLCameraNumber, GL_POSITION, &m_v4Position.GetDataFloat() );
00180     glCamerafv( m_eGLCameraNumber, GL_SPOT_DIRECTION, 
00181                &m_v3SpotDirection.GetDataFloat() );
00182     glCameraf( m_eGLCameraNumber, GL_SPOT_EXPONENT, m_tSpotExponent );
00183     glCameraf( m_eGLCameraNumber, GL_SPOT_CUTOFF, m_tSpotCutoff );
00184     glCameraf( m_eGLCameraNumber, GL_CONSTANT_ATTENUATION, 
00185               m_tConstantAttenuation );
00186     glCameraf( m_eGLCameraNumber, GL_LINEAR_ATTENUATION, 
00187               m_tLinearAttenuation );
00188     glCameraf( m_eGLCameraNumber, GL_QUADRATIC_ATTENUATION, 
00189               m_tQuadraticAttenuation );
00190     //-----------------------------------------------------
00191     glEnable( m_eGLCameraNumber );
00192 }
00193 //-----------------------------------------------------------------------------
00194 template <typename T>
00195 void OpenGLCameraModel<T>::Disable ()
00196 {
00197     m_bOn = false;
00198 }
00199 //-----------------------------------------------------------------------------
00200 template <typename T>
00201 void OpenGLCameraModel<T>::Switch ( bool on )
00202 {
00203     m_bOn = on;
00204     if ( m_bOn ) {
00205         glEnable( m_eGLCameraNumber );
00206     }
00207     else {
00208         glDisable( m_eGLCameraNumber );
00209     }
00210 }
00211 //-----------------------------------------------------------------------------
00212 template <typename T>
00213 void OpenGLCameraModel<T>::ToggleSwitch ()
00214 {
00215     m_bOn = !m_bOn;
00216     if ( m_bOn ) {
00217         glEnable( m_eGLCameraNumber );
00218     }
00219     else {
00220         glDisable( m_eGLCameraNumber );
00221     }
00222 }
00223 //-----------------------------------------------------------------------------
00224 template <typename T>
00225 bool OpenGLCameraModel<T>::IsOn () const
00226 {
00227     return m_bOn;
00228 }
00229 //-----------------------------------------------------------------------------
00230 template <typename T>
00231 bool OpenGLCameraModel<T>::IsEnabled () const
00232 {
00233     return m_bOn;
00234 }
00235 //*/
00236 //-----------------------------------------------------------------------------
00237 // Camera Position
00238 //-----------------------------------------------------------------------------
00239 template <typename T>
00240 void OpenGLCameraModel<T>::SetPosition ( const Vector3<T> & p )
00241 {
00242     m_v3Position = p;
00243     SetProjectionMatrix();
00244 }
00245 //-----------------------------------------------------------------------------
00246 template <typename T>
00247 void OpenGLCameraModel<T>::SetPosition ( T x, T y, T z )
00248 {
00249     m_v3Position.SetXYZ( x, y, z );
00250     SetProjectionMatrix();
00251 }
00252 //-----------------------------------------------------------------------------
00253 template <typename T>
00254 const Vector3<T> & OpenGLCameraModel<T>::GetPosition () const
00255 {
00256     return m_v3Position;
00257 }
00258 //-----------------------------------------------------------------------------
00259 // Camera Center
00260 //-----------------------------------------------------------------------------
00261 template <typename T>
00262 void OpenGLCameraModel<T>::SetCenter ( const Vector3<T> & p )
00263 {
00264     m_v3Center = p;
00265     SetProjectionMatrix();
00266 }
00267 //-----------------------------------------------------------------------------
00268 template <typename T>
00269 void OpenGLCameraModel<T>::SetCenter ( T x, T y, T z )
00270 {
00271     m_v3Center.SetXYZ( x, y, z );
00272     SetProjectionMatrix();
00273 }
00274 //-----------------------------------------------------------------------------
00275 template <typename T>
00276 const Vector3<T> & OpenGLCameraModel<T>::GetCenter () const
00277 {
00278     return m_v3Center;
00279 }
00280 //-----------------------------------------------------------------------------
00281 // Camera Orientation
00282 //-----------------------------------------------------------------------------
00283 template <typename T>
00284 void OpenGLCameraModel<T>::SetOrientation ( const Vector3<T> & v )
00285 {
00286     m_v3Orientation = v;
00287     SetProjectionMatrix();
00288 }
00289 //-----------------------------------------------------------------------------
00290 template <typename T>
00291 void OpenGLCameraModel<T>::SetOrientation ( T x, T y, T z )
00292 {
00293     m_v3Orientation.SetXYZ( x, y, z );
00294     SetProjectionMatrix();
00295 }
00296 //-----------------------------------------------------------------------------
00297 template <typename T>
00298 const Vector3<T> & OpenGLCameraModel<T>::GetOrientation () const
00299 {
00300     return m_v3Orientation;
00301 }
00302 //-----------------------------------------------------------------------------
00303 // View Volume for Either Orthographic or Perspective Camera
00304 //-----------------------------------------------------------------------------
00305 template <typename T>
00306 void OpenGLCameraModel<T>::SetViewVolume ( 
00307     T tLeft, T tRight, T tBottom, T tTop, T tNear, T tFar )
00308 {
00309     //-------------------------------------------
00310     m_tLeft   = tLeft;
00311     m_tRight  = tRight;
00312     m_tBottom = tBottom;
00313     m_tTop    = tTop;
00314     m_tNear   = tNear;
00315     m_tFar    = tFar;
00316     //-------------------------------------------
00317     //SetViewVolume();
00318 }
00319 //-----------------------------------------------------------------------------
00320 template <typename T>
00321 void OpenGLCameraModel<T>::SetViewVolumeLeftAndRight ( T tLeft, T tRight )
00322 {
00323     //-------------------------------------------
00324     m_tLeft   = tLeft;
00325     m_tRight  = tRight;
00326     //-------------------------------------------
00327     //SetViewVolume();
00328 }
00329 //-----------------------------------------------------------------------------
00330 template <typename T>
00331 void OpenGLCameraModel<T>::SetViewVolumeBottomAndTop ( T tBottom, T tTop )
00332 {
00333     //-------------------------------------------
00334     m_tBottom = tBottom;
00335     m_tTop    = tTop;
00336     //-------------------------------------------
00337     //SetViewVolume();
00338 }
00339 //-----------------------------------------------------------------------------
00340 template <typename T>
00341 void OpenGLCameraModel<T>::SetViewVolumeNearAndFar ( T tNear, T tFar )
00342 {
00343     //-------------------------------------------
00344     m_tViewVolumeNear   = tNear;
00345     m_tFar    = tFar;
00346     //-------------------------------------------
00347     //SetViewVolume();
00348 }
00349 //-----------------------------------------------------------------------------
00350 template <typename T>
00351 void OpenGLCameraModel<T>::GetViewVolume ( 
00352     T & tLeft, T & tRight, T & tBottom, T & tTop, T & tNear, T & tFar ) const
00353 {
00354     tLeft   = m_tViewVolumeLeft;
00355     tRight  = m_tRight;
00356     tBottom = m_tBottom;
00357     tTop    = m_tTop;
00358     tNear   = m_tNear;
00359     tFar    = m_tFar;
00360 }
00361 //-----------------------------------------------------------------------------
00362 template <typename T>
00363 void OpenGLCameraModel<T>::GetViewVolumeLeftAndRight ( 
00364     T & tLeft, T & tRight ) const
00365 {
00366     tLeft   = m_tLeft;
00367     tRight  = m_tRight;
00368 }
00369 //-----------------------------------------------------------------------------
00370 template <typename T>
00371 void OpenGLCameraModel<T>::GetViewVolumeBottomAndTop ( 
00372     T & tBottom, T & tTop ) const
00373 {
00374     tBottom = m_tBottom;
00375     tTop    = m_tTop;
00376 }
00377 //-----------------------------------------------------------------------------
00378 template <typename T>
00379 void OpenGLCameraModel<T>::GetViewVolumeNearAndFar ( T & tNear, T & tFar ) const
00380 {
00381     tNear   = m_tNear;
00382     tFar    = m_tFar;
00383 }
00384 //-----------------------------------------------------------------------------
00385 // View Volume for Perspective Camera Only
00386 //-----------------------------------------------------------------------------
00387 template <typename T>
00388 void OpenGLCameraModel<T>::SetViewVolumePerspective ( 
00389     T tFovy, T tAspect, T tNear, T tFar )
00390 {
00391     // ???
00392 }
00393 //-----------------------------------------------------------------------------
00394 template <typename T>
00395 void OpenGLCameraModel<T>::GetViewVolumePerspective ( 
00396     T & tFovy, T & tAspect, T & tNear, T & tFar ) const
00397 {
00398     // ???
00399 }
00400 //-----------------------------------------------------------------------------
00401 // Viewport
00402 //-----------------------------------------------------------------------------
00403 template <typename T>
00404 void OpenGLCameraModel<T>::SetViewPort ( const Vector4<int> & v )
00405 {
00406     m_v4iViewport.SetXYZW( v );
00407     glViewport( m_v4iViewport[0], m_v4iViewport[1], m_v4iViewport[2], m_v4iViewport[3] );
00408 }
00409 //-----------------------------------------------------------------------------
00410 template <typename T>
00411 void OpenGLCameraModel<T>::SetViewPort ( int x, int y, int width, int height )
00412 {
00413     m_v4iViewport.SetXYZW( x, y, width, height );
00414     glViewport( m_v4iViewport[0], m_v4iViewport[1], m_v4iViewport[2], m_v4iViewport[3] );
00415 }
00416 //-----------------------------------------------------------------------------
00417 template <typename T>
00418 void OpenGLCameraModel<T>::SetViewPortLowerLeftCorner ( int x, int y )
00419 {
00420     m_v4iViewport[0] = x;
00421     m_v4iViewport[1] = y;
00422     glViewport( m_v4iViewport[0], m_v4iViewport[1], m_v4iViewport[2], m_v4iViewport[3] );
00423 }
00424 //-----------------------------------------------------------------------------
00425 template <typename T>
00426 void OpenGLCameraModel<T>::SetViewPortWidthAndHeight ( int width, int height )
00427 {
00428     m_v4iViewport[2] = width;
00429     m_v4iViewport[3] = height;
00430     glViewport( m_v4iViewport[0], m_v4iViewport[1], m_v4iViewport[2], m_v4iViewport[3] );
00431 }
00432 //-----------------------------------------------------------------------------
00433 template <typename T>
00434 const Vector4<int> & OpenGLCameraModel<T>::GetViewPort () const
00435 {
00436     return m_v4iViewport;
00437 }
00438 //-----------------------------------------------------------------------------
00439 template <typename T>
00440 void OpenGLCameraModel<T>::GetViewPort ( 
00441     int & x, int & y, int & width, int & height ) const
00442 {
00443     m_v4iViewport.GetXYZW( x, y, width, height );
00444 }
00445 //-----------------------------------------------------------------------------
00446 template <typename T>
00447 void OpenGLCameraModel<T>::GetViewPortLowerLeftCorner ( 
00448     int & x, int & y ) const
00449 {
00450     x = m_v4iViewport[0];
00451     y = m_v4iViewport[1];
00452 }
00453 //-----------------------------------------------------------------------------
00454 template <typename T>
00455 void OpenGLCameraModel<T>::GetViewPortWidthAndHeight ( 
00456     int & width, int & height ) const
00457 {
00458     width  = m_v4iViewport[2];
00459     height = m_v4iViewport[3];
00460 }
00461 //-----------------------------------------------------------------------------
00462 //=============================================================================
00463 
00464 //=============================================================================
00465 // Static Function(s)
00466 //-----------------------------------------------------------------------------
00467 template <typename T>
00468 int OpenGLCameraModel<T>::GetNumberOfCameras ()
00469 {
00470     return g_iTotalCameras;
00471 }
00472 //-----------------------------------------------------------------------------
00473 //=============================================================================
00474 
00475 //=============================================================================
00476 // Private Function(s)
00477 //-----------------------------------------------------------------------------
00478 template <typename T>
00479 void OpenGLCameraModel<T>::SetDefaultValues ( Enum::CameraType cameraType )
00480 {
00481     m_eCameraType = cameraType;
00482     //-----------------------------------------------------
00483     if ( m_eCameraType == Enum::PERSPECTIVE ) {
00484         m_v3Position.SetXYZ( 0, 0, 25 );
00485         //-----------------------------
00486         // near and far must be positive (so near should be less than far, and it can be zero)
00487         m_tLeft   = -0.5;   m_tRight = 0.5;
00488         m_tBottom = -0.5;   m_tTop   = 0.5;
00489         m_tNear   =  1;     m_tFar   = 200;
00490         //T w = m_tRight - m_tLeft;
00491         //T h = m_tTop - m_tBottom;
00492         //T aspect = w / h;
00493         //-----------------------------
00494     }
00495     else { // for orthogonal view
00496         m_v3Position.SetXYZ( 0, 0, 25 );
00497         //-----------------------------
00498         // near and far can be negative if to include viewing volume behind the viewer
00499         m_tLeft   = -25;    m_tRight = 25;
00500         m_tBottom = -25;    m_tTop   = 25;
00501         m_tNear   =  0;     m_tFar   = 200;
00502         //-----------------------------
00503     }
00504     //-------------------------------------------
00505     m_v3Center.SetXYZ( 0, 0, 0 );
00506     //-------------------------------------------
00507     m_v3Orientation.SetXYZ( 0, 1, 0 );
00508     //-------------------------------------------
00509     m_v4iViewport.SetXYZW( 0, 0, 400, 400 );
00510     //-----------------------------------------------------
00511     SetProjectionMatrix();
00512 }
00513 //-----------------------------------------------------------------------------
00514 template <typename T>
00515 void OpenGLCameraModel<T>::SetProjectionMatrix ()
00516 {
00517     /*
00518     //-------------------------------------------
00519     // Projection Matrix
00520     //-------------------
00521     //         S[0]  S[1]  S[2]  0   // 0  4  8 12 
00522     // Proj =  U[0]  U[1]  U[2]  0   // 1  5  9 13
00523     //        -F[0] -F[1] -F[2]  0   // 2  6 10 14
00524     //          0     0     0    1   // 3  7 11 15
00525     // Where S = F x UP
00526     //       U = S x F
00527     //       F = (Center - Position) / ||Center - Position||
00528     //      UP = Orientation / ||Orientation||
00529     Vector3<T> F = (m_v3Center - m_v3Position).Normalized();
00530     Vector3<T> S = F ^ m_v3Orientation.Normalized();
00531     Vector3<T> U = S ^ F;
00532     //-------------------------------------------
00533     m_M4x4ProjectionMatrix[ 0] =  S[0];
00534     m_M4x4ProjectionMatrix[ 1] =  U[0];
00535     m_M4x4ProjectionMatrix[ 2] = -F[0];
00536     m_M4x4ProjectionMatrix[ 3] =  0;
00537     //---------------------------------
00538     m_M4x4ProjectionMatrix[ 4] =  S[1];
00539     m_M4x4ProjectionMatrix[ 5] =  U[1];
00540     m_M4x4ProjectionMatrix[ 6] = -F[1];
00541     m_M4x4ProjectionMatrix[ 7] =  0;
00542     //---------------------------------
00543     m_M4x4ProjectionMatrix[ 8] =  S[2];
00544     m_M4x4ProjectionMatrix[ 9] =  U[2];
00545     m_M4x4ProjectionMatrix[10] = -F[2];
00546     m_M4x4ProjectionMatrix[11] =  0;
00547     //---------------------------------
00548     m_M4x4ProjectionMatrix[12] =  0;
00549     m_M4x4ProjectionMatrix[13] =  0;
00550     m_M4x4ProjectionMatrix[14] =  0;
00551     m_M4x4ProjectionMatrix[15] =  1;
00552     //-------------------------------------------
00553     //*/
00554 
00555     //-----------------------------------------------------
00556     if ( m_eCameraType == Enum::PERSPECTIVE ) {
00557         //-----------------------------
00558         // From OpenGL Ref 2.1
00559         // | 2near/(right-left)         0               A       0 |
00560         // |        0           2near/(top-bottom)      B       0 |
00561         // |        0                   0               C       D |
00562         // |        0                   0               -1      0 |
00563         T RpL = m_tRight + m_tLeft;
00564         T RnL = m_tRight - m_tLeft;
00565         T TpB = m_tTop + m_tBottom;
00566         T TnB = m_tTop - m_tBottom;
00567         T FpN = m_tFar + m_tNear;
00568         T FnN = m_tFar - m_tNear;
00569 
00570         T A = RpL / RnL;
00571         T B = TpB / TnB;
00572         T C = -FpN / FnN;
00573         T n2 = 2*m_tNear;
00574         T D = -n2*m_tFar / FnN;
00575         T E = n2 / RnL;
00576         T F = n2 / TnB;
00577 
00578         m_M4x4ProjectionMatrix[ 0] = E;
00579         m_M4x4ProjectionMatrix[ 1] = 0;
00580         m_M4x4ProjectionMatrix[ 2] = A;
00581         m_M4x4ProjectionMatrix[ 3] = 0;
00582         m_M4x4ProjectionMatrix[ 4] = 0;
00583         m_M4x4ProjectionMatrix[ 5] = F;
00584         m_M4x4ProjectionMatrix[ 6] = B;
00585         m_M4x4ProjectionMatrix[ 7] = 0;
00586         m_M4x4ProjectionMatrix[ 8] = 0;
00587         m_M4x4ProjectionMatrix[ 9] = 0;
00588         m_M4x4ProjectionMatrix[10] = C;
00589         m_M4x4ProjectionMatrix[11] = D;
00590         m_M4x4ProjectionMatrix[12] = 0;
00591         m_M4x4ProjectionMatrix[13] = 0;
00592         m_M4x4ProjectionMatrix[14] = -1;
00593         m_M4x4ProjectionMatrix[15] = 0;
00594     }
00595     else { // for orthogonal view volume
00596         //-----------------------------
00597         // From OpenGL Ref 2.1
00598         // | 2/(right-left)         0               0       A |
00599         // |        0       2/(top-bottom)          0       B |
00600         // |        0               0       -2/(far-near)   C |
00601         // |        0               0               0       1 |
00602         T RpL = m_tRight + m_tLeft;
00603         T RnL = m_tRight - m_tLeft;
00604         T TpB = m_tTop + m_tBottom;
00605         T TnB = m_tTop - m_tBottom;
00606         T FpN = m_tFar + m_tNear;
00607         T FnN = m_tFar - m_tNear;
00608 
00609         T A = -RpL / RnL;
00610         T B = -TpB / TnB;
00611         T C = -FpN / FnN;
00612         T D =  2 / RnL;
00613         T E =  2 / TnB;
00614         T F = -2 / FnN;
00615 
00616         m_M4x4ProjectionMatrix[ 0] = D;
00617         m_M4x4ProjectionMatrix[ 1] = 0;
00618         m_M4x4ProjectionMatrix[ 2] = 0;
00619         m_M4x4ProjectionMatrix[ 3] = A;
00620         m_M4x4ProjectionMatrix[ 4] = 0;
00621         m_M4x4ProjectionMatrix[ 5] = E;
00622         m_M4x4ProjectionMatrix[ 6] = 0;
00623         m_M4x4ProjectionMatrix[ 7] = B;
00624         m_M4x4ProjectionMatrix[ 8] = 0;
00625         m_M4x4ProjectionMatrix[ 9] = 0;
00626         m_M4x4ProjectionMatrix[10] = F;
00627         m_M4x4ProjectionMatrix[11] = C;
00628         m_M4x4ProjectionMatrix[12] = 0;
00629         m_M4x4ProjectionMatrix[13] = 0;
00630         m_M4x4ProjectionMatrix[14] = 0;
00631         m_M4x4ProjectionMatrix[15] = 1;
00632     }
00633 
00634 }
00635 //-----------------------------------------------------------------------------
00636 template <typename T>
00637 void OpenGLCameraModel<T>::SetOrthographicViewVolume ()
00638 {
00639     glMatrixMode( GL_PROJECTION );
00640     T ratio = static_cast<T>( m_v4iViewport[2] ) / static_cast<T>( m_v4iViewport[3] );
00641     glLoadIdentity();
00642     glOrtho(    m_tViewVolumeLeft,      m_tRight, 
00643                 m_tLeft / ratio, m_tRight / ratio, 
00644 //              m_tBottom,  m_tTop, 
00645                 m_tNear,        m_tFar 
00646     );
00647 }
00648 //-----------------------------------------------------------------------------
00649 template <typename T>
00650 void OpenGLCameraModel<T>::SetPerspectiveViewVolume ()
00651 {
00652     glMatrixMode( GL_PROJECTION );
00653     glLoadIdentity();
00654 //  glFrustum(  m_tViewVolumeLeft,      m_tRight, 
00655 //              m_tBottom,  m_tTop, 
00656 //              m_tNear,        m_tFar 
00657     //gluPerspective( m_tFOVY, 
00658     //  // aspect ratio is width/height of viewport
00659     //  static_cast<T>( m_v4iViewport[2] ) / static_cast<T>( m_v4iViewport[3] ), 
00660     //  m_tNear, m_tFar
00661     //);
00662 }
00663 //-----------------------------------------------------------------------------
00664 //=============================================================================
00665 END_NAMESPACE_TAPs__OpenGL
00666 //-----------------------------------------------------------------------------
00667 //34567890123456789012345678901234567890123456789012345678901234567890123456789
00668 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines