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

OgreTerrainRenderable.h

Go to the documentation of this file.
00001 /***************************************************************************
00002                         terrainrenderable.h  -  description
00003                            -------------------
00004   begin                : Sat Oct 5 2002
00005   copyright            : (C) 2002 by Jon Anderson
00006   email                : janders@users.sf.net
00007 ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU Lesser General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #ifndef TERRAINRENDERABLE_H
00019 #define TERRAINRENDERABLE_H
00020 
00021 #include <OgreRenderable.h>
00022 #include <OgreMovableObject.h>
00023 #include <OgreAxisAlignedBox.h>
00024 #include <OgreString.h>
00025 #include <OgreHardwareBufferManager.h>
00026 
00027 #include <vector>
00028 
00029 namespace Ogre
00030 {
00031 
00032 typedef std::vector < IndexData* > IndexArray;
00033 typedef std::vector < IndexArray > LevelArray;
00034 
00039 class TerrainBufferCache
00040 {
00041 public:
00042     ~TerrainBufferCache()
00043     {
00044         for( size_t i=0; i<mCache.size(); i++ )
00045         {
00046             delete mCache[i];
00047         }
00048     }
00049 
00050     IndexArray mCache;
00051 };
00052 
00053 inline Real _max( Real x, Real y )
00054 {
00055     return ( x > y ) ? x : y;
00056 }
00057 
00062 class TerrainOptions
00063 {
00064 public:
00065     TerrainOptions()
00066     {
00067         data = 0;
00068         size = 0;
00069         world_size = 0;
00070         startx = 0;
00071         startz = 0;
00072         max_mipmap = 0;
00073         scalex = 1;
00074         scaley = 1;
00075         scalez = 1;
00076         max_pixel_error = 4;
00077         vert_res = 768;
00078         top_coord = 1;
00079         near_plane = 1;
00080         detail_tile = 1;
00081         lit = false;
00082         colored = false;
00083     };
00084 
00085 
00086     int _worldheight( int x, int z ) const
00087     {
00088         return data[ ( ( z * world_size ) + x ) ];
00089     };
00090 
00091     const uchar * data;     //pointer to the world 2D data.
00092     int size;         //size of this square block
00093     int world_size;   //size of the world.
00094     int startx;
00095     int startz; //starting coords of this block.
00096     int max_mipmap;  //max mip_map level
00097     float scalex, scaley, scalez;
00098 
00099     int max_pixel_error;
00100     int near_plane;
00101     int vert_res;
00102     int detail_tile;
00103     float top_coord;
00104 
00105     bool lit;
00106     bool colored;
00107 
00108 };
00109 
00110 #define TILE_NORTH  0x00000001
00111 #define TILE_SOUTH  0x00000002
00112 #define TILE_WEST   0x00000004
00113 #define TILE_EAST   0x00000008
00114 
00123 class TerrainRenderable : public Renderable, public MovableObject
00124 {
00125 public:
00126 
00127     TerrainRenderable();
00128     ~TerrainRenderable();
00129 
00130     void deleteGeometry();
00131 
00132     enum Neighbor
00133     {
00134         NORTH = 0,
00135         SOUTH = 1,
00136         EAST = 2,
00137         WEST = 3,
00138         HERE = 4
00139     };
00140 
00144     void init( TerrainOptions &options );
00145 
00146     //movable object methods
00148     virtual const String& getName( void ) const
00149     {
00150         return mName;
00151     };
00152 
00154     virtual const String& getMovableType( void ) const
00155     {
00156         return mType;
00157     };
00158 
00160     const AxisAlignedBox& getBoundingBox( void ) const
00161     {
00162         return mBounds;
00163     };
00164 
00166     virtual void _notifyCurrentCamera( Camera* cam );
00167 
00168     virtual void _updateRenderQueue( RenderQueue* queue );
00169 
00176     virtual void getRenderOperation( RenderOperation& rend );
00177 
00178     virtual Material* getMaterial( void ) const
00179     {
00180         return mMaterial;
00181     };
00182 
00183     virtual void getWorldTransforms( Matrix4* xform ) const;
00184 
00185     virtual const Quaternion& getWorldOrientation(void) const;
00186     virtual const Vector3& getWorldPosition(void) const;
00187 
00189     inline int getSize() const
00190     {
00191         return mSize;
00192     };
00193 
00195     inline int getRenderLevel() const
00196     {
00197         return mRenderLevel;
00198     };
00199 
00201     inline void setForcedRenderLevel( int i )
00202     {
00203         mForcedRenderLevel = i;
00204     }
00205 
00207     inline int getNumMipMaps() const
00208     {
00209         return mNumMipMaps;
00210     };
00211 
00213     void _getNormalAt( float x, float y, Vector3 * result );
00214 
00216     float getHeightAt( float x, float y );
00217 
00220     bool intersectSegment( const Vector3 & start, const Vector3 & end, Vector3 * result );
00221 
00225     void _setNeighbor( Neighbor n, TerrainRenderable *t )
00226     {
00227         mNeighbors[ n ] = t;
00228     };
00229 
00232     TerrainRenderable * _getNeighbor( Neighbor n )
00233     {
00234         return mNeighbors[ n ];
00235     }
00236 
00237 
00238     void setMaterial( Material *m )
00239     {
00240         mMaterial = m;
00241     };
00242 
00244     void _alignNeighbors();
00246     void _calculateNormals();
00247 
00248 
00249 
00250 
00253     void _generateVertexLighting( const Vector3 &sun, ColourValue ambient );
00254 
00255 
00256     static size_t mRenderedTris;
00257 
00259     Real getSquaredViewDepth(const Camera* cam) const;
00260 
00262     Real getBoundingRadius(void) const { return 0; /* not needed */ }
00263 
00265     const LightList& getLights(void) const;
00266 
00267 
00268 
00269 protected:
00270 
00272     inline int _index( int x, int z ) const
00273     {
00274         return ( x + z * mSize );
00275     };
00276 
00278     inline float _vertex( int x, int z, int n )
00279     {
00280         HardwareVertexBufferSharedPtr vbuf = 
00281             mTerrain->vertexBufferBinding->getBuffer(0);
00282 
00283         Real vertex = 0.0;
00284 
00285         vbuf->readData((x * 3 + z * mSize * 3 + n) * sizeof(Real), sizeof(Real), &vertex);
00286 
00287         return vertex;
00288       /* 
00289         return mTerrain.pVertices[ x * 3 + z * mSize * 3 + n ];
00290         */
00291     };
00292 
00293 
00294     inline int _numNeighbors() const
00295     {
00296         int n = 0;
00297 
00298         for ( int i = 0; i < 4; i++ )
00299         {
00300             if ( mNeighbors[ i ] != 0 )
00301                 n++;
00302         }
00303 
00304         return n;
00305     }
00306 
00307     inline bool _hasNeighborRenderLevel( int i ) const
00308     {
00309         for ( int j = 0; j < 4; j++ )
00310         {
00311             if ( mNeighbors[ j ] != 0 && mNeighbors[ j ] ->mRenderLevel == i )
00312                 return true;
00313         }
00314 
00315         return false;
00316 
00317     }
00318 
00319     void _adjustRenderLevel( int i );
00320 
00321     void _initLevelIndexes();
00322 
00323     bool _checkSize( int n );
00324 
00325     void _calculateMinLevelDist2( Real C );
00326 
00327     Real _calculateCFactor();
00328 
00329     VertexData* mTerrain;
00330 
00331     int mNumMipMaps;
00332     int mRenderLevel;
00333 
00334     Real *mMinLevelDistSqr;
00335 
00336     TerrainRenderable *mNeighbors [ 4 ];
00337 
00338     AxisAlignedBox mBounds;
00339     Vector3 mCenter;
00340     Vector3 mScale;
00341 
00342     int mSize;
00343     int mWorldSize;
00344 
00345     String mName;
00346     static String mType;
00347 
00348     Material *mMaterial;
00349 
00350     bool mRenderLevelChanged;
00351     bool mInit;
00352 
00353     static LevelArray mLevelIndex;
00354     static bool mLevelInit;
00355 
00356     int mNearPlane;
00357     int mMaxPixelError;
00358     int mVertResolution;
00359     Real mTopCoord;
00360 
00361     Real old_L;
00362 
00363     Real current_L;
00364 
00365     bool mColored;
00366     bool mLit;
00367 
00368     int mForcedRenderLevel;
00369 
00370 };
00371 
00372 }
00373 
00374 #endif

Copyright © 2002-2003 by The OGRE Team
Last modified Fri May 14 23:22:51 2004