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-2013 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 #include "OgreHeaderPrefix.h"
00037 
00038 namespace Ogre {
00039 
00121     class _OgreExport StaticGeometry : public BatchedGeometryAlloc
00122     {
00123     public:
00136         class _OgrePrivate OptimisedSubMeshGeometry : public BatchedGeometryAlloc
00137         {
00138         public:
00139             OptimisedSubMeshGeometry() :vertexData(0), indexData(0) {}
00140             ~OptimisedSubMeshGeometry() 
00141             {
00142                 OGRE_DELETE vertexData;
00143                 OGRE_DELETE indexData;
00144             }
00145             VertexData *vertexData;
00146             IndexData *indexData;
00147         };
00148         typedef list<OptimisedSubMeshGeometry*>::type OptimisedSubMeshGeometryList;
00151         struct SubMeshLodGeometryLink
00152         {
00153             VertexData* vertexData;
00154             IndexData* indexData;
00155         };
00156         typedef vector<SubMeshLodGeometryLink>::type SubMeshLodGeometryLinkList;
00157         typedef map<SubMesh*, SubMeshLodGeometryLinkList*>::type SubMeshGeometryLookup;
00159         struct QueuedSubMesh : public BatchedGeometryAlloc
00160         {
00161             SubMesh* submesh;
00163             SubMeshLodGeometryLinkList* geometryLodList;
00164             String materialName;
00165             Vector3 position;
00166             Quaternion orientation;
00167             Vector3 scale;
00169             AxisAlignedBox worldBounds;
00170         };
00171         typedef vector<QueuedSubMesh*>::type QueuedSubMeshList;
00173         struct QueuedGeometry : public BatchedGeometryAlloc
00174         {
00175             SubMeshLodGeometryLink* geometry;
00176             Vector3 position;
00177             Quaternion orientation;
00178             Vector3 scale;
00179         };
00180         typedef vector<QueuedGeometry*>::type QueuedGeometryList;
00181         
00182         // forward declarations
00183         class LODBucket;
00184         class MaterialBucket;
00185         class Region;
00186 
00191         class _OgreExport GeometryBucket :  public Renderable,  public BatchedGeometryAlloc
00192         {
00193         protected:
00195             QueuedGeometryList mQueuedGeometry;
00197             MaterialBucket* mParent;
00199             String mFormatString;
00202             VertexData* mVertexData;
00205             IndexData* mIndexData;
00207             HardwareIndexBuffer::IndexType mIndexType;
00209             size_t mMaxVertexIndex;
00210 
00211             template<typename T>
00212             void copyIndexes(const T* src, T* dst, size_t count, size_t indexOffset)
00213             {
00214                 if (indexOffset == 0)
00215                 {
00216                     memcpy(dst, src, sizeof(T) * count);
00217                 }
00218                 else
00219                 {
00220                     while(count--)
00221                     {
00222                         *dst++ = static_cast<T>(*src++ + indexOffset);
00223                     }
00224                 }
00225             }
00226         public:
00227             GeometryBucket(MaterialBucket* parent, const String& formatString, 
00228                 const VertexData* vData, const IndexData* iData);
00229             virtual ~GeometryBucket();
00230             MaterialBucket* getParent(void) { return mParent; }
00232             const VertexData* getVertexData(void) const { return mVertexData; }
00234             const IndexData* getIndexData(void) const { return mIndexData; }
00236             const MaterialPtr& getMaterial(void) const;
00237             Technique* getTechnique(void) const;
00238             void getRenderOperation(RenderOperation& op);
00239             void getWorldTransforms(Matrix4* xform) const;
00240             Real getSquaredViewDepth(const Camera* cam) const;
00241             const LightList& getLights(void) const;
00242             bool getCastsShadows(void) const;
00243             
00247             bool assign(QueuedGeometry* qsm);
00249             void build(bool stencilShadows);
00251             void dump(std::ofstream& of) const;
00252         };
00255         class _OgreExport MaterialBucket : public BatchedGeometryAlloc
00256         {
00257         public:
00259             typedef vector<GeometryBucket*>::type GeometryBucketList;
00260         protected:
00262             LODBucket* mParent;
00264             String mMaterialName;
00266             MaterialPtr mMaterial;
00268             Technique* mTechnique;
00269 
00271             GeometryBucketList mGeometryBucketList;
00272             // index to current Geometry Buckets for a given geometry format
00273             typedef map<String, GeometryBucket*>::type CurrentGeometryMap;
00274             CurrentGeometryMap mCurrentGeometryMap;
00276             String getGeometryFormatString(SubMeshLodGeometryLink* geom);
00277             
00278         public:
00279             MaterialBucket(LODBucket* parent, const String& materialName);
00280             virtual ~MaterialBucket();
00281             LODBucket* getParent(void) { return mParent; }
00283             const String& getMaterialName(void) const { return mMaterialName; }
00285             void assign(QueuedGeometry* qsm);
00287             void build(bool stencilShadows);
00289             void addRenderables(RenderQueue* queue, uint8 group, 
00290                 Real lodValue);
00292             const MaterialPtr& getMaterial(void) const { return mMaterial; }
00294             typedef VectorIterator<GeometryBucketList> GeometryIterator;
00296             GeometryIterator getGeometryIterator(void);
00298             Technique* getCurrentTechnique(void) const { return mTechnique; }
00300             void dump(std::ofstream& of) const;
00301             void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables);
00302         };
00308         class _OgreExport LODBucket : public BatchedGeometryAlloc
00309         {
00310         public:
00312             typedef map<String, MaterialBucket*>::type MaterialBucketMap;
00313         protected:
00315             class _OgreExport LODShadowRenderable : public ShadowRenderable
00316             {
00317             protected:
00318                 LODBucket* mParent;
00319                 // Shared link to position buffer
00320                 HardwareVertexBufferSharedPtr mPositionBuffer;
00321                 // Shared link to w-coord buffer (optional)
00322                 HardwareVertexBufferSharedPtr mWBuffer;
00323 
00324             public:
00325                 LODShadowRenderable(LODBucket* parent, 
00326                     HardwareIndexBufferSharedPtr* indexBuffer, const VertexData* vertexData, 
00327                     bool createSeparateLightCap, bool isLightCap = false);
00328                 ~LODShadowRenderable();
00330                 void getWorldTransforms(Matrix4* xform) const;
00331                 HardwareVertexBufferSharedPtr getPositionBuffer(void) { return mPositionBuffer; }
00332                 HardwareVertexBufferSharedPtr getWBuffer(void) { return mWBuffer; }
00334                 virtual void rebindIndexBuffer(const HardwareIndexBufferSharedPtr& indexBuffer);
00335 
00336             };
00338             Region* mParent;
00340             unsigned short mLod;
00342             Real mLodValue;
00344             MaterialBucketMap mMaterialBucketMap;
00346             QueuedGeometryList mQueuedGeometryList;
00348             EdgeData* mEdgeList;
00350             bool mVertexProgramInUse;
00352             ShadowCaster::ShadowRenderableList mShadowRenderables;
00353         public:
00354             LODBucket(Region* parent, unsigned short lod, Real lodValue);
00355             virtual ~LODBucket();
00356             Region* getParent(void) { return mParent; }
00358             ushort getLod(void) const { return mLod; }
00360             Real getLodValue(void) const { return mLodValue; }
00362             void assign(QueuedSubMesh* qsm, ushort atLod);
00364             void build(bool stencilShadows);
00366             void addRenderables(RenderQueue* queue, uint8 group, 
00367                 Real lodValue);
00369             typedef MapIterator<MaterialBucketMap> MaterialIterator;
00371             MaterialIterator getMaterialIterator(void);
00373             void dump(std::ofstream& of) const;
00374             void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables);
00375             EdgeData* getEdgeList() const { return mEdgeList; }
00376             ShadowCaster::ShadowRenderableList& getShadowRenderableList() { return mShadowRenderables; }
00377             bool isVertexProgramInUse() const { return mVertexProgramInUse; }
00378             void updateShadowRenderables(
00379                 ShadowTechnique shadowTechnique, const Vector4& lightPos, 
00380                 HardwareIndexBufferSharedPtr* indexBuffer, 
00381                 bool extrudeVertices, Real extrusionDistance, unsigned long flags = 0 );
00382             
00383         };
00392         class _OgreExport Region : public MovableObject
00393         {
00394             friend class MaterialBucket;
00395             friend class GeometryBucket;
00396         public:
00398             typedef vector<LODBucket*>::type LODBucketList;
00399         protected:
00401             StaticGeometry* mParent;
00403             SceneManager* mSceneMgr;
00405             SceneNode* mNode;
00407             QueuedSubMeshList mQueuedSubMeshes;
00409             uint32 mRegionID;
00411             Vector3 mCentre;
00413             Mesh::LodValueList mLodValues;
00415             AxisAlignedBox mAABB;
00417             Real mBoundingRadius;
00419             ushort mCurrentLod;
00421             Real mLodValue;
00423             LODBucketList mLodBucketList;
00425             mutable LightList mLightList;
00427             mutable ulong mLightListUpdated;
00429             const LodStrategy *mLodStrategy;
00431             Camera *mCamera;
00433             Real mSquaredViewDepth;
00434 
00435         public:
00436             Region(StaticGeometry* parent, const String& name, SceneManager* mgr, 
00437                 uint32 regionID, const Vector3& centre);
00438             virtual ~Region();
00439             // more fields can be added in subclasses
00440             StaticGeometry* getParent(void) const { return mParent;}
00442             void assign(QueuedSubMesh* qmesh);
00444             void build(bool stencilShadows);
00446             uint32 getID(void) const { return mRegionID; }
00448             const Vector3& getCentre(void) const { return mCentre; }
00449             const String& getMovableType(void) const;
00450             void _notifyCurrentCamera(Camera* cam);
00451             const AxisAlignedBox& getBoundingBox(void) const;
00452             Real getBoundingRadius(void) const;
00453             void _updateRenderQueue(RenderQueue* queue);
00455             void visitRenderables(Renderable::Visitor* visitor, 
00456                 bool debugRenderables = false);
00457             bool isVisible(void) const;
00458             uint32 getTypeFlags(void) const;
00459 
00460             typedef VectorIterator<LODBucketList> LODIterator;
00462             LODIterator getLODIterator(void);
00464             ShadowRenderableListIterator getShadowVolumeRenderableIterator(
00465                 ShadowTechnique shadowTechnique, const Light* light, 
00466                 HardwareIndexBufferSharedPtr* indexBuffer, size_t* indexBufferUsedSize,
00467                 bool extrudeVertices, Real extrusionDistance, unsigned long flags = 0 );
00469             EdgeData* getEdgeList(void);
00471             bool hasEdgeList(void);
00472 
00474             void dump(std::ofstream& of) const;
00475             
00476         };
00484         typedef map<uint32, Region*>::type RegionMap;
00485     protected:
00486         // General state & settings
00487         SceneManager* mOwner;
00488         String mName;
00489         bool mBuilt;
00490         Real mUpperDistance;
00491         Real mSquaredUpperDistance;
00492         bool mCastShadows;
00493         Vector3 mRegionDimensions;
00494         Vector3 mHalfRegionDimensions;
00495         Vector3 mOrigin;
00496         bool mVisible;
00498         uint8 mRenderQueueID;
00500         bool mRenderQueueIDSet;
00502         uint32 mVisibilityFlags;
00503 
00504         QueuedSubMeshList mQueuedSubMeshes;
00505 
00508         OptimisedSubMeshGeometryList mOptimisedSubMeshGeometryList;
00509 
00514         SubMeshGeometryLookup mSubMeshGeometryLookup;
00515             
00517         RegionMap mRegionMap;
00518 
00522         virtual Region* getRegion(const AxisAlignedBox& bounds, bool autoCreate);
00524         virtual Region* getRegion(const Vector3& point, bool autoCreate);
00526         virtual Region* getRegion(ushort x, ushort y, ushort z, bool autoCreate);
00528         virtual Region* getRegion(uint32 index);
00531         virtual void getRegionIndexes(const Vector3& point, 
00532             ushort& x, ushort& y, ushort& z);
00535         virtual uint32 packIndex(ushort x, ushort y, ushort z);
00538         virtual Real getVolumeIntersection(const AxisAlignedBox& box,  
00539             ushort x, ushort y, ushort z);
00542         virtual AxisAlignedBox getRegionBounds(ushort x, ushort y, ushort z);
00545         virtual Vector3 getRegionCentre(ushort x, ushort y, ushort z);
00547         virtual AxisAlignedBox calculateBounds(VertexData* vertexData, 
00548             const Vector3& position, const Quaternion& orientation, 
00549             const Vector3& scale);
00551         SubMeshLodGeometryLinkList* determineGeometry(SubMesh* sm);
00553         void splitGeometry(VertexData* vd, IndexData* id, 
00554             SubMeshLodGeometryLink* targetGeomLink);
00555 
00556         typedef map<size_t, size_t>::type IndexRemap;
00561         template <typename T>
00562         void buildIndexRemap(T* pBuffer, size_t numIndexes, IndexRemap& remap)
00563         {
00564             remap.clear();
00565             for (size_t i = 0; i < numIndexes; ++i)
00566             {
00567                 // use insert since duplicates are silently discarded
00568                 remap.insert(IndexRemap::value_type(*pBuffer++, remap.size()));
00569                 // this will have mapped oldindex -> new index IF oldindex
00570                 // wasn't already there
00571             }
00572         }
00574         template <typename T>
00575         void remapIndexes(T* src, T* dst, const IndexRemap& remap, 
00576                 size_t numIndexes)
00577         {
00578             for (size_t i = 0; i < numIndexes; ++i)
00579             {
00580                 // look up original and map to target
00581                 IndexRemap::const_iterator ix = remap.find(*src++);
00582                 assert(ix != remap.end());
00583                 *dst++ = static_cast<T>(ix->second);
00584             }
00585         }
00586         
00587     public:
00589         StaticGeometry(SceneManager* owner, const String& name);
00591         virtual ~StaticGeometry();
00592 
00594         const String& getName(void) const { return mName; }
00613         virtual void addEntity(Entity* ent, const Vector3& position,
00614             const Quaternion& orientation = Quaternion::IDENTITY, 
00615             const Vector3& scale = Vector3::UNIT_SCALE);
00616 
00635         virtual void addSceneNode(const SceneNode* node);
00636 
00647         virtual void build(void);
00648 
00654         virtual void destroy(void);
00655 
00659         virtual void reset(void);
00660 
00670         virtual void setRenderingDistance(Real dist) { 
00671             mUpperDistance = dist; 
00672             mSquaredUpperDistance = mUpperDistance * mUpperDistance;
00673         }
00674 
00676         virtual Real getRenderingDistance(void) const { return mUpperDistance; }
00677 
00679         virtual Real getSquaredRenderingDistance(void) const 
00680         { return mSquaredUpperDistance; }
00681 
00683         virtual void setVisible(bool visible);
00684 
00686         virtual bool isVisible(void) const { return mVisible; }
00687 
00705         virtual void setCastShadows(bool castShadows);
00707         virtual bool getCastShadows(void) { return mCastShadows; }
00708 
00719         virtual void setRegionDimensions(const Vector3& size) { 
00720             mRegionDimensions = size; 
00721             mHalfRegionDimensions = size * 0.5;
00722         }
00724         virtual const Vector3& getRegionDimensions(void) const { return mRegionDimensions; }
00736         virtual void setOrigin(const Vector3& origin) { mOrigin = origin; }
00738         virtual const Vector3& getOrigin(void) const { return mOrigin; }
00739 
00741         void setVisibilityFlags(uint32 flags);
00743         uint32 getVisibilityFlags() const;
00744 
00756         virtual void setRenderQueueGroup(uint8 queueID);
00757 
00759         virtual uint8 getRenderQueueGroup(void) const;
00761         void visitRenderables(Renderable::Visitor* visitor, 
00762             bool debugRenderables = false);
00763         
00765         typedef MapIterator<RegionMap> RegionIterator;
00767         RegionIterator getRegionIterator(void);
00768 
00772         virtual void dump(const String& filename) const;
00773 
00774 
00775     };
00779 }
00780 
00781 #include "OgreHeaderSuffix.h"
00782 
00783 #endif
00784 

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Mon Jul 27 2020 13:40:47