OgreInstancedGeometry.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 __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 #include "OgreHeaderPrefix.h"
00041 
00042 namespace Ogre {
00043 
00105     class _OgreExport  InstancedGeometry : public BatchedGeometryAlloc
00106     {
00107     public:
00120         class _OgrePrivate OptimisedSubMeshGeometry : public BatchedGeometryAlloc
00121         {
00122         public:
00123             OptimisedSubMeshGeometry() :vertexData(0), indexData(0) {}
00124             ~OptimisedSubMeshGeometry() 
00125             {
00126                 delete vertexData;
00127                 delete indexData;
00128             }
00129             VertexData *vertexData;
00130             IndexData *indexData;
00131         };
00132         typedef list<OptimisedSubMeshGeometry*>::type OptimisedSubMeshGeometryList;
00135         struct SubMeshLodGeometryLink
00136         {
00137             VertexData* vertexData;
00138             IndexData* indexData;
00139         };
00140         typedef vector<SubMeshLodGeometryLink>::type SubMeshLodGeometryLinkList;
00141         typedef map<SubMesh*, SubMeshLodGeometryLinkList*>::type SubMeshGeometryLookup;
00143         struct QueuedSubMesh : public BatchedGeometryAlloc
00144         {
00145             SubMesh* submesh;
00147             SubMeshLodGeometryLinkList* geometryLodList;
00148             String materialName;
00149             Vector3 position;
00150             Quaternion orientation;
00151             Vector3 scale;
00153             AxisAlignedBox worldBounds;
00154             unsigned int ID;
00155         };
00156         typedef vector<QueuedSubMesh*>::type QueuedSubMeshList;
00157         typedef vector<String>::type QueuedSubMeshOriginList;
00159         struct QueuedGeometry : public BatchedGeometryAlloc
00160         {
00161             SubMeshLodGeometryLink* geometry;
00162             Vector3 position;
00163             Quaternion orientation;
00164             Vector3 scale;
00165             unsigned int ID;
00166         };
00167         typedef vector<QueuedGeometry*>::type QueuedGeometryList;
00168         
00169         // forward declarations
00170         class LODBucket;
00171         class MaterialBucket;
00172         class BatchInstance;
00173         class InstancedObject;
00174 
00179         class _OgreExport  GeometryBucket : public SimpleRenderable
00180         {
00181         protected:
00182             
00184             QueuedGeometryList mQueuedGeometry;
00186             InstancedGeometry*mBatch;
00188             MaterialBucket* mParent;
00190             String mFormatString;
00193             VertexData* mVertexData;
00196             IndexData* mIndexData;
00198             HardwareIndexBuffer::IndexType mIndexType;
00200             size_t mMaxVertexIndex;
00202             unsigned short mTexCoordIndex;
00203             AxisAlignedBox mAABB;
00204 
00205             template<typename T>
00206             void copyIndexes(const T* src, T* dst, size_t count, size_t indexOffset)
00207             {
00208                 if (indexOffset == 0)
00209                 {
00210                     memcpy(dst, src, sizeof(T) * count);
00211                 }
00212                 else
00213                 {
00214                     while(count--)
00215                     {
00216                         *dst++ = static_cast<T>(*src++ + indexOffset);
00217                     }
00218                 }
00219             }
00220 
00221             void _initGeometryBucket(const VertexData* vData, const IndexData* iData);
00222             void _initGeometryBucket(GeometryBucket* bucket);
00223 
00224         public:
00225             GeometryBucket(MaterialBucket* parent, const String& formatString, 
00226                 const VertexData* vData, const IndexData* iData);
00227             GeometryBucket(const String& name, MaterialBucket* parent, const String& formatString, 
00228                 const VertexData* vData, const IndexData* iData);
00229             GeometryBucket(MaterialBucket* parent,const String& formatString,GeometryBucket*bucket);
00230             GeometryBucket(const String& name, MaterialBucket* parent,const String& formatString,GeometryBucket*bucket);
00231             virtual ~GeometryBucket();
00232             MaterialBucket* getParent(void) { return mParent; }
00233             Real getBoundingRadius(void) const;
00235             const VertexData* getVertexData(void) const { return mVertexData; }
00237             const IndexData* getIndexData(void) const { return mIndexData; }
00239             const MaterialPtr& getMaterial(void) const;
00240             Technique* getTechnique(void) const;
00241             void getWorldTransforms(Matrix4* xform) const;
00242             virtual unsigned short getNumWorldTransforms(void) const ;
00243             Real getSquaredViewDepth(const Camera* cam) const;
00244             const LightList& getLights(void) const;
00245             bool getCastsShadows(void) const;
00246             String getFormatString(void) const;
00250             bool assign(QueuedGeometry* qsm);
00252             void build();
00254             void dump(std::ofstream& of) const;
00256             AxisAlignedBox & getAABB(void){return mAABB;}
00258             void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables);
00259 
00260         };
00261         class _OgreExport  InstancedObject : public BatchedGeometryAlloc
00262         {
00263             friend class GeometryBucket;
00264         public:
00265              enum TransformSpace
00266         {
00268             TS_LOCAL,
00270             TS_PARENT,
00272             TS_WORLD
00273         };
00275             typedef vector<GeometryBucket*>::type GeometryBucketList;
00276         protected:
00277             GeometryBucketList mGeometryBucketList;
00278             unsigned short mIndex;
00279             Matrix4  mTransformation;
00280             Quaternion mOrientation;
00281             Vector3 mScale;
00282             Vector3 mPosition;
00283             SkeletonInstance* mSkeletonInstance;
00285             Matrix4 *mBoneWorldMatrices;
00287             Matrix4 *mBoneMatrices;
00289             AnimationStateSet* mAnimationState;
00290             unsigned short mNumBoneMatrices;
00292             unsigned long mFrameAnimationLastUpdated;
00293         public:
00294             InstancedObject(unsigned short index);
00295             InstancedObject(unsigned short index,SkeletonInstance *skeleton,AnimationStateSet*animations);
00296             ~InstancedObject();
00297             void setPosition( Vector3  position);
00298             const Vector3& getPosition(void) const;
00299             void yaw(const Radian& angle);
00300             void pitch(const Radian& angle);
00301             void roll(const Radian& angle);
00302             void rotate(const Quaternion& q);
00303             void setScale(const Vector3& scale);
00304             const Vector3& getScale() const;
00305             void setOrientation(const Quaternion& q);
00306             void setPositionAndOrientation(Vector3 p, const Quaternion& q);
00307             Quaternion & getOrientation(void);
00308             void addBucketToList(GeometryBucket* bucket);
00309             void needUpdate();
00310             GeometryBucketList&getGeometryBucketList(void){return mGeometryBucketList;}
00311             void translate(const Matrix3& axes, const Vector3& move);
00312             void translate(const Vector3& d);
00313             Matrix3 getLocalAxes(void) const;
00314             void updateAnimation(void);
00315             AnimationState* getAnimationState(const String& name) const;
00316             SkeletonInstance*getSkeletonInstance(void){return mSkeletonInstance;}
00317 
00318         };
00321         class _OgreExport  MaterialBucket : public BatchedGeometryAlloc
00322         {
00323         public:
00325             typedef vector<GeometryBucket*>::type GeometryBucketList;
00326         protected:
00328             LODBucket* mParent;
00330             String mMaterialName;
00332             MaterialPtr mMaterial;
00334             Technique* mTechnique;
00335             int mLastIndex;
00337             GeometryBucketList mGeometryBucketList;
00338             // index to current Geometry Buckets for a given geometry format
00339             typedef map<String, GeometryBucket*>::type CurrentGeometryMap;
00340             CurrentGeometryMap mCurrentGeometryMap;
00342             String getGeometryFormatString(SubMeshLodGeometryLink* geom);
00343             
00344         public:
00345             MaterialBucket(LODBucket* parent, const String& materialName);
00346             virtual ~MaterialBucket();
00347             LODBucket* getParent(void) { return mParent; }
00349             const String& getMaterialName(void) const { return mMaterialName; }
00351             void assign(QueuedGeometry* qsm);
00353             void build();
00355             void addRenderables(RenderQueue* queue, uint8 group, 
00356                 Real lodValue);
00358             const MaterialPtr& getMaterial(void) const { return mMaterial; }
00360             typedef VectorIterator<GeometryBucketList> GeometryIterator;
00362             GeometryIterator getGeometryIterator(void);
00364             Technique* getCurrentTechnique(void) const { return mTechnique; }
00366             void dump(std::ofstream& of) const;
00368             MaterialBucket::CurrentGeometryMap* getMaterialBucketMap(void) const;
00370             MaterialBucket::GeometryBucketList*getGeometryBucketList(void) const;
00372             void updateContainers(GeometryBucket* bucket, const String &format);
00373             void setLastIndex(int index){mLastIndex=index;}
00374             int getLastIndex(){return mLastIndex;}
00375             void setMaterial(const String & name);
00376             void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables);
00377         
00378         };
00384         class _OgreExport  LODBucket : public BatchedGeometryAlloc
00385         {
00386         public:
00388             typedef map<String, MaterialBucket*>::type MaterialBucketMap;
00389         protected:
00391             BatchInstance* mParent;
00393             unsigned short mLod;
00395             Real mLodValue;
00397             MaterialBucketMap mMaterialBucketMap;
00399             QueuedGeometryList mQueuedGeometryList;
00400         public:
00401             LODBucket(BatchInstance* parent, unsigned short lod, Real lodValue);
00402             virtual ~LODBucket();
00403             BatchInstance* getParent(void) { return mParent; }
00405             ushort getLod(void) const { return mLod; }
00407             Real getLodValue(void) const { return mLodValue; }
00409             void assign(QueuedSubMesh* qsm, ushort atLod);
00411             void build();
00413             void addRenderables(RenderQueue* queue, uint8 group, 
00414                 Real lodValue);
00416             typedef MapIterator<MaterialBucketMap> MaterialIterator;
00418             MaterialIterator getMaterialIterator(void);
00420             void dump(std::ofstream& of) const;
00422             void updateContainers(MaterialBucket* bucket, String& name );
00423             void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables);
00424             
00425         };
00434         class _OgreExport  BatchInstance : public MovableObject
00435         {
00436             friend class MaterialBucket;
00437             public:
00438         
00439 
00441             typedef vector<LODBucket*>::type LODBucketList;
00442             typedef map<unsigned short, InstancedObject*>::type ObjectsMap;
00443             typedef MapIterator<ObjectsMap> InstancedObjectIterator;
00444         protected:
00445             
00447             InstancedGeometry* mParent;
00449             SceneManager* mSceneMgr;
00451             SceneNode* mNode;
00453             QueuedSubMeshList mQueuedSubMeshes;
00455             uint32 mBatchInstanceID;
00456 
00457             ObjectsMap mInstancesMap;
00458         public:
00460             Mesh::LodValueList mLodValues;
00462             AxisAlignedBox mAABB;
00464             Real mBoundingRadius;
00466             ushort mCurrentLod;
00468             Real mLodValue;
00470             Camera *mCamera;
00472             Real mSquaredViewDepth;
00473         protected:
00475             LODBucketList mLodBucketList;
00477             const LodStrategy *mLodStrategy;
00478 
00479         public:
00480             BatchInstance(InstancedGeometry* parent, const String& name, SceneManager* mgr, 
00481                 uint32 BatchInstanceID);
00482             virtual ~BatchInstance();
00483             // more fields can be added in subclasses
00484             InstancedGeometry* getParent(void) const { return mParent;}
00486             void assign(QueuedSubMesh* qmesh);
00488             void build();
00490             uint32 getID(void) const { return mBatchInstanceID; }
00492 //          const Vector3& getCentre(void) const { return mCentre; }
00493             const String& getMovableType(void) const;
00494             void _notifyCurrentCamera(Camera* cam);
00495             const AxisAlignedBox& getBoundingBox(void) const;
00496             void  setBoundingBox(AxisAlignedBox& box);
00497             Real getBoundingRadius(void) const;
00498             void _updateRenderQueue(RenderQueue* queue);
00499             bool isVisible(void) const;
00501             void visitRenderables(Renderable::Visitor* visitor, 
00502                 bool debugRenderables = false);
00503 
00504         //  uint32 getTypeFlags(void) const;
00505 
00506             typedef VectorIterator<LODBucketList> LODIterator;
00508             LODIterator getLODIterator(void);
00510             const LightList& getLights(void) const;
00511 
00513             void updateBoundingBox();
00514 
00516             void dump(std::ofstream& of) const;
00518             void updateContainers(LODBucket* bucket );
00520             void attachToScene();
00521             void addInstancedObject(unsigned short index, InstancedObject* object);
00522             InstancedObject*  isInstancedObjectPresent(unsigned short index);
00523             InstancedObjectIterator getObjectIterator();
00524             SceneNode*getSceneNode(void){return mNode;}
00525             ObjectsMap& getInstancesMap(void){return  mInstancesMap;}
00527             
00528         };
00532         typedef map<uint32, BatchInstance*>::type BatchInstanceMap;
00538         typedef vector<RenderOperation*>::type RenderOperationVector;
00539     protected:
00540         // General state & settings
00541         SceneManager* mOwner;
00542         String mName;
00543         bool mBuilt;
00544         Real mUpperDistance;
00545         Real mSquaredUpperDistance;
00546         bool mCastShadows;
00547         Vector3 mBatchInstanceDimensions;
00548         Vector3 mHalfBatchInstanceDimensions;
00549         Vector3 mOrigin;
00550         bool mVisible;
00552         bool mProvideWorldInverses;
00554         uint8 mRenderQueueID;
00556         bool mRenderQueueIDSet;
00558         unsigned int mObjectCount;
00559         QueuedSubMeshList mQueuedSubMeshes;
00560         BatchInstance*mInstancedGeometryInstance;
00564         SkeletonPtr mBaseSkeleton;
00565         SkeletonInstance *mSkeletonInstance;
00569         AnimationStateSet* mAnimationState;
00572         OptimisedSubMeshGeometryList mOptimisedSubMeshGeometryList;
00573 
00578         SubMeshGeometryLookup mSubMeshGeometryLookup;
00579             
00581         BatchInstanceMap mBatchInstanceMap;
00585         RenderOperationVector mRenderOps;
00589         virtual BatchInstance* getBatchInstance(const AxisAlignedBox& bounds, bool autoCreate);
00591         virtual BatchInstance* getBatchInstance(const Vector3& point, bool autoCreate);
00593         virtual BatchInstance* getBatchInstance(ushort x, ushort y, ushort z, bool autoCreate);
00595         virtual BatchInstance* getBatchInstance(uint32 index);
00598         virtual void getBatchInstanceIndexes(const Vector3& point, 
00599             ushort& x, ushort& y, ushort& z);
00602         virtual BatchInstance* getInstancedGeometryInstance(void);
00605         virtual uint32 packIndex(ushort x, ushort y, ushort z);
00608         virtual Real getVolumeIntersection(const AxisAlignedBox& box,  
00609             ushort x, ushort y, ushort z);
00612         virtual AxisAlignedBox getBatchInstanceBounds(ushort x, ushort y, ushort z);
00615         virtual Vector3 getBatchInstanceCentre(ushort x, ushort y, ushort z);
00617         virtual AxisAlignedBox calculateBounds(VertexData* vertexData, 
00618             const Vector3& position, const Quaternion& orientation, 
00619             const Vector3& scale);
00621         SubMeshLodGeometryLinkList* determineGeometry(SubMesh* sm);
00623         void splitGeometry(VertexData* vd, IndexData* id, 
00624             SubMeshLodGeometryLink* targetGeomLink);
00625 
00626         typedef map<size_t, size_t>::type IndexRemap;
00631         template <typename T>
00632         void buildIndexRemap(T* pBuffer, size_t numIndexes, IndexRemap& remap)
00633         {
00634             remap.clear();
00635             for (size_t i = 0; i < numIndexes; ++i)
00636             {
00637                 // use insert since duplicates are silently discarded
00638                 remap.insert(IndexRemap::value_type(*pBuffer++, remap.size()));
00639                 // this will have mapped oldindex -> new index IF oldindex
00640                 // wasn't already there
00641             }
00642         }
00644         template <typename T>
00645         void remapIndexes(T* src, T* dst, const IndexRemap& remap, 
00646                 size_t numIndexes)
00647         {
00648             for (size_t i = 0; i < numIndexes; ++i)
00649             {
00650                 // look up original and map to target
00651                 IndexRemap::const_iterator ix = remap.find(*src++);
00652                 assert(ix != remap.end());
00653                 *dst++ = static_cast<T>(ix->second);
00654             }
00655         }
00656         
00657     public:
00659         InstancedGeometry(SceneManager* owner, const String& name);
00661         virtual ~InstancedGeometry();
00662 
00664         const String& getName(void) const { return mName; }
00684         virtual void addEntity(Entity* ent, const Vector3& position,
00685             const Quaternion& orientation = Quaternion::IDENTITY, 
00686             const Vector3& scale = Vector3::UNIT_SCALE);
00687 
00707         virtual void addSceneNode(const SceneNode* node);
00708 
00719         virtual void build(void);
00728         void addBatchInstance(void);
00734         virtual void destroy(void);
00735 
00739         virtual void reset(void);
00740 
00750         virtual void setRenderingDistance(Real dist) { 
00751             mUpperDistance = dist; 
00752             mSquaredUpperDistance = mUpperDistance * mUpperDistance;
00753         }
00754 
00756         virtual Real getRenderingDistance(void) const { return mUpperDistance; }
00757 
00759         virtual Real getSquaredRenderingDistance(void) const 
00760         { return mSquaredUpperDistance; }
00761 
00763         virtual void setVisible(bool visible);
00764 
00766         virtual bool isVisible(void) const { return mVisible; }
00767 
00785         virtual void setCastShadows(bool castShadows);
00787         virtual bool getCastShadows(void) { return mCastShadows; }
00788 
00799         virtual void setBatchInstanceDimensions(const Vector3& size) { 
00800             mBatchInstanceDimensions = size; 
00801             mHalfBatchInstanceDimensions = size * 0.5;
00802         }
00804         virtual const Vector3& getBatchInstanceDimensions(void) const { return mBatchInstanceDimensions; }
00816         virtual void setOrigin(const Vector3& origin) { mOrigin = origin; }
00818         virtual const Vector3& getOrigin(void) const { return mOrigin; }
00819 
00831         virtual void setRenderQueueGroup(uint8 queueID);
00832 
00834         virtual uint8 getRenderQueueGroup(void) const;
00836         typedef MapIterator<BatchInstanceMap> BatchInstanceIterator;
00838         BatchInstanceIterator getBatchInstanceIterator(void);
00840         RenderOperationVector& getRenderOperationVector(){return mRenderOps;}
00842         void visitRenderables(Renderable::Visitor* visitor, 
00843             bool debugRenderables = false);
00844 
00848         virtual void dump(const String& filename) const;
00853         SkeletonInstance *getBaseSkeletonInstance(void){return mSkeletonInstance;}
00858         SkeletonPtr getBaseSkeleton(void){return mBaseSkeleton;}
00863         AnimationStateSet* getBaseAnimationState(void){return mAnimationState;}
00868         unsigned int getObjectCount(void){return mObjectCount;}
00869 
00876         virtual void setProvideWorldInverses(bool flag);
00877 
00883         virtual bool getProvideWorldInverses(void) const { return mProvideWorldInverses; }
00884     };
00885 
00888 }
00889 
00890 #include "OgreHeaderSuffix.h"
00891 
00892 #endif
00893 

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