Torque3D Documentation / _generateds / sceneContainer.h

sceneContainer.h

Engine/source/scene/sceneContainer.h

SceneObject database.

More...

Classes:

class

Database for SceneObjects.

class

Reference to a scene object.

class

For simple queries. Simply creates a vector of the objects.

Public Enumerations

enum
PolyListContext {
  PLC_Collision 
  PLC_Decal 
  PLC_Selection 
  PLC_Navigation 
  PLC_Export 
}

A contextual hint passed to the polylist methods which allows it to return the appropriate geometry.

Detailed Description

SceneObject database.

Public Enumerations

PolyListContext

Enumerator

PLC_Collision

A hint that the polyist is intended for collision testing.

PLC_Decal

A hint that the polyist is for decal geometry generation.

PLC_Selection

A hint that the polyist is used for selection from an editor or other tool.

PLC_Navigation

A hint that the polylist is used for building a representation of the environment used for navigation.

PLC_Export

A hint that the polyist will be used to export geometry and would like to have texture coords and materials.


A contextual hint passed to the polylist methods which allows it to return the appropriate geometry.

Public Variables

SceneContainer gClientContainer 
SceneContainer gServerContainer 
  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//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
 25// Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
 26// Copyright (C) 2015 Faust Logic, Inc.
 27//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
 28
 29#ifndef _SCENECONTAINER_H_
 30#define _SCENECONTAINER_H_
 31
 32#ifndef _MBOX_H_
 33#include "math/mBox.h"
 34#endif
 35
 36#ifndef _MSPHERE_H_
 37#include "math/mSphere.h"
 38#endif
 39
 40#ifndef _TVECTOR_H_
 41#include "core/util/tVector.h"
 42#endif
 43
 44#ifndef _MPOLYHEDRON_H_
 45#include "math/mPolyhedron.h"
 46#endif
 47
 48#ifndef _SIMOBJECT_H_
 49#include "console/simObject.h"
 50#endif
 51
 52
 53/// @file
 54/// SceneObject database.
 55
 56
 57class SceneObject;
 58class AbstractPolyList;
 59class OptimizedPolyList;
 60class Frustum;
 61class Point3F;
 62
 63struct RayInfo;
 64
 65
 66template< typename T >
 67class SceneObjectRefBase
 68{
 69   public:
 70
 71      /// Object that is referenced in the link.
 72      SceneObject* object;
 73
 74      /// Next link in chain of container.
 75      T* nextInBin;
 76
 77      /// Previous link in chain of container.
 78      T* prevInBin;
 79
 80      /// Next link in chain that is associated with #object.
 81      T* nextInObj;
 82};
 83
 84
 85/// Reference to a scene object.
 86class SceneObjectRef : public SceneObjectRefBase< SceneObjectRef > {};
 87
 88
 89/// A contextual hint passed to the polylist methods which
 90/// allows it to return the appropriate geometry.
 91enum PolyListContext
 92{
 93   /// A hint that the polyist is intended 
 94   /// for collision testing.
 95   PLC_Collision,
 96   
 97   /// A hint that the polyist is for decal
 98   /// geometry generation.
 99   PLC_Decal,
100
101   /// A hint that the polyist is used for
102   /// selection from an editor or other tool.
103   PLC_Selection,
104
105   /// A hint that the polylist is used for
106   /// building a representation of the environment
107   /// used for navigation.
108   PLC_Navigation,
109
110   /// A hint that the polyist will be used
111   /// to export geometry and would like to have
112   /// texture coords and materials.   
113   PLC_Export
114};
115
116
117/// For simple queries.  Simply creates a vector of the objects
118class SimpleQueryList
119{
120   public:
121
122      Vector< SceneObject*> mList;
123
124      SimpleQueryList() 
125      {
126         VECTOR_SET_ASSOCIATION( mList );
127      }
128
129      void insertObject( SceneObject* obj ) { mList.push_back(obj); }
130      static void insertionCallback( SceneObject* obj, void* key )
131      {
132         SimpleQueryList* pList = reinterpret_cast< SimpleQueryList* >( key );
133         pList->insertObject( obj );
134      }
135};
136
137
138//----------------------------------------------------------------------------
139
140/// Database for SceneObjects.
141///
142/// ScenceContainer implements a grid-based spatial subdivision for the contents of a scene.
143class SceneContainer
144{
145      enum CastRayType
146      {
147         CollisionGeometry,
148         RenderedGeometry,
149      };
150
151   public:
152
153      struct Link
154      {
155         Link* mNext;
156         Link* mPrev;
157         Link();
158         void unlink();
159         void linkAfter(Link* ptr);
160      };
161
162      struct CallbackInfo 
163      {
164         PolyListContext context;
165         AbstractPolyList* polyList;
166         Box3F boundingBox;
167         SphereF boundingSphere;
168         void *key;
169      };
170
171   private:
172
173      Link mStart;
174      Link mEnd;
175
176      /// Container queries based on #mCurrSeqKey are are not re-entrant;
177      /// this is used to detect when it happens.
178      bool mSearchInProgress;
179
180      /// Current sequence key.
181      U32 mCurrSeqKey;
182
183      SceneObjectRef* mFreeRefPool;
184      Vector< SceneObjectRef*> mRefPoolBlocks;
185
186      SceneObjectRef* mBinArray;
187      SceneObjectRef mOverflowBin;
188
189      /// A vector that contains just the water and physical zone
190      /// object types which is used to optimize searches.
191      Vector< SceneObject*> mWaterAndZones;
192
193      /// Vector that contains just the terrain objects in the container.
194      Vector< SceneObject*> mTerrains;
195
196      static const U32 csmNumBins;
197      static const F32 csmBinSize;
198      static const F32 csmTotalBinSize;
199      static const U32 csmRefPoolBlockSize;
200
201   public:
202
203      SceneContainer();
204      ~SceneContainer();
205
206      /// Return a vector containing all the water and physical zone objects in this container.
207      const Vector< SceneObject*>& getWaterAndPhysicalZones() const { return mWaterAndZones; }
208
209      /// Return a vector containing all terrain objects in this container.
210      const Vector< SceneObject*>& getTerrains() const { return mTerrains; }
211
212      /// @name Basic database operations
213      /// @{
214
215      ///
216      typedef void ( *FindCallback )( SceneObject* object, void* key );
217
218      /// Find all objects of the given type(s) and invoke the given callback for each
219      /// of them.
220      /// @param mask Object type mask (@see SimObjectTypes).
221      /// @param callback Pointer to function to invoke for each object.
222      /// @param key User data to pass to the "key" argument of @a callback.
223      void findObjects( U32 mask, FindCallback callback, void* key = NULL );
224
225      void findObjects( const Box3F& box, U32 mask, FindCallback, void *key = NULL );
226      void findObjects( const Frustum& frustum, U32 mask, FindCallback, void *key = NULL );
227
228      void polyhedronFindObjects( const Polyhedron& polyhedron, U32 mask, FindCallback, void *key = NULL );
229
230      /// Find all objects of the given type(s) and add them to the given vector.
231      /// @param mask Object type mask (@see SimObjectTypes).
232      /// @param outFound Vector to add found objects to.
233      void findObjectList( U32 mask, Vector< SceneObject*>* outFound );
234
235      ///
236      void findObjectList( const Box3F& box, U32 mask, Vector< SceneObject*>* outFound );
237
238      ///
239      void findObjectList( const Frustum& frustum, U32 mask, Vector< SceneObject*>* outFound );
240
241      /// @}
242
243      /// @name Line intersection
244      /// @{
245
246      typedef bool ( *CastRayCallback )( RayInfo* ri );
247
248      /// Test against collision geometry -- fast.
249      bool castRay( const Point3F &start, const Point3F &end, U32 mask, RayInfo* info, CastRayCallback callback = NULL );
250
251      /// Test against rendered geometry -- slow.
252      bool castRayRendered( const Point3F &start, const Point3F &end, U32 mask, RayInfo* info, CastRayCallback callback = NULL );
253
254      bool collideBox(const Point3F &start, const Point3F &end, U32 mask, RayInfo* info);
255
256      /// @}
257
258      /// @name Poly list
259      /// @{
260
261      ///
262      bool buildPolyList(  PolyListContext context, 
263                           const Box3F &box, 
264                           U32 typeMask, 
265                           AbstractPolyList *polylist );
266
267      /// @}
268
269      /// Add an object to the database.
270      /// @param object A SceneObject.
271      bool addObject( SceneObject* object );
272
273      /// Remove an object from the database.
274      /// @param object A SceneObject.
275      bool removeObject( SceneObject* object );
276
277      void addRefPoolBlock();
278      SceneObjectRef* allocateObjectRef();
279      void freeObjectRef(SceneObjectRef*);
280      void insertIntoBins( SceneObject* object );
281      void removeFromBins( SceneObject* object );
282
283      /// Make sure that we're not just sticking the object right back
284      /// where it came from.  The overloaded insertInto is so we don't calculate
285      /// the ranges twice.
286      void checkBins( SceneObject* object );
287      void insertIntoBins(SceneObject*, U32, U32, U32, U32);
288
289      void initRadiusSearch(const Point3F& searchPoint,
290         const F32      searchRadius,
291         const U32      searchMask);
292      void initTypeSearch(const U32      searchMask);
293      SceneObject* containerSearchNextObject();
294      U32  containerSearchNext();
295      F32  containerSearchCurrDist();
296      F32  containerSearchCurrRadiusDist();
297
298   private:
299
300      Vector<SimObjectPtr<SceneObject>*>  mSearchList;///< Object searches to support console querying of the database.  ONLY WORKS ON SERVER
301      S32                                 mCurrSearchPos;
302      Point3F                             mSearchReferencePoint;
303
304      void cleanupSearchVectors();
305
306      /// Base cast ray code
307      bool _castRay( U32 type, const Point3F &start, const Point3F &end, U32 mask, RayInfo* info, CastRayCallback callback );
308
309      void _findSpecialObjects( const Vector< SceneObject*>& vector, U32 mask, FindCallback, void *key = NULL );
310      void _findSpecialObjects( const Vector< SceneObject*>& vector, const Box3F &box, U32 mask, FindCallback callback, void *key = NULL );   
311
312      static void getBinRange( const F32 min, const F32 max, U32& minBin, U32& maxBin );
313public:
314      Vector<SimObjectPtr<SceneObject>*>& getRadiusSearchList() { return mSearchList; }
315};
316
317//-----------------------------------------------------------------------------
318
319extern SceneContainer gServerContainer;
320extern SceneContainer gClientContainer;
321
322//-----------------------------------------------------------------------------
323
324inline void SceneContainer::freeObjectRef(SceneObjectRef* trash)
325{
326   trash->object = NULL;
327   trash->nextInBin = NULL;
328   trash->prevInBin = NULL;
329   trash->nextInObj = mFreeRefPool;
330   mFreeRefPool     = trash;
331}
332
333//-----------------------------------------------------------------------------
334
335inline SceneObjectRef* SceneContainer::allocateObjectRef()
336{
337   if( mFreeRefPool == NULL )
338      addRefPoolBlock();
339   AssertFatal( mFreeRefPool!=<a href="/coding/file/types_8lint_8h/#types_8lint_8h_1a070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>, "Error, should always have a free reference here!" );
340
341   SceneObjectRef* ret = mFreeRefPool;
342   mFreeRefPool = mFreeRefPool->nextInObj;
343
344   ret->nextInObj = NULL;
345   return ret;
346}
347
348#endif // !_SCENECONTAINER_H_
349