decalManager.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 _DECALMANAGER_H_
  23. #define _DECALMANAGER_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _MATHUTIL_FRUSTUM_H_
  28. #include "math/util/frustum.h"
  29. #endif
  30. #ifndef _GFXPRIMITIVEBUFFER_H_
  31. #include "gfx/gfxPrimitiveBuffer.h"
  32. #endif
  33. #ifndef _GFXDEVICE_H_
  34. #include "gfx/gfxDevice.h"
  35. #endif
  36. #ifndef _CLIPPEDPOLYLIST_H_
  37. #include "collision/clippedPolyList.h"
  38. #endif
  39. #ifndef _DECALDATAFILE_H_
  40. #include "decalDataFile.h"
  41. #endif
  42. #ifndef __RESOURCE_H__
  43. #include "core/resource.h"
  44. #endif
  45. #ifndef _DECALINSTANCE_H_
  46. #include "decalInstance.h"
  47. #endif
  48. #ifndef _TSIGNAL_H_
  49. #include "core/util/tSignal.h"
  50. #endif
  51. #ifndef _DATACHUNKER_H_
  52. #include "core/dataChunker.h"
  53. #endif
  54. //#define DECALMANAGER_DEBUG
  55. struct ObjectRenderInst;
  56. class Material;
  57. enum DecalFlags
  58. {
  59. PermanentDecal = 1 << 0,
  60. SaveDecal = 1 << 1,
  61. ClipDecal = 1 << 2,
  62. CustomDecal = 1 << 3 // DecalManager will not attempt to clip or remove this decal
  63. // it is managed by someone else.
  64. };
  65. /// Manage decals in the scene.
  66. class DecalManager : public SceneObject
  67. {
  68. public:
  69. typedef SceneObject Parent;
  70. // [rene, 11-Mar-11] This vector is very poorly managed; the logic is spread all over the place
  71. Vector<DecalInstance *> mDecalInstanceVec;
  72. protected:
  73. /// The clipper we keep around between decal updates
  74. /// to avoid excessive memory allocations.
  75. ClippedPolyList mClipper;
  76. Vector<DecalInstance*> mDecalQueue;
  77. StringTableEntry mDataFileName;
  78. Resource<DecalDataFile> mData;
  79. Signal< void() > mClearDataSignal;
  80. Vector< GFXVertexBufferHandle<DecalVertex>* > mVBs;
  81. Vector< GFXPrimitiveBufferHandle* > mPBs;
  82. Vector< GFXVertexBufferHandle<DecalVertex>* > mVBPool;
  83. Vector< GFXPrimitiveBufferHandle* > mPBPool;
  84. FreeListChunkerUntyped *mChunkers[3];
  85. #ifdef DECALMANAGER_DEBUG
  86. Vector<PlaneF> mDebugPlanes;
  87. #endif
  88. bool mDirty;
  89. struct DecalBatch
  90. {
  91. U32 startDecal;
  92. U32 decalCount;
  93. U32 iCount;
  94. U32 vCount;
  95. U8 priority;
  96. Material *mat;
  97. BaseMatInstance *matInst;
  98. bool dynamic;
  99. };
  100. /// Whether to render visualizations for debugging in the editor.
  101. static bool smDebugRender;
  102. static bool smDecalsOn;
  103. static F32 smDecalLifeTimeScale;
  104. static bool smPoolBuffers;
  105. static const U32 smMaxVerts;
  106. static const U32 smMaxIndices;
  107. // Assume that a class is already given for the object:
  108. // Point with coordinates {float x, y;}
  109. //===================================================================
  110. // isLeft(): tests if a point is Left|On|Right of an infinite line.
  111. // Input: three points P0, P1, and P2
  112. // Return: >0 for P2 left of the line through P0 and P1
  113. // =0 for P2 on the line
  114. // <0 for P2 right of the line
  115. // See: the January 2001 Algorithm on Area of Triangles
  116. inline F32 isLeft( const Point3F &P0, const Point3F &P1, const Point3F &P2 )
  117. {
  118. return (P1.x - P0.x)*(P2.y - P0.y) - (P2.x - P0.x)*(P1.y - P0.y);
  119. }
  120. U32 _generateConvexHull( const Vector<Point3F> &points, Vector<Point3F> *outPoints );
  121. // Rendering
  122. void prepRenderImage( SceneRenderState *state );
  123. void _generateWindingOrder( const Point3F &cornerPoint, Vector<Point3F> *sortPoints );
  124. // Helpers for creating and deleting the vert and index arrays
  125. // held by DecalInstance.
  126. void _allocBuffers( DecalInstance *inst );
  127. void _freeBuffers( DecalInstance *inst );
  128. void _freePools();
  129. /// Returns index used to index into the correct sized FreeListChunker for
  130. /// allocating vertex and index arrays.
  131. S32 _getSizeClass( DecalInstance *inst ) const;
  132. // Hide this from Doxygen
  133. /// @cond
  134. bool _handleGFXEvent(GFXDevice::GFXDeviceEventType event);
  135. /// @endcond
  136. void _renderDecalSpheres( ObjectRenderInst* inst, SceneRenderState* state, BaseMatInstance* overrideMat );
  137. ///
  138. void _handleZoningChangedEvent( SceneZoneSpaceManager* zoneManager );
  139. bool _createDataFile();
  140. // SceneObject.
  141. virtual bool onSceneAdd();
  142. virtual void onSceneRemove(); public:
  143. public:
  144. DecalManager();
  145. ~DecalManager();
  146. /// @name Decal Addition
  147. /// @{
  148. /// Adds a decal using a normal and a rotation.
  149. ///
  150. /// @param pos The 3d position for the decal center.
  151. /// @param normal The decal up vector.
  152. /// @param rotAroundNormal The decal rotation around the normal.
  153. /// @param decalData The datablock which defines this decal.
  154. /// @param decalScale A scalar for adjusting the default size of the decal.
  155. /// @param decalTexIndex Selects the texture coord index within the decal
  156. /// data to use. If it is less than zero then a random
  157. /// coord is selected.
  158. /// @param flags The decal flags. @see DecalFlags
  159. ///
  160. DecalInstance* addDecal( const Point3F &pos,
  161. const Point3F &normal,
  162. F32 rotAroundNormal,
  163. DecalData *decalData,
  164. F32 decalScale = 1.0f,
  165. S32 decalTexIndex = 0,
  166. U8 flags = 0x000 );
  167. /// Adds a decal using a normal and a tangent.
  168. ///
  169. /// @param pos The 3d position for the decal center.
  170. /// @param normal The decal up vector.
  171. /// @param tanget The decal right vector.
  172. /// @param decalData The datablock which defines this decal.
  173. /// @param decalScale A scalar for adjusting the default size of the decal.
  174. /// @param decalTexIndex Selects the texture coord index within the decal
  175. /// data to use. If it is less than zero then a random
  176. /// coord is selected.
  177. /// @param flags The decal flags. @see DecalFlags
  178. ///
  179. DecalInstance* addDecal( const Point3F &pos,
  180. const Point3F &normal,
  181. const Point3F &tangent,
  182. DecalData *decalData,
  183. F32 decalScale = 1.0f,
  184. S32 decalTexIndex = 0,
  185. U8 flags = 0 );
  186. /// @}
  187. /// @name Decal Removal
  188. /// @{
  189. void removeDecal( DecalInstance *inst );
  190. /// @}
  191. DecalInstance* getDecal( S32 id );
  192. DecalInstance* getClosestDecal( const Point3F &pos );
  193. /// Return the closest DecalInstance hit by a ray.
  194. DecalInstance* raycast( const Point3F &start, const Point3F &end, bool savedDecalsOnly = true );
  195. //void dataDeleted( DecalData *data );
  196. void saveDecals( const UTF8 *fileName );
  197. bool loadDecals( const UTF8 *fileName );
  198. void clearData();
  199. /// Returns true if changes have been made since the last load/save
  200. bool isDirty() const { return mDirty; }
  201. bool clipDecal( DecalInstance *decal, Vector<Point3F> *edgeVerts = NULL, const Point2F *clipDepth = NULL );
  202. void notifyDecalModified( DecalInstance *inst );
  203. Signal< void() >& getClearDataSignal() { return mClearDataSignal; }
  204. Resource<DecalDataFile> getDecalDataFile() { return mData; }
  205. // SimObject.
  206. DECLARE_CONOBJECT( DecalManager );
  207. static void consoleInit();
  208. };
  209. extern DecalManager* gDecalManager;
  210. #endif // _DECALMANAGER_H_