OgreTerrainQuadTreeNode.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-2011 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 
00029 #ifndef __Ogre_TerrainQuadTreeNode_H__
00030 #define __Ogre_TerrainQuadTreeNode_H__
00031 
00032 #include "OgreTerrainPrerequisites.h"
00033 #include "OgreCommon.h"
00034 #include "OgreHardwareIndexBuffer.h"
00035 #include "OgreMovableObject.h"
00036 #include "OgreRenderable.h"
00037 
00038 
00039 
00040 namespace Ogre
00041 {
00042     class HardwareVertexBufferSharedPtr;
00043     
00085     class _OgreTerrainExport TerrainQuadTreeNode : public TerrainAlloc
00086     {
00087     public:
00097         TerrainQuadTreeNode(Terrain* terrain, TerrainQuadTreeNode* parent, 
00098             uint16 xoff, uint16 yoff, uint16 size, uint16 lod, uint16 depth, uint16 quadrant);
00099         virtual ~TerrainQuadTreeNode();
00100 
00102         uint16 getXOffset() const { return mOffsetX; }
00104         uint16 getYOffset() const { return mOffsetY; }
00106         bool isLeaf() const;
00108         uint16 getBaseLod() const { return mBaseLod; }
00110         uint16 getLodCount() const;
00112         TerrainQuadTreeNode* getChild(unsigned short child) const;
00114         TerrainQuadTreeNode* getParent() const;
00116         Terrain* getTerrain() const;
00117 
00119         void prepare();
00121         void prepare(StreamSerialiser& stream);
00123         void load();
00125         void unload();
00127         void unprepare();
00129         void save(StreamSerialiser& stream);
00130 
00131         struct _OgreTerrainExport LodLevel : public TerrainAlloc
00132         {
00134             uint16 batchSize;
00136             IndexData* gpuIndexData;
00138             Real maxHeightDelta;
00140             Real calcMaxHeightDelta;
00142             Real lastTransitionDist;
00144             Real lastCFactor;
00145 
00146             LodLevel() : gpuIndexData(0), maxHeightDelta(0), calcMaxHeightDelta(0),
00147                 lastTransitionDist(0), lastCFactor(0) {}
00148         };
00149         typedef vector<LodLevel*>::type LodLevelList;
00150 
00156         const LodLevel* getLodLevel(uint16 lod);
00157 
00162         void preDeltaCalculation(const Rect& rect);
00163 
00165         void notifyDelta(uint16 x, uint16 y, uint16 lod, Real delta);
00166 
00169         void postDeltaCalculation(const Rect& rect);
00170 
00174         void finaliseDeltaValues(const Rect& rect);
00175 
00182         void assignVertexData(uint16 treeDepthStart, uint16 treeDepthEnd, uint16 resolution, uint sz);
00183 
00188         void useAncestorVertexData(TerrainQuadTreeNode* owner, uint16 treeDepthEnd, uint16 resolution);
00189 
00192         void updateVertexData(bool positions, bool deltas, const Rect& rect, bool cpuData);
00193 
00194 
00195 
00202         void mergeIntoBounds(long x, long y, const Vector3& pos);
00206         void resetBounds(const Rect& rect);
00207         
00212         bool rectIntersectsNode(const Rect& rect);
00217         bool rectContainsNode(const Rect& rect);
00222         bool pointIntersectsNode(long x, long y);
00223 
00225         const AxisAlignedBox& getAABB() const;
00227         Real getBoundingRadius() const;
00229         const Vector3& getLocalCentre() const { return mLocalCentre; }
00231         Real getMinHeight() const;
00233         Real getMaxHeight() const;
00234 
00240         bool calculateCurrentLod(const Camera* cam, Real cFactor);
00241 
00243         int getCurrentLod() const { return mCurrentLod; }
00245         bool isRenderedAtCurrentLod() const;
00247         bool isSelfOrChildRenderedAtCurrentLod() const;
00249         void setCurrentLod(int lod);
00251         float getLodTransition() const { return mLodTransition; }
00253         void setLodTransition(float t);
00254 
00256         static unsigned short POSITION_BUFFER;
00258         static unsigned short DELTA_BUFFER;
00259 
00260 
00261     protected:
00262         Terrain* mTerrain;
00263         TerrainQuadTreeNode* mParent;
00264         TerrainQuadTreeNode* mChildren[4];
00265         LodLevelList mLodLevels;
00266 
00267         uint16 mOffsetX, mOffsetY;
00268         uint16 mBoundaryX, mBoundaryY;
00270         uint16 mSize;
00271         uint16 mBaseLod;
00272         uint16 mDepth;
00273         uint16 mQuadrant;
00274         Vector3 mLocalCentre; // relative to terrain centre
00275         AxisAlignedBox mAABB; //relative to mLocalCentre
00276         Real mBoundingRadius; //relative to mLocalCentre
00277         int mCurrentLod; // -1 = none (do not render)
00278         unsigned short mMaterialLodIndex;
00279         float mLodTransition; // 0-1 transition to lower LOD
00281         TerrainQuadTreeNode* mChildWithMaxHeightDelta;
00282         bool mSelfOrChildRendered;
00283 
00284         struct VertexDataRecord : public TerrainAlloc
00285         {
00286             VertexData* cpuVertexData;
00287             VertexData* gpuVertexData;
00289             uint16 resolution;
00291             uint16 size;
00293             uint16 treeLevels;
00295             uint16 numSkirtRowsCols;
00297             uint16 skirtRowColSkip;
00299             bool gpuVertexDataDirty;
00300 
00301             VertexDataRecord(uint16 res, uint16 sz, uint16 lvls) 
00302                 : cpuVertexData(0), gpuVertexData(0), resolution(res), size(sz),
00303                 treeLevels(lvls), gpuVertexDataDirty(false) {}
00304         };
00305         
00306         TerrainQuadTreeNode* mNodeWithVertexData;
00307         VertexDataRecord* mVertexDataRecord;
00308 
00328         class _OgreTerrainExport Movable : public MovableObject
00329         {
00330         protected:
00331             TerrainQuadTreeNode* mParent;
00332         public:
00333             Movable(TerrainQuadTreeNode* parent);
00334             ~Movable();
00335             
00336             // necessary overrides
00337             const String& getMovableType(void) const;
00338             const AxisAlignedBox& getBoundingBox(void) const;
00339             Real getBoundingRadius(void) const;
00340             void _updateRenderQueue(RenderQueue* queue);
00341             void visitRenderables(Renderable::Visitor* visitor,  bool debugRenderables = false);
00342             bool isVisible(void) const;
00343             uint32 getVisibilityFlags(void) const;
00344             uint32 getQueryFlags(void) const;
00345             bool getCastShadows(void) const;
00346 
00347         };
00348         Movable* mMovable;
00349         friend class Movable;
00350         SceneNode* mLocalNode;
00351 
00353         class _OgreTerrainExport Rend : public Renderable, public TerrainAlloc
00354         {
00355         protected:
00356             TerrainQuadTreeNode* mParent;
00357         public:
00358             Rend(TerrainQuadTreeNode* parent);
00359             ~Rend();
00360 
00361             const MaterialPtr& getMaterial(void) const;
00362             Technique* getTechnique(void) const;
00363             void getRenderOperation(RenderOperation& op);
00364             void getWorldTransforms(Matrix4* xform) const;
00365             Real getSquaredViewDepth(const Camera* cam) const;
00366             const LightList& getLights(void) const;
00367             bool getCastsShadows(void) const;
00368 
00369         };
00370         Rend* mRend;
00371         friend class Rend;
00372 
00373         // actual implementation of MovableObject methods
00374         void updateRenderQueue(RenderQueue* queue);
00375         void visitRenderables(Renderable::Visitor* visitor,  bool debugRenderables = false);
00376         // actual implementations of Renderable methods
00377         const MaterialPtr& getMaterial(void) const;
00378         Technique* getTechnique(void) const;
00379         void getRenderOperation(RenderOperation& op);
00380         void getWorldTransforms(Matrix4* xform) const;
00381         Real getSquaredViewDepth(const Camera* cam) const;
00382         const LightList& getLights(void) const;
00383         bool getCastsShadows(void) const;
00384 
00385 
00386         const VertexDataRecord* getVertexDataRecord() const;
00387         void createCpuVertexData();
00388         /* Update the vertex buffers - the rect in question is relative to the whole terrain, 
00389             not the local vertex data (which may use a subset)
00390         */
00391         void updateVertexBuffer(HardwareVertexBufferSharedPtr& posbuf, HardwareVertexBufferSharedPtr& deltabuf, const Rect& rect);
00392         void destroyCpuVertexData();
00393 
00394         void createGpuVertexData();
00395         void destroyGpuVertexData();
00396         void updateGpuVertexData();
00397         void createGpuIndexData();
00398         void destroyGpuIndexData();
00399 
00400         void populateIndexData(uint16 batchSize, IndexData* destData);
00401         
00402         uint16 calcSkirtVertexIndex(uint16 mainIndex, bool isCol);
00403 
00404     };
00405 
00408 }
00409 
00410 #endif

Copyright © 2008 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sat Jan 14 2012 18:40:44