# 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
    00040 namespace Ogre {
    00041
    00095     class _OgreExport  InstancedGeometry
    00096     {
    00097     public:
    00110         class _OgrePrivate OptimisedSubMeshGeometry
    00111         {
    00112         public:
    00113             OptimisedSubMeshGeometry() :vertexData(0), indexData(0) {}
    00114             ~OptimisedSubMeshGeometry()
    00115             {
    00116                 delete vertexData;
    00117                 delete indexData;
    00118             }
    00119             VertexData *vertexData;
    00120             IndexData *indexData;
    00121         };
    00122         typedef std::list<OptimisedSubMeshGeometry*> OptimisedSubMeshGeometryList;
    00125         struct SubMeshLodGeometryLink
    00126         {
    00127             VertexData* vertexData;
    00128             IndexData* indexData;
    00129         };
    00130         typedef std::vector<SubMeshLodGeometryLink> SubMeshLodGeometryLinkList;
    00131         typedef std::map<SubMesh*, SubMeshLodGeometryLinkList*> SubMeshGeometryLookup;
    00133         struct QueuedSubMesh
    00134         {
    00135             SubMesh* submesh;
    00137             SubMeshLodGeometryLinkList* geometryLodList;
    00138             String materialName;
    00139             Vector3 position;
    00140             Quaternion orientation;
    00141             Vector3 scale;
    00143             AxisAlignedBox worldBounds;
    00144             unsigned int ID;
    00145         };
    00146         typedef std::vector<QueuedSubMesh*> QueuedSubMeshList;
    00147         typedef std::vector<String> QueuedSubMeshOriginList;
    00149         struct QueuedGeometry
    00150         {
    00151             SubMeshLodGeometryLink* geometry;
    00152             Vector3 position;
    00153             Quaternion orientation;
    00154             Vector3 scale;
    00155             unsigned int ID;
    00156         };
    00157         typedef std::vector<QueuedGeometry*> QueuedGeometryList;
    00158
    00159         // forward declarations
    00160         class LODBucket;
    00161         class MaterialBucket;
    00162         class BatchInstance;
    00163         class InstancedObject;
    00164
    00169         class _OgreExport  GeometryBucket : public SimpleRenderable
    00170         {
    00171         protected:
    00172
    00174             QueuedGeometryList mQueuedGeometry;
    00176             InstancedGeometry*mBatch;
    00178             MaterialBucket* mParent;
    00180             String mFormatString;
    00183             VertexData* mVertexData;
    00186             IndexData* mIndexData;
    00188             HardwareIndexBuffer::IndexType mIndexType;
    00190             size_t mMaxVertexIndex;
    00192             unsigned short mTexCoordIndex;
    00193             AxisAlignedBox mAABB;
    00194
    00195             template<typename T>
    00196             void copyIndexes(const T* src, T* dst, size_t count, size_t indexOffset)
    00197             {
    00198                 if (indexOffset == 0)
    00199                 {
    00200                     memcpy(dst, src, sizeof(T) * count);
    00201                 }
    00202                 else
    00203                 {
    00204                     while(count--)
    00205                     {
    00206                         *dst++ = static_cast<T>(*src++ + indexOffset);
    00207                     }
    00208                 }
    00209             }
    00210         public:
    00211             GeometryBucket(MaterialBucket* parent, const String& formatString,
    00212                 const VertexData* vData, const IndexData* iData);
    00213             GeometryBucket(MaterialBucket* parent,const String& formatString,GeometryBucket*bucket);
    00214             virtual ~GeometryBucket();
    00215             MaterialBucket* getParent(void) { return mParent; }
    00216             Real getBoundingRadius(void) const;
    00218             const VertexData* getVertexData(void) const { return mVertexData; }
    00220             const IndexData* getIndexData(void) const { return mIndexData; }
    00222             const MaterialPtr& getMaterial(void) const;
    00223             Technique* getTechnique(void) const;
    00224             void getWorldTransforms(Matrix4* xform) const;
    00225             virtual unsigned short getNumWorldTransforms(void) const ;
    00226             const Quaternion& getWorldOrientation(void) const;
    00227             const Vector3& getWorldPosition(void) const;
    00228             Real getSquaredViewDepth(const Camera* cam) const;
    00229             const LightList& getLights(void) const;
    00230             bool getCastsShadows(void) const;
    00231             String getFormatString(void) const;
    00235             bool assign(QueuedGeometry* qsm);
    00237             void build();
    00239             void dump(std::ofstream& of) const;
    00241             AxisAlignedBox & getAABB(void){return mAABB;};
    00242         };
    00243         class _OgreExport  InstancedObject
    00244         {
    00245             friend class GeometryBucket;
    00246         public:
    00247              enum TransformSpace
    00248         {
    00250             TS_LOCAL,
    00252             TS_PARENT,
    00254             TS_WORLD
    00255         };
    00257             typedef std::vector<GeometryBucket*> GeometryBucketList;
    00258         protected:
    00259             GeometryBucketList mGeometryBucketList;
    00260             unsigned short mIndex;
    00261             Matrix4  mTransformation;
    00262             Quaternion mOrientation;
    00263             Vector3 mScale;
    00264             Vector3 mPosition;
    00265             SkeletonInstance* mSkeletonInstance;
    00267             Matrix4 *mBoneWorldMatrices;
    00269             Matrix4 *mBoneMatrices;
    00271             AnimationStateSet* mAnimationState;
    00272             unsigned short mNumBoneMatrices;
    00274             unsigned long mFrameAnimationLastUpdated;
    00275         public:
    00276             InstancedObject(int index);
    00277             InstancedObject(int index,SkeletonInstance *skeleton,AnimationStateSet*animations);
    00278             ~InstancedObject();
    00279             void setPosition( Vector3  position);
    00280             Vector3 & getPosition(void);
    00281             void yaw(const Radian& angle);
    00282             void pitch(const Radian& angle);
    00283             void roll(const Radian& angle);
    00284             void rotate(const Quaternion& q);
    00285             void setScale(const Vector3& scale);
    00286             void addBucketToList(GeometryBucket* bucket);
    00287             void needUpdate();
    00288             GeometryBucketList&getGeometryBucketList(void){return mGeometryBucketList;}
    00289             void translate(const Matrix3& axes, const Vector3& move);
    00290             void translate(const Vector3& d);
    00291             Matrix3 getLocalAxes(void) const;
    00292             void updateAnimation(void);
    00293             AnimationState* getAnimationState(const String& name) const;
    00294             SkeletonInstance*getSkeletonInstance(void){return mSkeletonInstance;}
    00295
    00296         };
    00299         class _OgreExport  MaterialBucket
    00300         {
    00301         public:
    00303             typedef std::vector<GeometryBucket*> GeometryBucketList;
    00304         protected:
    00306             LODBucket* mParent;
    00308             String mMaterialName;
    00310             MaterialPtr mMaterial;
    00312             Technique* mTechnique;
    00313             int mLastIndex;
    00315             GeometryBucketList mGeometryBucketList;
    00316             // index to current Geometry Buckets for a given geometry format
    00317             typedef std::map<String, GeometryBucket*> CurrentGeometryMap;
    00318             CurrentGeometryMap mCurrentGeometryMap;
    00320             String getGeometryFormatString(SubMeshLodGeometryLink* geom);
    00321
    00322         public:
    00323             MaterialBucket(LODBucket* parent, const String& materialName);
    00324             virtual ~MaterialBucket();
    00325             LODBucket* getParent(void) { return mParent; }
    00327             const String& getMaterialName(void) const { return mMaterialName; }
    00329             void assign(QueuedGeometry* qsm);
    00331             void build();
    00333             void addRenderables(RenderQueue* queue, uint8 group,
    00334                 Real camSquaredDist);
    00336             const MaterialPtr& getMaterial(void) const { return mMaterial; }
    00338             typedef VectorIterator<GeometryBucketList> GeometryIterator;
    00340             GeometryIterator getGeometryIterator(void);
    00342             Technique* getCurrentTechnique(void) const { return mTechnique; }
    00344             void dump(std::ofstream& of) const;
    00346             MaterialBucket::CurrentGeometryMap* getMaterialBucketMap(void) const;
    00348             MaterialBucket::GeometryBucketList*getGeometryBucketList(void) const;
    00350             void updateContainers(GeometryBucket* bucket, const String &format);
    00351             void setLastIndex(int index){mLastIndex=index;}
    00352             int getLastIndex(){return mLastIndex;}
    00353             void setMaterial(const String & name);
    00354
    00355         };
    00361         class _OgreExport  LODBucket
    00362         {
    00363         public:
    00365             typedef std::map<String, MaterialBucket*> MaterialBucketMap;
    00366         protected:
    00368             BatchInstance* mParent;
    00370             unsigned short mLod;
    00372             Real mSquaredDistance;
    00374             MaterialBucketMap mMaterialBucketMap;
    00376             QueuedGeometryList mQueuedGeometryList;
    00377         public:
    00378             LODBucket(BatchInstance* parent, unsigned short lod, Real lodDist);
    00379             virtual ~LODBucket();
    00380             BatchInstance* getParent(void) { return mParent; }
    00382             ushort getLod(void) const { return mLod; }
    00384             Real getSquaredDistance(void) const { return mSquaredDistance; }
    00386             void assign(QueuedSubMesh* qsm, ushort atLod);
    00388             void build();
    00390             void addRenderables(RenderQueue* queue, uint8 group,
    00391                 Real camSquaredDistance);
    00393             typedef MapIterator<MaterialBucketMap> MaterialIterator;
    00395             MaterialIterator getMaterialIterator(void);
    00397             void dump(std::ofstream& of) const;
    00399             void updateContainers(MaterialBucket* bucket, String& name );
    00400
    00401         };
    00410         class _OgreExport  BatchInstance : public MovableObject
    00411         {
    00412             public:
    00413
    00414
    00416             typedef std::vector<LODBucket*> LODBucketList;
    00417             typedef std::map<int, InstancedObject*> ObjectsMap;
    00418         protected:
    00419
    00421             InstancedGeometry* mParent;
    00423             SceneManager* mSceneMgr;
    00425             SceneNode* mNode;
    00427             QueuedSubMeshList mQueuedSubMeshes;
    00429             uint32 mBatchInstanceID;
    00430
    00431             ObjectsMap mInstancesMap;
    00432         public:
    00434             std::vector<Real> mLodSquaredDistances;
    00436             AxisAlignedBox mAABB;
    00438             Real mBoundingRadius;
    00440             ushort mCurrentLod;
    00442             Real mCamDistanceSquared;
    00443         protected:
    00445             LODBucketList mLodBucketList;
    00446
    00447         public:
    00448             BatchInstance(InstancedGeometry* parent, const String& name, SceneManager* mgr,
    00449                 uint32 BatchInstanceID);
    00450             virtual ~BatchInstance();
    00451             // more fields can be added in subclasses
    00452             InstancedGeometry* getParent(void) const { return mParent;}
    00454             void assign(QueuedSubMesh* qmesh);
    00456             void build();
    00458             uint32 getID(void) const { return mBatchInstanceID; }
    00460 //          const Vector3& getCentre(void) const { return mCentre; }
    00461             const String& getMovableType(void) const;
    00462             void _notifyCurrentCamera(Camera* cam);
    00463             const AxisAlignedBox& getBoundingBox(void) const;
    00464             void  setBoundingBox(AxisAlignedBox& box);
    00465             Real getBoundingRadius(void) const;
    00466             void _updateRenderQueue(RenderQueue* queue);
    00467             bool isVisible(void) const;
    00468         //  uint32 getTypeFlags(void) const;
    00469
    00470             typedef VectorIterator<LODBucketList> LODIterator;
    00472             LODIterator getLODIterator(void);
    00474             const LightList& getLights(void) const;
    00475
    00477             void updateBoundingBox();
    00478
    00480             void dump(std::ofstream& of) const;
    00482             void updateContainers(LODBucket* bucket );
    00484             void attachToScene();
    00485             void addInstancedObject(int index, InstancedObject* object);
    00486             InstancedObject*  isInstancedObjectPresent(int index);
    00487             InstancedObject** getObjectsAsArray(unsigned short & size);
    00488             SceneNode*getSceneNode(void){return mNode;}
    00489             ObjectsMap&getInstancesMap(void){return  mInstancesMap;};
    00491
    00492         };
    00496         typedef std::map<uint32, BatchInstance*> BatchInstanceMap;
    00502         typedef std::vector<RenderOperation*> RenderOperationVector;
    00503     protected:
    00504         // General state & settings
    00505         SceneManager* mOwner;
    00506         String mName;
    00507         bool mBuilt;
    00508         Real mUpperDistance;
    00509         Real mSquaredUpperDistance;
    00510         bool mCastShadows;
    00511         Vector3 mBatchInstanceDimensions;
    00512         Vector3 mHalfBatchInstanceDimensions;
    00513         Vector3 mOrigin;
    00514         bool mVisible;
    00516         uint8 mRenderQueueID;
    00518         bool mRenderQueueIDSet;
    00520         unsigned int mObjectCount;
    00521         QueuedSubMeshList mQueuedSubMeshes;
    00522         BatchInstance*mInstancedGeometryInstance;
    00526         SkeletonPtr mBaseSkeleton;
    00527         SkeletonInstance *mSkeletonInstance;
    00531         AnimationStateSet* mAnimationState;
    00534         OptimisedSubMeshGeometryList mOptimisedSubMeshGeometryList;
    00535
    00540         SubMeshGeometryLookup mSubMeshGeometryLookup;
    00541
    00543         BatchInstanceMap mBatchInstanceMap;
    00547         RenderOperationVector mRenderOps;
    00551         virtual BatchInstance* getBatchInstance(const AxisAlignedBox& bounds, bool autoCreate);
    00553         virtual BatchInstance* getBatchInstance(const Vector3& point, bool autoCreate);
    00555         virtual BatchInstance* getBatchInstance(ushort x, ushort y, ushort z, bool autoCreate);
    00557         virtual BatchInstance* getBatchInstance(uint32 index);
    00560         virtual void getBatchInstanceIndexes(const Vector3& point,
    00561             ushort& x, ushort& y, ushort& z);
    00564         virtual BatchInstance* getInstancedGeometryInstance(void);
    00567         virtual uint32 packIndex(ushort x, ushort y, ushort z);
    00570         virtual Real getVolumeIntersection(const AxisAlignedBox& box,
    00571             ushort x, ushort y, ushort z);
    00574         virtual AxisAlignedBox getBatchInstanceBounds(ushort x, ushort y, ushort z);
    00577         virtual Vector3 getBatchInstanceCentre(ushort x, ushort y, ushort z);
    00579         virtual AxisAlignedBox calculateBounds(VertexData* vertexData,
    00580             const Vector3& position, const Quaternion& orientation,
    00581             const Vector3& scale);
    00583         SubMeshLodGeometryLinkList* determineGeometry(SubMesh* sm);
    00585         void splitGeometry(VertexData* vd, IndexData* id,
    00586             SubMeshLodGeometryLink* targetGeomLink);
    00587
    00588         typedef std::map<size_t, size_t> IndexRemap;
    00593         template <typename T>
    00594         void buildIndexRemap(T* pBuffer, size_t numIndexes, IndexRemap& remap)
    00595         {
    00596             remap.clear();
    00597             for (size_t i = 0; i < numIndexes; ++i)
    00598             {
    00599                 // use insert since duplicates are silently discarded
    00600                 remap.insert(IndexRemap::value_type(*pBuffer++, remap.size()));
    00601                 // this will have mapped oldindex -> new index IF oldindex
    00602                 // wasn't already there
    00603             }
    00604         }
    00606         template <typename T>
    00607         void remapIndexes(T* src, T* dst, const IndexRemap& remap,
    00608                 size_t numIndexes)
    00609         {
    00610             for (size_t i = 0; i < numIndexes; ++i)
    00611             {
    00612                 // look up original and map to target
    00613                 IndexRemap::const_iterator ix = remap.find(*src++);
    00614                 assert(ix != remap.end());
    00615                 *dst++ = static_cast<T>(ix->second);
    00616             }
    00617         }
    00618
    00619     public:
    00621         InstancedGeometry(SceneManager* owner, const String& name);
    00623         virtual ~InstancedGeometry();
    00624
    00626         const String& getName(void) const { return mName; }
    00645         virtual void addEntity(Entity* ent, const Vector3& position,
    00646             const Quaternion& orientation = Quaternion::IDENTITY,
    00647             const Vector3& scale = Vector3::UNIT_SCALE);
    00648
    00667         virtual void addSceneNode(const SceneNode* node);
    00668
    00679         virtual void build(void);
    00688         void addBatchInstance(void);
    00694         virtual void destroy(void);
    00695
    00699         virtual void reset(void);
    00700
    00710         virtual void setRenderingDistance(Real dist) {
    00711             mUpperDistance = dist;
    00712             mSquaredUpperDistance = mUpperDistance * mUpperDistance;
    00713         }
    00714
    00716         virtual Real getRenderingDistance(void) const { return mUpperDistance; }
    00717
    00719         virtual Real getSquaredRenderingDistance(void) const
    00720         { return mSquaredUpperDistance; }
    00721
    00723         virtual void setVisible(bool visible);
    00724
    00726         virtual bool isVisible(void) const { return mVisible; }
    00727
    00745         virtual void setCastShadows(bool castShadows);
    00747         virtual bool getCastShadows(void) { return mCastShadows; }
    00748
    00759         virtual void setBatchInstanceDimensions(const Vector3& size) {
    00760             mBatchInstanceDimensions = size;
    00761             mHalfBatchInstanceDimensions = size * 0.5;
    00762         }
    00764         virtual const Vector3& getBatchInstanceDimensions(void) const { return mBatchInstanceDimensions; }
    00776         virtual void setOrigin(const Vector3& origin) { mOrigin = origin; }
    00778         virtual const Vector3& getOrigin(void) const { return mOrigin; }
    00779
    00791         virtual void setRenderQueueGroup(uint8 queueID);
    00792
    00794         virtual uint8 getRenderQueueGroup(void) const;
    00796         typedef MapIterator<BatchInstanceMap> BatchInstanceIterator;
    00798         BatchInstanceIterator getBatchInstanceIterator(void);
    00800         RenderOperationVector& getRenderOperationVector(){return mRenderOps;}
    00804         virtual void dump(const String& filename) const;
    00809         SkeletonInstance *getBaseSkeletonInstance(void){return mSkeletonInstance;}
    00814         SkeletonPtr getBaseSkeleton(void){return mBaseSkeleton;}
    00819         AnimationStateSet* getBaseAnimationState(void){return mAnimationState;}
    00824         unsigned int getObjectCount(void){return mObjectCount;}
    00825
    00826
    00827
    00828     };
    00829
    00830 }
    00831
    00832 #endif
    00833

Copyright © 2000-2005 by The OGRE Team
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sat May 10 16:24:59 2008