convexShape.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 override;
  53. void getFeatures(const MatrixF& mat,const VectorF& n, ConvexFeature* cf) override;
  54. void getPolyList(AbstractPolyList* list) override;
  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. DECLARE_CATEGORY("Object \t Simple");
  158. // ConsoleObject
  159. static void initPersistFields();
  160. // SimObject
  161. void inspectPostApply() override;
  162. bool onAdd() override;
  163. void onRemove() override;
  164. void writeFields(Stream& stream, U32 tabStop) override;
  165. bool writeField(StringTableEntry fieldname, const char* value) override;
  166. U32 getSpecialFieldSize(StringTableEntry fieldName) override;
  167. const char* getSpecialFieldOut(StringTableEntry fieldName, const U32& index) override;
  168. // NetObject
  169. U32 packUpdate(NetConnection* conn, U32 mask, BitStream* stream) override;
  170. void unpackUpdate(NetConnection* conn, BitStream* stream) override;
  171. // SceneObject
  172. void onScaleChanged() override;
  173. void setTransform(const MatrixF& mat) override;
  174. void prepRenderImage(SceneRenderState* state) override;
  175. void buildConvex(const Box3F& box, Convex* convex) override;
  176. bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F& box, const SphereF& sphere) override;
  177. bool buildExportPolyList(ColladaUtils::ExportData* exportData, const Box3F& box, const SphereF&) override;
  178. bool castRay(const Point3F& start, const Point3F& end, RayInfo* info) override;
  179. bool collideBox(const Point3F& start, const Point3F& end, RayInfo* info) override;
  180. void updateBounds(bool recenter);
  181. void recenter();
  182. /// Geometry access.
  183. /// @{
  184. MatrixF getSurfaceWorldMat(S32 faceid, bool scaled = false) const;
  185. void cullEmptyPlanes(Vector< U32 >* removedPlanes);
  186. void exportToCollada();
  187. void resizePlanes(const Point3F& size);
  188. void getSurfaceLineList(S32 surfId, Vector< Point3F >& lineList);
  189. Geometry& getGeometry() { return mGeometry; }
  190. Vector<MatrixF>& getSurfaces() { return mSurfaces; }
  191. void getSurfaceTriangles(S32 surfId, Vector< Point3F >* outPoints, Vector< Point2F >* outCoords, bool worldSpace);
  192. /// @}
  193. /// Geometry Visualization.
  194. /// @{
  195. void renderFaceEdges(S32 faceid, const ColorI& color = ColorI::WHITE, F32 lineWidth = 1.0f);
  196. /// @}
  197. String getMaterialName() { return mMaterialName; }
  198. protected:
  199. void _updateMaterial();
  200. void _updateGeometry(bool updateCollision = false);
  201. void _updateCollision();
  202. void _export(OptimizedPolyList* plist, const Box3F& box, const SphereF& sphere);
  203. void _renderDebug(ObjectRenderInst* ri, SceneRenderState* state, BaseMatInstance* mat);
  204. static S32 QSORT_CALLBACK _comparePlaneDist(const void* a, const void* b);
  205. static bool protectedSetSurface(void* object, const char* index, const char* data);
  206. static bool protectedSetSurfaceTexture(void* object, const char* index, const char* data);
  207. static bool protectedSetSurfaceUV(void* object, const char* index, const char* data);
  208. protected:
  209. DECLARE_MATERIALASSET(ConvexShape, Material);
  210. DECLARE_ASSET_SETGET(ConvexShape, Material);
  211. // The actual Material instance
  212. BaseMatInstance* mMaterialInst;
  213. // The GFX vertex and primitive buffers
  214. /*GFXVertexBufferHandle< VertexType > mVertexBuffer;
  215. GFXPrimitiveBufferHandle mPrimitiveBuffer;
  216. U32 mVertCount;
  217. U32 mPrimCount;*/
  218. Geometry mGeometry;
  219. Vector< PlaneF > mPlanes;
  220. Vector< MatrixF > mSurfaces;
  221. Vector< Point3F > mFaceCenters;
  222. //this is mostly for storage purposes, so we can save the texture mods
  223. Vector< surfaceMaterial > mSurfaceTextures;
  224. Vector< surfaceUV > mSurfaceUVs;
  225. Vector< surfaceBuffers > mSurfaceBuffers;
  226. Convex* mConvexList;
  227. PhysicsBody* mPhysicsRep;
  228. /// Geometry visualization
  229. /// @{
  230. F32 mNormalLength;
  231. /// @}
  232. };
  233. #endif // _CONVEXSHAPE_H_