convexShape.h 8.5 KB

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