OgreStaticGeometry.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 __StaticGeometry_H__
00030 #define __StaticGeometry_H__
00031 
00032 #include "OgrePrerequisites.h"
00033 #include "OgreMovableObject.h"
00034 #include "OgreRenderable.h"
00035 
00036 namespace Ogre {
00037 
00113     class _OgreExport StaticGeometry : public BatchedGeometryAlloc
00114     {
00115     public:
00128         class _OgrePrivate OptimisedSubMeshGeometry : public BatchedGeometryAlloc
00129         {
00130         public:
00131             OptimisedSubMeshGeometry() :vertexData(0), indexData(0) {}
00132             ~OptimisedSubMeshGeometry() 
00133             {
00134                 OGRE_DELETE vertexData;
00135                 OGRE_DELETE indexData;
00136             }
00137             VertexData *vertexData;
00138             IndexData *indexData;
00139         };
00140         typedef std::list<OptimisedSubMeshGeometry*> OptimisedSubMeshGeometryList;
00143         struct SubMeshLodGeometryLink
00144         {
00145             VertexData* vertexData;
00146             IndexData* indexData;
00147         };
00148         typedef std::vector<SubMeshLodGeometryLink> SubMeshLodGeometryLinkList;
00149         typedef std::map<SubMesh*, SubMeshLodGeometryLinkList*> SubMeshGeometryLookup;
00151         struct QueuedSubMesh : public BatchedGeometryAlloc
00152         {
00153             SubMesh* submesh;
00155             SubMeshLodGeometryLinkList* geometryLodList;
00156             String materialName;
00157             Vector3 position;
00158             Quaternion orientation;
00159             Vector3 scale;
00161             AxisAlignedBox worldBounds;
00162         };
00163         typedef std::vector<QueuedSubMesh*> QueuedSubMeshList;
00165         struct QueuedGeometry : public BatchedGeometryAlloc
00166         {
00167             SubMeshLodGeometryLink* geometry;
00168             Vector3 position;
00169             Quaternion orientation;
00170             Vector3 scale;
00171         };
00172         typedef std::vector<QueuedGeometry*> QueuedGeometryList;
00173         
00174         // forward declarations
00175         class LODBucket;
00176         class MaterialBucket;
00177         class Region;
00178 
00183         class _OgreExport GeometryBucket :  public Renderable,  public BatchedGeometryAlloc
00184         {
00185         protected:
00187             QueuedGeometryList mQueuedGeometry;
00189             MaterialBucket* mParent;
00191             String mFormatString;
00194             VertexData* mVertexData;
00197             IndexData* mIndexData;
00199             HardwareIndexBuffer::IndexType mIndexType;
00201             size_t mMaxVertexIndex;
00202 
00203             template<typename T>
00204             void copyIndexes(const T* src, T* dst, size_t count, size_t indexOffset)
00205             {
00206                 if (indexOffset == 0)
00207                 {
00208                     memcpy(dst, src, sizeof(T) * count);
00209                 }
00210                 else
00211                 {
00212                     while(count--)
00213                     {
00214                         *dst++ = static_cast<T>(*src++ + indexOffset);
00215                     }
00216                 }
00217             }
00218         public:
00219             GeometryBucket(MaterialBucket* parent, const String& formatString, 
00220                 const VertexData* vData, const IndexData* iData);
00221             virtual ~GeometryBucket();
00222             MaterialBucket* getParent(void) { return mParent; }
00224             const VertexData* getVertexData(void) const { return mVertexData; }
00226             const IndexData* getIndexData(void) const { return mIndexData; }
00228             const MaterialPtr& getMaterial(void) const;
00229             Technique* getTechnique(void) const;
00230             void getRenderOperation(RenderOperation& op);
00231             void getWorldTransforms(Matrix4* xform) const;
00232             Real getSquaredViewDepth(const Camera* cam) const;
00233             const LightList& getLights(void) const;
00234             bool getCastsShadows(void) const;
00235             
00239             bool assign(QueuedGeometry* qsm);
00241             void build(bool stencilShadows);
00243             void dump(std::ofstream& of) const;
00244         };
00247         class _OgreExport MaterialBucket : public BatchedGeometryAlloc
00248         {
00249         public:
00251             typedef std::vector<GeometryBucket*> GeometryBucketList;
00252         protected:
00254             LODBucket* mParent;
00256             String mMaterialName;
00258             MaterialPtr mMaterial;
00260             Technique* mTechnique;
00261 
00263             GeometryBucketList mGeometryBucketList;
00264             // index to current Geometry Buckets for a given geometry format
00265             typedef std::map<String, GeometryBucket*> CurrentGeometryMap;
00266             CurrentGeometryMap mCurrentGeometryMap;
00268             String getGeometryFormatString(SubMeshLodGeometryLink* geom);
00269             
00270         public:
00271             MaterialBucket(LODBucket* parent, const String& materialName);
00272             virtual ~MaterialBucket();
00273             LODBucket* getParent(void) { return mParent; }
00275             const String& getMaterialName(void) const { return mMaterialName; }
00277             void assign(QueuedGeometry* qsm);
00279             void build(bool stencilShadows);
00281             void addRenderables(RenderQueue* queue, uint8 group, 
00282                 Real camSquaredDist);
00284             const MaterialPtr& getMaterial(void) const { return mMaterial; }
00286             typedef VectorIterator<GeometryBucketList> GeometryIterator;
00288             GeometryIterator getGeometryIterator(void);
00290             Technique* getCurrentTechnique(void) const { return mTechnique; }
00292             void dump(std::ofstream& of) const;
00293             void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables);
00294         };
00300         class _OgreExport LODBucket : public BatchedGeometryAlloc
00301         {
00302         public:
00304             typedef std::map<String, MaterialBucket*> MaterialBucketMap;
00305         protected:
00307             Region* mParent;
00309             unsigned short mLod;
00311             Real mSquaredDistance;
00313             MaterialBucketMap mMaterialBucketMap;
00315             QueuedGeometryList mQueuedGeometryList;
00316         public:
00317             LODBucket(Region* parent, unsigned short lod, Real lodDist);
00318             virtual ~LODBucket();
00319             Region* getParent(void) { return mParent; }
00321             ushort getLod(void) const { return mLod; }
00323             Real getSquaredDistance(void) const { return mSquaredDistance; }
00325             void assign(QueuedSubMesh* qsm, ushort atLod);
00327             void build(bool stencilShadows);
00329             void addRenderables(RenderQueue* queue, uint8 group, 
00330                 Real camSquaredDistance);
00332             typedef MapIterator<MaterialBucketMap> MaterialIterator;
00334             MaterialIterator getMaterialIterator(void);
00336             void dump(std::ofstream& of) const;
00337             void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables);
00338             
00339         };
00348         class _OgreExport Region : public MovableObject
00349         {
00350         public:
00352             typedef std::vector<LODBucket*> LODBucketList;
00353         protected:
00355             class _OgreExport RegionShadowRenderable : public ShadowRenderable
00356             {
00357             protected:
00358                 Region* mParent;
00359                 // Shared link to position buffer
00360                 HardwareVertexBufferSharedPtr mPositionBuffer;
00361                 // Shared link to w-coord buffer (optional)
00362                 HardwareVertexBufferSharedPtr mWBuffer;
00363 
00364             public:
00365                 RegionShadowRenderable(Region* parent, 
00366                     HardwareIndexBufferSharedPtr* indexBuffer, const VertexData* vertexData, 
00367                     bool createSeparateLightCap, bool isLightCap = false);
00368                 ~RegionShadowRenderable();
00370                 void getWorldTransforms(Matrix4* xform) const;
00371                 HardwareVertexBufferSharedPtr getPositionBuffer(void) { return mPositionBuffer; }
00372                 HardwareVertexBufferSharedPtr getWBuffer(void) { return mWBuffer; }
00373 
00374             };
00376             StaticGeometry* mParent;
00378             SceneManager* mSceneMgr;
00380             SceneNode* mNode;
00382             QueuedSubMeshList mQueuedSubMeshes;
00384             uint32 mRegionID;
00386             Vector3 mCentre;
00388             std::vector<Real> mLodSquaredDistances;
00390             AxisAlignedBox mAABB;
00392             Real mBoundingRadius;
00394             ushort mCurrentLod;
00396             Real mCamDistanceSquared;
00398             LODBucketList mLodBucketList;
00400             mutable LightList mLightList;
00402             mutable ulong mLightListUpdated;
00404             EdgeData* mEdgeList;
00406             ShadowRenderableList mShadowRenderables;
00408             bool mVertexProgramInUse;
00409 
00410 
00411 
00412         public:
00413             Region(StaticGeometry* parent, const String& name, SceneManager* mgr, 
00414                 uint32 regionID, const Vector3& centre);
00415             virtual ~Region();
00416             // more fields can be added in subclasses
00417             StaticGeometry* getParent(void) const { return mParent;}
00419             void assign(QueuedSubMesh* qmesh);
00421             void build(bool stencilShadows);
00423             uint32 getID(void) const { return mRegionID; }
00425             const Vector3& getCentre(void) const { return mCentre; }
00426             const String& getMovableType(void) const;
00427             void _notifyCurrentCamera(Camera* cam);
00428             const AxisAlignedBox& getBoundingBox(void) const;
00429             Real getBoundingRadius(void) const;
00430             void _updateRenderQueue(RenderQueue* queue);
00432             void visitRenderables(Renderable::Visitor* visitor, 
00433                 bool debugRenderables = false);
00434             bool isVisible(void) const;
00435             uint32 getTypeFlags(void) const;
00436 
00437             typedef VectorIterator<LODBucketList> LODIterator;
00439             LODIterator getLODIterator(void);
00441             ShadowRenderableListIterator getShadowVolumeRenderableIterator(
00442                 ShadowTechnique shadowTechnique, const Light* light, 
00443                 HardwareIndexBufferSharedPtr* indexBuffer, 
00444                 bool extrudeVertices, Real extrusionDistance, unsigned long flags = 0 );
00446             EdgeData* getEdgeList(void);
00448             bool hasEdgeList(void);
00449 
00451             void dump(std::ofstream& of) const;
00452             
00453         };
00461         typedef std::map<uint32, Region*> RegionMap;
00462     protected:
00463         // General state & settings
00464         SceneManager* mOwner;
00465         String mName;
00466         bool mBuilt;
00467         Real mUpperDistance;
00468         Real mSquaredUpperDistance;
00469         bool mCastShadows;
00470         Vector3 mRegionDimensions;
00471         Vector3 mHalfRegionDimensions;
00472         Vector3 mOrigin;
00473         bool mVisible;
00475         uint8 mRenderQueueID;
00477         bool mRenderQueueIDSet;
00478 
00479         QueuedSubMeshList mQueuedSubMeshes;
00480 
00483         OptimisedSubMeshGeometryList mOptimisedSubMeshGeometryList;
00484 
00489         SubMeshGeometryLookup mSubMeshGeometryLookup;
00490             
00492         RegionMap mRegionMap;
00493 
00497         virtual Region* getRegion(const AxisAlignedBox& bounds, bool autoCreate);
00499         virtual Region* getRegion(const Vector3& point, bool autoCreate);
00501         virtual Region* getRegion(ushort x, ushort y, ushort z, bool autoCreate);
00503         virtual Region* getRegion(uint32 index);
00506         virtual void getRegionIndexes(const Vector3& point, 
00507             ushort& x, ushort& y, ushort& z);
00510         virtual uint32 packIndex(ushort x, ushort y, ushort z);
00513         virtual Real getVolumeIntersection(const AxisAlignedBox& box,  
00514             ushort x, ushort y, ushort z);
00517         virtual AxisAlignedBox getRegionBounds(ushort x, ushort y, ushort z);
00520         virtual Vector3 getRegionCentre(ushort x, ushort y, ushort z);
00522         virtual AxisAlignedBox calculateBounds(VertexData* vertexData, 
00523             const Vector3& position, const Quaternion& orientation, 
00524             const Vector3& scale);
00526         SubMeshLodGeometryLinkList* determineGeometry(SubMesh* sm);
00528         void splitGeometry(VertexData* vd, IndexData* id, 
00529             SubMeshLodGeometryLink* targetGeomLink);
00530 
00531         typedef std::map<size_t, size_t> IndexRemap;
00536         template <typename T>
00537         void buildIndexRemap(T* pBuffer, size_t numIndexes, IndexRemap& remap)
00538         {
00539             remap.clear();
00540             for (size_t i = 0; i < numIndexes; ++i)
00541             {
00542                 // use insert since duplicates are silently discarded
00543                 remap.insert(IndexRemap::value_type(*pBuffer++, remap.size()));
00544                 // this will have mapped oldindex -> new index IF oldindex
00545                 // wasn't already there
00546             }
00547         }
00549         template <typename T>
00550         void remapIndexes(T* src, T* dst, const IndexRemap& remap, 
00551                 size_t numIndexes)
00552         {
00553             for (size_t i = 0; i < numIndexes; ++i)
00554             {
00555                 // look up original and map to target
00556                 IndexRemap::const_iterator ix = remap.find(*src++);
00557                 assert(ix != remap.end());
00558                 *dst++ = static_cast<T>(ix->second);
00559             }
00560         }
00561         
00562     public:
00564         StaticGeometry(SceneManager* owner, const String& name);
00566         virtual ~StaticGeometry();
00567 
00569         const String& getName(void) const { return mName; }
00588         virtual void addEntity(Entity* ent, const Vector3& position,
00589             const Quaternion& orientation = Quaternion::IDENTITY, 
00590             const Vector3& scale = Vector3::UNIT_SCALE);
00591 
00610         virtual void addSceneNode(const SceneNode* node);
00611 
00622         virtual void build(void);
00623 
00629         virtual void destroy(void);
00630 
00634         virtual void reset(void);
00635 
00645         virtual void setRenderingDistance(Real dist) { 
00646             mUpperDistance = dist; 
00647             mSquaredUpperDistance = mUpperDistance * mUpperDistance;
00648         }
00649 
00651         virtual Real getRenderingDistance(void) const { return mUpperDistance; }
00652 
00654         virtual Real getSquaredRenderingDistance(void) const 
00655         { return mSquaredUpperDistance; }
00656 
00658         virtual void setVisible(bool visible);
00659 
00661         virtual bool isVisible(void) const { return mVisible; }
00662 
00680         virtual void setCastShadows(bool castShadows);
00682         virtual bool getCastShadows(void) { return mCastShadows; }
00683 
00694         virtual void setRegionDimensions(const Vector3& size) { 
00695             mRegionDimensions = size; 
00696             mHalfRegionDimensions = size * 0.5;
00697         }
00699         virtual const Vector3& getRegionDimensions(void) const { return mRegionDimensions; }
00711         virtual void setOrigin(const Vector3& origin) { mOrigin = origin; }
00713         virtual const Vector3& getOrigin(void) const { return mOrigin; }
00714 
00726         virtual void setRenderQueueGroup(uint8 queueID);
00727 
00729         virtual uint8 getRenderQueueGroup(void) const;
00731         void visitRenderables(Renderable::Visitor* visitor, 
00732             bool debugRenderables = false);
00733         
00735         typedef MapIterator<RegionMap> RegionIterator;
00737         RegionIterator getRegionIterator(void);
00738 
00742         virtual void dump(const String& filename) const;
00743 
00744 
00745     };
00746 
00747 }
00748 
00749 #endif
00750 

Copyright © 2008 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Tue Nov 4 11:02:34 2008