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-2012 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; }
    00333                 virtual void rebindIndexBuffer(const HardwareIndexBufferSharedPtr& indexBuffer);
    00334
    00335             };
    00337             Region* mParent;
    00339             unsigned short mLod;
    00341             Real mLodValue;
    00343             MaterialBucketMap mMaterialBucketMap;
    00345             QueuedGeometryList mQueuedGeometryList;
    00347             EdgeData* mEdgeList;
    00349             bool mVertexProgramInUse;
    00351             ShadowCaster::ShadowRenderableList mShadowRenderables;
    00352         public:
    00353             LODBucket(Region* parent, unsigned short lod, Real lodValue);
    00354             virtual ~LODBucket();
    00355             Region* getParent(void) { return mParent; }
    00357             ushort getLod(void) const { return mLod; }
    00359             Real getLodValue(void) const { return mLodValue; }
    00361             void assign(QueuedSubMesh* qsm, ushort atLod);
    00363             void build(bool stencilShadows);
    00365             void addRenderables(RenderQueue* queue, uint8 group,
    00366                 Real lodValue);
    00368             typedef MapIterator<MaterialBucketMap> MaterialIterator;
    00370             MaterialIterator getMaterialIterator(void);
    00372             void dump(std::ofstream& of) const;
    00373             void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables);
    00374             EdgeData* getEdgeList() const { return mEdgeList; }
    00375             ShadowCaster::ShadowRenderableList& getShadowRenderableList() { return mShadowRenderables; }
    00376             bool isVertexProgramInUse() const { return mVertexProgramInUse; }
    00377             void updateShadowRenderables(
    00378                 ShadowTechnique shadowTechnique, const Vector4& lightPos,
    00379                 HardwareIndexBufferSharedPtr* indexBuffer,
    00380                 bool extrudeVertices, Real extrusionDistance, unsigned long flags = 0 );
    00381
    00382         };
    00391         class _OgreExport Region : public MovableObject
    00392         {
    00393             friend class MaterialBucket;
    00394             friend class GeometryBucket;
    00395         public:
    00397             typedef vector<LODBucket*>::type LODBucketList;
    00398         protected:
    00400             StaticGeometry* mParent;
    00402             SceneManager* mSceneMgr;
    00404             SceneNode* mNode;
    00406             QueuedSubMeshList mQueuedSubMeshes;
    00408             uint32 mRegionID;
    00410             Vector3 mCentre;
    00412             Mesh::LodValueList mLodValues;
    00414             AxisAlignedBox mAABB;
    00416             Real mBoundingRadius;
    00418             ushort mCurrentLod;
    00420             Real mLodValue;
    00422             LODBucketList mLodBucketList;
    00424             mutable LightList mLightList;
    00426             mutable ulong mLightListUpdated;
    00428             const LodStrategy *mLodStrategy;
    00430             Camera *mCamera;
    00432             Real mSquaredViewDepth;
    00433
    00434         public:
    00435             Region(StaticGeometry* parent, const String& name, SceneManager* mgr,
    00436                 uint32 regionID, const Vector3& centre);
    00437             virtual ~Region();
    00438             // more fields can be added in subclasses
    00439             StaticGeometry* getParent(void) const { return mParent;}
    00441             void assign(QueuedSubMesh* qmesh);
    00443             void build(bool stencilShadows);
    00445             uint32 getID(void) const { return mRegionID; }
    00447             const Vector3& getCentre(void) const { return mCentre; }
    00448             const String& getMovableType(void) const;
    00449             void _notifyCurrentCamera(Camera* cam);
    00450             const AxisAlignedBox& getBoundingBox(void) const;
    00451             Real getBoundingRadius(void) const;
    00452             void _updateRenderQueue(RenderQueue* queue);
    00454             void visitRenderables(Renderable::Visitor* visitor,
    00455                 bool debugRenderables = false);
    00456             bool isVisible(void) const;
    00457             uint32 getTypeFlags(void) const;
    00458
    00459             typedef VectorIterator<LODBucketList> LODIterator;
    00461             LODIterator getLODIterator(void);
    00463             ShadowRenderableListIterator getShadowVolumeRenderableIterator(
    00464                 ShadowTechnique shadowTechnique, const Light* light,
    00465                 HardwareIndexBufferSharedPtr* indexBuffer,
    00466                 bool extrudeVertices, Real extrusionDistance, unsigned long flags = 0 );
    00468             EdgeData* getEdgeList(void);
    00470             bool hasEdgeList(void);
    00471
    00473             void dump(std::ofstream& of) const;
    00474
    00475         };
    00483         typedef map<uint32, Region*>::type RegionMap;
    00484     protected:
    00485         // General state & settings
    00486         SceneManager* mOwner;
    00487         String mName;
    00488         bool mBuilt;
    00489         Real mUpperDistance;
    00490         Real mSquaredUpperDistance;
    00491         bool mCastShadows;
    00492         Vector3 mRegionDimensions;
    00493         Vector3 mHalfRegionDimensions;
    00494         Vector3 mOrigin;
    00495         bool mVisible;
    00497         uint8 mRenderQueueID;
    00499         bool mRenderQueueIDSet;
    00501         uint32 mVisibilityFlags;
    00502
    00503         QueuedSubMeshList mQueuedSubMeshes;
    00504
    00507         OptimisedSubMeshGeometryList mOptimisedSubMeshGeometryList;
    00508
    00513         SubMeshGeometryLookup mSubMeshGeometryLookup;
    00514
    00516         RegionMap mRegionMap;
    00517
    00521         virtual Region* getRegion(const AxisAlignedBox& bounds, bool autoCreate);
    00523         virtual Region* getRegion(const Vector3& point, bool autoCreate);
    00525         virtual Region* getRegion(ushort x, ushort y, ushort z, bool autoCreate);
    00527         virtual Region* getRegion(uint32 index);
    00530         virtual void getRegionIndexes(const Vector3& point,
    00531             ushort& x, ushort& y, ushort& z);
    00534         virtual uint32 packIndex(ushort x, ushort y, ushort z);
    00537         virtual Real getVolumeIntersection(const AxisAlignedBox& box,
    00538             ushort x, ushort y, ushort z);
    00541         virtual AxisAlignedBox getRegionBounds(ushort x, ushort y, ushort z);
    00544         virtual Vector3 getRegionCentre(ushort x, ushort y, ushort z);
    00546         virtual AxisAlignedBox calculateBounds(VertexData* vertexData,
    00547             const Vector3& position, const Quaternion& orientation,
    00548             const Vector3& scale);
    00550         SubMeshLodGeometryLinkList* determineGeometry(SubMesh* sm);
    00552         void splitGeometry(VertexData* vd, IndexData* id,
    00553             SubMeshLodGeometryLink* targetGeomLink);
    00554
    00555         typedef map<size_t, size_t>::type IndexRemap;
    00560         template <typename T>
    00561         void buildIndexRemap(T* pBuffer, size_t numIndexes, IndexRemap& remap)
    00562         {
    00563             remap.clear();
    00564             for (size_t i = 0; i < numIndexes; ++i)
    00565             {
    00566                 // use insert since duplicates are silently discarded
    00567                 remap.insert(IndexRemap::value_type(*pBuffer++, remap.size()));
    00568                 // this will have mapped oldindex -> new index IF oldindex
    00569                 // wasn't already there
    00570             }
    00571         }
    00573         template <typename T>
    00574         void remapIndexes(T* src, T* dst, const IndexRemap& remap,
    00575                 size_t numIndexes)
    00576         {
    00577             for (size_t i = 0; i < numIndexes; ++i)
    00578             {
    00579                 // look up original and map to target
    00580                 IndexRemap::const_iterator ix = remap.find(*src++);
    00581                 assert(ix != remap.end());
    00582                 *dst++ = static_cast<T>(ix->second);
    00583             }
    00584         }
    00585
    00586     public:
    00588         StaticGeometry(SceneManager* owner, const String& name);
    00590         virtual ~StaticGeometry();
    00591
    00593         const String& getName(void) const { return mName; }
    00612         virtual void addEntity(Entity* ent, const Vector3& position,
    00613             const Quaternion& orientation = Quaternion::IDENTITY,
    00614             const Vector3& scale = Vector3::UNIT_SCALE);
    00615
    00634         virtual void addSceneNode(const SceneNode* node);
    00635
    00646         virtual void build(void);
    00647
    00653         virtual void destroy(void);
    00654
    00658         virtual void reset(void);
    00659
    00669         virtual void setRenderingDistance(Real dist) {
    00670             mUpperDistance = dist;
    00671             mSquaredUpperDistance = mUpperDistance * mUpperDistance;
    00672         }
    00673
    00675         virtual Real getRenderingDistance(void) const { return mUpperDistance; }
    00676
    00678         virtual Real getSquaredRenderingDistance(void) const
    00679         { return mSquaredUpperDistance; }
    00680
    00682         virtual void setVisible(bool visible);
    00683
    00685         virtual bool isVisible(void) const { return mVisible; }
    00686
    00704         virtual void setCastShadows(bool castShadows);
    00706         virtual bool getCastShadows(void) { return mCastShadows; }
    00707
    00718         virtual void setRegionDimensions(const Vector3& size) {
    00719             mRegionDimensions = size;
    00720             mHalfRegionDimensions = size * 0.5;
    00721         }
    00723         virtual const Vector3& getRegionDimensions(void) const { return mRegionDimensions; }
    00735         virtual void setOrigin(const Vector3& origin) { mOrigin = origin; }
    00737         virtual const Vector3& getOrigin(void) const { return mOrigin; }
    00738
    00740         void setVisibilityFlags(uint32 flags);
    00742         uint32 getVisibilityFlags() const;
    00743
    00755         virtual void setRenderQueueGroup(uint8 queueID);
    00756
    00758         virtual uint8 getRenderQueueGroup(void) const;
    00760         void visitRenderables(Renderable::Visitor* visitor,
    00761             bool debugRenderables = false);
    00762
    00764         typedef MapIterator<RegionMap> RegionIterator;
    00766         RegionIterator getRegionIterator(void);
    00767
    00771         virtual void dump(const String& filename) const;
    00772
    00773
    00774     };
    00778 }
    00779
    00780 #endif
    00781

Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sun Sep 2 2012 07:27:24