D:/Zythum/DinoKod/Render/Camera.cpp

00001 //---------------------------------------------------------------------------------------------
00002 //      This file is a part of "DinoKod".
00003 //      Copyright © 2003 Dino Productions. All Rights Reserved.
00004 //      
00005 //      File                    : Camera.cpp
00006 //      Author                  : Sebastien LEIX        sebastien.leix@wanadoo.fr
00007 //      Date                    : 09/09/2002
00008 //      Modification    :
00009 //
00010 //---------------------------------------------------------------------------------------------
00011 #include "Common/Assert.h"
00012 #include "Common/types.h"
00013 #include "Common/Console.h"
00014 #include "Common/Plane.h"
00015 
00016 #include "Render/Camera.h"
00017 
00018 #include <math.h>
00019 
00020 //--------------------------------------------------------------------------------------------------------------------------------
00021 KCamera::KCamera( float Fov, float Aspect, float Near, float Far )
00022 {
00023         SetParam( Fov, Aspect, Near, Far );
00024         
00025         m_Matrix.LoadIdentity();
00026         m_fYaw          = 0.0f;
00027         m_fPitch        = PI / 2.0f;
00028         m_Pos           = KVector( 0.0f, 0.0f, 0.0f );
00029 }
00030 
00031 //--------------------------------------------------------------------------------------------------------------------------------
00032 KCamera::~KCamera()
00033 {
00034 }
00035 
00036 //--------------------------------------------------------------------------------------------------------------------------------
00037 void KCamera::SetParam( float Fov, float Aspect, float Near, float Far )
00038 {
00039         m_Fov           = Fov;
00040         m_Aspect        = Aspect;
00041         m_Near          = Near;
00042         m_Far           = Far;
00043 }
00044 
00045 //--------------------------------------------------------------------------------------------------------------------------------
00046 void KCamera::Reset()
00047 {
00048         m_Matrix.LoadIdentity();
00049         m_fYaw          = 0.0f;
00050         m_fPitch        = PI / 2.0f;
00051         m_Pos           = KVector( 0.0f, 0.0f, 0.0f );
00052 
00053         ComputeMatrix();
00054 //      Translate( 0.0f, 0.0f, 0.0f );
00055 }
00056 
00057 //--------------------------------------------------------------------------------------------------------------------------------
00058 KMatrix KCamera::GetMatrix()
00059 {
00060 /*      KMatrix         Matrix;
00061         KMatrix         Mat;
00062         float           cosine;
00063         float           sine;
00064 
00065         Matrix.LoadIdentity();
00066         
00067         // Pos
00068         Matrix._41      = m_Pos.x;
00069         Matrix._42      = m_Pos.y;
00070         Matrix._43      = m_Pos.z;
00071 
00072         // Yaw
00073         cosine  = (float)cos( -m_fYaw );
00074         sine    = (float)sin( -m_fYaw );
00075 
00076         Mat.LoadIdentity();
00077         Mat._11 = cosine;
00078         Mat._12 = -sine;
00079         Mat._21 = sine;
00080         Mat._22 = cosine;
00081         
00082         Matrix *= Mat;
00083 
00084         // Pitch
00085         cosine  = (float)cos( m_fPitch );
00086         sine    = (float)sin( m_fPitch );
00087 
00088         Mat.LoadIdentity();
00089         Mat._22 = cosine;
00090         Mat._23 = sine;
00091         Mat._32 = -sine;
00092         Mat._33 = cosine;
00093 
00094         Matrix *= Mat;
00095 */
00096         return m_Matrix;
00097 }
00098 /*
00099 //--------------------------------------------------------------------------------------------------------------------------------
00100 void KCamera::ComputeMatrix()
00101 {
00102         m_Matrix.LoadIdentity();
00103         
00104         m_Matrix._41    = m_Pos.x;
00105         m_Matrix._42    = m_Pos.y;
00106         m_Matrix._43    = m_Pos.z;
00107 /*      
00108         n.x     = (float)(sin( m_fPitch ) * cos( m_fYaw ) );
00109         n.y     = (float)(sin( m_fPitch ) * sin( m_fYaw ) );
00110         n.z     = (float)cos( m_fPitch );
00111         
00112         v = vUp - KVector::CrossProduct( ( KVector::CrossProduct( n, vUp ) ), n );
00113         u = n * v;
00114         
00115         m_Matrix._11    = u.x;
00116         m_Matrix._12    = u.y;
00117         m_Matrix._13    = u.z;
00118 
00119         m_Matrix._21    = v.x;
00120         m_Matrix._22    = v.y;
00121         m_Matrix._23    = v.z;
00122 
00123         m_Matrix._31    = n.x;
00124         m_Matrix._32    = n.y;
00125         m_Matrix._33    = n.z;
00126 */
00127 /*      // Pos
00128         m_Matrix._41    = m_Pos.x;
00129         m_Matrix._42    = m_Pos.y;
00130         m_Matrix._43    = m_Pos.z;
00131 
00132         RotY( -m_fYaw );
00133         RotX( m_fPitch );
00134 
00135 }*/
00136 
00137 //--------------------------------------------------------------------------------------------------------------------------------
00138 void KCamera::RotX( float ang )
00139 {
00140         KMatrix         Mat;
00141         float           cosine;
00142         float           sine;
00143 
00144         cosine  = (float)cos( ang );
00145         sine    = (float)sin( ang );
00146 
00147         Mat.LoadIdentity();
00148         Mat._22 = cosine;
00149         Mat._23 = sine;
00150         Mat._32 = -sine;
00151         Mat._33 = cosine;
00152 
00153         m_Matrix *= Mat;
00154 }
00155 
00156 //--------------------------------------------------------------------------------------------------------------------------------
00157 void KCamera::RotY( float ang )
00158 {
00159         KMatrix         Mat;
00160         float           cosine;
00161         float           sine;
00162 
00163         cosine  = (float)cos( ang );
00164         sine    = (float)sin( ang );
00165 
00166         Mat.LoadIdentity();
00167         Mat._11 = cosine;
00168         Mat._13 = sine;
00169         Mat._31 = -sine;
00170         Mat._33 = cosine;
00171         
00172         m_Matrix *= Mat;
00173 }
00174 
00175 //--------------------------------------------------------------------------------------------------------------------------------
00176 void KCamera::RotZ( float ang )
00177 {
00178         KMatrix         Mat;
00179         float           cosine;
00180         float           sine;
00181 
00182         cosine  = (float)cos( ang );
00183         sine    = (float)sin( ang );
00184 
00185         Mat.LoadIdentity();
00186         Mat._11 = cosine;
00187         Mat._12 = -sine;
00188         Mat._21 = sine;
00189         Mat._22 = cosine;
00190         
00191         m_Matrix *= Mat;
00192 }
00193 
00194 //--------------------------------------------------------------------------------------------------------------------------------
00195 void KCamera::ComputeLookAtMatrix()
00196 {
00197 /*      KMatrix matrix;
00198         KVector view;
00199         KVector right;
00200         KVector up;
00201         
00202         view    = m_TargetPos - m_CameraPos;
00203         view.Normalize();
00204         
00205     right       = KVector::CrossProduct( KVector( 1.0f, 0.0f, 0.0f ), view );
00206         up              = KVector::CrossProduct( view, right );
00207 
00208         right.Normalize();
00209         up.Normalize();
00210 
00211         matrix.LoadIdentity();
00212 
00213     matrix._11 = right.x;  matrix._12 = up.x;  matrix._13 = view.x;
00214     matrix._21 = right.y;  matrix._22 = up.y;  matrix._23 = view.y;
00215     matrix._31 = right.z;  matrix._32 = up.z;  matrix._33 = view.z;
00216 
00217     matrix._41 = - KVector::DotProduct( right, m_CameraPos );
00218     matrix._42 = - KVector::DotProduct( up, m_CameraPos );
00219     matrix._43 = - KVector::DotProduct( view, m_CameraPos );
00220 
00221         m_Matrix = matrix;*/
00222 }
00223 
00224 //--------------------------------------------------------------------------------------------------------------------------------
00225 void KCamera::SetTargetPosition( float x, float y, float z )
00226 {
00227         m_Target.LoadIdentity();
00228         m_Target._41 = x;
00229         m_Target._42 = y;
00230         m_Target._43 = z;
00231 
00232         m_TargetRot.LoadIdentity();
00233         m_TargetRot._41 = x;
00234         m_TargetRot._42 = y;
00235         m_TargetRot._42 = z;
00236 }
00237 
00238 //--------------------------------------------------------------------------------------------------------------------------------
00239 void KCamera::SetCameraPosition( float x, float y, float z )
00240 {
00241         m_CameraPos.x = x;
00242         m_CameraPos.y = y;
00243         m_CameraPos.z = z;
00244 }
00245 
00246 //--------------------------------------------------------------------------------------------------------------------------------
00247 void KCamera::RotateX( float ang )
00248 {
00249         m_fPitch += ang;
00250 
00251         ComputeMatrix();
00252 }
00253 
00254 //--------------------------------------------------------------------------------------------------------------------------------
00255 void KCamera::RotateY( float ang )
00256 {
00257         ComputeMatrix();
00258 }
00259 
00260 //--------------------------------------------------------------------------------------------------------------------------------
00261 void KCamera::RotateZ( float ang )
00262 {
00263         m_fYaw += ang;
00264         
00265         ComputeMatrix();
00266 }
00267 
00268 //--------------------------------------------------------------------------------------------------------------------------------
00269 void KCamera::Translate( float x, float y, float z )
00270 {
00271         KVector         vUp;
00272         KVector         vRight;
00273         KVector         vView;
00274 
00275         GetAxes( vRight, vUp, vView );
00276 
00277         vRight.Normalize();
00278         vRight  *= x;
00279         vUp.Normalize();
00280         vUp             *= y;
00281         vView.Normalize();
00282         vView   *= z;
00283 
00284         m_Pos   = m_Pos + vUp + vRight + vView;
00285 
00286         ComputeMatrix();
00287 }
00288 
00289 //--------------------------------------------------------------------------------------------------------------------------------
00290 KVector KCamera::GetPos()
00291 {
00292         return KVector( m_Pos.x, m_Pos.y, m_Pos.z );
00293 }
00294 
00295 //--------------------------------------------------------------------------------------------------------------------------------
00296 void KCamera::SetPos( float x, float y, float z )
00297 {
00298         m_Pos.x = x;
00299         m_Pos.y = y;
00300         m_Pos.z = z;
00301 
00302         ComputeMatrix();
00303 }
00304 
00305 /*
00306 //--------------------------------------------------------------------------------------------------------------------------------
00307 KVector KCamera::GetDir()
00308 {
00309         KMatrix         Matrix = GetMatrix();
00310 
00311         return KVector( Matrix._21, Matrix._23, Matrix._22 );
00312 }
00313 */
00314 //--------------------------------------------------------------------------------------------------------------------------------
00315 void KCamera::GetAxes( KVector& vRight, KVector& vUp, KVector& vView )
00316 {
00317         KMatrix         Matrix = GetMatrix();
00318 
00319         vRight  = KVector( Matrix._11, Matrix._21, Matrix._31 );
00320         vUp             = KVector( Matrix._12, Matrix._22, Matrix._32 );
00321         vView   = KVector( Matrix._13, Matrix._23, Matrix._33 );
00322 }
00323 
00324 //--------------------------------------------------------------------------------------------------------------------------------
00325 void KCamera::GetFrustumPlanes( KPlane pPlanes[6] )
00326 {
00327         KVector         vPos, vRight, vUp, vView;
00328         KVector         vzFar, vzNear;
00329         KVector         vx, vy;
00330         KVector         vUL, vUR, vDL, vDR;
00331 
00332         float           w, h;
00333 
00334         vPos = GetPos();
00335         GetAxes( vRight, vUp, vView );
00336 
00337         // Largeur & Hauteur du frustum au niveau du far plane
00338         w = m_Far * 2.0f * tanf( (m_Fov * ( PI / 180.0f )) / 2.0f );
00339         h = w / m_Aspect;
00340 
00341         vx              = vRight * ( w / 2.0f );
00342         vy              = vUp * ( h / 2.0f );
00343         vzFar   = vPos + ( vView * m_Far );
00344         vzNear  = vPos + ( vView * m_Near );
00345         vUL             = vzFar + vy - vx;
00346         vUR             = vzFar + vy + vx;
00347         vDL             = vzFar - vy - vx;
00348         vDR             = vzFar - vy + vx;
00349 
00350         // Near
00351         pPlanes[0] = KPlane( vzNear, vView );
00352         // Far
00353         pPlanes[1] = KPlane( vzFar, -vView );
00354         // Gauche
00355         pPlanes[2] = KPlane( vPos, vDL, vUL );
00356         // Droite
00357         pPlanes[3] = KPlane( vPos, vUR, vDR );
00358         // Haut
00359         pPlanes[4] = KPlane( vPos, vUL, vUR );
00360         // Bas
00361         pPlanes[5] = KPlane( vPos, vDR, vDL );
00362 }
00363 
00364 
00365 //--------------------------------------------------------------------------------------------------------------------------------
00366 void KCamera::GetFrustumPoints( KVector pPoints[8] )
00367 {
00368         KVector         vPos, vRight, vUp, vView;
00369         KVector         vzFar, vzNear;
00370         KVector         fvx, fvy, nvx, nvy;
00371         KVector         vUL, vUR, vDL, vDR;
00372 
00373         float           fw, fh, nw, nh;
00374 
00375         vPos = GetPos();
00376         GetAxes( vRight, vUp, vView );
00377 
00378         // Largeur & Hauteur du frustum au niveau du far plane
00379         fw = m_Far * 2.0f * tanf( (m_Fov * ( PI / 180.0f )) / 2.0f );
00380         fh = fw / m_Aspect;
00381 
00382         // Largeur & Hauteur du frustum au niveau du near plane
00383         nw = m_Near * 2.0f * tanf( (m_Fov * ( PI / 180.0f )) / 2.0f );
00384         nh = nw / m_Aspect;
00385 
00386         fvx             = vRight * ( fw / 2.0f );
00387         fvy             = vUp * ( fh / 2.0f );
00388         nvx             = vRight * ( nw / 2.0f );
00389         nvy             = vUp * ( nh / 2.0f );
00390         vzFar   = vPos + ( vView * m_Far );
00391         vzNear  = vPos + ( vView * m_Near );
00392         pPoints[0]      = vzFar + fvy - fvx;
00393         pPoints[1]      = vzFar + fvy + fvx;
00394         pPoints[2]      = vzFar - fvy - fvx;
00395         pPoints[3]      = vzFar - fvy + fvx;
00396         pPoints[4]      = vzNear + nvy - nvx;
00397         pPoints[5]      = vzNear + nvy + nvx;
00398         pPoints[6]      = vzNear - nvy - nvx;
00399         pPoints[7]      = vzNear - nvy + nvx;
00400 }
00401 
00402 //--------------------------------------------------------------------------------------------------------------------------------
00403 KFrameCamera::KFrameCamera()
00404 {
00405         m_nFrame        = 0;
00406         m_nKey          = 0;
00407         m_nFPS          = 10;
00408         m_pKeyFrameCamera = NULL;
00409         m_pKeyFrameTarget = NULL;
00410 }
00411 
00412 //--------------------------------------------------------------------------------------------------------------------------------
00413 KFrameCamera::~KFrameCamera()
00414 {
00415         if( m_pKeyFrameCamera )
00416                 Deletep( m_pKeyFrameCamera );
00417         if( m_pKeyFrameTarget )
00418                 Deletep( m_pKeyFrameTarget );
00419 }
00420 
00421 //--------------------------------------------------------------------------------------------------------------------------------
00422 KVector KFrameCamera::GetKeyFramePos( KKeyFrameCamera* pKeyFrame, int Time )
00423 {
00424         KASSERT( m_nKey );
00425 
00426         if( m_nKey == 1 )
00427                 return pKeyFrame[ 0 ].m_Pos;
00428 
00429         float           Frame = (((float)Time / 1000.0f) * (float)m_nFPS);
00430 
00431         Frame = (float)fmod(Frame, (float)m_nFrame);
00432 
00433         KVector         PosPrevKey;
00434         KVector         PosNextKey;
00435         int                     FramePrevKey;
00436         int                     FrameNextKey;
00437         int                     Key;
00438 
00439         for(Key = 0; Key < m_nKey; Key ++)
00440         {
00441                 if( ( pKeyFrame[ Key ].m_Frame <= Frame ) &&
00442                         ( pKeyFrame[ Key + 1].m_Frame > Frame ) )
00443                 {
00444                         PosPrevKey              = pKeyFrame[ Key ].m_Pos;
00445                         FramePrevKey    = pKeyFrame[ Key ].m_Frame;
00446                         PosNextKey              = pKeyFrame[ Key + 1 ].m_Pos;
00447                         FrameNextKey    = pKeyFrame[ Key + 1 ].m_Frame;
00448                         break;
00449                 }
00450         }
00451 
00452         float   delta = (Frame - (float)FramePrevKey ) / ((float)FrameNextKey - (float)FramePrevKey);
00453 
00454         KVector result = ((PosNextKey - PosPrevKey) * delta) + PosPrevKey;
00455 
00456         return result;
00457 }
00458 
00459 //--------------------------------------------------------------------------------------------------------------------------------
00460 KVector KFrameCamera::GetCameraPos( int Time )
00461 {
00462         return GetKeyFramePos( m_pKeyFrameCamera, Time );
00463 }
00464 
00465 //--------------------------------------------------------------------------------------------------------------------------------
00466 KVector KFrameCamera::GetTargetPos( int Time )
00467 {
00468         return GetKeyFramePos( m_pKeyFrameTarget, Time );
00469 }
00470 

Generated on Sun Mar 25 20:02:15 2007 for Zythum Project by  doxygen 1.5.1-p1