camera.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. /// The ExtendedMove position/rotation index used for camera movements
  62. static S32 smExtendedMovePosRotIndex;
  63. protected:
  64. enum MaskBits
  65. {
  66. MoveMask = Parent::NextFreeMask,
  67. UpdateMask = Parent::NextFreeMask << 1,
  68. NewtonCameraMask = Parent::NextFreeMask << 2,
  69. EditOrbitMask = Parent::NextFreeMask << 3,
  70. NextFreeMask = Parent::NextFreeMask << 4
  71. };
  72. struct StateDelta
  73. {
  74. Point3F pos;
  75. Point3F rot;
  76. VectorF posVec;
  77. VectorF rotVec;
  78. };
  79. CameraData* mDataBlock;
  80. Point3F mRot;
  81. StateDelta mDelta;
  82. Point3F mOffset;
  83. static F32 smMovementSpeed;
  84. SimObjectPtr<GameBase> mOrbitObject;
  85. F32 mMinOrbitDist;
  86. F32 mMaxOrbitDist;
  87. F32 mCurOrbitDist;
  88. Point3F mPosition;
  89. bool mObservingClientObject;
  90. F32 mLastAbsoluteYaw; ///< Stores that last absolute yaw value as passed in by ExtendedMove
  91. F32 mLastAbsolutePitch; ///< Stores that last absolute pitch value as passed in by ExtendedMove
  92. F32 mLastAbsoluteRoll; ///< Stores that last absolute roll value as passed in by ExtendedMove
  93. /// @name NewtonFlyMode
  94. /// @{
  95. VectorF mAngularVelocity;
  96. F32 mAngularForce;
  97. F32 mAngularDrag;
  98. VectorF mVelocity;
  99. bool mNewtonMode;
  100. bool mNewtonRotation;
  101. F32 mMass;
  102. F32 mDrag;
  103. F32 mFlyForce;
  104. F32 mSpeedMultiplier;
  105. F32 mBrakeMultiplier;
  106. /// @}
  107. /// @name EditOrbitMode
  108. /// @{
  109. bool mValidEditOrbitPoint;
  110. Point3F mEditOrbitPoint;
  111. F32 mCurrentEditOrbitDist;
  112. /// @}
  113. bool mLocked;
  114. CameraMotionMode mMode;
  115. void _setPosition(const Point3F& pos,const Point3F& viewRot);
  116. void _setRenderPosition(const Point3F& pos,const Point3F& viewRot);
  117. void _validateEyePoint( F32 pos, MatrixF* mat );
  118. void _calcOrbitPoint( MatrixF* mat, const Point3F& rot );
  119. void _calcEditOrbitPoint( MatrixF *mat, const Point3F& rot );
  120. static bool _setModeField( void *object, const char *index, const char *data );
  121. static bool _setNewtonField( void *object, const char *index, const char *data );
  122. // ShapeBase.
  123. virtual F32 getCameraFov();
  124. virtual void setCameraFov( F32 fov );
  125. virtual F32 getDefaultCameraFov();
  126. virtual bool isValidCameraFov( F32 fov );
  127. virtual F32 getDamageFlash() const;
  128. virtual F32 getWhiteOut() const;
  129. virtual void setTransform( const MatrixF& mat );
  130. virtual void setRenderTransform( const MatrixF& mat );
  131. public:
  132. Camera();
  133. ~Camera();
  134. CameraMotionMode getMode() const { return mMode; }
  135. Point3F getPosition();
  136. Point3F getRotation() { return mRot; };
  137. void setRotation( const Point3F& viewRot );
  138. Point3F getOffset() { return mOffset; };
  139. void lookAt( const Point3F& pos);
  140. void setOffset( const Point3F& offset) { if( mOffset != offset ) mOffset = offset; setMaskBits( UpdateMask ); }
  141. void setFlyMode();
  142. void setOrbitMode( GameBase *obj, const Point3F& pos, const Point3F& rot, const Point3F& offset,
  143. F32 minDist, F32 maxDist, F32 curDist, bool ownClientObject, bool locked = false );
  144. void setTrackObject( GameBase *obj, const Point3F& offset);
  145. void onDeleteNotify( SimObject* obj );
  146. GameBase* getOrbitObject() { return(mOrbitObject); }
  147. bool isObservingClientObject() { return(mObservingClientObject); }
  148. /// @name NewtonFlyMode
  149. /// @{
  150. void setNewtonFlyMode();
  151. VectorF getVelocity() const { return mVelocity; }
  152. void setVelocity( const VectorF& vel );
  153. VectorF getAngularVelocity() const { return mAngularVelocity; }
  154. void setAngularVelocity( const VectorF& vel );
  155. bool isRotationDamped() {return mNewtonRotation;}
  156. void setAngularForce( F32 force ) {mAngularForce = force; setMaskBits(NewtonCameraMask);}
  157. void setAngularDrag( F32 drag ) {mAngularDrag = drag; setMaskBits(NewtonCameraMask);}
  158. void setMass( F32 mass ) {mMass = mass; setMaskBits(NewtonCameraMask);}
  159. void setDrag( F32 drag ) {mDrag = drag; setMaskBits(NewtonCameraMask);}
  160. void setFlyForce( F32 force ) {mFlyForce = force; setMaskBits(NewtonCameraMask);}
  161. void setSpeedMultiplier( F32 mul ) {mSpeedMultiplier = mul; setMaskBits(NewtonCameraMask);}
  162. void setBrakeMultiplier( F32 mul ) {mBrakeMultiplier = mul; setMaskBits(NewtonCameraMask);}
  163. /// @}
  164. /// @name EditOrbitMode
  165. /// @{
  166. void setEditOrbitMode();
  167. bool isEditOrbitMode() {return mMode == EditOrbitMode;}
  168. bool getValidEditOrbitPoint() { return mValidEditOrbitPoint; }
  169. void setValidEditOrbitPoint( bool state );
  170. Point3F getEditOrbitPoint() const;
  171. void setEditOrbitPoint( const Point3F& pnt );
  172. /// Orient the camera to view the given radius. Requires that an
  173. /// edit orbit point has been set.
  174. void autoFitRadius( F32 radius );
  175. /// @}
  176. // ShapeBase.
  177. static void initPersistFields();
  178. static void consoleInit();
  179. virtual void onEditorEnable();
  180. virtual void onEditorDisable();
  181. virtual bool onAdd();
  182. virtual void onRemove();
  183. virtual bool onNewDataBlock( GameBaseData *dptr, bool reload );
  184. virtual void processTick( const Move* move );
  185. virtual void interpolateTick( F32 delta);
  186. virtual void getCameraTransform( F32* pos,MatrixF* mat );
  187. virtual void getEyeCameraTransform( IDisplayDevice *display, U32 eyeId, MatrixF *outMat );
  188. virtual DisplayPose calcCameraDeltaPose(GameConnection *con, const DisplayPose& inPose);
  189. virtual void writePacketData( GameConnection* conn, BitStream* stream );
  190. virtual void readPacketData( GameConnection* conn, BitStream* stream );
  191. virtual U32 packUpdate( NetConnection* conn, U32 mask, BitStream* stream );
  192. virtual void unpackUpdate( NetConnection* conn, BitStream* stream );
  193. DECLARE_CONOBJECT( Camera );
  194. DECLARE_CATEGORY( "Game" );
  195. DECLARE_DESCRIPTION( "Represents a position, direction and field of view to render a scene from." );
  196. };
  197. typedef Camera::CameraMotionMode CameraMotionMode;
  198. DefineEnumType( CameraMotionMode );
  199. #endif