tsStatic.h 6.7 KB

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