00001
00002
00003
00004
00005
00006
00007
00008
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
00055 }
00056
00057
00058 KMatrix KCamera::GetMatrix()
00059 {
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 return m_Matrix;
00097 }
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
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
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
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
00308
00309
00310
00311
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
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
00351 pPlanes[0] = KPlane( vzNear, vView );
00352
00353 pPlanes[1] = KPlane( vzFar, -vView );
00354
00355 pPlanes[2] = KPlane( vPos, vDL, vUL );
00356
00357 pPlanes[3] = KPlane( vPos, vUR, vDR );
00358
00359 pPlanes[4] = KPlane( vPos, vUL, vUR );
00360
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
00379 fw = m_Far * 2.0f * tanf( (m_Fov * ( PI / 180.0f )) / 2.0f );
00380 fh = fw / m_Aspect;
00381
00382
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