OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreInstanceManager.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4  (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef __InstanceManager_H__
29 #define __InstanceManager_H__
30 
31 #include "OgrePrerequisites.h"
32 #include "OgreRenderOperation.h"
33 #include "OgreHeaderPrefix.h"
34 #include "OgreIdString.h"
35 
36 namespace Ogre
37 {
64  {
65  public:
67  {
72  InstancingTechniquesCount
73  };
74 
77  {
79  CAST_SHADOWS = 0,
82 
83  NUM_SETTINGS
84  };
85 
86  private:
88  {
89  //These are all per material
90  bool setting[NUM_SETTINGS];
91 
93  {
94  setting[CAST_SHADOWS] = true;
95  setting[SHOW_BOUNDINGBOX] = false;
96  }
97  };
98 
99  typedef vector<InstanceBatch*>::type InstanceBatchVec; //vec[batchN] = Batch
100  typedef map<IdString, InstanceBatchVec>::type InstanceBatchMap; //map[materialName] = Vec
101 
103 
104  const IdString mName; //Not the name of the mesh
107 #ifndef NDEBUG
108  size_t mIdCount;
109 #endif
112 
114 
118  unsigned short mSubMeshIdx;
119 
122 
124  unsigned char mNumCustomParams; //Number of custom params per instance.
125 
129  inline InstanceBatch* getFreeBatch( const String &materialName, SceneMemoryMgrTypes sceneType );
130 
140  InstanceBatch* buildNewBatch( const String &materialName, SceneMemoryMgrTypes sceneType,
141  bool firstTime );
142 
145  void defragmentBatches( bool optimizeCull, vector<InstancedEntity*>::type &entities,
146  vector<Ogre::Vector4>::type &usedParams,
147  InstanceBatchVec &fragmentedBatches );
148 
152  void applySettingToBatches( BatchSettingId id, bool value, const InstanceBatchVec &container );
153 
157  void unshareVertices(const Ogre::MeshPtr &mesh);
158 
159  public:
160  InstanceManager( IdString customName, SceneManager *sceneManager,
161  const String &meshName, const String &groupName,
162  InstancingTechnique instancingTechnique, uint16 instancingFlags,
163  size_t instancesPerBatch, unsigned short subMeshIdx, bool useBoneMatrixLookup = false);
164  virtual ~InstanceManager();
165 
166  const IdString getName() const { return mName; }
167 
168  SceneManager* getSceneManager() const { return mSceneManager; }
169 
175  void setInstancesPerBatch( size_t instancesPerBatch );
176 
185  void setMaxLookupTableInstances( size_t maxLookupTableInstances );
186 
205  void setNumCustomParams( unsigned char numCustomParams );
206 
207  unsigned char getNumCustomParams() const
208  { return mNumCustomParams; }
209 
212  { return mInstancingTechnique; }
213 
225  size_t getMaxOrBestNumInstancesPerBatch( const String &materialName, size_t suggestedSize,
226  uint16 flags );
227 
229  InstancedEntity* createInstancedEntity( const String &materialName,
231 
238  void cleanupEmptyBatches(void);
239 
263  void defragmentBatches( bool optimizeCulling );
264 
280  void setSetting( BatchSettingId id, bool enabled, IdString materialName=IdString() );
281 
283  bool getSetting( BatchSettingId id, IdString materialName ) const;
284 
288  bool hasSettings( IdString materialName ) const
289  { return mBatchSettings.find( materialName ) != mBatchSettings.end(); }
290 
292  void setBatchesAsStatic( bool bStatic );
293 
299  void _addToDynamicBatchList( InstanceBatch *dynamicBatch );
300 
306  void _removeFromDynamicBatchList( InstanceBatch *batch );
307 
311  void _addDirtyStaticBatch( InstanceBatch *dirtyBatch );
312 
313 #ifdef OGRE_LEGACY_ANIMATIONS
314  void _updateAnimations(void);
316 #endif
317 
319  void _updateDirtyBatchesThread( size_t threadIdx );
320 
322  void _updateDirtyBatches(void);
323 
326 
329  { return InstanceBatchMapIterator( mInstanceBatches.begin(), mInstanceBatches.end() ); }
330 
337  InstanceBatchIterator getInstanceBatchIterator( const String &materialName,
338  SceneMemoryMgrTypes sceneType ) const;
339  };
340 
342  {
343  bool operator () ( const InstanceManager *a, const InstanceManager *b ) const
344  {
345  return a->getName() < b->getName();
346  }
347  bool operator () ( const InstanceManager *a, IdString name ) const
348  {
349  return a->getName() < name;
350  }
351  bool operator () ( IdString name, const InstanceManager *a ) const
352  {
353  return name < a->getName();
354  }
355  };
356 
357 } // namespace Ogre
358 
359 #include "OgreHeaderSuffix.h"
360 
361 #endif // __InstanceManager_H__
BatchSettingsMap mBatchSettings
#define _OgreExport
Definition: OgrePlatform.h:255
std::vector< T, A > type
Manages the organisation and rendering of a 'scene' i.e.
const IdString getName() const
ConstVectorIterator< InstanceBatchVec > InstanceBatchIterator
Needs Vertex Texture Fetch & SM 3.0+.
InstanceBatchMapIterator getInstanceBatchMapIterator(void) const
Get non-updateable iterator over instance batches per material.
bool operator()(const InstanceManager *a, const InstanceManager *b) const
map< IdString, InstanceBatchVec >::type InstanceBatchMap
InstanceBatchVec mDirtyStaticBatches
unsigned char getNumCustomParams() const
InstanceBatchMap mInstanceBatches
ConstMapIterator< InstanceBatchMap > InstanceBatchMapIterator
RenderOperation mSharedRenderOperation
InstanceBatchVec mDynamicBatches
InstancingTechnique getInstancingTechnique() const
Concrete IteratorWrapper for const access to the underlying container.
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
Hashed string.
Definition: OgreIdString.h:94
map< IdString, BatchSettings >::type BatchSettingsMap
bool hasSettings(IdString materialName) const
Returns true if settings were already created for the given material name.
InstanceBatch forms part of the new Instancing system This is an abstract class that must be derived ...
'New' rendering operation using vertex buffers.
SceneMemoryMgrTypes
The types of NodeMemoryManager & ObjectMemoryManagers.
Definition: OgreCommon.h:318
Concrete IteratorWrapper for const access to the underlying key-value container.
This is the main starting point for the new instancing system.
BatchSettingId
Values to be used in setSetting() & BatchSettings::setting.
_StringBase String
Definition: OgreCommon.h:53
vector< InstanceBatch * >::type InstanceBatchVec
Makes each batch to display it's bounding box. Useful for debugging or profiling. ...
unsigned short uint16
Definition: OgrePlatform.h:421
SceneManager * getSceneManager() const
Needs SM 3.0+, HW instancing support & VTF.
InstancingTechnique mInstancingTechnique
Needs SM 3.0+ and HW instancing support.