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 __InstancedGeometry_H__ 00029 #define __InstancedGeometry_H__ 00030 00031 #include "OgrePrerequisites.h" 00032 #include "OgreMovableObject.h" 00033 #include "OgreSimpleRenderable.h" 00034 #include "OgreSkeleton.h" 00035 #include "OgreSkeletonInstance.h" 00036 #include "OgreAnimationTrack.h" 00037 #include "OgreBone.h" 00038 #include "OgreIteratorWrappers.h" 00039 #include "OgreMesh.h" 00040 00041 namespace Ogre { 00042 00104 class _OgreExport InstancedGeometry : public BatchedGeometryAlloc 00105 { 00106 public: 00119 class _OgrePrivate OptimisedSubMeshGeometry : public BatchedGeometryAlloc 00120 { 00121 public: 00122 OptimisedSubMeshGeometry() :vertexData(0), indexData(0) {} 00123 ~OptimisedSubMeshGeometry() 00124 { 00125 delete vertexData; 00126 delete indexData; 00127 } 00128 VertexData *vertexData; 00129 IndexData *indexData; 00130 }; 00131 typedef list<OptimisedSubMeshGeometry*>::type OptimisedSubMeshGeometryList; 00134 struct SubMeshLodGeometryLink 00135 { 00136 VertexData* vertexData; 00137 IndexData* indexData; 00138 }; 00139 typedef vector<SubMeshLodGeometryLink>::type SubMeshLodGeometryLinkList; 00140 typedef map<SubMesh*, SubMeshLodGeometryLinkList*>::type SubMeshGeometryLookup; 00142 struct QueuedSubMesh : public BatchedGeometryAlloc 00143 { 00144 SubMesh* submesh; 00146 SubMeshLodGeometryLinkList* geometryLodList; 00147 String materialName; 00148 Vector3 position; 00149 Quaternion orientation; 00150 Vector3 scale; 00152 AxisAlignedBox worldBounds; 00153 unsigned int ID; 00154 }; 00155 typedef vector<QueuedSubMesh*>::type QueuedSubMeshList; 00156 typedef vector<String>::type QueuedSubMeshOriginList; 00158 struct QueuedGeometry : public BatchedGeometryAlloc 00159 { 00160 SubMeshLodGeometryLink* geometry; 00161 Vector3 position; 00162 Quaternion orientation; 00163 Vector3 scale; 00164 unsigned int ID; 00165 }; 00166 typedef vector<QueuedGeometry*>::type QueuedGeometryList; 00167 00168 // forward declarations 00169 class LODBucket; 00170 class MaterialBucket; 00171 class BatchInstance; 00172 class InstancedObject; 00173 00178 class _OgreExport GeometryBucket : public SimpleRenderable 00179 { 00180 protected: 00181 00183 QueuedGeometryList mQueuedGeometry; 00185 InstancedGeometry*mBatch; 00187 MaterialBucket* mParent; 00189 String mFormatString; 00192 VertexData* mVertexData; 00195 IndexData* mIndexData; 00197 HardwareIndexBuffer::IndexType mIndexType; 00199 size_t mMaxVertexIndex; 00201 unsigned short mTexCoordIndex; 00202 AxisAlignedBox mAABB; 00203 00204 template<typename T> 00205 void copyIndexes(const T* src, T* dst, size_t count, size_t indexOffset) 00206 { 00207 if (indexOffset == 0) 00208 { 00209 memcpy(dst, src, sizeof(T) * count); 00210 } 00211 else 00212 { 00213 while(count--) 00214 { 00215 *dst++ = static_cast<T>(*src++ + indexOffset); 00216 } 00217 } 00218 } 00219 00220 void _initGeometryBucket(const VertexData* vData, const IndexData* iData); 00221 void _initGeometryBucket(GeometryBucket* bucket); 00222 00223 public: 00224 GeometryBucket(MaterialBucket* parent, const String& formatString, 00225 const VertexData* vData, const IndexData* iData); 00226 GeometryBucket(const String& name, MaterialBucket* parent, const String& formatString, 00227 const VertexData* vData, const IndexData* iData); 00228 GeometryBucket(MaterialBucket* parent,const String& formatString,GeometryBucket*bucket); 00229 GeometryBucket(const String& name, MaterialBucket* parent,const String& formatString,GeometryBucket*bucket); 00230 virtual ~GeometryBucket(); 00231 MaterialBucket* getParent(void) { return mParent; } 00232 Real getBoundingRadius(void) const; 00234 const VertexData* getVertexData(void) const { return mVertexData; } 00236 const IndexData* getIndexData(void) const { return mIndexData; } 00238 const MaterialPtr& getMaterial(void) const; 00239 Technique* getTechnique(void) const; 00240 void getWorldTransforms(Matrix4* xform) const; 00241 virtual unsigned short getNumWorldTransforms(void) const ; 00242 Real getSquaredViewDepth(const Camera* cam) const; 00243 const LightList& getLights(void) const; 00244 bool getCastsShadows(void) const; 00245 String getFormatString(void) const; 00249 bool assign(QueuedGeometry* qsm); 00251 void build(); 00253 void dump(std::ofstream& of) const; 00255 AxisAlignedBox & getAABB(void){return mAABB;} 00257 void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables); 00258 00259 }; 00260 class _OgreExport InstancedObject : public BatchedGeometryAlloc 00261 { 00262 friend class GeometryBucket; 00263 public: 00264 enum TransformSpace 00265 { 00267 TS_LOCAL, 00269 TS_PARENT, 00271 TS_WORLD 00272 }; 00274 typedef vector<GeometryBucket*>::type GeometryBucketList; 00275 protected: 00276 GeometryBucketList mGeometryBucketList; 00277 unsigned short mIndex; 00278 Matrix4 mTransformation; 00279 Quaternion mOrientation; 00280 Vector3 mScale; 00281 Vector3 mPosition; 00282 SkeletonInstance* mSkeletonInstance; 00284 Matrix4 *mBoneWorldMatrices; 00286 Matrix4 *mBoneMatrices; 00288 AnimationStateSet* mAnimationState; 00289 unsigned short mNumBoneMatrices; 00291 unsigned long mFrameAnimationLastUpdated; 00292 public: 00293 InstancedObject(unsigned short index); 00294 InstancedObject(unsigned short index,SkeletonInstance *skeleton,AnimationStateSet*animations); 00295 ~InstancedObject(); 00296 void setPosition( Vector3 position); 00297 const Vector3& getPosition(void) const; 00298 void yaw(const Radian& angle); 00299 void pitch(const Radian& angle); 00300 void roll(const Radian& angle); 00301 void rotate(const Quaternion& q); 00302 void setScale(const Vector3& scale); 00303 const Vector3& getScale() const; 00304 void setOrientation(const Quaternion& q); 00305 void setPositionAndOrientation(Vector3 p, const Quaternion& q); 00306 Quaternion & getOrientation(void); 00307 void addBucketToList(GeometryBucket* bucket); 00308 void needUpdate(); 00309 GeometryBucketList&getGeometryBucketList(void){return mGeometryBucketList;} 00310 void translate(const Matrix3& axes, const Vector3& move); 00311 void translate(const Vector3& d); 00312 Matrix3 getLocalAxes(void) const; 00313 void updateAnimation(void); 00314 AnimationState* getAnimationState(const String& name) const; 00315 SkeletonInstance*getSkeletonInstance(void){return mSkeletonInstance;} 00316 00317 }; 00320 class _OgreExport MaterialBucket : public BatchedGeometryAlloc 00321 { 00322 public: 00324 typedef vector<GeometryBucket*>::type GeometryBucketList; 00325 protected: 00327 LODBucket* mParent; 00329 String mMaterialName; 00331 MaterialPtr mMaterial; 00333 Technique* mTechnique; 00334 int mLastIndex; 00336 GeometryBucketList mGeometryBucketList; 00337 // index to current Geometry Buckets for a given geometry format 00338 typedef map<String, GeometryBucket*>::type CurrentGeometryMap; 00339 CurrentGeometryMap mCurrentGeometryMap; 00341 String getGeometryFormatString(SubMeshLodGeometryLink* geom); 00342 00343 public: 00344 MaterialBucket(LODBucket* parent, const String& materialName); 00345 virtual ~MaterialBucket(); 00346 LODBucket* getParent(void) { return mParent; } 00348 const String& getMaterialName(void) const { return mMaterialName; } 00350 void assign(QueuedGeometry* qsm); 00352 void build(); 00354 void addRenderables(RenderQueue* queue, uint8 group, 00355 Real lodValue); 00357 const MaterialPtr& getMaterial(void) const { return mMaterial; } 00359 typedef VectorIterator<GeometryBucketList> GeometryIterator; 00361 GeometryIterator getGeometryIterator(void); 00363 Technique* getCurrentTechnique(void) const { return mTechnique; } 00365 void dump(std::ofstream& of) const; 00367 MaterialBucket::CurrentGeometryMap* getMaterialBucketMap(void) const; 00369 MaterialBucket::GeometryBucketList*getGeometryBucketList(void) const; 00371 void updateContainers(GeometryBucket* bucket, const String &format); 00372 void setLastIndex(int index){mLastIndex=index;} 00373 int getLastIndex(){return mLastIndex;} 00374 void setMaterial(const String & name); 00375 void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables); 00376 00377 }; 00383 class _OgreExport LODBucket : public BatchedGeometryAlloc 00384 { 00385 public: 00387 typedef map<String, MaterialBucket*>::type MaterialBucketMap; 00388 protected: 00390 BatchInstance* mParent; 00392 unsigned short mLod; 00394 Real mLodValue; 00396 MaterialBucketMap mMaterialBucketMap; 00398 QueuedGeometryList mQueuedGeometryList; 00399 public: 00400 LODBucket(BatchInstance* parent, unsigned short lod, Real lodValue); 00401 virtual ~LODBucket(); 00402 BatchInstance* getParent(void) { return mParent; } 00404 ushort getLod(void) const { return mLod; } 00406 Real getLodValue(void) const { return mLodValue; } 00408 void assign(QueuedSubMesh* qsm, ushort atLod); 00410 void build(); 00412 void addRenderables(RenderQueue* queue, uint8 group, 00413 Real lodValue); 00415 typedef MapIterator<MaterialBucketMap> MaterialIterator; 00417 MaterialIterator getMaterialIterator(void); 00419 void dump(std::ofstream& of) const; 00421 void updateContainers(MaterialBucket* bucket, String& name ); 00422 void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables); 00423 00424 }; 00433 class _OgreExport BatchInstance : public MovableObject 00434 { 00435 friend class MaterialBucket; 00436 public: 00437 00438 00440 typedef vector<LODBucket*>::type LODBucketList; 00441 typedef map<unsigned short, InstancedObject*>::type ObjectsMap; 00442 typedef MapIterator<ObjectsMap> InstancedObjectIterator; 00443 protected: 00444 00446 InstancedGeometry* mParent; 00448 SceneManager* mSceneMgr; 00450 SceneNode* mNode; 00452 QueuedSubMeshList mQueuedSubMeshes; 00454 uint32 mBatchInstanceID; 00455 00456 ObjectsMap mInstancesMap; 00457 public: 00459 Mesh::LodValueList mLodValues; 00461 AxisAlignedBox mAABB; 00463 Real mBoundingRadius; 00465 ushort mCurrentLod; 00467 Real mLodValue; 00469 Camera *mCamera; 00471 Real mSquaredViewDepth; 00472 protected: 00474 LODBucketList mLodBucketList; 00476 const LodStrategy *mLodStrategy; 00477 00478 public: 00479 BatchInstance(InstancedGeometry* parent, const String& name, SceneManager* mgr, 00480 uint32 BatchInstanceID); 00481 virtual ~BatchInstance(); 00482 // more fields can be added in subclasses 00483 InstancedGeometry* getParent(void) const { return mParent;} 00485 void assign(QueuedSubMesh* qmesh); 00487 void build(); 00489 uint32 getID(void) const { return mBatchInstanceID; } 00491 // const Vector3& getCentre(void) const { return mCentre; } 00492 const String& getMovableType(void) const; 00493 void _notifyCurrentCamera(Camera* cam); 00494 const AxisAlignedBox& getBoundingBox(void) const; 00495 void setBoundingBox(AxisAlignedBox& box); 00496 Real getBoundingRadius(void) const; 00497 void _updateRenderQueue(RenderQueue* queue); 00498 bool isVisible(void) const; 00500 void visitRenderables(Renderable::Visitor* visitor, 00501 bool debugRenderables = false); 00502 00503 // uint32 getTypeFlags(void) const; 00504 00505 typedef VectorIterator<LODBucketList> LODIterator; 00507 LODIterator getLODIterator(void); 00509 const LightList& getLights(void) const; 00510 00512 void updateBoundingBox(); 00513 00515 void dump(std::ofstream& of) const; 00517 void updateContainers(LODBucket* bucket ); 00519 void attachToScene(); 00520 void addInstancedObject(unsigned short index, InstancedObject* object); 00521 InstancedObject* isInstancedObjectPresent(unsigned short index); 00522 InstancedObjectIterator getObjectIterator(); 00523 SceneNode*getSceneNode(void){return mNode;} 00524 ObjectsMap& getInstancesMap(void){return mInstancesMap;} 00526 00527 }; 00531 typedef map<uint32, BatchInstance*>::type BatchInstanceMap; 00537 typedef vector<RenderOperation*>::type RenderOperationVector; 00538 protected: 00539 // General state & settings 00540 SceneManager* mOwner; 00541 String mName; 00542 bool mBuilt; 00543 Real mUpperDistance; 00544 Real mSquaredUpperDistance; 00545 bool mCastShadows; 00546 Vector3 mBatchInstanceDimensions; 00547 Vector3 mHalfBatchInstanceDimensions; 00548 Vector3 mOrigin; 00549 bool mVisible; 00551 bool mProvideWorldInverses; 00553 uint8 mRenderQueueID; 00555 bool mRenderQueueIDSet; 00557 unsigned int mObjectCount; 00558 QueuedSubMeshList mQueuedSubMeshes; 00559 BatchInstance*mInstancedGeometryInstance; 00563 SkeletonPtr mBaseSkeleton; 00564 SkeletonInstance *mSkeletonInstance; 00568 AnimationStateSet* mAnimationState; 00571 OptimisedSubMeshGeometryList mOptimisedSubMeshGeometryList; 00572 00577 SubMeshGeometryLookup mSubMeshGeometryLookup; 00578 00580 BatchInstanceMap mBatchInstanceMap; 00584 RenderOperationVector mRenderOps; 00588 virtual BatchInstance* getBatchInstance(const AxisAlignedBox& bounds, bool autoCreate); 00590 virtual BatchInstance* getBatchInstance(const Vector3& point, bool autoCreate); 00592 virtual BatchInstance* getBatchInstance(ushort x, ushort y, ushort z, bool autoCreate); 00594 virtual BatchInstance* getBatchInstance(uint32 index); 00597 virtual void getBatchInstanceIndexes(const Vector3& point, 00598 ushort& x, ushort& y, ushort& z); 00601 virtual BatchInstance* getInstancedGeometryInstance(void); 00604 virtual uint32 packIndex(ushort x, ushort y, ushort z); 00607 virtual Real getVolumeIntersection(const AxisAlignedBox& box, 00608 ushort x, ushort y, ushort z); 00611 virtual AxisAlignedBox getBatchInstanceBounds(ushort x, ushort y, ushort z); 00614 virtual Vector3 getBatchInstanceCentre(ushort x, ushort y, ushort z); 00616 virtual AxisAlignedBox calculateBounds(VertexData* vertexData, 00617 const Vector3& position, const Quaternion& orientation, 00618 const Vector3& scale); 00620 SubMeshLodGeometryLinkList* determineGeometry(SubMesh* sm); 00622 void splitGeometry(VertexData* vd, IndexData* id, 00623 SubMeshLodGeometryLink* targetGeomLink); 00624 00625 typedef map<size_t, size_t>::type IndexRemap; 00630 template <typename T> 00631 void buildIndexRemap(T* pBuffer, size_t numIndexes, IndexRemap& remap) 00632 { 00633 remap.clear(); 00634 for (size_t i = 0; i < numIndexes; ++i) 00635 { 00636 // use insert since duplicates are silently discarded 00637 remap.insert(IndexRemap::value_type(*pBuffer++, remap.size())); 00638 // this will have mapped oldindex -> new index IF oldindex 00639 // wasn't already there 00640 } 00641 } 00643 template <typename T> 00644 void remapIndexes(T* src, T* dst, const IndexRemap& remap, 00645 size_t numIndexes) 00646 { 00647 for (size_t i = 0; i < numIndexes; ++i) 00648 { 00649 // look up original and map to target 00650 IndexRemap::const_iterator ix = remap.find(*src++); 00651 assert(ix != remap.end()); 00652 *dst++ = static_cast<T>(ix->second); 00653 } 00654 } 00655 00656 public: 00658 InstancedGeometry(SceneManager* owner, const String& name); 00660 virtual ~InstancedGeometry(); 00661 00663 const String& getName(void) const { return mName; } 00683 virtual void addEntity(Entity* ent, const Vector3& position, 00684 const Quaternion& orientation = Quaternion::IDENTITY, 00685 const Vector3& scale = Vector3::UNIT_SCALE); 00686 00706 virtual void addSceneNode(const SceneNode* node); 00707 00718 virtual void build(void); 00727 void addBatchInstance(void); 00733 virtual void destroy(void); 00734 00738 virtual void reset(void); 00739 00749 virtual void setRenderingDistance(Real dist) { 00750 mUpperDistance = dist; 00751 mSquaredUpperDistance = mUpperDistance * mUpperDistance; 00752 } 00753 00755 virtual Real getRenderingDistance(void) const { return mUpperDistance; } 00756 00758 virtual Real getSquaredRenderingDistance(void) const 00759 { return mSquaredUpperDistance; } 00760 00762 virtual void setVisible(bool visible); 00763 00765 virtual bool isVisible(void) const { return mVisible; } 00766 00784 virtual void setCastShadows(bool castShadows); 00786 virtual bool getCastShadows(void) { return mCastShadows; } 00787 00798 virtual void setBatchInstanceDimensions(const Vector3& size) { 00799 mBatchInstanceDimensions = size; 00800 mHalfBatchInstanceDimensions = size * 0.5; 00801 } 00803 virtual const Vector3& getBatchInstanceDimensions(void) const { return mBatchInstanceDimensions; } 00815 virtual void setOrigin(const Vector3& origin) { mOrigin = origin; } 00817 virtual const Vector3& getOrigin(void) const { return mOrigin; } 00818 00830 virtual void setRenderQueueGroup(uint8 queueID); 00831 00833 virtual uint8 getRenderQueueGroup(void) const; 00835 typedef MapIterator<BatchInstanceMap> BatchInstanceIterator; 00837 BatchInstanceIterator getBatchInstanceIterator(void); 00839 RenderOperationVector& getRenderOperationVector(){return mRenderOps;} 00841 void visitRenderables(Renderable::Visitor* visitor, 00842 bool debugRenderables = false); 00843 00847 virtual void dump(const String& filename) const; 00852 SkeletonInstance *getBaseSkeletonInstance(void){return mSkeletonInstance;} 00857 SkeletonPtr getBaseSkeleton(void){return mBaseSkeleton;} 00862 AnimationStateSet* getBaseAnimationState(void){return mAnimationState;} 00867 unsigned int getObjectCount(void){return mObjectCount;} 00868 00875 virtual void setProvideWorldInverses(bool flag); 00876 00882 virtual bool getProvideWorldInverses(void) const { return mProvideWorldInverses; } 00883 }; 00884 00887 } 00888 00889 #endif 00890
Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Fri May 25 23:36:24 2012