My Project
BaseCamera.h
1 #pragma once
2 #include "TileObject.h"
3 #include "PortalFrustum.h"
4 #include "CameraFrustum.h"
5 
6 namespace ParaEngine
7 {
8  class CBaseObject;
9  class CShapeAABB;
10  class CPortalFrustum;
11 
12  enum CameraMode{
13  CameraFollow = 1,
23 
24  CameraCamera = 10,
27  };
28 
29 
30  //-----------------------------------------------------------------------------
31  // Name: enum CharacterAndCameraKeys
32  // Desc: used by CCamera to map WM_KEYDOWN keys
33  //-----------------------------------------------------------------------------
34  enum CharacterAndCameraKeys
35  {
36  MOVE_LEFT = 0,
37  MOVE_RIGHT,
38  MOVE_FORWARD,
39  MOVE_BACKWARD,
40  SHIFT_RIGHT,
41  SHIFT_LEFT,
42  CHARACTER_JUMP,
43  CAM_LOCK,
44  CAM_RESET,
45  ZOOM_IN,
46  ZOOM_OUT,
47  FLY_DOWNWARD,
48  MAX_KEYS,
49  KEY_UNKNOWN = 0xFF
50  };
51 
52 #ifndef WM_MOUSEWHEEL
53 #define WM_MOUSEWHEEL 0x020A
54 #endif
55 
56 #define KEY_WAS_DOWN_MASK 0x80
57 #define KEY_IS_DOWN_MASK 0x01
58 
59 #define MOUSE_LEFT_BUTTON 0x01
60 #define MOUSE_MIDDLE_BUTTON 0x02
61 #define MOUSE_RIGHT_BUTTON 0x04
62 #define MOUSE_WHEEL 0x08
63 
64  //-----------------------------------------------------------------------------
65  // Name: class CBaseCamera
66  // Desc: Simple base camera class that moves and rotates. The base class
67  // records mouse and keyboard input for use by a derived class, and
68  // keeps common state.
69  //-----------------------------------------------------------------------------
70  class CBaseCamera : public CTileObject
71  {
72  public:
73  CBaseCamera();
74  // mapping from key id to key's direct input's scan code.
75  static BYTE m_scancode_key_map[MAX_KEYS];
76  public:
78  // implementation of IAttributeFields
79 
81  virtual int GetAttributeClassID(){return ATTRIBUTE_CLASSID_CBaseCamera;}
83  virtual const char* GetAttributeClassName(){static const char name[] = "CBaseCamera"; return name;}
85  virtual const char* GetAttributeClassDescription(){static const char desc[] = ""; return desc;}
87  virtual int InstallFields(CAttributeClass* pClass, bool bOverride);
88 
89  ATTRIBUTE_METHOD1(CBaseCamera, GetNearPlane_s, float*) {*p1 = cls->GetNearPlane(); return S_OK;}
90  ATTRIBUTE_METHOD1(CBaseCamera, SetNearPlane_s, float) {cls->SetNearPlane(p1); return S_OK;}
91 
92  ATTRIBUTE_METHOD1(CBaseCamera, GetFarPlane_s, float*) {*p1 = cls->GetFarPlane(); return S_OK;}
93  ATTRIBUTE_METHOD1(CBaseCamera, SetFarPlane_s, float) {cls->SetFarPlane(p1); return S_OK;}
94 
95  ATTRIBUTE_METHOD1(CBaseCamera, GetFieldOfView_s, float*) {*p1 = cls->GetFieldOfView(); return S_OK;}
96  ATTRIBUTE_METHOD1(CBaseCamera, SetFieldOfView_s, float) {cls->SetFieldOfView(p1); return S_OK;}
97 
98  ATTRIBUTE_METHOD1(CBaseCamera, GetAspectRatio_s, float*) {*p1 = cls->GetAspectRatio(); return S_OK;}
99  ATTRIBUTE_METHOD1(CBaseCamera, SetAspectRatio_s, float) {cls->SetAspectRatio(p1); return S_OK;}
100 
101  ATTRIBUTE_METHOD1(CBaseCamera, IsPerspectiveView_s, bool*) {*p1 = cls->IsPerspectiveView(); return S_OK;}
102  ATTRIBUTE_METHOD1(CBaseCamera, SetIsPerspectiveView_s, bool) {cls->SetIsPerspectiveView(p1); return S_OK;}
103 
104  ATTRIBUTE_METHOD1(CBaseCamera, GetOrthoWidth_s, float*) {*p1 = cls->GetOrthoWidth(); return S_OK;}
105  ATTRIBUTE_METHOD1(CBaseCamera, SetOrthoWidth_s, float) {cls->SetOrthoWidth(p1); return S_OK;}
106 
107  ATTRIBUTE_METHOD1(CBaseCamera, GetOrthoHeight_s, float*) {*p1 = cls->GetOrthoHeight(); return S_OK;}
108  ATTRIBUTE_METHOD1(CBaseCamera, SetOrthoHeight_s, float) {cls->SetOrthoHeight(p1); return S_OK;}
109 
110  ATTRIBUTE_METHOD1(CBaseCamera, GetMoveScaler_s, float*) {*p1 = cls->GetMoveScaler(); return S_OK;}
111  ATTRIBUTE_METHOD1(CBaseCamera, SetMoveScaler_s, float) {cls->SetMoveScaler(p1); return S_OK;}
112 
113  ATTRIBUTE_METHOD1(CBaseCamera, GetRotationScaler_s, float*) {*p1 = cls->GetRotationScaler(); return S_OK;}
114  ATTRIBUTE_METHOD1(CBaseCamera, SetRotationScaler_s, float) {cls->SetRotationScaler(p1); return S_OK;}
115 
116 
117  ATTRIBUTE_METHOD1(CBaseCamera, IsShiftMoveSwitched_s, bool*) {*p1 = cls->IsShiftMoveSwitched(); return S_OK;}
118  ATTRIBUTE_METHOD1(CBaseCamera, SetShiftMoveSwitched_s, bool) {cls->SetShiftMoveSwitched(p1); return S_OK;}
119 
120  ATTRIBUTE_METHOD1(CBaseCamera, IsInvertPitch_s, bool*) {*p1 = cls->IsInvertPitch(); return S_OK;}
121  ATTRIBUTE_METHOD1(CBaseCamera, SetInvertPitch_s, bool) {cls->SetInvertPitch(p1); return S_OK;}
122 
123  ATTRIBUTE_METHOD1(CBaseCamera, GetNumberOfFramesToSmoothMouseData_s, int*) {*p1 = cls->GetNumberOfFramesToSmoothMouseData(); return S_OK;}
124  ATTRIBUTE_METHOD1(CBaseCamera, SetNumberOfFramesToSmoothMouseData_s, int) {cls->SetNumberOfFramesToSmoothMouseData(p1); return S_OK;}
125 
126  ATTRIBUTE_METHOD1(CBaseCamera, GetEyePosition_s, DVector3*) {*p1 = cls->GetEyePosition(); return S_OK;}
127  ATTRIBUTE_METHOD1(CBaseCamera, GetLookAtPosition_s, DVector3*) {*p1 =cls->GetLookAtPosition(); return S_OK;}
128 
129 
130  ATTRIBUTE_METHOD1(CBaseCamera, GetMovementDrag_s, bool*) {*p1 = cls->GetMovementDrag(); return S_OK;}
131  ATTRIBUTE_METHOD1(CBaseCamera, SetMovementDrag_s, bool) {cls->SetMovementDrag(p1); return S_OK;}
132 
133  ATTRIBUTE_METHOD1(CBaseCamera, GetTotalDragTime_s, float*) {*p1 = cls->GetTotalDragTime(); return S_OK;}
134  ATTRIBUTE_METHOD1(CBaseCamera, SetTotalDragTime_s, float) {cls->SetTotalDragTime(p1); return S_OK;}
135 
136  ATTRIBUTE_METHOD(CBaseCamera, FrameMove_s) {cls->FrameMove(0.f); return S_OK;}
137 
138 
139  DEFINE_SCRIPT_EVENT(CBaseCamera, FrameMove);
140 
141  // Call these from client and use Get*Matrix() to read new matrices
142  virtual LRESULT HandleMessages( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
143  virtual void FrameMove( FLOAT fElapsedTime );
144 
146  void UpdateFrustum();
147 
149  inline CCameraFrustum* GetFrustum(){return &m_frustum;};
150 
153 
158 
163 
165  bool CanSeeObject (IViewClippingObject* pObj);
166 
168  inline Plane& GetFogPlane() {return m_fog_plane;};
169 
171  virtual void Reset();
172  virtual void SetViewParams(const DVector3& vEyePt, const DVector3& vLookatPt);
173  void SetProjParams(FLOAT fFOV, FLOAT fAspect, FLOAT fNearPlane, FLOAT fFarPlane);
174 
175  virtual void SetFollowTarget(CBaseObject* pObj) {};
176  virtual CBaseObject* GetFollowTarget(){ return NULL; };
177  virtual CameraMode GetCameraMode() {return CameraFollowFirstPerson; }
178 
184  void UpdateProjParams();
185  void UpdateProjParams(FLOAT fAspect);
186 
187  // Functions to change behavior
188  VOID SetInvertPitch( bool bInvertPitch );
189  bool IsInvertPitch(){return m_bInvertPitch;}
190  VOID SetDrag( bool bMovementDrag, FLOAT fTotalDragTimeToZero = 0.25f ) { m_bMovementDrag = bMovementDrag; m_fTotalDragTimeToZero = fTotalDragTimeToZero; }
191  VOID SetEnableYAxisMovement( bool bEnableYAxisMovement ) { m_bEnableYAxisMovement = bEnableYAxisMovement; }
192  VOID SetEnablePositionMovement( bool bEnablePositionMovement ) { m_bEnablePositionMovement = bEnablePositionMovement; }
193  VOID SetClipToBoundary( bool bClipToBoundary, Vector3* pvMinBoundary, Vector3* pvMaxBoundary ) { m_bClipToBoundary = bClipToBoundary; if( pvMinBoundary ) m_vMinBoundary = *pvMinBoundary; if( pvMaxBoundary ) m_vMaxBoundary = *pvMaxBoundary; }
194  VOID SetScalers( FLOAT fRotationScaler = 0.01f, FLOAT fMoveScaler = 5.0f ) { m_fRotationScaler = fRotationScaler; m_fMoveScaler = fMoveScaler; }
195 
196  VOID SetRotationScaler( FLOAT fRotationScaler = 0.01f);
197  float GetRotationScaler();
198  VOID SetMoveScaler( FLOAT fMoveScaler = 5.0f);
199  float GetMoveScaler();
200 
201  VOID SetNumberOfFramesToSmoothMouseData( int nFrames ) { if( nFrames > 0 ) m_fFramesToSmoothMouseData = (float)nFrames; }
202  int GetNumberOfFramesToSmoothMouseData( ) { return (int)m_fFramesToSmoothMouseData; }
203  VOID SetResetCursorAfterMove( bool bResetCursorAfterMove ) { m_bResetCursorAfterMove = bResetCursorAfterMove; }
205  bool IsShiftMoveSwitched();
207  void SetShiftMoveSwitched(bool bSwitched = false);
208 
209  // Functions to get state
210  inline Matrix4* GetViewMatrix() { return &m_mView; }
211  inline Matrix4* GetProjMatrix() { return &m_mProj; }
212  inline bool IsBeingDragged() { return (m_bMouseLButtonDown || m_bMouseMButtonDown || m_bMouseRButtonDown); }
213  inline bool IsMouseLButtonDown() { return m_bMouseLButtonDown; }
214  inline bool IsMouseMButtonDown() { return m_bMouseMButtonDown; }
215  inline bool IsMouseRButtonDown() { return m_bMouseRButtonDown; }
216 
217  void GetMouseRay(Vector3& vPickRayOrig, Vector3& vPickRayDir, POINT ptCursor, UINT nWidth, UINT nHeight, const Matrix4* pMatWorld);
218 
219  // Get camera parameters
220  inline DVector3 GetEyePosition(){return m_vEye;};
221  inline DVector3 GetLookAtPosition(){return m_vLookAt;};
222  inline float GetFieldOfView(){return m_fFOV;};
223  inline float GetAspectRatio(){return m_fAspect;};
224  inline float GetNearPlane(){return m_fNearPlane;};
225  inline float GetFarPlane(){return m_fFarPlane;};
226  inline float GetOrthoHeight(){return m_fOrthoHeight;};
227  inline float GetOrthoWidth(){return m_fOrthoWidth;};
228 
230  inline bool IsPerspectiveView() { return m_bIsPerspectiveView; }
232  void SetIsPerspectiveView(bool bIsPerspectiveView);
233 
235  void SetFieldOfView(float fFieldofview);
237  void SetNearPlane(float fDist);
239  void SetFarPlane(float fDist);
241  void SetAspectRatio(float fAspect);
243  void SetOrthoWidth(float fWidth);
245  void SetOrthoHeight(float fHeight);
246 
248  void CopyCameraParamsFrom(CBaseCamera* pFromCamera);
249 
250  // Functions to get state
251  inline Vector3 GetWorldRight() { return Vector3( m_mCameraWorld._11, m_mCameraWorld._12, m_mCameraWorld._13 ); }
252  inline Vector3 GetWorldUp() { return Vector3( m_mCameraWorld._21, m_mCameraWorld._22, m_mCameraWorld._23 ); }
253  inline Vector3 GetWorldAhead() { return Vector3( m_mCameraWorld._31, m_mCameraWorld._32, m_mCameraWorld._33 ); }
254  inline Vector3 GetEyePt() { return Vector3( m_mCameraWorld._41, m_mCameraWorld._42, m_mCameraWorld._43 ); }
255 
256  virtual Vector3 GetRenderEyePosition();
257  // we can alter key map at runtime
258  static void SetKeyMap(CharacterAndCameraKeys key, BYTE scancode);
259  // get scancode from key id
260  static BYTE GetKeyMap(CharacterAndCameraKeys key);
261 
262  protected:
263 
264  // Functions to map a WM_KEYDOWN key to a CharacterAndCameraKeysenum
265  virtual CharacterAndCameraKeys MapKey( UINT nKey );
266  BOOL IsKeyDown( BYTE key ) { return( (key & KEY_IS_DOWN_MASK) == KEY_IS_DOWN_MASK ); }
267  BOOL WasKeyDown( BYTE key ) { return( (key & KEY_WAS_DOWN_MASK) == KEY_WAS_DOWN_MASK ); }
268 
270  void ConstrainToBoundary( DVector3* pV);
271  void UpdateMouseDelta( float fElapsedTime );
276  void UpdateMouseDelta( int nDX, int nDY);
277  void UpdateVelocity( float fElapsedTime );
278 
279 
284  void SetMovementDrag(bool bEnable);
285  bool GetMovementDrag();
286 
291  void SetTotalDragTime(float fTime);
292  float GetTotalDragTime();
293 
296 
299 
302 
305 
310 
317 
318  Matrix4 m_mView; // View matrix
319  Matrix4 m_mProj; // Projection matrix
320 
321  BYTE m_aKeys[MAX_KEYS]; // State of input - KEY_WAS_DOWN_MASK|KEY_IS_DOWN_MASK
322  POINT m_ptLastMousePosition; // Last absolute position of mouse cursor
323  bool m_bMouseLButtonDown; // True if left button is down
324  bool m_bMouseMButtonDown; // True if middle button is down
325  bool m_bMouseRButtonDown; // True if right button is down
326  int m_nCurrentButtonMask; // mask of which buttons are down
327  int m_nMouseWheelDelta; // Amount of middle wheel scroll (+/-)
328  Vector2 m_vMouseDelta; // Mouse relative delta smoothed over a few frames
329  float m_fFramesToSmoothMouseData; // Number of frames to smooth mouse data over
330 
331  DVector3 m_vDefaultEye; // Default camera eye position
332  DVector3 m_vDefaultLookAt; // Default LookAt position
333  DVector3 m_vEye; // Camera eye position
334  DVector3 m_vLookAt; // LookAt position
335  float m_fCameraYawAngle; // Yaw angle of camera
336  float m_fCameraPitchAngle; // Pitch angle of camera
337 
338  Vector3 m_vVelocity; // Velocity of camera
339  bool m_bMovementDrag; // If true, then camera movement will slow to a stop otherwise movement is instant
340  Vector3 m_vVelocityDrag; // Velocity drag force
341  FLOAT m_fDragTimer; // Countdown timer to apply drag
342  FLOAT m_fTotalDragTimeToZero; // Time it takes for velocity to go from full to 0
343  Vector2 m_vRotVelocity; // Velocity of camera
344 
345  float m_fFOV; // Field of view
346  float m_fAspect; // Aspect ratio
347  float m_fNearPlane; // Near plane
348  float m_fFarPlane; // Far plane
349  float m_fOrthoWidth; // width of the orthographic view volumn
350  float m_fOrthoHeight; // height of the orthographic view volumn
351  bool m_bIsPerspectiveView; // whether to use perspective view.
352 
353  float m_fRotationScaler; // Scaler for rotation
354  float m_fMoveScaler; // Scaler for movement
355 
356  bool m_bInvertPitch; // Invert the pitch axis
357  bool m_bEnablePositionMovement; // If true, then the user can translate the camera/model
358  bool m_bEnableYAxisMovement; // If true, then camera can move in the y-axis
359 
360  bool m_bClipToBoundary; // If true, then the camera will be clipped to the boundary
361  DVector3 m_vMinBoundary; // Min point in clip boundary
362  DVector3 m_vMaxBoundary; // Max point in clip boundary
363 
364  bool m_bResetCursorAfterMove;// If true, the class will reset the cursor position so that the cursor always has space to move
365 
366  Matrix4 m_mCameraWorld; // World matrix of the camera (inverse of the view matrix)
367  };
368 
369  //-----------------------------------------------------------------------------
370  // Name: class CFirstPersonCamera
371  // Desc: Simple first person camera class that moves and rotates.
372  // It allows yaw and pitch but not roll. It uses WM_KEYDOWN and
373  // GetCursorPos() to respond to keyboard and mouse input and updates the
374  // view matrix based on input.
375  //-----------------------------------------------------------------------------
377  {
378  public:
380 
381  // Call these from client and use Get*Matrix() to read new matrices
382  virtual void FrameMove( float fElapsedTime );
383 
384 
385  protected:
386  //Matrix4 m_mCameraWorld; // World matrix of the camera (inverse of the view matrix)
387 
388  };
389 }
bool IsPerspectiveView()
get whether we use perspective view or orthogonal view
Definition: BaseCamera.h:230
void SetOrthoWidth(float fWidth)
change orthographic width of view volumn.
Definition: BaseCamera.cpp:193
void SetAspectRatio(float fAspect)
change the aspect ratio.
Definition: BaseCamera.cpp:199
CCameraFrustum * GetShadowFrustum()
get the shadow view frustum info shadow view frustum is smaller than m_frustum_object; its far plane ...
Definition: BaseCamera.h:162
3-dimensional vector with double precision.
Definition: ParaDVector3.h:17
an attribute class is a collection of attribute fields.
Definition: AttributeClass.h:10
First person view of the Target object.
Definition: BaseCamera.h:15
void SetTotalDragTime(float fTime)
Time it takes for velocity to go from full to 0.
Definition: BaseCamera.cpp:625
Definition: BaseCamera.h:376
bool CanSeeObject(IViewClippingObject *pObj)
test again view frustum
Definition: BaseCamera.cpp:803
virtual int GetAttributeClassID()
attribute class ID should be identical, unless one knows how overriding rules work.
Definition: BaseCamera.h:81
Definition: combase.h:159
Pure interface for CBaseObject (3D scene object) It defines basic shapes and collision detection...
Definition: IViewClippingObject.h:29
different physics engine has different winding order.
Definition: EventBinding.h:32
void SetFarPlane(float fDist)
change the far plane.
Definition: BaseCamera.cpp:223
Definition: PEtypes.h:116
DVector3 m_vEyeReferencePoint
: the eye position is always added by the m_vEyeReferencePoint when changed it is initialized to be (...
Definition: BaseCamera.h:316
CameraMode
Definition: BaseCamera.h:12
CCameraFrustum m_frustum_shadow
shadow view frustum, this is smaller than m_frustum_object; its far plane is set at a fixed size...
Definition: BaseCamera.h:301
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
void SetFieldOfView(float fFieldofview)
change the field of view .
Definition: BaseCamera.cpp:208
CPortalFrustum m_frustum_portal
portal frustum with many planes formed by the portal vertices and the camera eye position.
Definition: BaseCamera.h:304
Definition: BaseCamera.h:70
void UpdateProjParams()
update the projection matrix using the current field of view and near and far plane values...
Definition: BaseCamera.cpp:177
void GetMouseRay(Vector3 &vPickRayOrig, Vector3 &vPickRayDir, POINT ptCursor, UINT nWidth, UINT nHeight, const Matrix4 *pMatWorld)
Desc: Get the mouse pay for object picking [in] fCursor: mouse position [in] nWidth, nHeight: Device Screen size [in] world transform matrix.
Definition: BaseCamera.cpp:703
Standard 2-dimensional vector.
Definition: ParaVector2.h:16
void SetMovementDrag(bool bEnable)
If true, then camera movement will slow to a stop otherwise movement is instant.
Definition: BaseCamera.cpp:615
void UpdateFrustum()
update the culling planes according to view and projection matrix on top of the global stack ...
Definition: BaseCamera.cpp:738
virtual void Reset()
Reset the camera's position back to the default.
Definition: BaseCamera.cpp:513
virtual int InstallFields(CAttributeClass *pClass, bool bOverride)
this class should be implemented if one wants to add new attribute.
Definition: BaseCamera.cpp:584
Specialized frustum shaped culling volume that has culling planes created from portals This isn't rea...
Definition: PortalFrustum.h:61
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: ParaMatrix4.h:23
Camera as a first person.
Definition: BaseCamera.h:26
Defines a plane in 3D space.
Definition: ParaPlane.h:23
void SetIsPerspectiveView(bool bIsPerspectiveView)
set whether we use perspective view or orthogonal view
Definition: BaseCamera.cpp:171
CCameraFrustum m_frustum_object
object view frustum, this is smaller than m_frustum; its far plane is set at fog end ...
Definition: BaseCamera.h:298
CCameraFrustum m_frustum
camera view frustum
Definition: BaseCamera.h:295
virtual const char * GetAttributeClassDescription()
a static string, describing the attribute class object
Definition: BaseCamera.h:85
virtual const char * GetAttributeClassName()
a static string, describing the attribute class object's name
Definition: BaseCamera.h:83
Third person view, allow rotation, character centered.
Definition: BaseCamera.h:17
CCameraFrustum * GetFrustum()
get the camera view frustum info
Definition: BaseCamera.h:149
bool IsShiftMoveSwitched()
whether the AD and QE key are switched for character movement.
Definition: BaseCamera.cpp:670
this value is used for the end of Camera follow modes.
Definition: BaseCamera.h:19
Plane & GetFogPlane()
test to see if any of the vertices are inside the fog plane
Definition: BaseCamera.h:168
Plane m_fog_plane
this is the fog end plane, it is plane with normal of the eye vector and distance of the fog range be...
Definition: BaseCamera.h:309
CCameraFrustum * GetObjectFrustum()
get the object view frustum info object view frustum is smaller than m_frustum; its far plane is set ...
Definition: BaseCamera.h:157
Tile Object have position and bounding rect and can usually be attached to quad-tree terrain tile...
Definition: TileObject.h:10
void SetNearPlane(float fDist)
change the near plane.
Definition: BaseCamera.cpp:217
Defines the base class of all scene elements:CBaseObject for Parallel World Engine.
Definition: BaseObject.h:230
: Third person view, disable rotation, character restricted to a rectangular, if character is reaches...
Definition: BaseCamera.h:22
void SetShiftMoveSwitched(bool bSwitched=false)
whether the AD and QE key are switched for character movement.
Definition: BaseCamera.cpp:675
void ConstrainToBoundary(DVector3 *pV)
Clamps pV to lie inside m_vMinBoundary & m_vMaxBoundary.
Definition: BaseCamera.cpp:459
void SetOrthoHeight(float fHeight)
change orthographic height of view volumn.
Definition: BaseCamera.cpp:187
CPortalFrustum * GetPortalFrustum()
get the camera view frustum info
Definition: BaseCamera.h:152
void CopyCameraParamsFrom(CBaseCamera *pFromCamera)
copy only basic parameters like projection and camera transforms, etc.
Definition: BaseCamera.cpp:100
the camera view culling frustum
Definition: CameraFrustum.h:10