convexShape.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 surfaceMaterial
  112. {
  113. // The name of the Material we will use for rendering
  114. String materialName;
  115. // The actual Material instance
  116. BaseMatInstance* materialInst;
  117. surfaceMaterial()
  118. {
  119. materialName = "";
  120. materialInst = NULL;
  121. }
  122. };
  123. struct surfaceUV
  124. {
  125. S32 matID;
  126. Point2F offset;
  127. Point2F scale;
  128. float zRot;
  129. bool horzFlip;
  130. bool vertFlip;
  131. surfaceUV() : matID(0), offset(Point2F(0.0f, 0.0f)), scale(Point2F(1.0f, 1.0f)), zRot(0.0f), horzFlip(false), vertFlip(false) {}
  132. };
  133. struct surfaceBuffers
  134. {
  135. // The GFX vertex and primitive buffers
  136. GFXVertexBufferHandle< VertexType > mVertexBuffer;
  137. GFXPrimitiveBufferHandle mPrimitiveBuffer;
  138. U32 mVertCount;
  139. U32 mPrimCount;
  140. };
  141. struct Geometry
  142. {
  143. void generate(const Vector< PlaneF > &planes, const Vector< Point3F > &tangents, const Vector< surfaceMaterial > surfaceTextures, const Vector< Point2F > texOffset, const Vector< Point2F > texScale, const Vector< bool > horzFlip, const Vector< bool > vertFlip);
  144. Vector< Point3F > points;
  145. Vector< Face > faces;
  146. };
  147. static bool smRenderEdges;
  148. // To prevent bitpack overflows.
  149. // This is only indirectly enforced by trucation when serializing.
  150. static const S32 smMaxSurfaces = 100;
  151. public:
  152. ConvexShape();
  153. virtual ~ConvexShape();
  154. DECLARE_CONOBJECT( ConvexShape );
  155. // ConsoleObject
  156. static void initPersistFields();
  157. // SimObject
  158. virtual void inspectPostApply();
  159. virtual bool onAdd();
  160. virtual void onRemove();
  161. virtual void writeFields(Stream &stream, U32 tabStop);
  162. virtual bool writeField( StringTableEntry fieldname, const char *value );
  163. // NetObject
  164. virtual U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream );
  165. virtual void unpackUpdate( NetConnection *conn, BitStream *stream );
  166. // SceneObject
  167. virtual void onScaleChanged();
  168. virtual void setTransform( const MatrixF &mat );
  169. virtual void prepRenderImage( SceneRenderState *state );
  170. virtual void buildConvex( const Box3F &box, Convex *convex );
  171. virtual bool buildPolyList( PolyListContext context, AbstractPolyList *polyList, const Box3F &box, const SphereF &sphere );
  172. virtual bool buildExportPolyList(ColladaUtils::ExportData* exportData, const Box3F &box, const SphereF &);
  173. virtual bool castRay( const Point3F &start, const Point3F &end, RayInfo *info );
  174. virtual bool collideBox( const Point3F &start, const Point3F &end, RayInfo *info );
  175. void updateBounds( bool recenter );
  176. void recenter();
  177. /// Geometry access.
  178. /// @{
  179. MatrixF getSurfaceWorldMat( S32 faceid, bool scaled = false ) const;
  180. void cullEmptyPlanes( Vector< U32 > *removedPlanes );
  181. void exportToCollada();
  182. void resizePlanes( const Point3F &size );
  183. void getSurfaceLineList( S32 surfId, Vector< Point3F > &lineList );
  184. Geometry& getGeometry() { return mGeometry; }
  185. Vector<MatrixF>& getSurfaces() { return mSurfaces; }
  186. void getSurfaceTriangles( S32 surfId, Vector< Point3F > *outPoints, Vector< Point2F > *outCoords, bool worldSpace );
  187. /// @}
  188. /// Geometry Visualization.
  189. /// @{
  190. void renderFaceEdges( S32 faceid, const ColorI &color = ColorI::WHITE, F32 lineWidth = 1.0f );
  191. /// @}
  192. String getMaterialName() { return mMaterialName; }
  193. protected:
  194. void _updateMaterial();
  195. void _updateGeometry( bool updateCollision = false );
  196. void _updateCollision();
  197. void _export( OptimizedPolyList *plist, const Box3F &box, const SphereF &sphere );
  198. void _renderDebug( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *mat );
  199. static S32 QSORT_CALLBACK _comparePlaneDist( const void *a, const void *b );
  200. static bool protectedSetSurface( void *object, const char *index, const char *data );
  201. static bool protectedSetSurfaceTexture( void *object, const char *index, const char *data );
  202. static bool protectedSetSurfaceUV(void *object, const char *index, const char *data);
  203. protected:
  204. // The name of the Material we will use for rendering
  205. String mMaterialName;
  206. // The actual Material instance
  207. BaseMatInstance* mMaterialInst;
  208. // The GFX vertex and primitive buffers
  209. /*GFXVertexBufferHandle< VertexType > mVertexBuffer;
  210. GFXPrimitiveBufferHandle mPrimitiveBuffer;
  211. U32 mVertCount;
  212. U32 mPrimCount;*/
  213. Geometry mGeometry;
  214. Vector< PlaneF > mPlanes;
  215. Vector< MatrixF > mSurfaces;
  216. Vector< Point3F > mFaceCenters;
  217. //this is mostly for storage purposes, so we can save the texture mods
  218. Vector< surfaceMaterial > mSurfaceTextures;
  219. Vector< surfaceUV > mSurfaceUVs;
  220. Vector< surfaceBuffers > mSurfaceBuffers;
  221. Convex *mConvexList;
  222. PhysicsBody *mPhysicsRep;
  223. /// Geometry visualization
  224. /// @{
  225. F32 mNormalLength;
  226. /// @}
  227. };
  228. #endif // _CONVEXSHAPE_H_