raSystem  1.0 bata
racamera.cpp
Go to the documentation of this file.
1 //--------------------------------------------------------------------------------------
2 // File: DXUTcamera.cpp
3 //
4 // Copyright (c) Microsoft Corporation. All rights reserved
5 //--------------------------------------------------------------------------------------
6 #include "..\include\raMain.h"
7 #undef min // use __min instead
8 #undef max // use __max instead
9 
10 namespace System
11 {
12 //--------------------------------------------------------------------------------------
13 // Constructor
14 //--------------------------------------------------------------------------------------
16 {
17  m_cKeysDown = 0;
18  ZeroMemory( m_aKeys, sizeof( BYTE ) * CAM_MAX_KEYS );
19 
20  // Set attributes for the view matrix
21  raVector3 vEyePt = raVector3( 0.0f, 0.0f, 0.0f );
22  raVector3 vLookatPt = raVector3( 0.0f, 0.0f, 1.0f );
23 
24  // Setup the view matrix
25  SetViewParams( &vEyePt, &vLookatPt );
26 
27  // Setup the projection matrix
28  SetProjParams( PI / 4.0f, 1.0f, 1.0f, 1000.0f );
29 
30  GetCursorPos( &m_ptLastMousePosition );
31  m_bMouseLButtonDown = false;
32  m_bMouseMButtonDown = false;
33  m_bMouseRButtonDown = false;
34  m_nCurrentButtonMask = 0;
35  m_nMouseWheelDelta = 0;
36 
37  m_fCameraYawAngle = 0.0f;
38  m_fCameraPitchAngle = 0.0f;
39 
40  SetRect( &m_rcDrag, LONG_MIN, LONG_MIN, LONG_MAX, LONG_MAX );
41  m_vVelocity = raVector3( 0, 0, 0 );
42  m_bMovementDrag = false;
43  m_vVelocityDrag = raVector3( 0, 0, 0 );
44  m_fDragTimer = 0.0f;
45  m_fTotalDragTimeToZero = 0.25;
46  m_vRotVelocity = raVector2( 0, 0 );
47 
48  m_fRotationScaler = 0.01f;
49  m_fMoveScaler = 5.0f;
50 
51  m_bInvertPitch = false;
52  m_bEnableYAxisMovement = true;
53  m_bEnablePositionMovement = true;
54 
55  m_vMouseDelta = raVector2( 0, 0 );
56  m_fFramesToSmoothMouseData = 2.0f;
57 
58  m_bClipToBoundary = false;
59  m_vMinBoundary = raVector3( -1, -1, -1 );
60  m_vMaxBoundary = raVector3( 1, 1, 1 );
61 
62  m_bResetCursorAfterMove = false;
63 }
64 
65 //--------------------------------------------------------------------------------------
66 // Client can call this to change the position and direction of camera
67 //--------------------------------------------------------------------------------------
68 VOID raCamera::SetViewParams( raVector3* pvEyePt, raVector3* pvLookatPt )
69 {
70  if( NULL == pvEyePt || NULL == pvLookatPt )
71  return;
72 
73  m_vDefaultEye = m_vEye = *pvEyePt;
74  m_vDefaultLookAt = m_vLookAt = *pvLookatPt;
75 
76  // Calc the view matrix
77  raVector3 vUp( 0,1,0 );
78  D3DXMatrixLookAtLH((D3DXMATRIX*)&m_mView, (D3DXVECTOR3*)pvEyePt, (D3DXVECTOR3*)pvLookatPt, (D3DXVECTOR3*)&vUp );
79 
80  D3DXMATRIX mInvView;
81  D3DXMatrixInverse( &mInvView, NULL, (D3DXMATRIX *)&m_mView );
82 
83  // The axis basis vectors and camera position are stored inside the
84  // position matrix in the 4 rows of the camera's world matrix.
85  // To figure out the yaw/pitch of the camera, we just need the Z basis vector
86  raVector3* pZBasis = ( raVector3* )&mInvView._31;
87 
88  m_fCameraYawAngle = atan2f( pZBasis->x, pZBasis->z );
89  float fLen = sqrtf( pZBasis->z * pZBasis->z + pZBasis->x * pZBasis->x );
90  m_fCameraPitchAngle = -atan2f( pZBasis->y, fLen );
91 }
92 
93 //--------------------------------------------------------------------------------------
94 // Calculates the projection matrix based on input params
95 //--------------------------------------------------------------------------------------
96 VOID raCamera::SetProjParams( FLOAT fFOV, FLOAT fAspect, FLOAT fNearPlane,
97  FLOAT fFarPlane )
98 {
99  // Set attributes for the projection matrix
100  m_fFOV = fFOV;
101  m_fAspect = fAspect;
102  m_fNearPlane = fNearPlane;
103  m_fFarPlane = fFarPlane;
104  D3DXMatrixPerspectiveFovLH( (D3DXMATRIX*)&m_mProj, fFOV, fAspect, fNearPlane, fFarPlane );
105 }
106 void raCamera::SetOrthoCamera( raVector2* pvWindowSize, float fNearPlane, float fFarPlane)
107 {
108  D3DXMatrixOrthoLH((D3DXMATRIX*)&m_mOrtho, pvWindowSize->x, pvWindowSize->y, fNearPlane, fFarPlane);
109 }
110 long raCamera::OnKeyDown(HWND wnd, WPARAM wParam, LPARAM lParam)
111 {
112  UNREFERENCED_PARAMETER( lParam );
113  m_hwnd = wnd;
114 
115  D3DUtil_CameraKeys mappedKey = MapKey( ( UINT )wParam );
116  if( mappedKey != CAM_UNKNOWN )
117  {
118  if( FALSE == IsKeyDown( m_aKeys[mappedKey] ) )
119  {
120  m_aKeys[ mappedKey ] = KEY_WAS_DOWN_MASK | KEY_IS_DOWN_MASK;
121  ++m_cKeysDown;
122  }
123  }
124  return 0;
125 }
126 long raCamera::OnKeyUp(HWND wnd, WPARAM wParam, LPARAM lParam)
127 {
128  // Map this key to a D3DUtil_CameraKeys enum and update the
129  // state of m_aKeys[] by removing the KEY_IS_DOWN_MASK mask.
130  D3DUtil_CameraKeys mappedKey = MapKey( ( UINT )wParam );
131  if( mappedKey != CAM_UNKNOWN && ( DWORD )mappedKey < 8 )
132  {
133  m_aKeys[ mappedKey ] &= ~KEY_IS_DOWN_MASK;
134  --m_cKeysDown;
135  }
136  return 0;
137 }
138 long raCamera::OnRMouseUp(HWND wnd, WPARAM wParam, LPARAM lParam)
139 {
140  m_bMouseRButtonDown = false; m_nCurrentButtonMask &= ~MOUSE_RIGHT_BUTTON;
141  if( !m_bMouseLButtonDown &&
142  !m_bMouseRButtonDown &&
143  !m_bMouseMButtonDown )
144  {
145  ReleaseCapture();
146  }
147 
148  return 0;
149 }
150 long raCamera::OnRMouseDown(HWND wnd, WPARAM wParam, LPARAM lParam)
151 {
152  POINT ptCursor =
153  {
154  ( short )LOWORD( lParam ), ( short )HIWORD(lParam )
155  };
156  m_bMouseRButtonDown = true; m_nCurrentButtonMask |= MOUSE_RIGHT_BUTTON;
157  SetCapture( wnd);
158  GetCursorPos( &m_ptLastMousePosition );
159 
160  return 0;
161 }
162 long raCamera::OnLMouseUp(HWND wnd, WPARAM wParam, LPARAM lParam)
163 {
164  m_bMouseLButtonDown = false; m_nCurrentButtonMask &= ~MOUSE_LEFT_BUTTON;
165  if( !m_bMouseLButtonDown &&
166  !m_bMouseRButtonDown &&
167  !m_bMouseMButtonDown )
168  {
169  ReleaseCapture();
170  }
171 
172  return 0;
173 }
174 long raCamera::OnLMouseDown(HWND wnd, WPARAM wParam, LPARAM lParam)
175 {
176  POINT ptCursor =
177  {
178  ( short )LOWORD( lParam ), ( short )HIWORD(lParam )
179  };
180  m_bMouseLButtonDown = true; m_nCurrentButtonMask |= MOUSE_LEFT_BUTTON;
181  SetCapture( wnd);
182  GetCursorPos( &m_ptLastMousePosition );
183  return 0;
184 }
185 long raCamera::OnMMouseUp(HWND wnd, WPARAM wParam, LPARAM lParam)
186 {
187  m_bMouseMButtonDown = false; m_nCurrentButtonMask &= ~MOUSE_MIDDLE_BUTTON;
188  if( !m_bMouseLButtonDown &&
189  !m_bMouseRButtonDown &&
190  !m_bMouseMButtonDown )
191  {
192  ReleaseCapture();
193  }
194  return 0;
195 }
196 long raCamera::OnMMouseDown(HWND wnd, WPARAM wParam, LPARAM lParam)
197 {
198  POINT ptCursor =
199  {
200  ( short )LOWORD( lParam ), ( short )HIWORD(lParam )
201  };
202  m_bMouseMButtonDown = true; m_nCurrentButtonMask |= MOUSE_MIDDLE_BUTTON;
203  SetCapture( wnd );
204  GetCursorPos( &m_ptLastMousePosition );
205 
206  return 0;
207 }
208 long raCamera::OnCaptureChange(HWND wnd, WPARAM wParam, LPARAM lParam)
209 {
210  if( ( HWND )lParam != wnd )
211  {
212  if( ( m_nCurrentButtonMask & MOUSE_LEFT_BUTTON ) ||
213  ( m_nCurrentButtonMask & MOUSE_MIDDLE_BUTTON ) ||
214  ( m_nCurrentButtonMask & MOUSE_RIGHT_BUTTON ) )
215  {
216  m_bMouseLButtonDown = false;
217  m_bMouseMButtonDown = false;
218  m_bMouseRButtonDown = false;
219  m_nCurrentButtonMask &= ~MOUSE_LEFT_BUTTON;
220  m_nCurrentButtonMask &= ~MOUSE_MIDDLE_BUTTON;
221  m_nCurrentButtonMask &= ~MOUSE_RIGHT_BUTTON;
222  ReleaseCapture();
223  }
224  }
225  return 0;
226 }
227 long raCamera::OnMouseWheel(HWND wnd, WPARAM wParam, LPARAM lParam)
228 {
229  m_nMouseWheelDelta += ( short )HIWORD( wParam );
230  return 0;
231 }
232 
233 //--------------------------------------------------------------------------------------
234 // Figure out the velocity based on keyboard input & drag if any
235 //--------------------------------------------------------------------------------------
236 void raCamera::GetInput( bool bGetKeyboardInput, bool bGetMouseInput, bool bGetGamepadInput,
237  bool bResetCursorAfterMove )
238 {
239  m_vKeyboardDirection = raVector3( 0, 0, 0 );
240  if( bGetKeyboardInput )
241  {
242  // Update acceleration vector based on keyboard state
243  if( IsKeyDown( m_aKeys[CAM_MOVE_FORWARD] ) )
244  m_vKeyboardDirection.z += 1.0f;
245  if( IsKeyDown( m_aKeys[CAM_MOVE_BACKWARD] ) )
246  m_vKeyboardDirection.z -= 1.0f;
247  if( m_bEnableYAxisMovement )
248  {
249  if( IsKeyDown( m_aKeys[CAM_MOVE_UP] ) )
250  m_vKeyboardDirection.y += 1.0f;
251  if( IsKeyDown( m_aKeys[CAM_MOVE_DOWN] ) )
252  m_vKeyboardDirection.y -= 1.0f;
253  }
254  if( IsKeyDown( m_aKeys[CAM_STRAFE_RIGHT] ) )
255  m_vKeyboardDirection.x += 1.0f;
256  if( IsKeyDown( m_aKeys[CAM_STRAFE_LEFT] ) )
257  m_vKeyboardDirection.x -= 1.0f;
258  }
259 
260  if( bGetMouseInput )
261  {
262  UpdateMouseDelta();
263  }
264 }
265 
266 //--------------------------------------------------------------------------------------
267 // Figure out the mouse delta based on mouse movement
268 //--------------------------------------------------------------------------------------
270 {
271  POINT ptCurMouseDelta;
272  POINT ptCurMousePos;
273 
274  // Get current position of mouse
275  GetCursorPos( &ptCurMousePos );
276 
277  // Calc how far it's moved since last frame
278  ptCurMouseDelta.x = ptCurMousePos.x - m_ptLastMousePosition.x;
279  ptCurMouseDelta.y = ptCurMousePos.y - m_ptLastMousePosition.y;
280 
281  // Record current position for next time
282  m_ptLastMousePosition = ptCurMousePos;
283 
284  if( m_bResetCursorAfterMove)
285  {
286  // Set position of camera to center of desktop,
287  // so it always has room to move. This is very useful
288  // if the cursor is hidden. If this isn't done and cursor is hidden,
289  // then invisible cursor will hit the edge of the screen
290  // and the user can't tell what happened
291  POINT ptCenter;
292 
293  // Get the center of the current monitor
294  MONITORINFO mi;
295  mi.cbSize = sizeof( MONITORINFO );
296  GetMonitorInfo( MonitorFromWindow( m_hwnd, MONITOR_DEFAULTTONEAREST ), &mi );
297  ptCenter.x = ( mi.rcMonitor.left + mi.rcMonitor.right ) / 2;
298  ptCenter.y = ( mi.rcMonitor.top + mi.rcMonitor.bottom ) / 2;
299  SetCursorPos( ptCenter.x, ptCenter.y );
300  m_ptLastMousePosition = ptCenter;
301  }
302 
303  // Smooth the relative mouse data over a few frames so it isn't
304  // jerky when moving slowly at low frame rates.
305  float fPercentOfNew = 1.0f / m_fFramesToSmoothMouseData;
306  float fPercentOfOld = 1.0f - fPercentOfNew;
307  m_vMouseDelta.x = m_vMouseDelta.x * (raFloat)fPercentOfOld + (raFloat)ptCurMouseDelta.x * (raFloat)fPercentOfNew;
308  m_vMouseDelta.y = m_vMouseDelta.y * (raFloat)fPercentOfOld + (raFloat)ptCurMouseDelta.y * (raFloat)fPercentOfNew;
309 
310  m_vRotVelocity = m_vMouseDelta * m_fRotationScaler;
311 }
312 
313 //--------------------------------------------------------------------------------------
314 // Figure out the velocity based on keyboard input & drag if any
315 //--------------------------------------------------------------------------------------
316 void raCamera::UpdateVelocity( float fElapsedTime )
317 {
318  raVector2 vGamePadRightThumb = raVector2( m_vGamePadRightThumb.x, - (raFloat)m_vGamePadRightThumb.z );
319  m_vRotVelocity = m_vMouseDelta * m_fRotationScaler + vGamePadRightThumb * 0.02f * fElapsedTime;
320 
321  raVector3 vAccel = m_vKeyboardDirection + m_vGamePadLeftThumb;
322 
323  // Normalize vector so if moving 2 dirs (left & forward),
324  // the camera doesn't move faster than if moving in 1 dir
325  vAccel = raVector3NormalizeEx(vAccel );
326 
327  // Scale the acceleration vector
328  vAccel *= m_fMoveScaler;
329 
330  if( m_bMovementDrag )
331  {
332  // Is there any acceleration this frame?
333  if( raVector3LenghtSq(vAccel ) > 0 )
334  {
335  // If so, then this means the user has pressed a movement key\
336  // so change the velocity immediately to acceleration
337  // upon keyboard input. This isn't normal physics
338  // but it will give a quick response to keyboard input
339  m_vVelocity = vAccel;
340  m_fDragTimer = m_fTotalDragTimeToZero;
341  m_vVelocityDrag = vAccel / m_fDragTimer;
342  }
343  else
344  {
345  // If no key being pressed, then slowly decrease velocity to 0
346  if( m_fDragTimer > 0 )
347  {
348  // Drag until timer is <= 0
349  m_vVelocity -= m_vVelocityDrag * fElapsedTime;
350  m_fDragTimer -= fElapsedTime;
351  }
352  else
353  {
354  // Zero velocity
355  m_vVelocity = raVector3( 0, 0, 0 );
356  }
357  }
358  }
359  else
360  {
361  // No drag, so immediately change the velocity
362  m_vVelocity = vAccel;
363  }
364 }
365 
366 //--------------------------------------------------------------------------------------
367 // Clamps pV to lie inside m_vMinBoundary & m_vMaxBoundary
368 //--------------------------------------------------------------------------------------
370 {
371  // Constrain vector to a bounding box
372  pV->x = __max( pV->x, m_vMinBoundary.x );
373  pV->y = __max( pV->y, m_vMinBoundary.y );
374  pV->z = __max( pV->z, m_vMinBoundary.z );
375 
376  pV->x = __min( pV->x, m_vMaxBoundary.x );
377  pV->y = __min( pV->y, m_vMaxBoundary.y );
378  pV->z = __min( pV->z, m_vMaxBoundary.z );
379 }
380 
381 //--------------------------------------------------------------------------------------
382 // Maps a windows virtual key to an enum
383 //--------------------------------------------------------------------------------------
385 {
386  // This could be upgraded to a method that's user-definable but for
387  // simplicity, we'll use a hardcoded mapping.
388  switch( nKey )
389  {
390  case VK_CONTROL:
391  return CAM_CONTROLDOWN;
392  case VK_LEFT:
393  return CAM_STRAFE_LEFT;
394  case VK_RIGHT:
395  return CAM_STRAFE_RIGHT;
396  case VK_UP:
397  return CAM_MOVE_FORWARD;
398  case VK_DOWN:
399  return CAM_MOVE_BACKWARD;
400  case VK_PRIOR:
401  return CAM_MOVE_UP; // pgup
402  case VK_NEXT:
403  return CAM_MOVE_DOWN; // pgdn
404 
405  case 'A':
406  return CAM_STRAFE_LEFT;
407  case 'D':
408  return CAM_STRAFE_RIGHT;
409  case 'W':
410  return CAM_MOVE_FORWARD;
411  case 'S':
412  return CAM_MOVE_BACKWARD;
413  case 'Q':
414  return CAM_MOVE_DOWN;
415  case 'E':
416  return CAM_MOVE_UP;
417 
418  case VK_NUMPAD4:
419  return CAM_STRAFE_LEFT;
420  case VK_NUMPAD6:
421  return CAM_STRAFE_RIGHT;
422  case VK_NUMPAD8:
423  return CAM_MOVE_FORWARD;
424  case VK_NUMPAD2:
425  return CAM_MOVE_BACKWARD;
426  case VK_NUMPAD9:
427  return CAM_MOVE_UP;
428  case VK_NUMPAD3:
429  return CAM_MOVE_DOWN;
430 
431  case VK_F12:
432  return CAM_RESET;
433  }
434 
435  return CAM_UNKNOWN;
436 }
437 
438 //--------------------------------------------------------------------------------------
439 // Reset the camera's position back to the default
440 //--------------------------------------------------------------------------------------
442 {
443  SetViewParams(&m_vDefaultEye, &m_vDefaultLookAt );
444 }
445 
446 //--------------------------------------------------------------------------------------
447 // Constructor
448 //--------------------------------------------------------------------------------------
449 raCameraFP::raCameraFP() : m_nActiveButtonMask( 0x07 ), raCamera()
450 {
452 }
453 
454 //--------------------------------------------------------------------------------------
455 // Update the view matrix based on user input & elapsed time
456 //--------------------------------------------------------------------------------------
457 VOID raCameraFP::FrameMove(float fElapsedTime, float FPS )
458 {
459  if( IsKeyDown( m_aKeys[CAM_RESET] ) )
460  Reset();
461 
462  // Get keyboard/mouse/gamepad input
464  true, m_bResetCursorAfterMove );
465 
466  UpdateVelocity( fElapsedTime );
467 
468  // Simple euler method to calculate position delta
469  raVector3 vPosDelta = m_vVelocity * fElapsedTime;
470 
471  // If rotating the camera
472  if( ( m_nActiveButtonMask & m_nCurrentButtonMask ) ||
474  (float)m_vGamePadRightThumb.x != 0.0f ||
475  (float)m_vGamePadRightThumb.z != 0.0f )
476  {
477  // Update the pitch & yaw angle based on mouse movement
478  float fYawDelta = m_vRotVelocity.x;
479  float fPitchDelta = m_vRotVelocity.y;
480 
481  // Invert pitch if requested
482  if( m_bInvertPitch )
483  fPitchDelta = -fPitchDelta;
484 
485  m_fCameraPitchAngle += fPitchDelta;
486  m_fCameraYawAngle += fYawDelta;
487 
488  // Limit pitch to straight up or straight down
489  m_fCameraPitchAngle = __max( -PI / 2.0f, m_fCameraPitchAngle );
490  m_fCameraPitchAngle = __min( +PI / 2.0f, m_fCameraPitchAngle );
491  }
492 
493  // Make a rotation matrix based on the camera's yaw & pitch
494  D3DXMATRIX mCameraRot;
495  D3DXMatrixRotationYawPitchRoll( &mCameraRot, m_fCameraYawAngle, m_fCameraPitchAngle, 0 );
496 
497  // Transform vectors based on camera's rotation matrix
498  D3DXVECTOR3 vWorldUp, vWorldAhead;
499  D3DXVECTOR3 vLocalUp = D3DXVECTOR3( 0, 1, 0 );
500  D3DXVECTOR3 vLocalAhead = D3DXVECTOR3( 0, 0, 1 );
501  D3DXVec3TransformCoord( &vWorldUp, &vLocalUp, &mCameraRot );
502  D3DXVec3TransformCoord( &vWorldAhead, &vLocalAhead, &mCameraRot );
503 
504  // Transform the position delta by the camera's rotation
505  D3DXVECTOR3 vPosDeltaWorld;
507  {
508  // If restricting Y movement, do not include pitch
509  // when transforming position delta vector.
510  D3DXMatrixRotationYawPitchRoll( &mCameraRot, m_fCameraYawAngle, 0.0f, 0.0f );
511  }
512  D3DXVec3TransformCoord( &vPosDeltaWorld, (D3DXVECTOR3 *)&vPosDelta, (D3DXMATRIX *)&mCameraRot );
513 
514  // Move the eye position
515  m_vEye += vPosDeltaWorld;
516 
517  if( m_bClipToBoundary )
519 
520  // Update the lookAt position based on the eye position
521  m_vLookAt = m_vEye + vWorldAhead;
522 
523  // Update the view matrix
524  D3DXMatrixLookAtLH( (D3DXMATRIX *)&m_mView, (D3DXVECTOR3*)&m_vEye, (D3DXVECTOR3*)&m_vLookAt, (D3DXVECTOR3*)&vWorldUp );
525 
526  D3DXMatrixInverse((D3DXMATRIX *) &m_mCameraWorld, NULL,(D3DXMATRIX *) &m_mView );
527 }
528 
529 //--------------------------------------------------------------------------------------
530 // Enable or disable each of the mouse buttons for rotation drag.
531 //--------------------------------------------------------------------------------------
532 void raCameraFP::SetRotateButtons( bool bLeft, bool bMiddle, bool bRight, bool bRotateWithoutButtonDown )
533 {
534  m_nActiveButtonMask = ( bLeft ? MOUSE_LEFT_BUTTON : 0 ) |
535  ( bMiddle ? MOUSE_MIDDLE_BUTTON : 0 ) |
536  ( bRight ? MOUSE_RIGHT_BUTTON : 0 );
537  m_bRotateWithoutButtonDown = bRotateWithoutButtonDown;
538 }
539 //-----------------------------------------------------------------------
540 void raColCamera::FrameMove( float fElapsedTime, float FPS )
541 {
542  if( IsKeyDown( m_aKeys[CAM_RESET] ) )
543  Reset();
544 
545  // Get keyboard/mouse/gamepad input
547  true, m_bResetCursorAfterMove );
548 
549  UpdateVelocity( fElapsedTime );
550 
551  // Simple euler method to calculate position delta
552  raVector3 vPosDelta = m_vVelocity * fElapsedTime;
553 
554  // If rotating the camera
555  if( ( m_nActiveButtonMask & m_nCurrentButtonMask ) ||
557  (float)m_vGamePadRightThumb.x != 0.0f ||
558  (float)m_vGamePadRightThumb.z != 0.0f )
559  {
560  // Update the pitch & yaw angle based on mouse movement
561  float fYawDelta = m_vRotVelocity.x;
562  float fPitchDelta = m_vRotVelocity.y;
563 
564  // Invert pitch if requested
565  if( m_bInvertPitch )
566  fPitchDelta = -fPitchDelta;
567 
568  m_fCameraPitchAngle += fPitchDelta;
569  m_fCameraYawAngle += fYawDelta;
570 
571  // Limit pitch to straight up or straight down
572  m_fCameraPitchAngle = __max( -PI / 2.0f, m_fCameraPitchAngle );
573  m_fCameraPitchAngle = __min( +PI / 2.0f, m_fCameraPitchAngle );
574  }
575 
576  // Make a rotation matrix based on the camera's yaw & pitch
577  D3DXMATRIX mCameraRot;
578  D3DXMatrixRotationYawPitchRoll( &mCameraRot, m_fCameraYawAngle, m_fCameraPitchAngle, 0 );
579 
580  // Transform vectors based on camera's rotation matrix
581  D3DXVECTOR3 vWorldUp, vWorldAhead;
582  D3DXVECTOR3 vLocalUp = D3DXVECTOR3( 0, 1, 0 );
583  D3DXVECTOR3 vLocalAhead = D3DXVECTOR3( 0, 0, 1 );
584  D3DXVec3TransformCoord( &vWorldUp, &vLocalUp, &mCameraRot );
585  D3DXVec3TransformCoord( &vWorldAhead, &vLocalAhead, &mCameraRot );
586 
587  // Transform the position delta by the camera's rotation
588  D3DXVECTOR3 vPosDeltaWorld;
590  {
591  // If restricting Y movement, do not include pitch
592  // when transforming position delta vector.
593  D3DXMatrixRotationYawPitchRoll( &mCameraRot, m_fCameraYawAngle, 0.0f, 0.0f );
594  }
595  D3DXVec3TransformCoord( &vPosDeltaWorld, (D3DXVECTOR3 *)&vPosDelta, (D3DXMATRIX *)&mCameraRot );
596 
597  m_cDist = FLT_MAX;
598  //raColObject temp;
599  bool Move = true;
600 
601  for(int i = 0; i < m_pObject.GetSize(); i++)
602  {
603  raColObject temp = m_pObject.GetAt(i);
604  if(temp.Object)
605  {
606  raVector3 rayDir;
607  rayDir = raVector3Normalize(vPosDeltaWorld);
608  temp.Object->Intersects(&m_vEye, &rayDir, &m_cDist);
609  }
610  int itemp = (int)(raVector3Lenght(vPosDeltaWorld) + temp.Distance + m_fNearPlane);
611  if((m_cDist >= itemp) && (Move == true))
612  {
613  // Move the eye position
614  m_vEye += vPosDeltaWorld;
615 
616  if( m_bClipToBoundary )
618  }
619  else
620  Move = false;
621  }
622 
623  // Update the lookAt position based on the eye position
624  m_vLookAt = m_vEye + vWorldAhead;
625  D3DXMatrixLookAtLH( (D3DXMATRIX *)&m_mView, (D3DXVECTOR3*)&m_vEye, (D3DXVECTOR3*)&m_vLookAt, (D3DXVECTOR3*)&vWorldUp );
626 
627  D3DXMatrixInverse((D3DXMATRIX *) &m_mCameraWorld, NULL,(D3DXMATRIX *) &m_mView );
628 }
629 };
virtual long OnMMouseUp(HWND wnd, WPARAM wParam, LPARAM lParam)
Definition: racamera.cpp:185
virtual D3DUtil_CameraKeys MapKey(UINT nKey)
Definition: racamera.cpp:384
bool m_bClipToBoundary
Definition: raCamera.h:238
raFloat y
Definition: raVector3.h:13
raFloat x
Definition: raVector3.h:12
raVector3 m_vGamePadRightThumb
Definition: raCamera.h:196
#define MOUSE_MIDDLE_BUTTON
Definition: raCamera.h:31
raMatrix m_mView
Definition: raCamera.h:191
bool m_bInvertPitch
Definition: raCamera.h:234
virtual long OnRMouseDown(HWND wnd, WPARAM wParam, LPARAM lParam)
Definition: racamera.cpp:150
raMatrix m_mCameraWorld
Definition: raCamera.h:192
virtual long OnMMouseDown(HWND wnd, WPARAM wParam, LPARAM lParam)
Definition: racamera.cpp:196
float m_fCameraYawAngle
Definition: raCamera.h:215
void SetRotateButtons(bool bLeft, bool bMiddle, bool bRight, bool bRotateWithoutButtonDown=false)
Definition: racamera.cpp:532
raRender * Object
Definition: raCamera.h:266
raFloat x
Definition: raVector2.h:10
float m_fCameraPitchAngle
Definition: raCamera.h:216
D3DUtil_CameraKeys
Definition: raCamera.h:13
bool m_bEnablePositionMovement
Definition: raCamera.h:235
raVector2 m_vRotVelocity
Definition: raCamera.h:224
#define MOUSE_RIGHT_BUTTON
Definition: raCamera.h:32
#define KEY_WAS_DOWN_MASK
Definition: raCamera.h:27
void UpdateMouseDelta()
Definition: racamera.cpp:269
virtual void Reset()
Definition: racamera.cpp:441
#define KEY_IS_DOWN_MASK
Definition: raCamera.h:28
virtual void SetViewParams(raVector3 *pvEyePt, raVector3 *pvLookatPt)
Definition: racamera.cpp:68
raVector3 raVector3Normalize(const raVector3 &v)
Definition: raVector3.h:60
virtual long OnLMouseUp(HWND wnd, WPARAM wParam, LPARAM lParam)
Definition: racamera.cpp:162
raFloat z
Definition: raVector3.h:14
virtual void SetOrthoCamera(raVector2 *pvWindowSize, float fNearPlane, float fFarPlane)
Definition: racamera.cpp:106
raVector3 m_vEye
Definition: raCamera.h:213
float raFloat
Definition: raMain.h:106
virtual bool Intersects(const raVector3 *pRayPos, const raVector3 *pRayDir, float *pDist)
Definition: raRender.h:28
bool m_bRotateWithoutButtonDown
Definition: raCamera.h:260
raVector3 m_vVelocity
Definition: raCamera.h:219
virtual long OnRMouseUp(HWND wnd, WPARAM wParam, LPARAM lParam)
Definition: racamera.cpp:138
bool m_bEnableYAxisMovement
Definition: raCamera.h:236
void ConstrainToBoundary(raVector3 *pV)
Definition: racamera.cpp:369
virtual void FrameMove(float fTime, float FPS)
Definition: racamera.cpp:457
virtual long OnKeyUp(HWND wnd, WPARAM wParam, LPARAM lParam)
Definition: racamera.cpp:126
virtual void FrameMove(float fTime, float FPS)
Definition: racamera.cpp:540
raFloat raVector3Lenght(const raVector3 &v)
Definition: raVector3.h:59
virtual long OnKeyDown(HWND wnd, WPARAM wParam, LPARAM lParam)
Definition: racamera.cpp:110
void UpdateVelocity(float fElapsedTime)
Definition: racamera.cpp:316
raFloat raVector3LenghtSq(const raVector3 &v)
Definition: raVector3.h:58
virtual long OnLMouseDown(HWND wnd, WPARAM wParam, LPARAM lParam)
Definition: racamera.cpp:174
virtual long OnCaptureChange(HWND wnd, WPARAM wParam, LPARAM lParam)
Definition: racamera.cpp:208
int m_nActiveButtonMask
Definition: raCamera.h:259
raCamera()
Definition: racamera.cpp:15
virtual void SetProjParams(float fFOV, float fAspect, float fNearPlane, float fFarPlane)
Definition: racamera.cpp:96
float Distance
Definition: raCamera.h:267
raVector3 m_vLookAt
Definition: raCamera.h:214
raFloat y
Definition: raVector2.h:11
void GetInput(bool bGetKeyboardInput, bool bGetMouseInput, bool bGetGamepadInput, bool bResetCursorAfterMove)
Definition: racamera.cpp:236
BYTE m_aKeys[CAM_MAX_KEYS]
Definition: raCamera.h:200
raVector3 raVector3NormalizeEx(const raVector3 &v)
Definition: raVector3.h:61
float m_fNearPlane
Definition: raCamera.h:228
#define MOUSE_LEFT_BUTTON
Definition: raCamera.h:30
bool IsKeyDown(BYTE key) const
Definition: raCamera.h:175
virtual long OnMouseWheel(HWND wnd, WPARAM wParam, LPARAM lParam)
Definition: racamera.cpp:227
bool m_bResetCursorAfterMove
Definition: raCamera.h:242
#define PI
Definition: raMain.h:129
int m_nCurrentButtonMask
Definition: raCamera.h:206