OGRE  1.9
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 "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 
225  InstancedEntity* createInstancedEntity( const String &materialName );
226 
233  void cleanupEmptyBatches(void);
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 
295  void _updateDirtyBatches(void);
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__
BatchSettingsMap mBatchSettings
#define _OgreExport
Definition: OgrePlatform.h:260
std::vector< T, A > type
bool hasSettings(const String &materialName) const
Returns true if settings were already created for the given material name.
map< String, BatchSettings >::type BatchSettingsMap
Manages the organisation and rendering of a 'scene' i.e.
ConstVectorIterator< InstanceBatchVec > InstanceBatchIterator
InstanceBatchVec mDirtyBatches
Needs Vertex Texture Fetch & SM 3.0+.
InstanceBatchIterator getInstanceBatchIterator(const String &materialName) const
Get non-updateable iterator over instance batches for given material.
InstanceBatchMapIterator getInstanceBatchMapIterator(void) const
Get non-updateable iterator over instance batches per material.
unsigned char getNumCustomParams() const
InstanceBatchMap mInstanceBatches
ConstMapIterator< InstanceBatchMap > InstanceBatchMapIterator
RenderOperation mSharedRenderOperation
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...
InstanceBatch forms part of the new Instancing system This is an abstract class that must be derived ...
#define OGRE_EXCEPT(code, desc, src)
static const String BLANK
Constant blank string, useful for returning by ref where local does not exist.
Definition: OgreString.h:196
'New' rendering operation using vertex buffers.
const String & getName() const
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.
map< String, InstanceBatchVec >::type InstanceBatchMap
_StringBase String
vector< InstanceBatch * >::type InstanceBatchVec
Makes each batch to display it's bounding box. Useful for debugging or profiling. ...
unsigned short uint16
Definition: OgrePlatform.h:345
SceneManager * getSceneManager() const
Needs SM 3.0+, HW instancing support & VTF.
InstancingTechnique mInstancingTechnique
Needs SM 3.0+ and HW instancing support.