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-2012 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 00035 namespace Ogre 00036 { 00062 class _OgreExport InstanceManager : public FactoryAlloc 00063 { 00064 public: 00065 enum InstancingTechnique 00066 { 00067 ShaderBased, //Any SM 2.0+ @See InstanceBatchShader 00068 TextureVTF, //Needs Vertex Texture Fetch & SM 3.0+ @See InstanceBatchVTF 00069 HWInstancingBasic, //Needs SM 3.0+ and HW instancing support @See InstanceBatchHW 00070 HWInstancingVTF, //Needs SM 3.0+, HW instancing support & VTF @See InstanceBatchHW_VTF 00071 InstancingTechniquesCount 00072 }; 00073 00075 enum BatchSettingId 00076 { 00078 CAST_SHADOWS = 0, 00080 SHOW_BOUNDINGBOX, 00081 00082 NUM_SETTINGS 00083 }; 00084 00085 private: 00086 struct BatchSettings 00087 { 00088 //These are all per material 00089 bool setting[NUM_SETTINGS]; 00090 00091 BatchSettings() 00092 { 00093 setting[CAST_SHADOWS] = true; 00094 setting[SHOW_BOUNDINGBOX] = false; 00095 } 00096 }; 00097 00098 typedef vector<InstanceBatch*>::type InstanceBatchVec; //vec[batchN] = Batch 00099 typedef map<String, InstanceBatchVec>::type InstanceBatchMap; //map[materialName] = Vec 00100 00101 typedef map<String, BatchSettings>::type BatchSettingsMap; 00102 00103 const String mName; //Not the name of the mesh 00104 MeshPtr mMeshReference; 00105 InstanceBatchMap mInstanceBatches; 00106 size_t mIdCount; 00107 00108 InstanceBatchVec mDirtyBatches; 00109 00110 RenderOperation mSharedRenderOperation; 00111 00112 size_t mInstancesPerBatch; 00113 InstancingTechnique mInstancingTechnique; 00114 uint16 mInstancingFlags; //@see InstanceManagerFlags 00115 unsigned short mSubMeshIdx; 00116 00117 BatchSettingsMap mBatchSettings; 00118 SceneManager* mSceneManager; 00119 00120 size_t mMaxLookupTableInstances; 00124 inline InstanceBatch* getFreeBatch( const String &materialName ); 00125 00135 InstanceBatch* buildNewBatch( const String &materialName, bool firstTime ); 00136 00139 void defragmentBatches( bool optimizeCull, vector<InstancedEntity*>::type &entities, 00140 InstanceBatchVec &fragmentedBatches ); 00141 00145 void applySettingToBatches( BatchSettingId id, bool value, const InstanceBatchVec &container ); 00146 00150 void unshareVertices(const Ogre::MeshPtr &mesh); 00151 00152 public: 00153 InstanceManager( const String &customName, SceneManager *sceneManager, 00154 const String &meshName, const String &groupName, 00155 InstancingTechnique instancingTechnique, uint16 instancingFlags, 00156 size_t instancesPerBatch, unsigned short subMeshIdx, bool useBoneMatrixLookup = false); 00157 virtual ~InstanceManager(); 00158 00159 const String& getName() const { return mName; } 00160 00166 void setInstancesPerBatch( size_t instancesPerBatch ); 00167 00176 void setMaxLookupTableInstances( size_t maxLookupTableInstances ); 00177 00189 size_t getMaxOrBestNumInstancesPerBatch( String materialName, size_t suggestedSize, uint16 flags ); 00190 00192 InstancedEntity* createInstancedEntity( const String &materialName ); 00193 00200 void cleanupEmptyBatches(void); 00201 00225 void defragmentBatches( bool optimizeCulling ); 00226 00242 void setSetting( BatchSettingId id, bool value, const String &materialName = StringUtil::BLANK ); 00243 00245 bool getSetting( BatchSettingId id, const String &materialName ) const; 00246 00250 bool hasSettings( const String &materialName ) const 00251 { return mBatchSettings.find( materialName ) != mBatchSettings.end(); } 00252 00254 void setBatchesAsStaticAndUpdate( bool bStatic ); 00255 00259 void _addDirtyBatch( InstanceBatch *dirtyBatch ); 00260 00262 void _updateDirtyBatches(void); 00263 00264 typedef ConstMapIterator<InstanceBatchMap> InstanceBatchMapIterator; 00265 typedef ConstVectorIterator<InstanceBatchVec> InstanceBatchIterator; 00266 00268 InstanceBatchMapIterator getInstanceBatchMapIterator(void) const 00269 { return InstanceBatchMapIterator( mInstanceBatches.begin(), mInstanceBatches.end() ); } 00270 00277 InstanceBatchIterator getInstanceBatchIterator( const String &materialName ) const 00278 { 00279 InstanceBatchMap::const_iterator it = mInstanceBatches.find( materialName ); 00280 return InstanceBatchIterator( it->second.begin(), it->second.end() ); 00281 } 00282 }; 00283 } 00284 00285 #endif
Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sun Sep 2 2012 07:27:22