# 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, TerrainSceneManager* tsm);
    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
    00185
    00187         virtual const String& getMovableType( void ) const
    00188         {
    00189             return mType;
    00190         };
    00191
    00193         const AxisAlignedBox& getBoundingBox( void ) const
    00194         {
    00195             return mBounds;
    00196         };
    00197
    00199         virtual void _notifyCurrentCamera( Camera* cam );
    00200
    00201         virtual void _updateRenderQueue( RenderQueue* queue );
    00202
    00209         virtual void getRenderOperation( RenderOperation& rend );
    00210
    00211         virtual const MaterialPtr& getMaterial( void ) const
    00212         {
    00213             return mMaterial;
    00214         };
    00215
    00216         virtual void getWorldTransforms( Matrix4* xform ) const;
    00217
    00218         virtual const Quaternion& getWorldOrientation(void) const;
    00219         virtual const Vector3& getWorldPosition(void) const;
    00220
    00222         inline int getRenderLevel() const
    00223         {
    00224             return mRenderLevel;
    00225         };
    00226
    00228         inline void setForcedRenderLevel( int i )
    00229         {
    00230             mForcedRenderLevel = i;
    00231         }
    00232
    00234         void _getNormalAt( float x, float y, Vector3 * result );
    00235
    00237         float getHeightAt( float x, float y );
    00238
    00241         bool intersectSegment( const Vector3 & start, const Vector3 & end, Vector3 * result );
    00242
    00246         void _setNeighbor( Neighbor n, TerrainRenderable *t )
    00247         {
    00248             mNeighbors[ n ] = t;
    00249         };
    00250
    00253         TerrainRenderable * _getNeighbor( Neighbor n )
    00254         {
    00255             return mNeighbors[ n ];
    00256         }
    00257
    00258
    00259         void setMaterial(const MaterialPtr& m )
    00260         {
    00261             mMaterial = m;
    00262         };
    00263
    00265         void _calculateNormals();
    00266
    00267
    00268
    00269
    00272         void _generateVertexLighting( const Vector3 &sunlight, ColourValue ambient );
    00273
    00274
    00276         Real getSquaredViewDepth(const Camera* cam) const;
    00277
    00279         Real getBoundingRadius(void) const { return mBoundingRadius; }
    00280
    00282         const LightList& getLights(void) const;
    00283
    00285         void _updateCustomGpuParameter(
    00286             const GpuProgramParameters::AutoConstantEntry& constantEntry,
    00287             GpuProgramParameters* params) const;
    00289         uint32 getTypeFlags(void) const;
    00290     protected:
    00292         TerrainSceneManager* mSceneManager;
    00294         const TerrainOptions* mOptions;
    00295
    00297         inline size_t _index( int x, int z ) const
    00298         {
    00299             return ( x + z * mOptions->tileSize );
    00300         };
    00301
    00303         inline float _vertex( int x, int z, int n )
    00304         {
    00305             return mPositionBuffer[x * 3 + z * mOptions->tileSize * 3 + n];
    00306         };
    00307
    00308
    00309         inline int _numNeighbors() const
    00310         {
    00311             int n = 0;
    00312
    00313             for ( int i = 0; i < 4; i++ )
    00314             {
    00315                 if ( mNeighbors[ i ] != 0 )
    00316                     n++;
    00317             }
    00318
    00319             return n;
    00320         }
    00321
    00322         inline bool _hasNeighborRenderLevel( int i ) const
    00323         {
    00324             for ( int j = 0; j < 4; j++ )
    00325             {
    00326                 if ( mNeighbors[ j ] != 0 && mNeighbors[ j ] ->mRenderLevel == i )
    00327                     return true;
    00328             }
    00329
    00330             return false;
    00331
    00332         }
    00333
    00334         void _adjustRenderLevel( int i );
    00335
    00336         bool _checkSize( int n );
    00337
    00338         void _calculateMinLevelDist2( Real C );
    00339
    00340         Real _calculateCFactor();
    00341
    00342         VertexData* mTerrain;
    00343
    00345         int mRenderLevel;
    00347         int mLastNextLevel;
    00349         Real mLODMorphFactor;
    00351         Real *mMinLevelDistSqr;
    00353         TerrainRenderable *mNeighbors [ 4 ];
    00355         mutable bool mLightListDirty;
    00357         mutable LightList mLightList;
    00359         Real mBoundingRadius;
    00361         AxisAlignedBox mBounds;
    00363         Vector3 mCenter;
    00365         static String mType;
    00367         MaterialPtr mMaterial;
    00369         bool mInit;
    00371         HardwareVertexBufferSharedPtr mMainBuffer;
    00373         HardwareVertexBufferSharedPtr* mDeltaBuffers;
    00375         float* mPositionBuffer;
    00377         int mForcedRenderLevel;
    00380         int mNextLevelDown[10];
    00382         IndexData* getIndexData(void);
    00384         IndexData* generateTriStripIndexes(unsigned int stitchFlags);
    00386         IndexData* generateTriListIndexes(unsigned int stitchFlags);
    00401         int stitchEdge(Neighbor neighbor, int hiLOD, int loLOD,
    00402             bool omitFirstTri, bool omitLastTri, unsigned short** ppIdx);
    00403
    00405         HardwareVertexBufferSharedPtr createDeltaBuffer(void);
    00406
    00407     };
    00408
    00409 }
    00410
    00411 #endif

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