camera.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #ifndef _CAMERA_H_
  27. #define _CAMERA_H_
  28. #ifndef _SHAPEBASE_H_
  29. #include "T3D/shapeBase.h"
  30. #endif
  31. #ifndef _DYNAMIC_CONSOLETYPES_H_
  32. #include "console/dynamicTypes.h"
  33. #endif
  34. class CameraData: public ShapeBaseData
  35. {
  36. public:
  37. typedef ShapeBaseData Parent;
  38. // ShapeBaseData.
  39. DECLARE_CONOBJECT( CameraData );
  40. DECLARE_CATEGORY( "Game" );
  41. DECLARE_DESCRIPTION( "A datablock that describes a camera." );
  42. static void initPersistFields();
  43. virtual void packData(BitStream* stream);
  44. virtual void unpackData(BitStream* stream);
  45. };
  46. /// Implements a basic camera object.
  47. class Camera: public ShapeBase
  48. {
  49. public:
  50. typedef ShapeBase Parent;
  51. /// Movement behavior type for camera.
  52. enum CameraMotionMode
  53. {
  54. StationaryMode = 0,
  55. FreeRotateMode,
  56. FlyMode,
  57. OrbitObjectMode,
  58. OrbitPointMode,
  59. TrackObjectMode,
  60. OverheadMode,
  61. EditOrbitMode, ///< Used by the World Editor
  62. CameraFirstMode = 0,
  63. CameraLastMode = EditOrbitMode
  64. };
  65. /// The ExtendedMove position/rotation index used for camera movements
  66. static S32 smExtendedMovePosRotIndex;
  67. protected:
  68. enum MaskBits
  69. {
  70. MoveMask = Parent::NextFreeMask,
  71. UpdateMask = Parent::NextFreeMask << 1,
  72. NewtonCameraMask = Parent::NextFreeMask << 2,
  73. EditOrbitMask = Parent::NextFreeMask << 3,
  74. NextFreeMask = Parent::NextFreeMask << 4
  75. };
  76. struct StateDelta
  77. {
  78. Point3F pos;
  79. Point3F rot;
  80. VectorF posVec;
  81. VectorF rotVec;
  82. };
  83. CameraData* mDataBlock;
  84. Point3F mRot;
  85. StateDelta mDelta;
  86. Point3F mOffset;
  87. static F32 smMovementSpeed;
  88. SimObjectPtr<GameBase> mOrbitObject;
  89. F32 mMinOrbitDist;
  90. F32 mMaxOrbitDist;
  91. F32 mCurOrbitDist;
  92. Point3F mPosition;
  93. bool mObservingClientObject;
  94. F32 mLastAbsoluteYaw; ///< Stores that last absolute yaw value as passed in by ExtendedMove
  95. F32 mLastAbsolutePitch; ///< Stores that last absolute pitch value as passed in by ExtendedMove
  96. F32 mLastAbsoluteRoll; ///< Stores that last absolute roll value as passed in by ExtendedMove
  97. /// @name NewtonFlyMode
  98. /// @{
  99. VectorF mAngularVelocity;
  100. F32 mAngularForce;
  101. F32 mAngularDrag;
  102. VectorF mVelocity;
  103. bool mNewtonMode;
  104. bool mNewtonRotation;
  105. F32 mMass;
  106. F32 mDrag;
  107. F32 mFlyForce;
  108. F32 mSpeedMultiplier;
  109. F32 mBrakeMultiplier;
  110. /// @}
  111. /// @name EditOrbitMode
  112. /// @{
  113. bool mValidEditOrbitPoint;
  114. Point3F mEditOrbitPoint;
  115. F32 mCurrentEditOrbitDist;
  116. /// @}
  117. bool mLocked;
  118. CameraMotionMode mMode;
  119. void _setPosition(const Point3F& pos,const Point3F& viewRot);
  120. void _setRenderPosition(const Point3F& pos,const Point3F& viewRot);
  121. void _validateEyePoint( F32 pos, MatrixF* mat );
  122. void _calcOrbitPoint( MatrixF* mat, const Point3F& rot );
  123. void _calcEditOrbitPoint( MatrixF *mat, const Point3F& rot );
  124. static bool _setModeField( void *object, const char *index, const char *data );
  125. static bool _setNewtonField( void *object, const char *index, const char *data );
  126. // ShapeBase.
  127. virtual F32 getCameraFov();
  128. virtual void setCameraFov( F32 fov );
  129. virtual F32 getDefaultCameraFov();
  130. virtual bool isValidCameraFov( F32 fov );
  131. virtual F32 getDamageFlash() const;
  132. virtual F32 getWhiteOut() const;
  133. virtual void setTransform( const MatrixF& mat );
  134. virtual void setRenderTransform( const MatrixF& mat );
  135. public:
  136. Camera();
  137. ~Camera();
  138. CameraMotionMode getMode() const { return mMode; }
  139. Point3F getPosition();
  140. Point3F getRotation() { return mRot; };
  141. void setRotation( const Point3F& viewRot );
  142. Point3F getOffset() { return mOffset; };
  143. void lookAt( const Point3F& pos);
  144. void setOffset( const Point3F& offset) { if( mOffset != offset ) mOffset = offset; setMaskBits( UpdateMask ); }
  145. void setFlyMode();
  146. void setOrbitMode( GameBase *obj, const Point3F& pos, const Point3F& rot, const Point3F& offset,
  147. F32 minDist, F32 maxDist, F32 curDist, bool ownClientObject, bool locked = false );
  148. void setTrackObject( GameBase *obj, const Point3F& offset);
  149. void onDeleteNotify( SimObject* obj );
  150. GameBase* getOrbitObject() { return(mOrbitObject); }
  151. bool isObservingClientObject() { return(mObservingClientObject); }
  152. /// @name NewtonFlyMode
  153. /// @{
  154. void setNewtonFlyMode();
  155. VectorF getVelocity() const { return mVelocity; }
  156. void setVelocity( const VectorF& vel );
  157. VectorF getAngularVelocity() const { return mAngularVelocity; }
  158. void setAngularVelocity( const VectorF& vel );
  159. bool isRotationDamped() {return mNewtonRotation;}
  160. void setAngularForce( F32 force ) {mAngularForce = force; setMaskBits(NewtonCameraMask);}
  161. void setAngularDrag( F32 drag ) {mAngularDrag = drag; setMaskBits(NewtonCameraMask);}
  162. void setMass( F32 mass ) {mMass = mass; setMaskBits(NewtonCameraMask);}
  163. void setDrag( F32 drag ) {mDrag = drag; setMaskBits(NewtonCameraMask);}
  164. void setFlyForce( F32 force ) {mFlyForce = force; setMaskBits(NewtonCameraMask);}
  165. void setSpeedMultiplier( F32 mul ) {mSpeedMultiplier = mul; setMaskBits(NewtonCameraMask);}
  166. void setBrakeMultiplier( F32 mul ) {mBrakeMultiplier = mul; setMaskBits(NewtonCameraMask);}
  167. /// @}
  168. /// @name EditOrbitMode
  169. /// @{
  170. void setEditOrbitMode();
  171. bool isEditOrbitMode() {return mMode == EditOrbitMode;}
  172. bool getValidEditOrbitPoint() { return mValidEditOrbitPoint; }
  173. void setValidEditOrbitPoint( bool state );
  174. Point3F getEditOrbitPoint() const;
  175. void setEditOrbitPoint( const Point3F& pnt );
  176. /// Orient the camera to view the given radius. Requires that an
  177. /// edit orbit point has been set.
  178. void autoFitRadius( F32 radius );
  179. /// @}
  180. // ShapeBase.
  181. static void initPersistFields();
  182. static void consoleInit();
  183. virtual void onEditorEnable();
  184. virtual void onEditorDisable();
  185. virtual bool onAdd();
  186. virtual void onRemove();
  187. virtual bool onNewDataBlock( GameBaseData *dptr, bool reload );
  188. virtual void processTick( const Move* move );
  189. virtual void interpolateTick( F32 delta);
  190. virtual void getCameraTransform( F32* pos,MatrixF* mat );
  191. virtual void getEyeCameraTransform( IDisplayDevice *display, U32 eyeId, MatrixF *outMat );
  192. virtual void writePacketData( GameConnection* conn, BitStream* stream );
  193. virtual void readPacketData( GameConnection* conn, BitStream* stream );
  194. virtual U32 packUpdate( NetConnection* conn, U32 mask, BitStream* stream );
  195. virtual void unpackUpdate( NetConnection* conn, BitStream* stream );
  196. DECLARE_CONOBJECT( Camera );
  197. DECLARE_CATEGORY( "Game" );
  198. DECLARE_DESCRIPTION( "Represents a position, direction and field of view to render a scene from." );
  199. static F32 getMovementSpeed() { return smMovementSpeed; }
  200. bool isCamera() const { return true; }
  201. //Not yet implemented
  202. GFXTexHandle getCameraRenderTarget() { return GFXTexHandle(); }
  203. };
  204. typedef Camera::CameraMotionMode CameraMotionMode;
  205. DefineEnumType( CameraMotionMode );
  206. #endif