decalManager.h

Engine/source/T3D/decal/decalManager.h

More...

Classes:

class

Manage decals in the scene.

Public Enumerations

enum
DecalFlags {
  PermanentDecal = 1 << 0
  SaveDecal = 1 << 1
  ClipDecal = 1 << 2
  CustomDecal = 1 << 3
}

Public Variables

Detailed Description

Public Enumerations

DecalFlags

Enumerator

PermanentDecal = 1 << 0
SaveDecal = 1 << 1
ClipDecal = 1 << 2
CustomDecal = 1 << 3

Public Variables

DecalManager * gDecalManager 
  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _DECALMANAGER_H_
 25#define _DECALMANAGER_H_
 26
 27#ifndef _SCENEOBJECT_H_
 28#include "scene/sceneObject.h"
 29#endif
 30
 31#ifndef _MATHUTIL_FRUSTUM_H_
 32#include "math/util/frustum.h"
 33#endif
 34
 35#ifndef _GFXPRIMITIVEBUFFER_H_
 36#include "gfx/gfxPrimitiveBuffer.h"
 37#endif
 38
 39#ifndef _GFXDEVICE_H_
 40#include "gfx/gfxDevice.h"
 41#endif
 42
 43#ifndef _CLIPPEDPOLYLIST_H_
 44#include "collision/clippedPolyList.h"
 45#endif
 46
 47#ifndef _DECALDATAFILE_H_
 48#include "decalDataFile.h"
 49#endif
 50
 51#ifndef __RESOURCE_H__
 52#include "core/resource.h"
 53#endif
 54
 55#ifndef _DECALINSTANCE_H_
 56#include "decalInstance.h"
 57#endif
 58
 59#ifndef _TSIGNAL_H_
 60#include "core/util/tSignal.h"
 61#endif
 62
 63#ifndef _DATACHUNKER_H_
 64#include "core/dataChunker.h"
 65#endif
 66
 67
 68//#define DECALMANAGER_DEBUG
 69
 70
 71struct ObjectRenderInst;
 72class Material;
 73
 74
 75enum DecalFlags 
 76{
 77   PermanentDecal = 1 << 0,
 78   SaveDecal      = 1 << 1,
 79   ClipDecal      = 1 << 2,
 80   CustomDecal    = 1 << 3 // DecalManager will not attempt to clip or remove this decal
 81                           // it is managed by someone else.
 82};
 83
 84
 85/// Manage decals in the scene.
 86class DecalManager : public SceneObject
 87{
 88   public:
 89      
 90      typedef SceneObject Parent;
 91
 92      // [rene, 11-Mar-11] This vector is very poorly managed; the logic is spread all over the place
 93      Vector<DecalInstance*> mDecalInstanceVec;
 94
 95   protected:
 96      
 97      /// The clipper we keep around between decal updates
 98      /// to avoid excessive memory allocations.
 99      ClippedPolyList mClipper;
100
101      Vector<DecalInstance*> mDecalQueue;
102
103      StringTableEntry mDataFileName;
104      Resource<DecalDataFile> mData;
105      
106      Signal< void() > mClearDataSignal;
107      
108      Vector< GFXVertexBufferHandle<DecalVertex>* > mVBs;
109      Vector< GFXPrimitiveBufferHandle*> mPBs;
110
111      Vector< GFXVertexBufferHandle<DecalVertex>* > mVBPool;
112      Vector< GFXPrimitiveBufferHandle*> mPBPool;
113
114      FreeListChunkerUntyped *mChunkers[3];
115
116      #ifdef DECALMANAGER_DEBUG
117      Vector<PlaneF> mDebugPlanes;
118      #endif
119
120      bool mDirty;
121
122      struct DecalBatch
123      {
124         U32 startDecal;
125         U32 decalCount;
126         U32 iCount;
127         U32 vCount;
128         U8 priority;
129         Material *mat;
130         BaseMatInstance *matInst;
131         bool dynamic;
132      };
133
134      /// Whether to render visualizations for debugging in the editor.
135      static bool smDebugRender;
136
137      static bool smDecalsOn;
138      static F32 smDecalLifeTimeScale;   
139      static bool smPoolBuffers;
140      static const U32 smMaxVerts;
141      static const U32 smMaxIndices;
142
143      // Assume that a class is already given for the object:
144      //    Point with coordinates {float x, y;}
145      //===================================================================
146
147      // isLeft(): tests if a point is Left|On|Right of an infinite line.
148      //    Input:  three points P0, P1, and P2
149      //    Return: >0 for P2 left of the line through P0 and P1
150      //            =0 for P2 on the line
151      //            <0 for P2 right of the line
152      //    See: the January 2001 Algorithm on Area of Triangles
153      inline F32 isLeft( const Point3F &P0, const Point3F &P1, const Point3F &P2 )
154      {
155          return (P1.x - P0.x)*(P2.y - P0.y) - (P2.x - P0.x)*(P1.y - P0.y);
156      }
157
158      U32 _generateConvexHull( const Vector<Point3F> &points, Vector<Point3F> *outPoints );
159
160      // Rendering
161      void prepRenderImage( SceneRenderState *state );
162      
163      void _generateWindingOrder( const Point3F &cornerPoint, Vector<Point3F> *sortPoints );
164
165      // Helpers for creating and deleting the vert and index arrays
166      // held by DecalInstance.
167      void _allocBuffers( DecalInstance *inst );
168      void _freeBuffers( DecalInstance *inst );
169      void _freePools();
170
171      /// Returns index used to index into the correct sized FreeListChunker for
172      /// allocating vertex and index arrays.
173      S32 _getSizeClass( DecalInstance *inst ) const;
174
175      // Hide this from Doxygen
176      /// @cond
177      bool _handleGFXEvent(GFXDevice::GFXDeviceEventType event);
178      /// @endcond
179
180      void _renderDecalSpheres( ObjectRenderInst* inst, SceneRenderState* state, BaseMatInstance* overrideMat );
181
182      ///
183      void _handleZoningChangedEvent( SceneZoneSpaceManager* zoneManager );
184
185      bool _createDataFile();
186
187      // SceneObject.
188      virtual bool onSceneAdd();
189      virtual void onSceneRemove();   public:
190
191   public:
192
193      DecalManager();
194      ~DecalManager();
195
196      /// @name Decal Addition
197      /// @{
198
199      /// Adds a decal using a normal and a rotation.
200      ///
201      /// @param pos The 3d position for the decal center.
202      /// @param normal The decal up vector.
203      /// @param rotAroundNormal The decal rotation around the normal.
204      /// @param decalData The datablock which defines this decal.
205      /// @param decalScale A scalar for adjusting the default size of the decal.
206      /// @param decalTexIndex   Selects the texture coord index within the decal
207      ///                        data to use.  If it is less than zero then a random
208      ///                        coord is selected.
209      /// @param flags  The decal flags. @see DecalFlags
210      ///
211      DecalInstance* addDecal( const Point3F &pos,
212                               const Point3F &normal,
213                               F32 rotAroundNormal,
214                               DecalData *decalData,
215                               F32 decalScale = 1.0f,
216                               S32 decalTexIndex = 0,
217                               U8 flags = 0x000 );
218
219      /// Adds a decal using a normal and a tangent.
220      ///
221      /// @param pos The 3d position for the decal center.
222      /// @param normal The decal up vector.
223      /// @param tanget The decal right vector.
224      /// @param decalData The datablock which defines this decal.
225      /// @param decalScale A scalar for adjusting the default size of the decal.
226      /// @param decalTexIndex   Selects the texture coord index within the decal
227      ///                        data to use.  If it is less than zero then a random
228      ///                        coord is selected.
229      /// @param flags  The decal flags. @see DecalFlags
230      ///
231      DecalInstance* addDecal( const Point3F &pos,
232                               const Point3F &normal,
233                               const Point3F &tangent,
234                               DecalData *decalData,
235                               F32 decalScale = 1.0f,
236                               S32 decalTexIndex = 0,
237                               U8 flags = 0 );
238
239      /// @}
240
241      /// @name Decal Removal
242      /// @{
243
244      void removeDecal( DecalInstance *inst );
245
246      /// @}
247
248      DecalInstance* getDecal( S32 id );
249
250      DecalInstance* getClosestDecal( const Point3F &pos );
251
252      /// Return the closest DecalInstance hit by a ray.
253      DecalInstance* raycast( const Point3F &start, const Point3F &end, bool savedDecalsOnly = true );
254
255      //void dataDeleted( DecalData *data );
256
257      void saveDecals( const UTF8 *fileName );
258      bool loadDecals( const UTF8 *fileName );
259      void clearData();
260
261      /// Returns true if changes have been made since the last load/save
262      bool isDirty() const { return mDirty; }
263
264      bool clipDecal( DecalInstance *decal, Vector<Point3F> *edgeVerts = NULL, const Point2F *clipDepth = NULL );
265
266      void notifyDecalModified( DecalInstance *inst );
267      
268      Signal< void() >& getClearDataSignal() { return mClearDataSignal; }
269
270      Resource<DecalDataFile> getDecalDataFile() { return mData; }
271
272      // SimObject.
273      DECLARE_CONOBJECT( DecalManager );
274      static void consoleInit();
275};
276
277extern DecalManager* gDecalManager;
278
279#endif // _DECALMANAGER_H_
280