Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

OgreTerrainRenderable.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 /***************************************************************************
00026 terrainrenderable.h  -  description
00027 -------------------
00028 begin                : Sat Oct 5 2002
00029 copyright            : (C) 2002 by Jon Anderson
00030 email                : janders@users.sf.net
00031 
00032 Enhancements 2003 - 2004 (C) The OGRE Team
00033 
00034 ***************************************************************************/
00035 
00036 #ifndef TERRAINRENDERABLE_H
00037 #define TERRAINRENDERABLE_H
00038 
00039 #include "OgreTerrainPrerequisites.h"
00040 #include <OgreRenderable.h>
00041 #include <OgreMovableObject.h>
00042 #include <OgreAxisAlignedBox.h>
00043 #include <OgreString.h>
00044 #include <OgreHardwareBufferManager.h>
00045 
00046 #include <vector>
00047 
00048 #define MORPH_CUSTOM_PARAM_ID 77
00049 
00050 namespace Ogre
00051 {
00052 
00053     typedef std::map <unsigned int, IndexData* > IndexMap;
00054     typedef std::vector < IndexData* > IndexArray;
00055     typedef std::vector < IndexMap* > LevelArray;
00056 
00061     class TerrainBufferCache
00062     {
00063     public:
00064         void shutdown(void)
00065         {
00066             for( size_t i=0; i<mCache.size(); i++ )
00067             {
00068                 delete mCache[i];
00069             }
00070             mCache.clear();
00071         }
00072         ~TerrainBufferCache()
00073         {
00074             shutdown();
00075         }
00076 
00077         IndexArray mCache;
00078     };
00079 
00080     inline Real _max( Real x, Real y )
00081     {
00082         return ( x > y ) ? x : y;
00083     }
00084 
00088     class TerrainOptions
00089     {
00090     public:
00091         TerrainOptions()
00092         {
00093             pageSize = 0;
00094             tileSize = 0;
00095             tilesPerPage = 0;
00096             maxGeoMipMapLevel = 0;
00097             scale = Vector3::UNIT_SCALE;
00098             maxPixelError = 4;
00099             detailTile = 1;
00100             lit = false;
00101             coloured = false;
00102             lodMorph = false;
00103             lodMorphStart = 0.5;
00104             useTriStrips = false;
00105             primaryCamera = 0;
00106             terrainMaterial.setNull();
00107         };
00109         size_t pageSize;
00111         size_t tileSize; 
00113         size_t tilesPerPage;
00115         const Camera* primaryCamera;
00117         size_t maxGeoMipMapLevel;
00120         Vector3 scale;
00122         size_t maxPixelError;
00124         bool useTriStrips;
00126         size_t detailTile;
00128         bool lodMorph;
00130         Real lodMorphStart;
00132         bool lit;
00134         bool coloured;
00136         MaterialPtr terrainMaterial;
00137 
00138     };
00139 
00140 #define STITCH_NORTH_SHIFT 0
00141 #define STITCH_SOUTH_SHIFT 8
00142 #define STITCH_WEST_SHIFT  16
00143 #define STITCH_EAST_SHIFT  24
00144 
00145 #define STITCH_NORTH  128 << STITCH_NORTH_SHIFT
00146 #define STITCH_SOUTH  128 << STITCH_SOUTH_SHIFT
00147 #define STITCH_WEST   128 << STITCH_WEST_SHIFT
00148 #define STITCH_EAST   128 << STITCH_EAST_SHIFT
00149 
00158     class _OgreTerrainExport TerrainRenderable : public Renderable, public MovableObject
00159     {
00160     public:
00161 
00162         TerrainRenderable(const String& name);
00163         ~TerrainRenderable();
00164 
00165         void deleteGeometry();
00166 
00167         enum Neighbor
00168         {
00169             NORTH = 0,
00170             SOUTH = 1,
00171             EAST = 2,
00172             WEST = 3,
00173             HERE = 4
00174         };
00175 
00182         void initialise(int startx, int startz, Real* pageHeightData);
00183 
00184         //movable object methods
00186         virtual const String& getName( void ) const
00187         {
00188             return mName;
00189         };
00190 
00192         virtual const String& getMovableType( void ) const
00193         {
00194             return mType;
00195         };
00196 
00198         const AxisAlignedBox& getBoundingBox( void ) const
00199         {
00200             return mBounds;
00201         };
00202 
00204         virtual void _notifyCurrentCamera( Camera* cam );
00205 
00206         virtual void _updateRenderQueue( RenderQueue* queue );
00207 
00214         virtual void getRenderOperation( RenderOperation& rend );
00215 
00216         virtual const MaterialPtr& getMaterial( void ) const
00217         {
00218             return mMaterial;
00219         };
00220 
00221         virtual void getWorldTransforms( Matrix4* xform ) const;
00222 
00223         virtual const Quaternion& getWorldOrientation(void) const;
00224         virtual const Vector3& getWorldPosition(void) const;
00225 
00227         inline int getRenderLevel() const
00228         {
00229             return mRenderLevel;
00230         };
00231 
00233         inline void setForcedRenderLevel( int i )
00234         {
00235             mForcedRenderLevel = i;
00236         }
00237 
00239         void _getNormalAt( float x, float y, Vector3 * result );
00240 
00242         float getHeightAt( float x, float y );
00243 
00246         bool intersectSegment( const Vector3 & start, const Vector3 & end, Vector3 * result );
00247 
00251         void _setNeighbor( Neighbor n, TerrainRenderable *t )
00252         {
00253             mNeighbors[ n ] = t;
00254         };
00255 
00258         TerrainRenderable * _getNeighbor( Neighbor n )
00259         {
00260             return mNeighbors[ n ];
00261         }
00262 
00263 
00264         void setMaterial(const MaterialPtr& m )
00265         {
00266             mMaterial = m;
00267         };
00268 
00270         void _calculateNormals();
00271 
00272 
00273 
00274 
00277         void _generateVertexLighting( const Vector3 &sun, ColourValue ambient );
00278 
00279 
00281         Real getSquaredViewDepth(const Camera* cam) const;
00282 
00284         Real getBoundingRadius(void) const { return mBoundingRadius; }
00285 
00287         const LightList& getLights(void) const;
00288 
00290         void _updateCustomGpuParameter(
00291             const GpuProgramParameters::AutoConstantEntry& constantEntry,
00292             GpuProgramParameters* params) const;
00294         static TerrainBufferCache& _getIndexCache(void) {return msIndexCache;}
00295     protected:
00297         static TerrainBufferCache msIndexCache;
00299         static const TerrainOptions* msOptions;
00300 
00302         inline size_t _index( int x, int z ) const
00303         {
00304             return ( x + z * msOptions->tileSize );
00305         };
00306 
00308         inline float _vertex( int x, int z, int n )
00309         {
00310             return mPositionBuffer[x * 3 + z * msOptions->tileSize * 3 + n];
00311         };
00312 
00313 
00314         inline int _numNeighbors() const
00315         {
00316             int n = 0;
00317 
00318             for ( int i = 0; i < 4; i++ )
00319             {
00320                 if ( mNeighbors[ i ] != 0 )
00321                     n++;
00322             }
00323 
00324             return n;
00325         }
00326 
00327         inline bool _hasNeighborRenderLevel( int i ) const
00328         {
00329             for ( int j = 0; j < 4; j++ )
00330             {
00331                 if ( mNeighbors[ j ] != 0 && mNeighbors[ j ] ->mRenderLevel == i )
00332                     return true;
00333             }
00334 
00335             return false;
00336 
00337         }
00338 
00339         void _adjustRenderLevel( int i );
00340 
00341         void _initLevelIndexes();
00342         void _destroyLevelIndexes();
00343 
00344         bool _checkSize( int n );
00345 
00346         void _calculateMinLevelDist2( Real C );
00347 
00348         Real _calculateCFactor();
00349 
00350         VertexData* mTerrain;
00351 
00353         int mRenderLevel;
00355         int mLastNextLevel; 
00357         Real mLODMorphFactor;
00359         Real *mMinLevelDistSqr;
00361         TerrainRenderable *mNeighbors [ 4 ];
00363         mutable bool mLightListDirty;
00365         mutable LightList mLightList;
00367         Real mBoundingRadius;
00369         AxisAlignedBox mBounds;
00371         Vector3 mCenter;
00373         String mName;
00375         static String mType;
00377         MaterialPtr mMaterial;    
00379         bool mInit;
00381         static LevelArray mLevelIndex;
00383         static bool mLevelInit;
00385         HardwareVertexBufferSharedPtr mMainBuffer;
00387         HardwareVertexBufferSharedPtr* mDeltaBuffers;
00389         float* mPositionBuffer;
00391         int mForcedRenderLevel;
00394         int mNextLevelDown[10];
00396         IndexData* getIndexData(void);
00398         IndexData* generateTriStripIndexes(unsigned int stitchFlags);
00400         IndexData* generateTriListIndexes(unsigned int stitchFlags);
00415         int stitchEdge(Neighbor neighbor, int hiLOD, int loLOD, 
00416             bool omitFirstTri, bool omitLastTri, unsigned short** ppIdx);
00417 
00419         HardwareVertexBufferSharedPtr createDeltaBuffer(void);
00420 
00421     };
00422 
00423 }
00424 
00425 #endif

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