# 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-2006 Torus Knot Software Ltd
    00008 Also see acknowledgements in Readme.html
    00009
    00010 This program is free software; you can redistribute it and/or modify it under
    00011 the terms of the GNU Lesser General Public License as published by the Free Software
    00012 Foundation; either version 2 of the License, or (at your option) any later
    00013 version.
    00014
    00015 This program is distributed in the hope that it will be useful, but WITHOUT
    00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    00018
    00019 You should have received a copy of the GNU Lesser General Public License along with
    00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
    00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
    00022 http://www.gnu.org/copyleft/lesser.txt.
    00023
    00024 You may alternatively use this source under the terms of a specific version of
    00025 the OGRE Unrestricted License provided you have obtained such a license from
    00026 Torus Knot Software Ltd.
    00027 -----------------------------------------------------------------------------
    00028 */
    00029 #ifndef __InstancedGeometry_H__
    00030 #define __InstancedGeometry_H__
    00031
    00032 #include "OgrePrerequisites.h"
    00033 #include "OgreMovableObject.h"
    00034 #include "OgreSimpleRenderable.h"
    00035 #include "OgreSkeleton.h"
    00036 #include "OgreSkeletonInstance.h"
    00037 #include "OgreAnimationTrack.h"
    00038 #include "OgreBone.h"
    00039 #include "OgreIteratorWrappers.h"
    00040
    00041 namespace Ogre {
    00042
    00098     class _OgreExport  InstancedGeometry : public BatchedGeometryAlloc
    00099     {
    00100     public:
    00113         class _OgrePrivate OptimisedSubMeshGeometry : public BatchedGeometryAlloc
    00114         {
    00115         public:
    00116             OptimisedSubMeshGeometry() :vertexData(0), indexData(0) {}
    00117             ~OptimisedSubMeshGeometry()
    00118             {
    00119                 delete vertexData;
    00120                 delete indexData;
    00121             }
    00122             VertexData *vertexData;
    00123             IndexData *indexData;
    00124         };
    00125         typedef std::list<OptimisedSubMeshGeometry*> OptimisedSubMeshGeometryList;
    00128         struct SubMeshLodGeometryLink
    00129         {
    00130             VertexData* vertexData;
    00131             IndexData* indexData;
    00132         };
    00133         typedef std::vector<SubMeshLodGeometryLink> SubMeshLodGeometryLinkList;
    00134         typedef std::map<SubMesh*, SubMeshLodGeometryLinkList*> SubMeshGeometryLookup;
    00136         struct QueuedSubMesh : public BatchedGeometryAlloc
    00137         {
    00138             SubMesh* submesh;
    00140             SubMeshLodGeometryLinkList* geometryLodList;
    00141             String materialName;
    00142             Vector3 position;
    00143             Quaternion orientation;
    00144             Vector3 scale;
    00146             AxisAlignedBox worldBounds;
    00147             unsigned int ID;
    00148         };
    00149         typedef std::vector<QueuedSubMesh*> QueuedSubMeshList;
    00150         typedef std::vector<String> QueuedSubMeshOriginList;
    00152         struct QueuedGeometry : public BatchedGeometryAlloc
    00153         {
    00154             SubMeshLodGeometryLink* geometry;
    00155             Vector3 position;
    00156             Quaternion orientation;
    00157             Vector3 scale;
    00158             unsigned int ID;
    00159         };
    00160         typedef std::vector<QueuedGeometry*> QueuedGeometryList;
    00161
    00162         // forward declarations
    00163         class LODBucket;
    00164         class MaterialBucket;
    00165         class BatchInstance;
    00166         class InstancedObject;
    00167
    00172         class _OgreExport  GeometryBucket : public SimpleRenderable
    00173         {
    00174         protected:
    00175
    00177             QueuedGeometryList mQueuedGeometry;
    00179             InstancedGeometry*mBatch;
    00181             MaterialBucket* mParent;
    00183             String mFormatString;
    00186             VertexData* mVertexData;
    00189             IndexData* mIndexData;
    00191             HardwareIndexBuffer::IndexType mIndexType;
    00193             size_t mMaxVertexIndex;
    00195             unsigned short mTexCoordIndex;
    00196             AxisAlignedBox mAABB;
    00197
    00198             template<typename T>
    00199             void copyIndexes(const T* src, T* dst, size_t count, size_t indexOffset)
    00200             {
    00201                 if (indexOffset == 0)
    00202                 {
    00203                     memcpy(dst, src, sizeof(T) * count);
    00204                 }
    00205                 else
    00206                 {
    00207                     while(count--)
    00208                     {
    00209                         *dst++ = static_cast<T>(*src++ + indexOffset);
    00210                     }
    00211                 }
    00212             }
    00213         public:
    00214             GeometryBucket(MaterialBucket* parent, const String& formatString,
    00215                 const VertexData* vData, const IndexData* iData);
    00216             GeometryBucket(MaterialBucket* parent,const String& formatString,GeometryBucket*bucket);
    00217             virtual ~GeometryBucket();
    00218             MaterialBucket* getParent(void) { return mParent; }
    00219             Real getBoundingRadius(void) const;
    00221             const VertexData* getVertexData(void) const { return mVertexData; }
    00223             const IndexData* getIndexData(void) const { return mIndexData; }
    00225             const MaterialPtr& getMaterial(void) const;
    00226             Technique* getTechnique(void) const;
    00227             void getWorldTransforms(Matrix4* xform) const;
    00228             virtual unsigned short getNumWorldTransforms(void) const ;
    00229             Real getSquaredViewDepth(const Camera* cam) const;
    00230             const LightList& getLights(void) const;
    00231             bool getCastsShadows(void) const;
    00232             String getFormatString(void) const;
    00236             bool assign(QueuedGeometry* qsm);
    00238             void build();
    00240             void dump(std::ofstream& of) const;
    00242             AxisAlignedBox & getAABB(void){return mAABB;};
    00244             void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables);
    00245
    00246         };
    00247         class _OgreExport  InstancedObject : public BatchedGeometryAlloc
    00248         {
    00249             friend class GeometryBucket;
    00250         public:
    00251              enum TransformSpace
    00252         {
    00254             TS_LOCAL,
    00256             TS_PARENT,
    00258             TS_WORLD
    00259         };
    00261             typedef std::vector<GeometryBucket*> GeometryBucketList;
    00262         protected:
    00263             GeometryBucketList mGeometryBucketList;
    00264             unsigned short mIndex;
    00265             Matrix4  mTransformation;
    00266             Quaternion mOrientation;
    00267             Vector3 mScale;
    00268             Vector3 mPosition;
    00269             SkeletonInstance* mSkeletonInstance;
    00271             Matrix4 *mBoneWorldMatrices;
    00273             Matrix4 *mBoneMatrices;
    00275             AnimationStateSet* mAnimationState;
    00276             unsigned short mNumBoneMatrices;
    00278             unsigned long mFrameAnimationLastUpdated;
    00279         public:
    00280             InstancedObject(int index);
    00281             InstancedObject(int index,SkeletonInstance *skeleton,AnimationStateSet*animations);
    00282             ~InstancedObject();
    00283             void setPosition( Vector3  position);
    00284             Vector3 & getPosition(void);
    00285             void yaw(const Radian& angle);
    00286             void pitch(const Radian& angle);
    00287             void roll(const Radian& angle);
    00288             void rotate(const Quaternion& q);
    00289             void setScale(const Vector3& scale);
    00290             void setOrientation(const Quaternion& q);
    00291             void setPositionAndOrientation(Vector3 p, const Quaternion& q);
    00292             Quaternion & getOrientation(void);
    00293             void addBucketToList(GeometryBucket* bucket);
    00294             void needUpdate();
    00295             GeometryBucketList&getGeometryBucketList(void){return mGeometryBucketList;}
    00296             void translate(const Matrix3& axes, const Vector3& move);
    00297             void translate(const Vector3& d);
    00298             Matrix3 getLocalAxes(void) const;
    00299             void updateAnimation(void);
    00300             AnimationState* getAnimationState(const String& name) const;
    00301             SkeletonInstance*getSkeletonInstance(void){return mSkeletonInstance;}
    00302
    00303         };
    00306         class _OgreExport  MaterialBucket : public BatchedGeometryAlloc
    00307         {
    00308         public:
    00310             typedef std::vector<GeometryBucket*> GeometryBucketList;
    00311         protected:
    00313             LODBucket* mParent;
    00315             String mMaterialName;
    00317             MaterialPtr mMaterial;
    00319             Technique* mTechnique;
    00320             int mLastIndex;
    00322             GeometryBucketList mGeometryBucketList;
    00323             // index to current Geometry Buckets for a given geometry format
    00324             typedef std::map<String, GeometryBucket*> CurrentGeometryMap;
    00325             CurrentGeometryMap mCurrentGeometryMap;
    00327             String getGeometryFormatString(SubMeshLodGeometryLink* geom);
    00328
    00329         public:
    00330             MaterialBucket(LODBucket* parent, const String& materialName);
    00331             virtual ~MaterialBucket();
    00332             LODBucket* getParent(void) { return mParent; }
    00334             const String& getMaterialName(void) const { return mMaterialName; }
    00336             void assign(QueuedGeometry* qsm);
    00338             void build();
    00340             void addRenderables(RenderQueue* queue, uint8 group,
    00341                 Real camSquaredDist);
    00343             const MaterialPtr& getMaterial(void) const { return mMaterial; }
    00345             typedef VectorIterator<GeometryBucketList> GeometryIterator;
    00347             GeometryIterator getGeometryIterator(void);
    00349             Technique* getCurrentTechnique(void) const { return mTechnique; }
    00351             void dump(std::ofstream& of) const;
    00353             MaterialBucket::CurrentGeometryMap* getMaterialBucketMap(void) const;
    00355             MaterialBucket::GeometryBucketList*getGeometryBucketList(void) const;
    00357             void updateContainers(GeometryBucket* bucket, const String &format);
    00358             void setLastIndex(int index){mLastIndex=index;}
    00359             int getLastIndex(){return mLastIndex;}
    00360             void setMaterial(const String & name);
    00361             void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables);
    00362
    00363         };
    00369         class _OgreExport  LODBucket : public BatchedGeometryAlloc
    00370         {
    00371         public:
    00373             typedef std::map<String, MaterialBucket*> MaterialBucketMap;
    00374         protected:
    00376             BatchInstance* mParent;
    00378             unsigned short mLod;
    00380             Real mSquaredDistance;
    00382             MaterialBucketMap mMaterialBucketMap;
    00384             QueuedGeometryList mQueuedGeometryList;
    00385         public:
    00386             LODBucket(BatchInstance* parent, unsigned short lod, Real lodDist);
    00387             virtual ~LODBucket();
    00388             BatchInstance* getParent(void) { return mParent; }
    00390             ushort getLod(void) const { return mLod; }
    00392             Real getSquaredDistance(void) const { return mSquaredDistance; }
    00394             void assign(QueuedSubMesh* qsm, ushort atLod);
    00396             void build();
    00398             void addRenderables(RenderQueue* queue, uint8 group,
    00399                 Real camSquaredDistance);
    00401             typedef MapIterator<MaterialBucketMap> MaterialIterator;
    00403             MaterialIterator getMaterialIterator(void);
    00405             void dump(std::ofstream& of) const;
    00407             void updateContainers(MaterialBucket* bucket, String& name );
    00408             void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables);
    00409
    00410         };
    00419         class _OgreExport  BatchInstance : public MovableObject
    00420         {
    00421             public:
    00422
    00423
    00425             typedef std::vector<LODBucket*> LODBucketList;
    00426             typedef std::map<int, InstancedObject*> ObjectsMap;
    00427             typedef MapIterator<ObjectsMap> InstancedObjectIterator;
    00428         protected:
    00429
    00431             InstancedGeometry* mParent;
    00433             SceneManager* mSceneMgr;
    00435             SceneNode* mNode;
    00437             QueuedSubMeshList mQueuedSubMeshes;
    00439             uint32 mBatchInstanceID;
    00440
    00441             ObjectsMap mInstancesMap;
    00442         public:
    00444             std::vector<Real> mLodSquaredDistances;
    00446             AxisAlignedBox mAABB;
    00448             Real mBoundingRadius;
    00450             ushort mCurrentLod;
    00452             Real mCamDistanceSquared;
    00453         protected:
    00455             LODBucketList mLodBucketList;
    00456
    00457         public:
    00458             BatchInstance(InstancedGeometry* parent, const String& name, SceneManager* mgr,
    00459                 uint32 BatchInstanceID);
    00460             virtual ~BatchInstance();
    00461             // more fields can be added in subclasses
    00462             InstancedGeometry* getParent(void) const { return mParent;}
    00464             void assign(QueuedSubMesh* qmesh);
    00466             void build();
    00468             uint32 getID(void) const { return mBatchInstanceID; }
    00470 //          const Vector3& getCentre(void) const { return mCentre; }
    00471             const String& getMovableType(void) const;
    00472             void _notifyCurrentCamera(Camera* cam);
    00473             const AxisAlignedBox& getBoundingBox(void) const;
    00474             void  setBoundingBox(AxisAlignedBox& box);
    00475             Real getBoundingRadius(void) const;
    00476             void _updateRenderQueue(RenderQueue* queue);
    00477             bool isVisible(void) const;
    00479             void visitRenderables(Renderable::Visitor* visitor,
    00480                 bool debugRenderables = false);
    00481
    00482         //  uint32 getTypeFlags(void) const;
    00483
    00484             typedef VectorIterator<LODBucketList> LODIterator;
    00486             LODIterator getLODIterator(void);
    00488             const LightList& getLights(void) const;
    00489
    00491             void updateBoundingBox();
    00492
    00494             void dump(std::ofstream& of) const;
    00496             void updateContainers(LODBucket* bucket );
    00498             void attachToScene();
    00499             void addInstancedObject(int index, InstancedObject* object);
    00500             InstancedObject*  isInstancedObjectPresent(int index);
    00501             InstancedObjectIterator getObjectIterator();
    00502             SceneNode*getSceneNode(void){return mNode;}
    00503             ObjectsMap& getInstancesMap(void){return  mInstancesMap;}
    00505
    00506         };
    00510         typedef std::map<uint32, BatchInstance*> BatchInstanceMap;
    00516         typedef std::vector<RenderOperation*> RenderOperationVector;
    00517     protected:
    00518         // General state & settings
    00519         SceneManager* mOwner;
    00520         String mName;
    00521         bool mBuilt;
    00522         Real mUpperDistance;
    00523         Real mSquaredUpperDistance;
    00524         bool mCastShadows;
    00525         Vector3 mBatchInstanceDimensions;
    00526         Vector3 mHalfBatchInstanceDimensions;
    00527         Vector3 mOrigin;
    00528         bool mVisible;
    00530         uint8 mRenderQueueID;
    00532         bool mRenderQueueIDSet;
    00534         unsigned int mObjectCount;
    00535         QueuedSubMeshList mQueuedSubMeshes;
    00536         BatchInstance*mInstancedGeometryInstance;
    00540         SkeletonPtr mBaseSkeleton;
    00541         SkeletonInstance *mSkeletonInstance;
    00545         AnimationStateSet* mAnimationState;
    00548         OptimisedSubMeshGeometryList mOptimisedSubMeshGeometryList;
    00549
    00554         SubMeshGeometryLookup mSubMeshGeometryLookup;
    00555
    00557         BatchInstanceMap mBatchInstanceMap;
    00561         RenderOperationVector mRenderOps;
    00565         virtual BatchInstance* getBatchInstance(const AxisAlignedBox& bounds, bool autoCreate);
    00567         virtual BatchInstance* getBatchInstance(const Vector3& point, bool autoCreate);
    00569         virtual BatchInstance* getBatchInstance(ushort x, ushort y, ushort z, bool autoCreate);
    00571         virtual BatchInstance* getBatchInstance(uint32 index);
    00574         virtual void getBatchInstanceIndexes(const Vector3& point,
    00575             ushort& x, ushort& y, ushort& z);
    00578         virtual BatchInstance* getInstancedGeometryInstance(void);
    00581         virtual uint32 packIndex(ushort x, ushort y, ushort z);
    00584         virtual Real getVolumeIntersection(const AxisAlignedBox& box,
    00585             ushort x, ushort y, ushort z);
    00588         virtual AxisAlignedBox getBatchInstanceBounds(ushort x, ushort y, ushort z);
    00591         virtual Vector3 getBatchInstanceCentre(ushort x, ushort y, ushort z);
    00593         virtual AxisAlignedBox calculateBounds(VertexData* vertexData,
    00594             const Vector3& position, const Quaternion& orientation,
    00595             const Vector3& scale);
    00597         SubMeshLodGeometryLinkList* determineGeometry(SubMesh* sm);
    00599         void splitGeometry(VertexData* vd, IndexData* id,
    00600             SubMeshLodGeometryLink* targetGeomLink);
    00601
    00602         typedef std::map<size_t, size_t> IndexRemap;
    00607         template <typename T>
    00608         void buildIndexRemap(T* pBuffer, size_t numIndexes, IndexRemap& remap)
    00609         {
    00610             remap.clear();
    00611             for (size_t i = 0; i < numIndexes; ++i)
    00612             {
    00613                 // use insert since duplicates are silently discarded
    00614                 remap.insert(IndexRemap::value_type(*pBuffer++, remap.size()));
    00615                 // this will have mapped oldindex -> new index IF oldindex
    00616                 // wasn't already there
    00617             }
    00618         }
    00620         template <typename T>
    00621         void remapIndexes(T* src, T* dst, const IndexRemap& remap,
    00622                 size_t numIndexes)
    00623         {
    00624             for (size_t i = 0; i < numIndexes; ++i)
    00625             {
    00626                 // look up original and map to target
    00627                 IndexRemap::const_iterator ix = remap.find(*src++);
    00628                 assert(ix != remap.end());
    00629                 *dst++ = static_cast<T>(ix->second);
    00630             }
    00631         }
    00632
    00633     public:
    00635         InstancedGeometry(SceneManager* owner, const String& name);
    00637         virtual ~InstancedGeometry();
    00638
    00640         const String& getName(void) const { return mName; }
    00659         virtual void addEntity(Entity* ent, const Vector3& position,
    00660             const Quaternion& orientation = Quaternion::IDENTITY,
    00661             const Vector3& scale = Vector3::UNIT_SCALE);
    00662
    00681         virtual void addSceneNode(const SceneNode* node);
    00682
    00693         virtual void build(void);
    00702         void addBatchInstance(void);
    00708         virtual void destroy(void);
    00709
    00713         virtual void reset(void);
    00714
    00724         virtual void setRenderingDistance(Real dist) {
    00725             mUpperDistance = dist;
    00726             mSquaredUpperDistance = mUpperDistance * mUpperDistance;
    00727         }
    00728
    00730         virtual Real getRenderingDistance(void) const { return mUpperDistance; }
    00731
    00733         virtual Real getSquaredRenderingDistance(void) const
    00734         { return mSquaredUpperDistance; }
    00735
    00737         virtual void setVisible(bool visible);
    00738
    00740         virtual bool isVisible(void) const { return mVisible; }
    00741
    00759         virtual void setCastShadows(bool castShadows);
    00761         virtual bool getCastShadows(void) { return mCastShadows; }
    00762
    00773         virtual void setBatchInstanceDimensions(const Vector3& size) {
    00774             mBatchInstanceDimensions = size;
    00775             mHalfBatchInstanceDimensions = size * 0.5;
    00776         }
    00778         virtual const Vector3& getBatchInstanceDimensions(void) const { return mBatchInstanceDimensions; }
    00790         virtual void setOrigin(const Vector3& origin) { mOrigin = origin; }
    00792         virtual const Vector3& getOrigin(void) const { return mOrigin; }
    00793
    00805         virtual void setRenderQueueGroup(uint8 queueID);
    00806
    00808         virtual uint8 getRenderQueueGroup(void) const;
    00810         typedef MapIterator<BatchInstanceMap> BatchInstanceIterator;
    00812         BatchInstanceIterator getBatchInstanceIterator(void);
    00814         RenderOperationVector& getRenderOperationVector(){return mRenderOps;}
    00816         void visitRenderables(Renderable::Visitor* visitor,
    00817             bool debugRenderables = false);
    00818
    00822         virtual void dump(const String& filename) const;
    00827         SkeletonInstance *getBaseSkeletonInstance(void){return mSkeletonInstance;}
    00832         SkeletonPtr getBaseSkeleton(void){return mBaseSkeleton;}
    00837         AnimationStateSet* getBaseAnimationState(void){return mAnimationState;}
    00842         unsigned int getObjectCount(void){return mObjectCount;}
    00843
    00844
    00845
    00846     };
    00847
    00848 }
    00849
    00850 #endif
    00851

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Jul 26 11:32:18 2009