OgreInstanceManager.h
Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2013 Torus Knot Software Ltd
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015 
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 -----------------------------------------------------------------------------
00027 */
00028 #ifndef __InstanceManager_H__
00029 #define __InstanceManager_H__
00030 
00031 #include "OgrePrerequisites.h"
00032 #include "OgreMesh.h"
00033 #include "OgreRenderOperation.h"
00034 #include "OgreHeaderPrefix.h"
00035 
00036 namespace Ogre
00037 {
00063     class _OgreExport InstanceManager : public FactoryAlloc
00064     {
00065     public:
00066         enum InstancingTechnique
00067         {
00068             ShaderBased,            
00069             TextureVTF,             
00070             HWInstancingBasic,      
00071             HWInstancingVTF,        
00072             InstancingTechniquesCount
00073         };
00074 
00076         enum BatchSettingId
00077         {
00079             CAST_SHADOWS        = 0,
00081             SHOW_BOUNDINGBOX,
00082 
00083             NUM_SETTINGS
00084         };
00085 
00086     private:
00087         struct BatchSettings
00088         {
00089             //These are all per material
00090             bool setting[NUM_SETTINGS];
00091 
00092             BatchSettings()
00093             {
00094                 setting[CAST_SHADOWS]     = true;
00095                 setting[SHOW_BOUNDINGBOX] = false;
00096             }
00097         };
00098 
00099         typedef vector<InstanceBatch*>::type        InstanceBatchVec;   //vec[batchN] = Batch
00100         typedef map<String, InstanceBatchVec>::type InstanceBatchMap;   //map[materialName] = Vec
00101 
00102         typedef map<String, BatchSettings>::type    BatchSettingsMap;
00103 
00104         const String            mName;                  //Not the name of the mesh
00105         MeshPtr                 mMeshReference;
00106         InstanceBatchMap        mInstanceBatches;
00107         size_t                  mIdCount;
00108 
00109         InstanceBatchVec        mDirtyBatches;
00110 
00111         RenderOperation         mSharedRenderOperation;
00112 
00113         size_t                  mInstancesPerBatch;
00114         InstancingTechnique     mInstancingTechnique;
00115         uint16                  mInstancingFlags;       
00116         unsigned short          mSubMeshIdx;
00117         
00118         BatchSettingsMap        mBatchSettings;
00119         SceneManager*           mSceneManager;
00120 
00121         size_t                  mMaxLookupTableInstances;
00122         unsigned char           mNumCustomParams;       //Number of custom params per instance.
00123 
00127         inline InstanceBatch* getFreeBatch( const String &materialName );
00128 
00138         InstanceBatch* buildNewBatch( const String &materialName, bool firstTime );
00139 
00142         void defragmentBatches( bool optimizeCull, vector<InstancedEntity*>::type &entities,
00143                                 vector<Ogre::Vector4>::type &usedParams,
00144                                 InstanceBatchVec &fragmentedBatches );
00145 
00149         void applySettingToBatches( BatchSettingId id, bool value, const InstanceBatchVec &container );
00150 
00154         void unshareVertices(const Ogre::MeshPtr &mesh);
00155 
00156     public:
00157         InstanceManager( const String &customName, SceneManager *sceneManager,
00158                          const String &meshName, const String &groupName,
00159                          InstancingTechnique instancingTechnique, uint16 instancingFlags,
00160                          size_t instancesPerBatch, unsigned short subMeshIdx, bool useBoneMatrixLookup = false);
00161         virtual ~InstanceManager();
00162 
00163         const String& getName() const { return mName; }
00164 
00165         SceneManager* getSceneManager() const { return mSceneManager; }
00166 
00172         void setInstancesPerBatch( size_t instancesPerBatch );
00173 
00182         void setMaxLookupTableInstances( size_t maxLookupTableInstances );
00183 
00202         void setNumCustomParams( unsigned char numCustomParams );
00203 
00204         unsigned char getNumCustomParams() const
00205         { return mNumCustomParams; }
00206 
00208         InstancingTechnique getInstancingTechnique() const
00209         { return mInstancingTechnique; }
00210 
00222         size_t getMaxOrBestNumInstancesPerBatch( String materialName, size_t suggestedSize, uint16 flags );
00223 
00225         InstancedEntity* createInstancedEntity( const String &materialName );
00226 
00233         void cleanupEmptyBatches(void);
00234 
00258         void defragmentBatches( bool optimizeCulling );
00259 
00275         void setSetting( BatchSettingId id, bool enabled, const String &materialName = StringUtil::BLANK );
00276 
00278         bool getSetting( BatchSettingId id, const String &materialName ) const;
00279 
00283         bool hasSettings( const String &materialName ) const
00284         { return mBatchSettings.find( materialName ) != mBatchSettings.end(); }
00285 
00287         void setBatchesAsStaticAndUpdate( bool bStatic );
00288 
00292         void _addDirtyBatch( InstanceBatch *dirtyBatch );
00293 
00295         void _updateDirtyBatches(void);
00296 
00297         typedef ConstMapIterator<InstanceBatchMap> InstanceBatchMapIterator;
00298         typedef ConstVectorIterator<InstanceBatchVec> InstanceBatchIterator;
00299 
00301         InstanceBatchMapIterator getInstanceBatchMapIterator(void) const
00302         { return InstanceBatchMapIterator( mInstanceBatches.begin(), mInstanceBatches.end() ); }
00303 
00310         InstanceBatchIterator getInstanceBatchIterator( const String &materialName ) const
00311         {
00312             InstanceBatchMap::const_iterator it = mInstanceBatches.find( materialName );
00313             if(it != mInstanceBatches.end())
00314                 return InstanceBatchIterator( it->second.begin(), it->second.end() );
00315             else
00316                 OGRE_EXCEPT(Exception::ERR_INVALID_STATE, "Cannot create instance batch iterator. "
00317                             "Material " + materialName + " cannot be found.", "InstanceManager::getInstanceBatchIterator");
00318         }
00319     };
00320 } // namespace Ogre
00321 
00322 #include "OgreHeaderSuffix.h"
00323 
00324 #endif // __InstanceManager_H__

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Mon Jul 27 2020 13:40:43