camera.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _CAMERA_H_
  23. #define _CAMERA_H_
  24. #ifndef _SHAPEBASE_H_
  25. #include "T3D/shapeBase.h"
  26. #endif
  27. #ifndef _DYNAMIC_CONSOLETYPES_H_
  28. #include "console/dynamicTypes.h"
  29. #endif
  30. class CameraData: public ShapeBaseData
  31. {
  32. public:
  33. typedef ShapeBaseData Parent;
  34. // ShapeBaseData.
  35. DECLARE_CONOBJECT( CameraData );
  36. DECLARE_CATEGORY( "Game" );
  37. DECLARE_DESCRIPTION( "A datablock that describes a camera." );
  38. static void initPersistFields();
  39. virtual void packData(BitStream* stream);
  40. virtual void unpackData(BitStream* stream);
  41. };
  42. /// Implements a basic camera object.
  43. class Camera: public ShapeBase
  44. {
  45. public:
  46. typedef ShapeBase Parent;
  47. /// Movement behavior type for camera.
  48. enum CameraMotionMode
  49. {
  50. StationaryMode = 0,
  51. FreeRotateMode,
  52. FlyMode,
  53. OrbitObjectMode,
  54. OrbitPointMode,
  55. TrackObjectMode,
  56. OverheadMode,
  57. EditOrbitMode, ///< Used by the World Editor
  58. CameraFirstMode = 0,
  59. CameraLastMode = EditOrbitMode
  60. };
  61. protected:
  62. enum MaskBits
  63. {
  64. MoveMask = Parent::NextFreeMask,
  65. UpdateMask = Parent::NextFreeMask << 1,
  66. NewtonCameraMask = Parent::NextFreeMask << 2,
  67. EditOrbitMask = Parent::NextFreeMask << 3,
  68. NextFreeMask = Parent::NextFreeMask << 4
  69. };
  70. struct StateDelta
  71. {
  72. Point3F pos;
  73. Point3F rot;
  74. VectorF posVec;
  75. VectorF rotVec;
  76. };
  77. Point3F mRot;
  78. StateDelta mDelta;
  79. Point3F mOffset;
  80. static F32 smMovementSpeed;
  81. SimObjectPtr<GameBase> mOrbitObject;
  82. F32 mMinOrbitDist;
  83. F32 mMaxOrbitDist;
  84. F32 mCurOrbitDist;
  85. Point3F mPosition;
  86. bool mObservingClientObject;
  87. /// @name NewtonFlyMode
  88. /// @{
  89. VectorF mAngularVelocity;
  90. F32 mAngularForce;
  91. F32 mAngularDrag;
  92. VectorF mVelocity;
  93. bool mNewtonMode;
  94. bool mNewtonRotation;
  95. F32 mMass;
  96. F32 mDrag;
  97. F32 mFlyForce;
  98. F32 mSpeedMultiplier;
  99. F32 mBrakeMultiplier;
  100. /// @}
  101. /// @name EditOrbitMode
  102. /// @{
  103. bool mValidEditOrbitPoint;
  104. Point3F mEditOrbitPoint;
  105. F32 mCurrentEditOrbitDist;
  106. /// @}
  107. bool mLocked;
  108. CameraMotionMode mMode;
  109. void _setPosition(const Point3F& pos,const Point3F& viewRot);
  110. void _setRenderPosition(const Point3F& pos,const Point3F& viewRot);
  111. void _validateEyePoint( F32 pos, MatrixF* mat );
  112. void _calcOrbitPoint( MatrixF* mat, const Point3F& rot );
  113. void _calcEditOrbitPoint( MatrixF *mat, const Point3F& rot );
  114. static bool _setModeField( void *object, const char *index, const char *data );
  115. static bool _setNewtonField( void *object, const char *index, const char *data );
  116. // ShapeBase.
  117. virtual F32 getCameraFov();
  118. virtual void setCameraFov( F32 fov );
  119. virtual F32 getDefaultCameraFov();
  120. virtual bool isValidCameraFov( F32 fov );
  121. virtual F32 getDamageFlash() const;
  122. virtual F32 getWhiteOut() const;
  123. virtual void setTransform( const MatrixF& mat );
  124. virtual void setRenderTransform( const MatrixF& mat );
  125. public:
  126. Camera();
  127. ~Camera();
  128. CameraMotionMode getMode() const { return mMode; }
  129. Point3F getPosition();
  130. Point3F getRotation() { return mRot; };
  131. void setRotation( const Point3F& viewRot );
  132. Point3F getOffset() { return mOffset; };
  133. void lookAt( const Point3F& pos);
  134. void setOffset( const Point3F& offset) { if( mOffset != offset ) mOffset = offset; setMaskBits( UpdateMask ); }
  135. void setFlyMode();
  136. void setOrbitMode( GameBase *obj, const Point3F& pos, const Point3F& rot, const Point3F& offset,
  137. F32 minDist, F32 maxDist, F32 curDist, bool ownClientObject, bool locked = false );
  138. void setTrackObject( GameBase *obj, const Point3F& offset);
  139. void onDeleteNotify( SimObject* obj );
  140. GameBase* getOrbitObject() { return(mOrbitObject); }
  141. bool isObservingClientObject() { return(mObservingClientObject); }
  142. /// @name NewtonFlyMode
  143. /// @{
  144. void setNewtonFlyMode();
  145. VectorF getVelocity() const { return mVelocity; }
  146. void setVelocity( const VectorF& vel );
  147. VectorF getAngularVelocity() const { return mAngularVelocity; }
  148. void setAngularVelocity( const VectorF& vel );
  149. bool isRotationDamped() {return mNewtonRotation;}
  150. void setAngularForce( F32 force ) {mAngularForce = force; setMaskBits(NewtonCameraMask);}
  151. void setAngularDrag( F32 drag ) {mAngularDrag = drag; setMaskBits(NewtonCameraMask);}
  152. void setMass( F32 mass ) {mMass = mass; setMaskBits(NewtonCameraMask);}
  153. void setDrag( F32 drag ) {mDrag = drag; setMaskBits(NewtonCameraMask);}
  154. void setFlyForce( F32 force ) {mFlyForce = force; setMaskBits(NewtonCameraMask);}
  155. void setSpeedMultiplier( F32 mul ) {mSpeedMultiplier = mul; setMaskBits(NewtonCameraMask);}
  156. void setBrakeMultiplier( F32 mul ) {mBrakeMultiplier = mul; setMaskBits(NewtonCameraMask);}
  157. /// @}
  158. /// @name EditOrbitMode
  159. /// @{
  160. void setEditOrbitMode();
  161. bool isEditOrbitMode() {return mMode == EditOrbitMode;}
  162. bool getValidEditOrbitPoint() { return mValidEditOrbitPoint; }
  163. void setValidEditOrbitPoint( bool state );
  164. Point3F getEditOrbitPoint() const;
  165. void setEditOrbitPoint( const Point3F& pnt );
  166. /// Orient the camera to view the given radius. Requires that an
  167. /// edit orbit point has been set.
  168. void autoFitRadius( F32 radius );
  169. /// @}
  170. // ShapeBase.
  171. static void initPersistFields();
  172. static void consoleInit();
  173. virtual void onEditorEnable();
  174. virtual void onEditorDisable();
  175. virtual bool onAdd();
  176. virtual void onRemove();
  177. virtual void processTick( const Move* move );
  178. virtual void interpolateTick( F32 delta);
  179. virtual void getCameraTransform( F32* pos,MatrixF* mat );
  180. virtual void writePacketData( GameConnection* conn, BitStream* stream );
  181. virtual void readPacketData( GameConnection* conn, BitStream* stream );
  182. virtual U32 packUpdate( NetConnection* conn, U32 mask, BitStream* stream );
  183. virtual void unpackUpdate( NetConnection* conn, BitStream* stream );
  184. DECLARE_CONOBJECT( Camera );
  185. DECLARE_CATEGORY( "Game" );
  186. DECLARE_DESCRIPTION( "Represents a position, direction and field of view to render a scene from." );
  187. };
  188. typedef Camera::CameraMotionMode CameraMotionMode;
  189. DefineEnumType( CameraMotionMode );
  190. #endif