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             class _OgreExport LODShadowRenderable : public ShadowRenderable
00308             {
00309             protected:
00310                 LODBucket* mParent;
00311                 // Shared link to position buffer
00312                 HardwareVertexBufferSharedPtr mPositionBuffer;
00313                 // Shared link to w-coord buffer (optional)
00314                 HardwareVertexBufferSharedPtr mWBuffer;
00315 
00316             public:
00317                 LODShadowRenderable(LODBucket* parent, 
00318                     HardwareIndexBufferSharedPtr* indexBuffer, const VertexData* vertexData, 
00319                     bool createSeparateLightCap, bool isLightCap = false);
00320                 ~LODShadowRenderable();
00322                 void getWorldTransforms(Matrix4* xform) const;
00323                 HardwareVertexBufferSharedPtr getPositionBuffer(void) { return mPositionBuffer; }
00324                 HardwareVertexBufferSharedPtr getWBuffer(void) { return mWBuffer; }
00325 
00326             };
00328             Region* mParent;
00330             unsigned short mLod;
00332             Real mSquaredDistance;
00334             MaterialBucketMap mMaterialBucketMap;
00336             QueuedGeometryList mQueuedGeometryList;
00338             EdgeData* mEdgeList;
00340             bool mVertexProgramInUse;
00342             ShadowCaster::ShadowRenderableList mShadowRenderables;
00343         public:
00344             LODBucket(Region* parent, unsigned short lod, Real lodDist);
00345             virtual ~LODBucket();
00346             Region* getParent(void) { return mParent; }
00348             ushort getLod(void) const { return mLod; }
00350             Real getSquaredDistance(void) const { return mSquaredDistance; }
00352             void assign(QueuedSubMesh* qsm, ushort atLod);
00354             void build(bool stencilShadows);
00356             void addRenderables(RenderQueue* queue, uint8 group, 
00357                 Real camSquaredDistance);
00359             typedef MapIterator<MaterialBucketMap> MaterialIterator;
00361             MaterialIterator getMaterialIterator(void);
00363             void dump(std::ofstream& of) const;
00364             void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables);
00365             EdgeData* getEdgeList() const { return mEdgeList; }
00366             ShadowCaster::ShadowRenderableList& getShadowRenderableList() { return mShadowRenderables; }
00367             bool isVertexProgramInUse() const { return mVertexProgramInUse; }
00368             void updateShadowRenderables(
00369                 ShadowTechnique shadowTechnique, const Vector4& lightPos, 
00370                 HardwareIndexBufferSharedPtr* indexBuffer, 
00371                 bool extrudeVertices, Real extrusionDistance, unsigned long flags = 0 );
00372             
00373         };
00382         class _OgreExport Region : public MovableObject
00383         {
00384         public:
00386             typedef std::vector<LODBucket*> LODBucketList;
00387         protected:
00389             StaticGeometry* mParent;
00391             SceneManager* mSceneMgr;
00393             SceneNode* mNode;
00395             QueuedSubMeshList mQueuedSubMeshes;
00397             uint32 mRegionID;
00399             Vector3 mCentre;
00401             std::vector<Real> mLodSquaredDistances;
00403             AxisAlignedBox mAABB;
00405             Real mBoundingRadius;
00407             ushort mCurrentLod;
00409             Real mCamDistanceSquared;
00411             LODBucketList mLodBucketList;
00413             mutable LightList mLightList;
00415             mutable ulong mLightListUpdated;
00416 
00417 
00418 
00419         public:
00420             Region(StaticGeometry* parent, const String& name, SceneManager* mgr, 
00421                 uint32 regionID, const Vector3& centre);
00422             virtual ~Region();
00423             // more fields can be added in subclasses
00424             StaticGeometry* getParent(void) const { return mParent;}
00426             void assign(QueuedSubMesh* qmesh);
00428             void build(bool stencilShadows);
00430             uint32 getID(void) const { return mRegionID; }
00432             const Vector3& getCentre(void) const { return mCentre; }
00433             const String& getMovableType(void) const;
00434             void _notifyCurrentCamera(Camera* cam);
00435             const AxisAlignedBox& getBoundingBox(void) const;
00436             Real getBoundingRadius(void) const;
00437             void _updateRenderQueue(RenderQueue* queue);
00439             void visitRenderables(Renderable::Visitor* visitor, 
00440                 bool debugRenderables = false);
00441             bool isVisible(void) const;
00442             uint32 getTypeFlags(void) const;
00443 
00444             typedef VectorIterator<LODBucketList> LODIterator;
00446             LODIterator getLODIterator(void);
00448             ShadowRenderableListIterator getShadowVolumeRenderableIterator(
00449                 ShadowTechnique shadowTechnique, const Light* light, 
00450                 HardwareIndexBufferSharedPtr* indexBuffer, 
00451                 bool extrudeVertices, Real extrusionDistance, unsigned long flags = 0 );
00453             EdgeData* getEdgeList(void);
00455             bool hasEdgeList(void);
00456 
00458             void dump(std::ofstream& of) const;
00459             
00460         };
00468         typedef std::map<uint32, Region*> RegionMap;
00469     protected:
00470         // General state & settings
00471         SceneManager* mOwner;
00472         String mName;
00473         bool mBuilt;
00474         Real mUpperDistance;
00475         Real mSquaredUpperDistance;
00476         bool mCastShadows;
00477         Vector3 mRegionDimensions;
00478         Vector3 mHalfRegionDimensions;
00479         Vector3 mOrigin;
00480         bool mVisible;
00482         uint8 mRenderQueueID;
00484         bool mRenderQueueIDSet;
00485 
00486         QueuedSubMeshList mQueuedSubMeshes;
00487 
00490         OptimisedSubMeshGeometryList mOptimisedSubMeshGeometryList;
00491 
00496         SubMeshGeometryLookup mSubMeshGeometryLookup;
00497             
00499         RegionMap mRegionMap;
00500 
00504         virtual Region* getRegion(const AxisAlignedBox& bounds, bool autoCreate);
00506         virtual Region* getRegion(const Vector3& point, bool autoCreate);
00508         virtual Region* getRegion(ushort x, ushort y, ushort z, bool autoCreate);
00510         virtual Region* getRegion(uint32 index);
00513         virtual void getRegionIndexes(const Vector3& point, 
00514             ushort& x, ushort& y, ushort& z);
00517         virtual uint32 packIndex(ushort x, ushort y, ushort z);
00520         virtual Real getVolumeIntersection(const AxisAlignedBox& box,  
00521             ushort x, ushort y, ushort z);
00524         virtual AxisAlignedBox getRegionBounds(ushort x, ushort y, ushort z);
00527         virtual Vector3 getRegionCentre(ushort x, ushort y, ushort z);
00529         virtual AxisAlignedBox calculateBounds(VertexData* vertexData, 
00530             const Vector3& position, const Quaternion& orientation, 
00531             const Vector3& scale);
00533         SubMeshLodGeometryLinkList* determineGeometry(SubMesh* sm);
00535         void splitGeometry(VertexData* vd, IndexData* id, 
00536             SubMeshLodGeometryLink* targetGeomLink);
00537 
00538         typedef std::map<size_t, size_t> IndexRemap;
00543         template <typename T>
00544         void buildIndexRemap(T* pBuffer, size_t numIndexes, IndexRemap& remap)
00545         {
00546             remap.clear();
00547             for (size_t i = 0; i < numIndexes; ++i)
00548             {
00549                 // use insert since duplicates are silently discarded
00550                 remap.insert(IndexRemap::value_type(*pBuffer++, remap.size()));
00551                 // this will have mapped oldindex -> new index IF oldindex
00552                 // wasn't already there
00553             }
00554         }
00556         template <typename T>
00557         void remapIndexes(T* src, T* dst, const IndexRemap& remap, 
00558                 size_t numIndexes)
00559         {
00560             for (size_t i = 0; i < numIndexes; ++i)
00561             {
00562                 // look up original and map to target
00563                 IndexRemap::const_iterator ix = remap.find(*src++);
00564                 assert(ix != remap.end());
00565                 *dst++ = static_cast<T>(ix->second);
00566             }
00567         }
00568         
00569     public:
00571         StaticGeometry(SceneManager* owner, const String& name);
00573         virtual ~StaticGeometry();
00574 
00576         const String& getName(void) const { return mName; }
00595         virtual void addEntity(Entity* ent, const Vector3& position,
00596             const Quaternion& orientation = Quaternion::IDENTITY, 
00597             const Vector3& scale = Vector3::UNIT_SCALE);
00598 
00617         virtual void addSceneNode(const SceneNode* node);
00618 
00629         virtual void build(void);
00630 
00636         virtual void destroy(void);
00637 
00641         virtual void reset(void);
00642 
00652         virtual void setRenderingDistance(Real dist) { 
00653             mUpperDistance = dist; 
00654             mSquaredUpperDistance = mUpperDistance * mUpperDistance;
00655         }
00656 
00658         virtual Real getRenderingDistance(void) const { return mUpperDistance; }
00659 
00661         virtual Real getSquaredRenderingDistance(void) const 
00662         { return mSquaredUpperDistance; }
00663 
00665         virtual void setVisible(bool visible);
00666 
00668         virtual bool isVisible(void) const { return mVisible; }
00669 
00687         virtual void setCastShadows(bool castShadows);
00689         virtual bool getCastShadows(void) { return mCastShadows; }
00690 
00701         virtual void setRegionDimensions(const Vector3& size) { 
00702             mRegionDimensions = size; 
00703             mHalfRegionDimensions = size * 0.5;
00704         }
00706         virtual const Vector3& getRegionDimensions(void) const { return mRegionDimensions; }
00718         virtual void setOrigin(const Vector3& origin) { mOrigin = origin; }
00720         virtual const Vector3& getOrigin(void) const { return mOrigin; }
00721 
00733         virtual void setRenderQueueGroup(uint8 queueID);
00734 
00736         virtual uint8 getRenderQueueGroup(void) const;
00738         void visitRenderables(Renderable::Visitor* visitor, 
00739             bool debugRenderables = false);
00740         
00742         typedef MapIterator<RegionMap> RegionIterator;
00744         RegionIterator getRegionIterator(void);
00745 
00749         virtual void dump(const String& filename) const;
00750 
00751 
00752     };
00753 
00754 }
00755 
00756 #endif
00757 

Copyright © 2008 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Sep 27 22:02:26 2009