convexShape.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 _CONVEXSHAPE_H_
  23. #define _CONVEXSHAPE_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _GFXVERTEXBUFFER_H_
  28. #include "gfx/gfxVertexBuffer.h"
  29. #endif
  30. #ifndef _GFXPRIMITIVEBUFFER_H_
  31. #include "gfx/gfxPrimitiveBuffer.h"
  32. #endif
  33. #ifndef _CONVEX_H_
  34. #include "collision/convex.h"
  35. #endif
  36. class ConvexShape;
  37. // Crap name, but whatcha gonna do.
  38. class ConvexShapeCollisionConvex : public Convex
  39. {
  40. typedef Convex Parent;
  41. friend class ConvexShape;
  42. protected:
  43. ConvexShape *pShape;
  44. public:
  45. ConvexShapeCollisionConvex() { mType = ConvexShapeCollisionConvexType; }
  46. ConvexShapeCollisionConvex( const ConvexShapeCollisionConvex& cv ) {
  47. mType = ConvexShapeCollisionConvexType;
  48. mObject = cv.mObject;
  49. pShape = cv.pShape;
  50. }
  51. Point3F support(const VectorF& vec) const;
  52. void getFeatures(const MatrixF& mat,const VectorF& n, ConvexFeature* cf);
  53. void getPolyList(AbstractPolyList* list);
  54. };
  55. GFXDeclareVertexFormat( ConvexVert )
  56. {
  57. Point3F point;
  58. GFXVertexColor color;
  59. Point3F normal;
  60. Point3F tangent;
  61. Point2F texCoord;
  62. };
  63. class PhysicsBody;
  64. // Define our vertex format here so we don't have to
  65. // change it in multiple spots later
  66. typedef ConvexVert VertexType;
  67. class ConvexShape : public SceneObject
  68. {
  69. typedef SceneObject Parent;
  70. friend class GuiConvexEditorCtrl;
  71. friend class GuiConvexEditorUndoAction;
  72. friend class ConvexShapeCollisionConvex;
  73. public:
  74. enum NetBits {
  75. TransformMask = Parent::NextFreeMask,
  76. UpdateMask = Parent::NextFreeMask << 1,
  77. NextFreeMask = Parent::NextFreeMask << 2
  78. };
  79. // Declaring these structs directly within ConvexShape to prevent
  80. // the otherwise excessively deep scoping we had.
  81. // eg. ConvexShape::Face::Triangle ...
  82. struct Edge
  83. {
  84. U32 p0;
  85. U32 p1;
  86. };
  87. struct Triangle
  88. {
  89. U32 p0;
  90. U32 p1;
  91. U32 p2;
  92. U32 operator []( U32 index ) const
  93. {
  94. AssertFatal( index >= 0 && index <= 2, "index out of range" );
  95. return *( (&p0) + index );
  96. }
  97. };
  98. struct Face
  99. {
  100. Vector< Edge > edges;
  101. Vector< U32 > points;
  102. Vector< U32 > winding;
  103. Vector< Point2F > texcoords;
  104. Vector< Triangle > triangles;
  105. Point3F tangent;
  106. Point3F normal;
  107. Point3F centroid;
  108. F32 area;
  109. S32 id;
  110. };
  111. struct Geometry
  112. {
  113. void generate( const Vector< PlaneF > &planes, const Vector< Point3F > &tangents );
  114. Vector< Point3F > points;
  115. Vector< Face > faces;
  116. };
  117. static bool smRenderEdges;
  118. // To prevent bitpack overflows.
  119. // This is only indirectly enforced by trucation when serializing.
  120. static const S32 smMaxSurfaces = 100;
  121. public:
  122. ConvexShape();
  123. virtual ~ConvexShape();
  124. DECLARE_CONOBJECT( ConvexShape );
  125. // ConsoleObject
  126. static void initPersistFields();
  127. // SimObject
  128. virtual void inspectPostApply();
  129. virtual bool onAdd();
  130. virtual void onRemove();
  131. virtual void writeFields(Stream &stream, U32 tabStop);
  132. virtual bool writeField( StringTableEntry fieldname, const char *value );
  133. // NetObject
  134. virtual U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream );
  135. virtual void unpackUpdate( NetConnection *conn, BitStream *stream );
  136. // SceneObject
  137. virtual void onScaleChanged();
  138. virtual void setTransform( const MatrixF &mat );
  139. virtual void prepRenderImage( SceneRenderState *state );
  140. virtual void buildConvex( const Box3F &box, Convex *convex );
  141. virtual bool buildPolyList( PolyListContext context, AbstractPolyList *polyList, const Box3F &box, const SphereF &sphere );
  142. virtual bool castRay( const Point3F &start, const Point3F &end, RayInfo *info );
  143. virtual bool collideBox( const Point3F &start, const Point3F &end, RayInfo *info );
  144. void updateBounds( bool recenter );
  145. void recenter();
  146. /// Geometry access.
  147. /// @{
  148. MatrixF getSurfaceWorldMat( S32 faceid, bool scaled = false ) const;
  149. void cullEmptyPlanes( Vector< U32 > *removedPlanes );
  150. void exportToCollada();
  151. void resizePlanes( const Point3F &size );
  152. void getSurfaceLineList( S32 surfId, Vector< Point3F > &lineList );
  153. Geometry& getGeometry() { return mGeometry; }
  154. Vector<MatrixF>& getSurfaces() { return mSurfaces; }
  155. void getSurfaceTriangles( S32 surfId, Vector< Point3F > *outPoints, Vector< Point2F > *outCoords, bool worldSpace );
  156. /// @}
  157. /// Geometry Visualization.
  158. /// @{
  159. void renderFaceEdges( S32 faceid, const ColorI &color = ColorI::WHITE, F32 lineWidth = 1.0f );
  160. /// @}
  161. protected:
  162. void _updateMaterial();
  163. void _updateGeometry( bool updateCollision = false );
  164. void _updateCollision();
  165. void _export( OptimizedPolyList *plist, const Box3F &box, const SphereF &sphere );
  166. void _renderDebug( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *mat );
  167. static S32 QSORT_CALLBACK _comparePlaneDist( const void *a, const void *b );
  168. static bool protectedSetSurface( void *object, const char *index, const char *data );
  169. protected:
  170. // The name of the Material we will use for rendering
  171. String mMaterialName;
  172. // The actual Material instance
  173. BaseMatInstance* mMaterialInst;
  174. // The GFX vertex and primitive buffers
  175. GFXVertexBufferHandle< VertexType > mVertexBuffer;
  176. GFXPrimitiveBufferHandle mPrimitiveBuffer;
  177. U32 mVertCount;
  178. U32 mPrimCount;
  179. Geometry mGeometry;
  180. Vector< PlaneF > mPlanes;
  181. Vector< MatrixF > mSurfaces;
  182. Vector< Point3F > mFaceCenters;
  183. Convex *mConvexList;
  184. PhysicsBody *mPhysicsRep;
  185. /// Geometry visualization
  186. /// @{
  187. F32 mNormalLength;
  188. /// @}
  189. };
  190. #endif // _CONVEXSHAPE_H_