# 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
    00109     class _OgreExport StaticGeometry
    00110     {
    00111     public:
    00124         class _OgrePrivate OptimisedSubMeshGeometry
    00125         {
    00126         public:
    00127             OptimisedSubMeshGeometry() :vertexData(0), indexData(0) {}
    00128             ~OptimisedSubMeshGeometry()
    00129             {
    00130                 delete vertexData;
    00131                 delete indexData;
    00132             }
    00133             VertexData *vertexData;
    00134             IndexData *indexData;
    00135         };
    00136         typedef std::list<OptimisedSubMeshGeometry*> OptimisedSubMeshGeometryList;
    00139         struct SubMeshLodGeometryLink
    00140         {
    00141             VertexData* vertexData;
    00142             IndexData* indexData;
    00143         };
    00144         typedef std::vector<SubMeshLodGeometryLink> SubMeshLodGeometryLinkList;
    00145         typedef std::map<SubMesh*, SubMeshLodGeometryLinkList*> SubMeshGeometryLookup;
    00147         struct QueuedSubMesh
    00148         {
    00149             SubMesh* submesh;
    00151             SubMeshLodGeometryLinkList* geometryLodList;
    00152             String materialName;
    00153             Vector3 position;
    00154             Quaternion orientation;
    00155             Vector3 scale;
    00157             AxisAlignedBox worldBounds;
    00158         };
    00159         typedef std::vector<QueuedSubMesh*> QueuedSubMeshList;
    00161         struct QueuedGeometry
    00162         {
    00163             SubMeshLodGeometryLink* geometry;
    00164             Vector3 position;
    00165             Quaternion orientation;
    00166             Vector3 scale;
    00167         };
    00168         typedef std::vector<QueuedGeometry*> QueuedGeometryList;
    00169
    00170         // forward declarations
    00171         class LODBucket;
    00172         class MaterialBucket;
    00173         class Region;
    00174
    00179         class _OgreExport GeometryBucket :  public Renderable
    00180         {
    00181         protected:
    00183             QueuedGeometryList mQueuedGeometry;
    00185             MaterialBucket* mParent;
    00187             String mFormatString;
    00190             VertexData* mVertexData;
    00193             IndexData* mIndexData;
    00195             HardwareIndexBuffer::IndexType mIndexType;
    00197             size_t mMaxVertexIndex;
    00198
    00199             template<typename T>
    00200             void copyIndexes(const T* src, T* dst, size_t count, size_t indexOffset)
    00201             {
    00202                 if (indexOffset == 0)
    00203                 {
    00204                     memcpy(dst, src, sizeof(T) * count);
    00205                 }
    00206                 else
    00207                 {
    00208                     while(count--)
    00209                     {
    00210                         *dst++ = static_cast<T>(*src++ + indexOffset);
    00211                     }
    00212                 }
    00213             }
    00214         public:
    00215             GeometryBucket(MaterialBucket* parent, const String& formatString,
    00216                 const VertexData* vData, const IndexData* iData);
    00217             virtual ~GeometryBucket();
    00218             MaterialBucket* getParent(void) { return mParent; }
    00220             const VertexData* getVertexData(void) const { return mVertexData; }
    00222             const IndexData* getIndexData(void) const { return mIndexData; }
    00224             const MaterialPtr& getMaterial(void) const;
    00225             Technique* getTechnique(void) const;
    00226             void getRenderOperation(RenderOperation& op);
    00227             void getWorldTransforms(Matrix4* xform) const;
    00228             const Quaternion& getWorldOrientation(void) const;
    00229             const Vector3& getWorldPosition(void) const;
    00230             Real getSquaredViewDepth(const Camera* cam) const;
    00231             const LightList& getLights(void) const;
    00232             bool getCastsShadows(void) const;
    00233
    00237             bool assign(QueuedGeometry* qsm);
    00239             void build(bool stencilShadows);
    00241             void dump(std::ofstream& of) const;
    00242         };
    00245         class _OgreExport MaterialBucket
    00246         {
    00247         public:
    00249             typedef std::vector<GeometryBucket*> GeometryBucketList;
    00250         protected:
    00252             LODBucket* mParent;
    00254             String mMaterialName;
    00256             MaterialPtr mMaterial;
    00258             Technique* mTechnique;
    00259
    00261             GeometryBucketList mGeometryBucketList;
    00262             // index to current Geometry Buckets for a given geometry format
    00263             typedef std::map<String, GeometryBucket*> CurrentGeometryMap;
    00264             CurrentGeometryMap mCurrentGeometryMap;
    00266             String getGeometryFormatString(SubMeshLodGeometryLink* geom);
    00267
    00268         public:
    00269             MaterialBucket(LODBucket* parent, const String& materialName);
    00270             virtual ~MaterialBucket();
    00271             LODBucket* getParent(void) { return mParent; }
    00273             const String& getMaterialName(void) const { return mMaterialName; }
    00275             void assign(QueuedGeometry* qsm);
    00277             void build(bool stencilShadows);
    00279             void addRenderables(RenderQueue* queue, uint8 group,
    00280                 Real camSquaredDist);
    00282             const MaterialPtr& getMaterial(void) const { return mMaterial; }
    00284             typedef VectorIterator<GeometryBucketList> GeometryIterator;
    00286             GeometryIterator getGeometryIterator(void);
    00288             Technique* getCurrentTechnique(void) const { return mTechnique; }
    00290             void dump(std::ofstream& of) const;
    00291         };
    00297         class _OgreExport LODBucket
    00298         {
    00299         public:
    00301             typedef std::map<String, MaterialBucket*> MaterialBucketMap;
    00302         protected:
    00304             Region* mParent;
    00306             unsigned short mLod;
    00308             Real mSquaredDistance;
    00310             MaterialBucketMap mMaterialBucketMap;
    00312             QueuedGeometryList mQueuedGeometryList;
    00313         public:
    00314             LODBucket(Region* parent, unsigned short lod, Real lodDist);
    00315             virtual ~LODBucket();
    00316             Region* getParent(void) { return mParent; }
    00318             ushort getLod(void) const { return mLod; }
    00320             Real getSquaredDistance(void) const { return mSquaredDistance; }
    00322             void assign(QueuedSubMesh* qsm, ushort atLod);
    00324             void build(bool stencilShadows);
    00326             void addRenderables(RenderQueue* queue, uint8 group,
    00327                 Real camSquaredDistance);
    00329             typedef MapIterator<MaterialBucketMap> MaterialIterator;
    00331             MaterialIterator getMaterialIterator(void);
    00333             void dump(std::ofstream& of) const;
    00334
    00335         };
    00344         class _OgreExport Region : public MovableObject
    00345         {
    00346         public:
    00348             typedef std::vector<LODBucket*> LODBucketList;
    00349         protected:
    00351             class _OgreExport RegionShadowRenderable : public ShadowRenderable
    00352             {
    00353             protected:
    00354                 Region* mParent;
    00355                 // Shared link to position buffer
    00356                 HardwareVertexBufferSharedPtr mPositionBuffer;
    00357                 // Shared link to w-coord buffer (optional)
    00358                 HardwareVertexBufferSharedPtr mWBuffer;
    00359
    00360             public:
    00361                 RegionShadowRenderable(Region* parent,
    00362                     HardwareIndexBufferSharedPtr* indexBuffer, const VertexData* vertexData,
    00363                     bool createSeparateLightCap, bool isLightCap = false);
    00364                 ~RegionShadowRenderable();
    00366                 void getWorldTransforms(Matrix4* xform) const;
    00368                 const Quaternion& getWorldOrientation(void) const;
    00370                 const Vector3& getWorldPosition(void) const;
    00371                 HardwareVertexBufferSharedPtr getPositionBuffer(void) { return mPositionBuffer; }
    00372                 HardwareVertexBufferSharedPtr getWBuffer(void) { return mWBuffer; }
    00373
    00374             };
    00376             StaticGeometry* mParent;
    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             EdgeData* mEdgeList;
    00406             ShadowRenderableList mShadowRenderables;
    00408             bool mVertexProgramInUse;
    00409
    00410
    00411
    00412         public:
    00413             Region(StaticGeometry* parent, const String& name, SceneManager* mgr,
    00414                 uint32 regionID, const Vector3& centre);
    00415             virtual ~Region();
    00416             // more fields can be added in subclasses
    00417             StaticGeometry* getParent(void) const { return mParent;}
    00419             void assign(QueuedSubMesh* qmesh);
    00421             void build(bool stencilShadows);
    00423             uint32 getID(void) const { return mRegionID; }
    00425             const Vector3& getCentre(void) const { return mCentre; }
    00426             const String& getMovableType(void) const;
    00427             void _notifyCurrentCamera(Camera* cam);
    00428             const AxisAlignedBox& getBoundingBox(void) const;
    00429             Real getBoundingRadius(void) const;
    00430             void _updateRenderQueue(RenderQueue* queue);
    00431             bool isVisible(void) const;
    00432             uint32 getTypeFlags(void) const;
    00433
    00434             typedef VectorIterator<LODBucketList> LODIterator;
    00436             LODIterator getLODIterator(void);
    00438             const LightList& getLights(void) const;
    00440             ShadowRenderableListIterator getShadowVolumeRenderableIterator(
    00441                 ShadowTechnique shadowTechnique, const Light* light,
    00442                 HardwareIndexBufferSharedPtr* indexBuffer,
    00443                 bool extrudeVertices, Real extrusionDistance, unsigned long flags = 0 );
    00445             EdgeData* getEdgeList(void);
    00446
    00447
    00449             void dump(std::ofstream& of) const;
    00450
    00451         };
    00459         typedef std::map<uint32, Region*> RegionMap;
    00460     protected:
    00461         // General state & settings
    00462         SceneManager* mOwner;
    00463         String mName;
    00464         bool mBuilt;
    00465         Real mUpperDistance;
    00466         Real mSquaredUpperDistance;
    00467         bool mCastShadows;
    00468         Vector3 mRegionDimensions;
    00469         Vector3 mHalfRegionDimensions;
    00470         Vector3 mOrigin;
    00471         bool mVisible;
    00473         uint8 mRenderQueueID;
    00475         bool mRenderQueueIDSet;
    00476
    00477         QueuedSubMeshList mQueuedSubMeshes;
    00478
    00481         OptimisedSubMeshGeometryList mOptimisedSubMeshGeometryList;
    00482
    00487         SubMeshGeometryLookup mSubMeshGeometryLookup;
    00488
    00490         RegionMap mRegionMap;
    00491
    00495         virtual Region* getRegion(const AxisAlignedBox& bounds, bool autoCreate);
    00497         virtual Region* getRegion(const Vector3& point, bool autoCreate);
    00499         virtual Region* getRegion(ushort x, ushort y, ushort z, bool autoCreate);
    00501         virtual Region* getRegion(uint32 index);
    00504         virtual void getRegionIndexes(const Vector3& point,
    00505             ushort& x, ushort& y, ushort& z);
    00508         virtual uint32 packIndex(ushort x, ushort y, ushort z);
    00511         virtual Real getVolumeIntersection(const AxisAlignedBox& box,
    00512             ushort x, ushort y, ushort z);
    00515         virtual AxisAlignedBox getRegionBounds(ushort x, ushort y, ushort z);
    00518         virtual Vector3 getRegionCentre(ushort x, ushort y, ushort z);
    00520         virtual AxisAlignedBox calculateBounds(VertexData* vertexData,
    00521             const Vector3& position, const Quaternion& orientation,
    00522             const Vector3& scale);
    00524         SubMeshLodGeometryLinkList* determineGeometry(SubMesh* sm);
    00526         void splitGeometry(VertexData* vd, IndexData* id,
    00527             SubMeshLodGeometryLink* targetGeomLink);
    00528
    00529         typedef std::map<size_t, size_t> IndexRemap;
    00534         template <typename T>
    00535         void buildIndexRemap(T* pBuffer, size_t numIndexes, IndexRemap& remap)
    00536         {
    00537             remap.clear();
    00538             for (size_t i = 0; i < numIndexes; ++i)
    00539             {
    00540                 // use insert since duplicates are silently discarded
    00541                 remap.insert(IndexRemap::value_type(*pBuffer++, remap.size()));
    00542                 // this will have mapped oldindex -> new index IF oldindex
    00543                 // wasn't already there
    00544             }
    00545         }
    00547         template <typename T>
    00548         void remapIndexes(T* src, T* dst, const IndexRemap& remap,
    00549                 size_t numIndexes)
    00550         {
    00551             for (size_t i = 0; i < numIndexes; ++i)
    00552             {
    00553                 // look up original and map to target
    00554                 IndexRemap::const_iterator ix = remap.find(*src++);
    00555                 assert(ix != remap.end());
    00556                 *dst++ = static_cast<T>(ix->second);
    00557             }
    00558         }
    00559
    00560     public:
    00562         StaticGeometry(SceneManager* owner, const String& name);
    00564         virtual ~StaticGeometry();
    00565
    00567         const String& getName(void) const { return mName; }
    00586         virtual void addEntity(Entity* ent, const Vector3& position,
    00587             const Quaternion& orientation = Quaternion::IDENTITY,
    00588             const Vector3& scale = Vector3::UNIT_SCALE);
    00589
    00608         virtual void addSceneNode(const SceneNode* node);
    00609
    00620         virtual void build(void);
    00621
    00627         virtual void destroy(void);
    00628
    00632         virtual void reset(void);
    00633
    00643         virtual void setRenderingDistance(Real dist) {
    00644             mUpperDistance = dist;
    00645             mSquaredUpperDistance = mUpperDistance * mUpperDistance;
    00646         }
    00647
    00649         virtual Real getRenderingDistance(void) const { return mUpperDistance; }
    00650
    00652         virtual Real getSquaredRenderingDistance(void) const
    00653         { return mSquaredUpperDistance; }
    00654
    00656         virtual void setVisible(bool visible);
    00657
    00659         virtual bool isVisible(void) const { return mVisible; }
    00660
    00678         virtual void setCastShadows(bool castShadows);
    00680         virtual bool getCastShadows(void) { return mCastShadows; }
    00681
    00692         virtual void setRegionDimensions(const Vector3& size) {
    00693             mRegionDimensions = size;
    00694             mHalfRegionDimensions = size * 0.5;
    00695         }
    00697         virtual const Vector3& getRegionDimensions(void) const { return mRegionDimensions; }
    00709         virtual void setOrigin(const Vector3& origin) { mOrigin = origin; }
    00711         virtual const Vector3& getOrigin(void) const { return mOrigin; }
    00712
    00724         virtual void setRenderQueueGroup(uint8 queueID);
    00725
    00727         virtual uint8 getRenderQueueGroup(void) const;
    00728
    00730         typedef MapIterator<RegionMap> RegionIterator;
    00732         RegionIterator getRegionIterator(void);
    00733
    00737         virtual void dump(const String& filename) const;
    00738
    00739
    00740     };
    00741
    00742 }
    00743
    00744 #endif
    00745

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