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 
00032 namespace Ogre {
00033 
00106     class _OgreExport StaticGeometry
00107     {
00108     public:
00121         class _OgrePrivate OptimisedSubMeshGeometry
00122         {
00123         public:
00124             OptimisedSubMeshGeometry() :vertexData(0), indexData(0) {}
00125             ~OptimisedSubMeshGeometry() 
00126             {
00127                 delete vertexData;
00128                 delete indexData;
00129             }
00130             VertexData *vertexData;
00131             IndexData *indexData;
00132         };
00133         typedef std::list<OptimisedSubMeshGeometry*> OptimisedSubMeshGeometryList;
00136         struct SubMeshLodGeometryLink
00137         {
00138             VertexData* vertexData;
00139             IndexData* indexData;
00140         };
00141         typedef std::vector<SubMeshLodGeometryLink> SubMeshLodGeometryLinkList;
00142         typedef std::map<SubMesh*, SubMeshLodGeometryLinkList*> SubMeshGeometryLookup;
00144         struct QueuedSubMesh
00145         {
00146             SubMesh* submesh;
00148             SubMeshLodGeometryLinkList* geometryLodList;
00149             String materialName;
00150             Vector3 position;
00151             Quaternion orientation;
00152             Vector3 scale;
00154             AxisAlignedBox worldBounds;
00155         };
00156         typedef std::vector<QueuedSubMesh*> QueuedSubMeshList;
00158         struct QueuedGeometry
00159         {
00160             SubMeshLodGeometryLink* geometry;
00161             Vector3 position;
00162             Quaternion orientation;
00163             Vector3 scale;
00164         };
00165         typedef std::vector<QueuedGeometry*> QueuedGeometryList;
00166         
00167         // forward declarations
00168         class LODBucket;
00169         class MaterialBucket;
00170         class Region;
00171 
00176         class _OgreExport GeometryBucket :  public Renderable
00177         {
00178         protected:
00180             QueuedGeometryList mQueuedGeometry;
00182             MaterialBucket* mParent;
00184             String mFormatString;
00187             VertexData* mVertexData;
00190             IndexData* mIndexData;
00192             HardwareIndexBuffer::IndexType mIndexType;
00194             size_t mMaxVertexIndex;
00195 
00196             template<typename T>
00197             void copyIndexes(const T* src, T* dst, size_t count, size_t indexOffset)
00198             {
00199                 if (indexOffset == 0)
00200                 {
00201                     memcpy(dst, src, sizeof(T) * count);
00202                 }
00203                 else
00204                 {
00205                     while(count--)
00206                     {
00207                         *dst++ = static_cast<T>(*src++ + indexOffset);
00208                     }
00209                 }
00210             }
00211         public:
00212             GeometryBucket(MaterialBucket* parent, const String& formatString, 
00213                 const VertexData* vData, const IndexData* iData);
00214             virtual ~GeometryBucket();
00215             MaterialBucket* getParent(void) { return mParent; }
00217             const VertexData* getVertexData(void) const { return mVertexData; }
00219             const IndexData* getIndexData(void) const { return mIndexData; }
00221             const MaterialPtr& getMaterial(void) const;
00222             Technique* getTechnique(void) const;
00223             void getRenderOperation(RenderOperation& op);
00224             void getWorldTransforms(Matrix4* xform) const;
00225             const Quaternion& getWorldOrientation(void) const;
00226             const Vector3& getWorldPosition(void) const;
00227             Real getSquaredViewDepth(const Camera* cam) const;
00228             const LightList& getLights(void) const;
00229             bool getCastsShadows(void) const;
00230             
00234             bool assign(QueuedGeometry* qsm);
00236             void build(bool stencilShadows);
00238             void dump(std::ofstream& of) const;
00239         };
00242         class _OgreExport MaterialBucket
00243         {
00244         public:
00246             typedef std::vector<GeometryBucket*> GeometryBucketList;
00247         protected:
00249             LODBucket* mParent;
00251             String mMaterialName;
00253             MaterialPtr mMaterial;
00255             Technique* mTechnique;
00256 
00258             GeometryBucketList mGeometryBucketList;
00259             // index to current Geometry Buckets for a given geometry format
00260             typedef std::map<String, GeometryBucket*> CurrentGeometryMap;
00261             CurrentGeometryMap mCurrentGeometryMap;
00263             String getGeometryFormatString(SubMeshLodGeometryLink* geom);
00264             
00265         public:
00266             MaterialBucket(LODBucket* parent, const String& materialName);
00267             virtual ~MaterialBucket();
00268             LODBucket* getParent(void) { return mParent; }
00270             const String& getMaterialName(void) const { return mMaterialName; }
00272             void assign(QueuedGeometry* qsm);
00274             void build(bool stencilShadows);
00276             void addRenderables(RenderQueue* queue, uint8 group, 
00277                 Real camSquaredDist);
00279             const MaterialPtr& getMaterial(void) const { return mMaterial; }
00281             typedef VectorIterator<GeometryBucketList> GeometryIterator;
00283             GeometryIterator getGeometryIterator(void);
00285             Technique* getCurrentTechnique(void) const { return mTechnique; }
00287             void dump(std::ofstream& of) const;
00288         };
00294         class _OgreExport LODBucket
00295         {
00296         public:
00298             typedef std::map<String, MaterialBucket*> MaterialBucketMap;
00299         protected:
00301             Region* mParent;
00303             unsigned short mLod;
00305             Real mSquaredDistance;
00307             MaterialBucketMap mMaterialBucketMap;
00309             QueuedGeometryList mQueuedGeometryList;
00310         public:
00311             LODBucket(Region* parent, unsigned short lod, Real lodDist);
00312             virtual ~LODBucket();
00313             Region* getParent(void) { return mParent; }
00315             ushort getLod(void) const { return mLod; }
00317             Real getSquaredDistance(void) const { return mSquaredDistance; }
00319             void assign(QueuedSubMesh* qsm, ushort atLod);
00321             void build(bool stencilShadows);
00323             void addRenderables(RenderQueue* queue, uint8 group, 
00324                 Real camSquaredDistance);
00326             typedef MapIterator<MaterialBucketMap> MaterialIterator;
00328             MaterialIterator getMaterialIterator(void);
00330             void dump(std::ofstream& of) const;
00331             
00332         };
00341         class _OgreExport Region : public MovableObject
00342         {
00343         public:
00345             typedef std::vector<LODBucket*> LODBucketList;
00346         protected:
00348             class _OgreExport RegionShadowRenderable : public ShadowRenderable
00349             {
00350             protected:
00351                 Region* mParent;
00352                 // Shared link to position buffer
00353                 HardwareVertexBufferSharedPtr mPositionBuffer;
00354                 // Shared link to w-coord buffer (optional)
00355                 HardwareVertexBufferSharedPtr mWBuffer;
00356 
00357             public:
00358                 RegionShadowRenderable(Region* parent, 
00359                     HardwareIndexBufferSharedPtr* indexBuffer, const VertexData* vertexData, 
00360                     bool createSeparateLightCap, bool isLightCap = false);
00361                 ~RegionShadowRenderable();
00363                 void getWorldTransforms(Matrix4* xform) const;
00365                 const Quaternion& getWorldOrientation(void) const;
00367                 const Vector3& getWorldPosition(void) const;
00368                 HardwareVertexBufferSharedPtr getPositionBuffer(void) { return mPositionBuffer; }
00369                 HardwareVertexBufferSharedPtr getWBuffer(void) { return mWBuffer; }
00370 
00371             };
00373             StaticGeometry* mParent;
00375             SceneManager* mSceneMgr;
00377             SceneNode* mNode;
00379             QueuedSubMeshList mQueuedSubMeshes;
00381             uint32 mRegionID;
00383             Vector3 mCentre;
00385             std::vector<Real> mLodSquaredDistances;
00387             AxisAlignedBox mAABB;
00389             Real mBoundingRadius;
00391             ushort mCurrentLod;
00393             Real mCamDistanceSquared;
00395             LODBucketList mLodBucketList;
00397             mutable LightList mLightList;
00399             mutable ulong mLightListUpdated;
00401             EdgeData* mEdgeList;
00403             ShadowRenderableList mShadowRenderables;
00405             bool mVertexProgramInUse;
00406 
00407 
00408 
00409         public:
00410             Region(StaticGeometry* parent, const String& name, SceneManager* mgr, 
00411                 uint32 regionID, const Vector3& centre);
00412             virtual ~Region();
00413             // more fields can be added in subclasses
00414             StaticGeometry* getParent(void) const { return mParent;}
00416             void assign(QueuedSubMesh* qmesh);
00418             void build(bool stencilShadows);
00420             uint32 getID(void) const { return mRegionID; }
00422             const Vector3& getCentre(void) const { return mCentre; }
00423             const String& getMovableType(void) const;
00424             void _notifyCurrentCamera(Camera* cam);
00425             const AxisAlignedBox& getBoundingBox(void) const;
00426             Real getBoundingRadius(void) const;
00427             void _updateRenderQueue(RenderQueue* queue);
00428             bool isVisible(void) const;
00429             uint32 getTypeFlags(void) const;
00430 
00431             typedef VectorIterator<LODBucketList> LODIterator;
00433             LODIterator getLODIterator(void);
00435             const LightList& getLights(void) const;
00437             ShadowRenderableListIterator getShadowVolumeRenderableIterator(
00438                 ShadowTechnique shadowTechnique, const Light* light, 
00439                 HardwareIndexBufferSharedPtr* indexBuffer, 
00440                 bool extrudeVertices, Real extrusionDistance, unsigned long flags = 0 );
00442             EdgeData* getEdgeList(void);
00443 
00444 
00446             void dump(std::ofstream& of) const;
00447             
00448         };
00456         typedef std::map<uint32, Region*> RegionMap;
00457     protected:
00458         // General state & settings
00459         SceneManager* mOwner;
00460         String mName;
00461         bool mBuilt;
00462         Real mUpperDistance;
00463         Real mSquaredUpperDistance;
00464         bool mCastShadows;
00465         Vector3 mRegionDimensions;
00466         Vector3 mHalfRegionDimensions;
00467         Vector3 mOrigin;
00468         bool mVisible;
00470         uint8 mRenderQueueID;
00472         bool mRenderQueueIDSet;
00473 
00474         QueuedSubMeshList mQueuedSubMeshes;
00475 
00478         OptimisedSubMeshGeometryList mOptimisedSubMeshGeometryList;
00479 
00484         SubMeshGeometryLookup mSubMeshGeometryLookup;
00485             
00487         RegionMap mRegionMap;
00488 
00492         virtual Region* getRegion(const AxisAlignedBox& bounds, bool autoCreate);
00494         virtual Region* getRegion(const Vector3& point, bool autoCreate);
00496         virtual Region* getRegion(ushort x, ushort y, ushort z, bool autoCreate);
00498         virtual Region* getRegion(uint32 index);
00501         virtual void getRegionIndexes(const Vector3& point, 
00502             ushort& x, ushort& y, ushort& z);
00505         virtual uint32 packIndex(ushort x, ushort y, ushort z);
00508         virtual Real getVolumeIntersection(const AxisAlignedBox& box,  
00509             ushort x, ushort y, ushort z);
00512         virtual AxisAlignedBox getRegionBounds(ushort x, ushort y, ushort z);
00515         virtual Vector3 getRegionCentre(ushort x, ushort y, ushort z);
00517         virtual AxisAlignedBox calculateBounds(VertexData* vertexData, 
00518             const Vector3& position, const Quaternion& orientation, 
00519             const Vector3& scale);
00521         SubMeshLodGeometryLinkList* determineGeometry(SubMesh* sm);
00523         void splitGeometry(VertexData* vd, IndexData* id, 
00524             SubMeshLodGeometryLink* targetGeomLink);
00525 
00526         typedef std::map<size_t, size_t> IndexRemap;
00531         template <typename T>
00532         void buildIndexRemap(T* pBuffer, size_t numIndexes, IndexRemap& remap)
00533         {
00534             remap.clear();
00535             for (size_t i = 0; i < numIndexes; ++i)
00536             {
00537                 // use insert since duplicates are silently discarded
00538                 remap.insert(IndexRemap::value_type(*pBuffer++, remap.size()));
00539                 // this will have mapped oldindex -> new index IF oldindex
00540                 // wasn't already there
00541             }
00542         }
00544         template <typename T>
00545         void remapIndexes(T* src, T* dst, const IndexRemap& remap, 
00546                 size_t numIndexes)
00547         {
00548             for (size_t i = 0; i < numIndexes; ++i)
00549             {
00550                 // look up original and map to target
00551                 IndexRemap::const_iterator ix = remap.find(*src++);
00552                 assert(ix != remap.end());
00553                 *dst++ = static_cast<T>(ix->second);
00554             }
00555         }
00556         
00557     public:
00559         StaticGeometry(SceneManager* owner, const String& name);
00561         virtual ~StaticGeometry();
00562 
00564         const String& getName(void) const { return mName; }
00583         virtual void addEntity(Entity* ent, const Vector3& position,
00584             const Quaternion& orientation = Quaternion::IDENTITY, 
00585             const Vector3& scale = Vector3::UNIT_SCALE);
00586 
00605         virtual void addSceneNode(const SceneNode* node);
00606 
00617         virtual void build(void);
00618 
00624         virtual void destroy(void);
00625 
00629         virtual void reset(void);
00630 
00640         virtual void setRenderingDistance(Real dist) { 
00641             mUpperDistance = dist; 
00642             mSquaredUpperDistance = mUpperDistance * mUpperDistance;
00643         }
00644 
00646         virtual Real getRenderingDistance(void) const { return mUpperDistance; }
00647 
00649         virtual Real getSquaredRenderingDistance(void) const 
00650         { return mSquaredUpperDistance; }
00651 
00653         virtual void setVisible(bool visible);
00654 
00656         virtual bool isVisible(void) const { return mVisible; }
00657 
00675         virtual void setCastShadows(bool castShadows);
00677         virtual bool getCastShadows(void) { return mCastShadows; }
00678 
00689         virtual void setRegionDimensions(const Vector3& size) { 
00690             mRegionDimensions = size; 
00691             mHalfRegionDimensions = size * 0.5;
00692         }
00694         virtual const Vector3& getRegionDimensions(void) const { return mRegionDimensions; }
00706         virtual void setOrigin(const Vector3& origin) { mOrigin = origin; }
00708         virtual const Vector3& getOrigin(void) const { return mOrigin; }
00709 
00721         virtual void setRenderQueueGroup(uint8 queueID);
00722 
00724         virtual uint8 getRenderQueueGroup(void) const;
00725         
00727         typedef MapIterator<RegionMap> RegionIterator;
00729         RegionIterator getRegionIterator(void);
00730 
00734         virtual void dump(const String& filename) const;
00735 
00736 
00737     };
00738 
00739 }
00740 
00741 #endif
00742 

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 Mar 12 14:37:50 2006