Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

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-2005 The OGRE Team
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 */
00025 #ifndef __StaticGeometry_H__
00026 #define __StaticGeometry_H__
00027 
00028 #include "OgrePrerequisites.h"
00029 #include "OgreMovableObject.h"
00030 #include "OgreRenderable.h"
00031 #include "OgreEdgeListBuilder.h"
00032 
00033 namespace Ogre {
00034 
00107     class _OgreExport StaticGeometry
00108     {
00109     public:
00122         class _OgrePrivate OptimisedSubMeshGeometry
00123         {
00124         public:
00125             OptimisedSubMeshGeometry() :vertexData(0), indexData(0) {}
00126             ~OptimisedSubMeshGeometry() 
00127             {
00128                 delete vertexData;
00129                 delete indexData;
00130             }
00131             VertexData *vertexData;
00132             IndexData *indexData;
00133         };
00134         typedef std::list<OptimisedSubMeshGeometry*> OptimisedSubMeshGeometryList;
00137         struct SubMeshLodGeometryLink
00138         {
00139             VertexData* vertexData;
00140             IndexData* indexData;
00141         };
00142         typedef std::vector<SubMeshLodGeometryLink> SubMeshLodGeometryLinkList;
00143         typedef std::map<SubMesh*, SubMeshLodGeometryLinkList*> SubMeshGeometryLookup;
00145         struct QueuedSubMesh
00146         {
00147             SubMesh* submesh;
00149             SubMeshLodGeometryLinkList* geometryLodList;
00150             String materialName;
00151             Vector3 position;
00152             Quaternion orientation;
00153             Vector3 scale;
00155             AxisAlignedBox worldBounds;
00156         };
00157         typedef std::vector<QueuedSubMesh*> QueuedSubMeshList;
00159         struct QueuedGeometry
00160         {
00161             SubMeshLodGeometryLink* geometry;
00162             Vector3 position;
00163             Quaternion orientation;
00164             Vector3 scale;
00165         };
00166         typedef std::vector<QueuedGeometry*> QueuedGeometryList;
00167         
00168         // forward declarations
00169         class LODBucket;
00170         class MaterialBucket;
00171         class Region;
00172 
00177         class _OgreExport GeometryBucket : public Renderable
00178         {
00179         protected:
00181             QueuedGeometryList mQueuedGeometry;
00183             MaterialBucket* mParent;
00185             String mFormatString;
00188             VertexData* mVertexData;
00191             IndexData* mIndexData;
00193             HardwareIndexBuffer::IndexType mIndexType;
00195             size_t mMaxVertexIndex;
00196 
00197             template<typename T>
00198             void copyIndexes(const T* src, T* dst, size_t count, size_t indexOffset)
00199             {
00200                 if (indexOffset == 0)
00201                 {
00202                     memcpy(dst, src, sizeof(T) * count);
00203                 }
00204                 else
00205                 {
00206                     while(count--)
00207                     {
00208                         *dst++ = static_cast<T>(*src++ + indexOffset);
00209                     }
00210                 }
00211             }
00212         public:
00213             GeometryBucket(MaterialBucket* parent, const String& formatString, 
00214                 const VertexData* vData, const IndexData* iData);
00215             virtual ~GeometryBucket();
00216             MaterialBucket* getParent(void) { return mParent; }
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 getRenderOperation(RenderOperation& op);
00225             void getWorldTransforms(Matrix4* xform) 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             
00235             bool assign(QueuedGeometry* qsm);
00237             void build(bool stencilShadows);
00239             void dump(std::ofstream& of) const;
00240         };
00243         class _OgreExport MaterialBucket
00244         {
00245         public:
00247             typedef std::vector<GeometryBucket*> GeometryBucketList;
00248         protected:
00250             LODBucket* mParent;
00252             String mMaterialName;
00254             MaterialPtr mMaterial;
00256             Technique* mTechnique;
00257 
00259             GeometryBucketList mGeometryBucketList;
00260             // index to current Geometry Buckets for a given geometry format
00261             typedef std::map<String, GeometryBucket*> CurrentGeometryMap;
00262             CurrentGeometryMap mCurrentGeometryMap;
00264             String getGeometryFormatString(SubMeshLodGeometryLink* geom);
00265             
00266         public:
00267             MaterialBucket(LODBucket* parent, const String& materialName);
00268             virtual ~MaterialBucket();
00269             LODBucket* getParent(void) { return mParent; }
00271             const String& getMaterialName(void) const { return mMaterialName; }
00273             void assign(QueuedGeometry* qsm);
00275             void build(bool stencilShadows);
00277             void addRenderables(RenderQueue* queue, RenderQueueGroupID group, 
00278                 Real camSquaredDist);
00280             const MaterialPtr& getMaterial(void) const { return mMaterial; }
00282             typedef VectorIterator<GeometryBucketList> GeometryIterator;
00284             GeometryIterator getGeometryIterator(void);
00286             Technique* getCurrentTechnique(void) const { return mTechnique; }
00288             void dump(std::ofstream& of) const;
00289         };
00295         class _OgreExport LODBucket
00296         {
00297         public:
00299             typedef std::map<String, MaterialBucket*> MaterialBucketMap;
00300         protected:
00302             Region* mParent;
00304             unsigned short mLod;
00306             Real mSquaredDistance;
00308             MaterialBucketMap mMaterialBucketMap;
00310             QueuedGeometryList mQueuedGeometryList;
00311         public:
00312             LODBucket(Region* parent, unsigned short lod, Real lodDist);
00313             virtual ~LODBucket();
00314             Region* getParent(void) { return mParent; }
00316             ushort getLod(void) const { return mLod; }
00318             Real getSquaredDistance(void) const { return mSquaredDistance; }
00320             void assign(QueuedSubMesh* qsm, ushort atLod);
00322             void build(bool stencilShadows);
00324             void addRenderables(RenderQueue* queue, RenderQueueGroupID group, 
00325                 Real camSquaredDistance);
00327             typedef MapIterator<MaterialBucketMap> MaterialIterator;
00329             MaterialIterator getMaterialIterator(void);
00331             void dump(std::ofstream& of) const;
00332             
00333         };
00342         class _OgreExport Region : public MovableObject
00343         {
00344         public:
00346             typedef std::vector<LODBucket*> LODBucketList;
00347         protected:
00349             class _OgreExport RegionShadowRenderable : public ShadowRenderable
00350             {
00351             protected:
00352                 Region* mParent;
00353                 // Shared link to position buffer
00354                 HardwareVertexBufferSharedPtr mPositionBuffer;
00355                 // Shared link to w-coord buffer (optional)
00356                 HardwareVertexBufferSharedPtr mWBuffer;
00357 
00358             public:
00359                 RegionShadowRenderable(Region* parent, 
00360                     HardwareIndexBufferSharedPtr* indexBuffer, const VertexData* vertexData, 
00361                     bool createSeparateLightCap, bool isLightCap = false);
00362                 ~RegionShadowRenderable();
00364                 void getWorldTransforms(Matrix4* xform) const;
00366                 const Quaternion& getWorldOrientation(void) const;
00368                 const Vector3& getWorldPosition(void) const;
00369                 HardwareVertexBufferSharedPtr getPositionBuffer(void) { return mPositionBuffer; }
00370                 HardwareVertexBufferSharedPtr getWBuffer(void) { return mWBuffer; }
00371 
00372             };
00374             StaticGeometry* mParent;
00376             String mName;
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             bool mBeyondFarDistance;
00406             EdgeData* mEdgeList;
00408             ShadowRenderableList mShadowRenderables;
00410             bool mVertexProgramInUse;
00411 
00412 
00413 
00414         public:
00415             Region(StaticGeometry* parent, const String& name, SceneManager* mgr, 
00416                 uint32 regionID, const Vector3& centre);
00417             virtual ~Region();
00418             // more fields can be added in subclasses
00419             StaticGeometry* getParent(void) const { return mParent;}
00421             void assign(QueuedSubMesh* qmesh);
00423             void build(bool stencilShadows);
00425             uint32 getID(void) const { return mRegionID; }
00427             const Vector3& getCentre(void) const { return mCentre; }
00428             const String& getName(void) const;
00429             const String& getMovableType(void) const;
00430             void _notifyCurrentCamera(Camera* cam);
00431             const AxisAlignedBox& getBoundingBox(void) const;
00432             Real getBoundingRadius(void) const;
00433             void _updateRenderQueue(RenderQueue* queue);
00434             bool isVisible(void) const;
00435 
00436             typedef VectorIterator<LODBucketList> LODIterator;
00438             LODIterator getLODIterator(void);
00440             const LightList& getLights(void) const;
00442             ShadowRenderableListIterator getShadowVolumeRenderableIterator(
00443                 ShadowTechnique shadowTechnique, const Light* light, 
00444                 HardwareIndexBufferSharedPtr* indexBuffer, 
00445                 bool extrudeVertices, Real extrusionDistance, unsigned long flags = 0 );
00447             EdgeData* getEdgeList(void);
00448 
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         RenderQueueGroupID 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(RenderQueueGroupID queueID);
00727 
00729         virtual RenderQueueGroupID getRenderQueueGroup(void) const;
00730         
00732         typedef MapIterator<RegionMap> RegionIterator;
00734         RegionIterator getRegionIterator(void);
00735 
00739         virtual void dump(const String& filename) const;
00740 
00741 
00742     };
00743 
00744 }
00745 
00746 #endif
00747 

Copyright © 2000-2005 by The OGRE Team
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Feb 12 12:59:53 2006