# 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-2009 Torus Knot Software Ltd
    00008
    00009 Permission is hereby granted, free of charge, to any person obtaining a copy
    00010 of this software and associated documentation files (the "Software"), to deal
    00011 in the Software without restriction, including without limitation the rights
    00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    00013 copies of the Software, and to permit persons to whom the Software is
    00014 furnished to do so, subject to the following conditions:
    00015
    00016 The above copyright notice and this permission notice shall be included in
    00017 all copies or substantial portions of the Software.
    00018
    00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    00025 THE SOFTWARE.
    00026 -----------------------------------------------------------------------------
    00027 */
    00028 #ifndef __StaticGeometry_H__
    00029 #define __StaticGeometry_H__
    00030
    00031 #include "OgrePrerequisites.h"
    00032 #include "OgreMovableObject.h"
    00033 #include "OgreRenderable.h"
    00034 #include "OgreMesh.h"
    00035 #include "OgreLodStrategy.h"
    00036
    00037 namespace Ogre {
    00038
    00120     class _OgreExport StaticGeometry : public BatchedGeometryAlloc
    00121     {
    00122     public:
    00135         class _OgrePrivate OptimisedSubMeshGeometry : public BatchedGeometryAlloc
    00136         {
    00137         public:
    00138             OptimisedSubMeshGeometry() :vertexData(0), indexData(0) {}
    00139             ~OptimisedSubMeshGeometry()
    00140             {
    00141                 OGRE_DELETE vertexData;
    00142                 OGRE_DELETE indexData;
    00143             }
    00144             VertexData *vertexData;
    00145             IndexData *indexData;
    00146         };
    00147         typedef list<OptimisedSubMeshGeometry*>::type OptimisedSubMeshGeometryList;
    00150         struct SubMeshLodGeometryLink
    00151         {
    00152             VertexData* vertexData;
    00153             IndexData* indexData;
    00154         };
    00155         typedef vector<SubMeshLodGeometryLink>::type SubMeshLodGeometryLinkList;
    00156         typedef map<SubMesh*, SubMeshLodGeometryLinkList*>::type SubMeshGeometryLookup;
    00158         struct QueuedSubMesh : public BatchedGeometryAlloc
    00159         {
    00160             SubMesh* submesh;
    00162             SubMeshLodGeometryLinkList* geometryLodList;
    00163             String materialName;
    00164             Vector3 position;
    00165             Quaternion orientation;
    00166             Vector3 scale;
    00168             AxisAlignedBox worldBounds;
    00169         };
    00170         typedef vector<QueuedSubMesh*>::type QueuedSubMeshList;
    00172         struct QueuedGeometry : public BatchedGeometryAlloc
    00173         {
    00174             SubMeshLodGeometryLink* geometry;
    00175             Vector3 position;
    00176             Quaternion orientation;
    00177             Vector3 scale;
    00178         };
    00179         typedef vector<QueuedGeometry*>::type QueuedGeometryList;
    00180
    00181         // forward declarations
    00182         class LODBucket;
    00183         class MaterialBucket;
    00184         class Region;
    00185
    00190         class _OgreExport GeometryBucket :  public Renderable,  public BatchedGeometryAlloc
    00191         {
    00192         protected:
    00194             QueuedGeometryList mQueuedGeometry;
    00196             MaterialBucket* mParent;
    00198             String mFormatString;
    00201             VertexData* mVertexData;
    00204             IndexData* mIndexData;
    00206             HardwareIndexBuffer::IndexType mIndexType;
    00208             size_t mMaxVertexIndex;
    00209
    00210             template<typename T>
    00211             void copyIndexes(const T* src, T* dst, size_t count, size_t indexOffset)
    00212             {
    00213                 if (indexOffset == 0)
    00214                 {
    00215                     memcpy(dst, src, sizeof(T) * count);
    00216                 }
    00217                 else
    00218                 {
    00219                     while(count--)
    00220                     {
    00221                         *dst++ = static_cast<T>(*src++ + indexOffset);
    00222                     }
    00223                 }
    00224             }
    00225         public:
    00226             GeometryBucket(MaterialBucket* parent, const String& formatString,
    00227                 const VertexData* vData, const IndexData* iData);
    00228             virtual ~GeometryBucket();
    00229             MaterialBucket* getParent(void) { return mParent; }
    00231             const VertexData* getVertexData(void) const { return mVertexData; }
    00233             const IndexData* getIndexData(void) const { return mIndexData; }
    00235             const MaterialPtr& getMaterial(void) const;
    00236             Technique* getTechnique(void) const;
    00237             void getRenderOperation(RenderOperation& op);
    00238             void getWorldTransforms(Matrix4* xform) const;
    00239             Real getSquaredViewDepth(const Camera* cam) const;
    00240             const LightList& getLights(void) const;
    00241             bool getCastsShadows(void) const;
    00242
    00246             bool assign(QueuedGeometry* qsm);
    00248             void build(bool stencilShadows);
    00250             void dump(std::ofstream& of) const;
    00251         };
    00254         class _OgreExport MaterialBucket : public BatchedGeometryAlloc
    00255         {
    00256         public:
    00258             typedef vector<GeometryBucket*>::type GeometryBucketList;
    00259         protected:
    00261             LODBucket* mParent;
    00263             String mMaterialName;
    00265             MaterialPtr mMaterial;
    00267             Technique* mTechnique;
    00268
    00270             GeometryBucketList mGeometryBucketList;
    00271             // index to current Geometry Buckets for a given geometry format
    00272             typedef map<String, GeometryBucket*>::type CurrentGeometryMap;
    00273             CurrentGeometryMap mCurrentGeometryMap;
    00275             String getGeometryFormatString(SubMeshLodGeometryLink* geom);
    00276
    00277         public:
    00278             MaterialBucket(LODBucket* parent, const String& materialName);
    00279             virtual ~MaterialBucket();
    00280             LODBucket* getParent(void) { return mParent; }
    00282             const String& getMaterialName(void) const { return mMaterialName; }
    00284             void assign(QueuedGeometry* qsm);
    00286             void build(bool stencilShadows);
    00288             void addRenderables(RenderQueue* queue, uint8 group,
    00289                 Real lodValue);
    00291             const MaterialPtr& getMaterial(void) const { return mMaterial; }
    00293             typedef VectorIterator<GeometryBucketList> GeometryIterator;
    00295             GeometryIterator getGeometryIterator(void);
    00297             Technique* getCurrentTechnique(void) const { return mTechnique; }
    00299             void dump(std::ofstream& of) const;
    00300             void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables);
    00301         };
    00307         class _OgreExport LODBucket : public BatchedGeometryAlloc
    00308         {
    00309         public:
    00311             typedef map<String, MaterialBucket*>::type MaterialBucketMap;
    00312         protected:
    00314             class _OgreExport LODShadowRenderable : public ShadowRenderable
    00315             {
    00316             protected:
    00317                 LODBucket* mParent;
    00318                 // Shared link to position buffer
    00319                 HardwareVertexBufferSharedPtr mPositionBuffer;
    00320                 // Shared link to w-coord buffer (optional)
    00321                 HardwareVertexBufferSharedPtr mWBuffer;
    00322
    00323             public:
    00324                 LODShadowRenderable(LODBucket* parent,
    00325                     HardwareIndexBufferSharedPtr* indexBuffer, const VertexData* vertexData,
    00326                     bool createSeparateLightCap, bool isLightCap = false);
    00327                 ~LODShadowRenderable();
    00329                 void getWorldTransforms(Matrix4* xform) const;
    00330                 HardwareVertexBufferSharedPtr getPositionBuffer(void) { return mPositionBuffer; }
    00331                 HardwareVertexBufferSharedPtr getWBuffer(void) { return mWBuffer; }
    00332
    00333             };
    00335             Region* mParent;
    00337             unsigned short mLod;
    00339             Real mLodValue;
    00341             MaterialBucketMap mMaterialBucketMap;
    00343             QueuedGeometryList mQueuedGeometryList;
    00345             EdgeData* mEdgeList;
    00347             bool mVertexProgramInUse;
    00349             ShadowCaster::ShadowRenderableList mShadowRenderables;
    00350         public:
    00351             LODBucket(Region* parent, unsigned short lod, Real lodValue);
    00352             virtual ~LODBucket();
    00353             Region* getParent(void) { return mParent; }
    00355             ushort getLod(void) const { return mLod; }
    00357             Real getLodValue(void) const { return mLodValue; }
    00359             void assign(QueuedSubMesh* qsm, ushort atLod);
    00361             void build(bool stencilShadows);
    00363             void addRenderables(RenderQueue* queue, uint8 group,
    00364                 Real lodValue);
    00366             typedef MapIterator<MaterialBucketMap> MaterialIterator;
    00368             MaterialIterator getMaterialIterator(void);
    00370             void dump(std::ofstream& of) const;
    00371             void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables);
    00372             EdgeData* getEdgeList() const { return mEdgeList; }
    00373             ShadowCaster::ShadowRenderableList& getShadowRenderableList() { return mShadowRenderables; }
    00374             bool isVertexProgramInUse() const { return mVertexProgramInUse; }
    00375             void updateShadowRenderables(
    00376                 ShadowTechnique shadowTechnique, const Vector4& lightPos,
    00377                 HardwareIndexBufferSharedPtr* indexBuffer,
    00378                 bool extrudeVertices, Real extrusionDistance, unsigned long flags = 0 );
    00379
    00380         };
    00389         class _OgreExport Region : public MovableObject
    00390         {
    00391             friend class MaterialBucket;
    00392             friend class GeometryBucket;
    00393         public:
    00395             typedef vector<LODBucket*>::type LODBucketList;
    00396         protected:
    00398             StaticGeometry* mParent;
    00400             SceneManager* mSceneMgr;
    00402             SceneNode* mNode;
    00404             QueuedSubMeshList mQueuedSubMeshes;
    00406             uint32 mRegionID;
    00408             Vector3 mCentre;
    00410             Mesh::LodValueList mLodValues;
    00412             AxisAlignedBox mAABB;
    00414             Real mBoundingRadius;
    00416             ushort mCurrentLod;
    00418             Real mLodValue;
    00420             LODBucketList mLodBucketList;
    00422             mutable LightList mLightList;
    00424             mutable ulong mLightListUpdated;
    00426             const LodStrategy *mLodStrategy;
    00428             Camera *mCamera;
    00430             Real mSquaredViewDepth;
    00431
    00432         public:
    00433             Region(StaticGeometry* parent, const String& name, SceneManager* mgr,
    00434                 uint32 regionID, const Vector3& centre);
    00435             virtual ~Region();
    00436             // more fields can be added in subclasses
    00437             StaticGeometry* getParent(void) const { return mParent;}
    00439             void assign(QueuedSubMesh* qmesh);
    00441             void build(bool stencilShadows);
    00443             uint32 getID(void) const { return mRegionID; }
    00445             const Vector3& getCentre(void) const { return mCentre; }
    00446             const String& getMovableType(void) const;
    00447             void _notifyCurrentCamera(Camera* cam);
    00448             const AxisAlignedBox& getBoundingBox(void) const;
    00449             Real getBoundingRadius(void) const;
    00450             void _updateRenderQueue(RenderQueue* queue);
    00452             void visitRenderables(Renderable::Visitor* visitor,
    00453                 bool debugRenderables = false);
    00454             bool isVisible(void) const;
    00455             uint32 getTypeFlags(void) const;
    00456
    00457             typedef VectorIterator<LODBucketList> LODIterator;
    00459             LODIterator getLODIterator(void);
    00461             ShadowRenderableListIterator getShadowVolumeRenderableIterator(
    00462                 ShadowTechnique shadowTechnique, const Light* light,
    00463                 HardwareIndexBufferSharedPtr* indexBuffer,
    00464                 bool extrudeVertices, Real extrusionDistance, unsigned long flags = 0 );
    00466             EdgeData* getEdgeList(void);
    00468             bool hasEdgeList(void);
    00469
    00471             void dump(std::ofstream& of) const;
    00472
    00473         };
    00481         typedef map<uint32, Region*>::type RegionMap;
    00482     protected:
    00483         // General state & settings
    00484         SceneManager* mOwner;
    00485         String mName;
    00486         bool mBuilt;
    00487         Real mUpperDistance;
    00488         Real mSquaredUpperDistance;
    00489         bool mCastShadows;
    00490         Vector3 mRegionDimensions;
    00491         Vector3 mHalfRegionDimensions;
    00492         Vector3 mOrigin;
    00493         bool mVisible;
    00495         uint8 mRenderQueueID;
    00497         bool mRenderQueueIDSet;
    00499         uint32 mVisibilityFlags;
    00500
    00501         QueuedSubMeshList mQueuedSubMeshes;
    00502
    00505         OptimisedSubMeshGeometryList mOptimisedSubMeshGeometryList;
    00506
    00511         SubMeshGeometryLookup mSubMeshGeometryLookup;
    00512
    00514         RegionMap mRegionMap;
    00515
    00519         virtual Region* getRegion(const AxisAlignedBox& bounds, bool autoCreate);
    00521         virtual Region* getRegion(const Vector3& point, bool autoCreate);
    00523         virtual Region* getRegion(ushort x, ushort y, ushort z, bool autoCreate);
    00525         virtual Region* getRegion(uint32 index);
    00528         virtual void getRegionIndexes(const Vector3& point,
    00529             ushort& x, ushort& y, ushort& z);
    00532         virtual uint32 packIndex(ushort x, ushort y, ushort z);
    00535         virtual Real getVolumeIntersection(const AxisAlignedBox& box,
    00536             ushort x, ushort y, ushort z);
    00539         virtual AxisAlignedBox getRegionBounds(ushort x, ushort y, ushort z);
    00542         virtual Vector3 getRegionCentre(ushort x, ushort y, ushort z);
    00544         virtual AxisAlignedBox calculateBounds(VertexData* vertexData,
    00545             const Vector3& position, const Quaternion& orientation,
    00546             const Vector3& scale);
    00548         SubMeshLodGeometryLinkList* determineGeometry(SubMesh* sm);
    00550         void splitGeometry(VertexData* vd, IndexData* id,
    00551             SubMeshLodGeometryLink* targetGeomLink);
    00552
    00553         typedef map<size_t, size_t>::type IndexRemap;
    00558         template <typename T>
    00559         void buildIndexRemap(T* pBuffer, size_t numIndexes, IndexRemap& remap)
    00560         {
    00561             remap.clear();
    00562             for (size_t i = 0; i < numIndexes; ++i)
    00563             {
    00564                 // use insert since duplicates are silently discarded
    00565                 remap.insert(IndexRemap::value_type(*pBuffer++, remap.size()));
    00566                 // this will have mapped oldindex -> new index IF oldindex
    00567                 // wasn't already there
    00568             }
    00569         }
    00571         template <typename T>
    00572         void remapIndexes(T* src, T* dst, const IndexRemap& remap,
    00573                 size_t numIndexes)
    00574         {
    00575             for (size_t i = 0; i < numIndexes; ++i)
    00576             {
    00577                 // look up original and map to target
    00578                 IndexRemap::const_iterator ix = remap.find(*src++);
    00579                 assert(ix != remap.end());
    00580                 *dst++ = static_cast<T>(ix->second);
    00581             }
    00582         }
    00583
    00584     public:
    00586         StaticGeometry(SceneManager* owner, const String& name);
    00588         virtual ~StaticGeometry();
    00589
    00591         const String& getName(void) const { return mName; }
    00610         virtual void addEntity(Entity* ent, const Vector3& position,
    00611             const Quaternion& orientation = Quaternion::IDENTITY,
    00612             const Vector3& scale = Vector3::UNIT_SCALE);
    00613
    00632         virtual void addSceneNode(const SceneNode* node);
    00633
    00644         virtual void build(void);
    00645
    00651         virtual void destroy(void);
    00652
    00656         virtual void reset(void);
    00657
    00667         virtual void setRenderingDistance(Real dist) {
    00668             mUpperDistance = dist;
    00669             mSquaredUpperDistance = mUpperDistance * mUpperDistance;
    00670         }
    00671
    00673         virtual Real getRenderingDistance(void) const { return mUpperDistance; }
    00674
    00676         virtual Real getSquaredRenderingDistance(void) const
    00677         { return mSquaredUpperDistance; }
    00678
    00680         virtual void setVisible(bool visible);
    00681
    00683         virtual bool isVisible(void) const { return mVisible; }
    00684
    00702         virtual void setCastShadows(bool castShadows);
    00704         virtual bool getCastShadows(void) { return mCastShadows; }
    00705
    00716         virtual void setRegionDimensions(const Vector3& size) {
    00717             mRegionDimensions = size;
    00718             mHalfRegionDimensions = size * 0.5;
    00719         }
    00721         virtual const Vector3& getRegionDimensions(void) const { return mRegionDimensions; }
    00733         virtual void setOrigin(const Vector3& origin) { mOrigin = origin; }
    00735         virtual const Vector3& getOrigin(void) const { return mOrigin; }
    00736
    00738         void setVisibilityFlags(uint32 flags);
    00740         uint32 getVisibilityFlags() const;
    00741
    00753         virtual void setRenderQueueGroup(uint8 queueID);
    00754
    00756         virtual uint8 getRenderQueueGroup(void) const;
    00758         void visitRenderables(Renderable::Visitor* visitor,
    00759             bool debugRenderables = false);
    00760
    00762         typedef MapIterator<RegionMap> RegionIterator;
    00764         RegionIterator getRegionIterator(void);
    00765
    00769         virtual void dump(const String& filename) const;
    00770
    00771
    00772     };
    00776 }
    00777
    00778 #endif
    00779

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Wed Nov 3 2010 19:24:52