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-2013 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 "OgreMesh.h"
33 #include "OgreRenderOperation.h"
34 #include "OgreHeaderPrefix.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<String, InstanceBatchVec>::type InstanceBatchMap; //map[materialName] = Vec
101 
103 
104  const String mName; //Not the name of the mesh
107  size_t mIdCount;
108 
110 
112 
116  unsigned short mSubMeshIdx;
117 
120 
122  unsigned char mNumCustomParams; //Number of custom params per instance.
123 
127  inline InstanceBatch* getFreeBatch( const String &materialName );
128 
138  InstanceBatch* buildNewBatch( const String &materialName, bool firstTime );
139 
142  void defragmentBatches( bool optimizeCull, vector<InstancedEntity*>::type &entities,
143  vector<Ogre::Vector4>::type &usedParams,
144  InstanceBatchVec &fragmentedBatches );
145 
149  void applySettingToBatches( BatchSettingId id, bool value, const InstanceBatchVec &container );
150 
154  void unshareVertices(const Ogre::MeshPtr &mesh);
155 
156  public:
157  InstanceManager( const String &customName, SceneManager *sceneManager,
158  const String &meshName, const String &groupName,
159  InstancingTechnique instancingTechnique, uint16 instancingFlags,
160  size_t instancesPerBatch, unsigned short subMeshIdx, bool useBoneMatrixLookup = false);
161  virtual ~InstanceManager();
162 
163  const String& getName() const { return mName; }
164 
165  SceneManager* getSceneManager() const { return mSceneManager; }
166 
172  void setInstancesPerBatch( size_t instancesPerBatch );
173 
182  void setMaxLookupTableInstances( size_t maxLookupTableInstances );
183 
202  void setNumCustomParams( unsigned char numCustomParams );
203 
204  unsigned char getNumCustomParams() const
205  { return mNumCustomParams; }
206 
209  { return mInstancingTechnique; }
210 
222  size_t getMaxOrBestNumInstancesPerBatch( String materialName, size_t suggestedSize, uint16 flags );
223 
226 
234 
258  void defragmentBatches( bool optimizeCulling );
259 
275  void setSetting( BatchSettingId id, bool enabled, const String &materialName = StringUtil::BLANK );
276 
278  bool getSetting( BatchSettingId id, const String &materialName ) const;
279 
283  bool hasSettings( const String &materialName ) const
284  { return mBatchSettings.find( materialName ) != mBatchSettings.end(); }
285 
287  void setBatchesAsStaticAndUpdate( bool bStatic );
288 
292  void _addDirtyBatch( InstanceBatch *dirtyBatch );
293 
296 
299 
302  { return InstanceBatchMapIterator( mInstanceBatches.begin(), mInstanceBatches.end() ); }
303 
311  {
312  InstanceBatchMap::const_iterator it = mInstanceBatches.find( materialName );
313  if(it != mInstanceBatches.end())
314  return InstanceBatchIterator( it->second.begin(), it->second.end() );
315  else
316  OGRE_EXCEPT(Exception::ERR_INVALID_STATE, "Cannot create instance batch iterator. "
317  "Material " + materialName + " cannot be found.", "InstanceManager::getInstanceBatchIterator");
318  }
319  };
320 } // namespace Ogre
321 
322 #include "OgreHeaderSuffix.h"
323 
324 #endif // __InstanceManager_H__
#define _OgreExport
Definition: OgrePlatform.h:257
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
Concrete IteratorWrapper for const access to the underlying key-value container.
Concrete IteratorWrapper for const access to the underlying container.
InstanceBatch forms part of the new Instancing system This is an abstract class that must be derived ...
This is the main starting point for the new instancing system.
'New' rendering operation using vertex buffers.
Manages the organisation and rendering of a 'scene' i.e.
static const String BLANK
Constant blank string, useful for returning by ref where local does not exist.
Definition: OgreString.h:196
#define OGRE_EXCEPT(num, desc, src)
RenderOperation mSharedRenderOperation
bool getSetting(BatchSettingId id, const String &materialName) const
If settings for the given material didn't exist, default value is returned.
InstanceBatchIterator getInstanceBatchIterator(const String &materialName) const
Get non-updateable iterator over instance batches for given material.
SceneManager * getSceneManager() const
void applySettingToBatches(BatchSettingId id, bool value, const InstanceBatchVec &container)
void setInstancesPerBatch(size_t instancesPerBatch)
Raises an exception if trying to change it after creating the first InstancedEntity.
size_t getMaxOrBestNumInstancesPerBatch(String materialName, size_t suggestedSize, uint16 flags)
Calculates the maximum (or the best amount, depending on flags) of instances per batch given the sugg...
map< String, InstanceBatchVec >::type InstanceBatchMap
InstancingTechnique getInstancingTechnique() const
virtual ~InstanceManager()
vector< InstanceBatch * >::type InstanceBatchVec
void _addDirtyBatch(InstanceBatch *dirtyBatch)
Called by an InstanceBatch when it requests their bounds to be updated for proper culling.
InstanceBatchVec mDirtyBatches
ConstMapIterator< InstanceBatchMap > InstanceBatchMapIterator
InstancedEntity * createInstancedEntity(const String &materialName)
Creates an InstancedEntity based on an existing InstanceManager (.
void _updateDirtyBatches(void)
Called by SceneManager when we told it we have at least one dirty batch.
BatchSettingId
Values to be used in setSetting() & BatchSettings::setting.
void setMaxLookupTableInstances(size_t maxLookupTableInstances)
Sets the size of the lookup table for techniques supporting bone lookup table.
void cleanupEmptyBatches(void)
This function can be useful to improve CPU speed after having too many instances created,...
ConstVectorIterator< InstanceBatchVec > InstanceBatchIterator
BatchSettingsMap mBatchSettings
InstanceBatchMapIterator getInstanceBatchMapIterator(void) const
Get non-updateable iterator over instance batches per material.
InstanceBatchMap mInstanceBatches
void defragmentBatches(bool optimizeCull, vector< InstancedEntity * >::type &entities, vector< Ogre::Vector4 >::type &usedParams, InstanceBatchVec &fragmentedBatches)
void setNumCustomParams(unsigned char numCustomParams)
Sets the number of custom parameters per instance.
void setBatchesAsStaticAndUpdate(bool bStatic)
Tells this batch to stop updating animations, positions, rotations, and display all it's active insta...
InstanceManager(const String &customName, SceneManager *sceneManager, const String &meshName, const String &groupName, InstancingTechnique instancingTechnique, uint16 instancingFlags, size_t instancesPerBatch, unsigned short subMeshIdx, bool useBoneMatrixLookup=false)
unsigned char getNumCustomParams() const
void unshareVertices(const Ogre::MeshPtr &mesh)
Called when we you use a mesh which has shared vertices, the function creates separate vertex/index b...
InstanceBatch * getFreeBatch(const String &materialName)
Finds a batch with at least one free instanced entity we can use.
bool hasSettings(const String &materialName) const
Returns true if settings were already created for the given material name.
InstancingTechnique mInstancingTechnique
void defragmentBatches(bool optimizeCulling)
After creating many entities (which turns in many batches) and then removing entities that are in the...
InstanceBatch * buildNewBatch(const String &materialName, bool firstTime)
Called when batches are fully exhausted (can't return more instances) so a new batch is created.
void setSetting(BatchSettingId id, bool enabled, const String &materialName=StringUtil::BLANK)
Applies a setting for all batches using the same material_ existing ones and those that will be creat...
map< String, BatchSettings >::type BatchSettingsMap
const String & getName() const
@ SHOW_BOUNDINGBOX
Makes each batch to display it's bounding box. Useful for debugging or profiling.
@ HWInstancingBasic
Needs SM 3.0+ and HW instancing support.
@ TextureVTF
Needs Vertex Texture Fetch & SM 3.0+.
@ HWInstancingVTF
Needs SM 3.0+, HW instancing support & VTF.
unsigned short uint16
Definition: OgrePlatform.h:360
_StringBase String
std::vector< T, A > type

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.