tsStatic.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 _TSSTATIC_H_
  23. #define _TSSTATIC_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _CONVEX_H_
  28. #include "collision/convex.h"
  29. #endif
  30. #ifndef __RESOURCE_H__
  31. #include "core/resource.h"
  32. #endif
  33. #ifndef _NETSTRINGTABLE_H_
  34. #include "sim/netStringTable.h"
  35. #endif
  36. #ifndef _TSSHAPE_H_
  37. #include "ts/tsShape.h"
  38. #endif
  39. #ifndef _REFLECTOR_H_
  40. #include "scene/reflector.h"
  41. #endif
  42. class TSShapeInstance;
  43. class TSThread;
  44. class TSStatic;
  45. class PhysicsBody;
  46. struct ObjectRenderInst;
  47. class TSStaticPolysoupConvex : public Convex
  48. {
  49. typedef Convex Parent;
  50. friend class TSMesh;
  51. public:
  52. TSStaticPolysoupConvex();
  53. ~TSStaticPolysoupConvex() {};
  54. public:
  55. Box3F box;
  56. Point3F verts[4];
  57. PlaneF normal;
  58. S32 idx;
  59. TSMesh *mesh;
  60. static SceneObject* smCurObject;
  61. public:
  62. // Returns the bounding box in world coordinates
  63. Box3F getBoundingBox() const;
  64. Box3F getBoundingBox(const MatrixF& mat, const Point3F& scale) const;
  65. void getFeatures(const MatrixF& mat,const VectorF& n, ConvexFeature* cf);
  66. // This returns a list of convex faces to collide against
  67. void getPolyList(AbstractPolyList* list);
  68. // This returns the furthest point from the input vector
  69. Point3F support(const VectorF& v) const;
  70. };
  71. /// A simple mesh shape with optional ambient animation.
  72. class TSStatic : public SceneObject
  73. {
  74. typedef SceneObject Parent;
  75. static U32 smUniqueIdentifier;
  76. enum MaskBits
  77. {
  78. TransformMask = Parent::NextFreeMask << 0,
  79. AdvancedStaticOptionsMask = Parent::NextFreeMask << 1,
  80. UpdateCollisionMask = Parent::NextFreeMask << 2,
  81. SkinMask = Parent::NextFreeFlag << 3,
  82. NextFreeMask = Parent::NextFreeMask << 4
  83. };
  84. public:
  85. void setAlphaFade(bool enable, F32 start, F32 end, bool inverse)
  86. {
  87. mUseAlphaFade = enable;
  88. mAlphaFadeStart = start;
  89. mAlphaFadeEnd = end;
  90. mInvertAlphaFade = inverse;
  91. }
  92. /// The different types of mesh data types
  93. enum MeshType
  94. {
  95. None = 0, ///< No mesh
  96. Bounds = 1, ///< Bounding box of the shape
  97. CollisionMesh = 2, ///< Specifically designated collision meshes
  98. VisibleMesh = 3 ///< Rendered mesh polygons
  99. };
  100. protected:
  101. bool mUseAlphaFade;
  102. F32 mAlphaFadeStart;
  103. F32 mAlphaFadeEnd;
  104. F32 mAlphaFade;
  105. bool mInvertAlphaFade;
  106. bool onAdd();
  107. void onRemove();
  108. // Collision
  109. void prepCollision();
  110. bool castRay(const Point3F &start, const Point3F &end, RayInfo* info);
  111. bool castRayRendered(const Point3F &start, const Point3F &end, RayInfo* info);
  112. bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF& sphere);
  113. void buildConvex(const Box3F& box, Convex* convex);
  114. bool _createShape();
  115. void _updatePhysics();
  116. void _renderNormals( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  117. void _onResourceChanged( const Torque::Path &path );
  118. // ProcessObject
  119. virtual void processTick( const Move *move );
  120. virtual void interpolateTick( F32 delta );
  121. virtual void advanceTime( F32 dt );
  122. /// Start or stop processing ticks depending on our state.
  123. void _updateShouldTick();
  124. String cubeDescName;
  125. U32 cubeDescId;
  126. ReflectorDesc *reflectorDesc;
  127. CubeReflector mCubeReflector;
  128. protected:
  129. Convex *mConvexList;
  130. StringTableEntry mShapeName;
  131. U32 mShapeHash;
  132. Resource<TSShape> mShape;
  133. Vector<S32> mCollisionDetails;
  134. Vector<S32> mLOSDetails;
  135. TSShapeInstance *mShapeInstance;
  136. NetStringHandle mSkinNameHandle;
  137. String mAppliedSkinName;
  138. bool mPlayAmbient;
  139. TSThread* mAmbientThread;
  140. /// The type of mesh data to return for collision queries.
  141. MeshType mCollisionType;
  142. /// The type of mesh data to return for decal polylist queries.
  143. MeshType mDecalType;
  144. bool mAllowPlayerStep;
  145. /// If true each submesh within the TSShape is culled
  146. /// against the object space frustum.
  147. bool mMeshCulling;
  148. /// If true the shape is sorted by the origin of the
  149. /// model instead of the nearest point of the bounds.
  150. bool mUseOriginSort;
  151. PhysicsBody *mPhysicsRep;
  152. // Debug stuff
  153. F32 mRenderNormalScalar;
  154. S32 mForceDetail;
  155. public:
  156. TSStatic();
  157. ~TSStatic();
  158. DECLARE_CONOBJECT(TSStatic);
  159. static void initPersistFields();
  160. static bool _setFieldSkin( void *object, const char* index, const char* data );
  161. static const char *_getFieldSkin( void *object, const char *data );
  162. // Skinning
  163. void setSkinName( const char *name );
  164. void reSkin();
  165. // NetObject
  166. U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream );
  167. void unpackUpdate( NetConnection *conn, BitStream *stream );
  168. // SceneObject
  169. void setTransform( const MatrixF &mat );
  170. void onScaleChanged();
  171. void prepRenderImage( SceneRenderState *state );
  172. void inspectPostApply();
  173. virtual void onMount( SceneObject *obj, S32 node );
  174. virtual void onUnmount( SceneObject *obj, S32 node );
  175. /// The type of mesh data use for collision queries.
  176. MeshType getCollisionType() const { return mCollisionType; }
  177. bool allowPlayerStep() const { return mAllowPlayerStep; }
  178. Resource<TSShape> getShape() const { return mShape; }
  179. StringTableEntry getShapeFileName() { return mShapeName; }
  180. void setShapeFileName(StringTableEntry shapeName) { mShapeName = shapeName; }
  181. TSShapeInstance* getShapeInstance() const { return mShapeInstance; }
  182. const Vector<S32>& getCollisionDetails() const { return mCollisionDetails; }
  183. const Vector<S32>& getLOSDetails() const { return mLOSDetails; }
  184. };
  185. typedef TSStatic::MeshType TSMeshType;
  186. DefineEnumType( TSMeshType );
  187. #endif // _H_TSSTATIC