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 <OgreGeometryData.h> 00026 00027 #include <vector> 00028 00029 namespace Ogre 00030 { 00031 00032 class TerrainIndexBuffer 00033 { 00034 public: 00035 TerrainIndexBuffer( int s ) 00036 { 00037 indexes = new unsigned short[ s ]; 00038 } 00039 00040 ~TerrainIndexBuffer() 00041 { 00042 delete indexes; 00043 } 00044 00045 unsigned short * indexes; 00046 int length; 00047 }; 00048 00049 00050 00051 typedef std::vector < TerrainIndexBuffer * > IndexArray; 00052 typedef std::vector < IndexArray > LevelArray; 00053 00054 00055 inline Real _max( Real x, Real y ) 00056 { 00057 return ( x > y ) ? x : y; 00058 } 00059 00064 class TerrainOptions 00065 { 00066 public: 00067 TerrainOptions() 00068 { 00069 data = 0; 00070 size = 0; 00071 world_size = 0; 00072 startx = 0; 00073 startz = 0; 00074 max_mipmap = 0; 00075 scalex = 1; 00076 scaley = 1; 00077 scalez = 1; 00078 max_pixel_error = 4; 00079 vert_res = 768; 00080 top_coord = 1; 00081 near_plane = 1; 00082 detail_tile = 1; 00083 lit = false; 00084 colored = false; 00085 }; 00086 00087 00088 int _worldheight( int x, int z ) 00089 { 00090 return data[ ( ( z * world_size ) + x ) ]; 00091 }; 00092 00093 const uchar * data; //pointer to the world 2D data. 00094 int size; //size of this square block 00095 int world_size; //size of the world. 00096 int startx; 00097 int startz; //starting coords of this block. 00098 int max_mipmap; //max mip_map level 00099 float scalex, scaley, scalez; 00100 00101 int max_pixel_error; 00102 int near_plane; 00103 int vert_res; 00104 int detail_tile; 00105 float top_coord; 00106 00107 bool lit; 00108 bool colored; 00109 00110 }; 00111 00112 #define TILE_NORTH 0x00000001 00113 #define TILE_SOUTH 0x00000002 00114 #define TILE_WEST 0x00000004 00115 #define TILE_EAST 0x00000008 00116 00125 class TerrainRenderable : public Renderable, public MovableObject 00126 { 00127 public: 00128 00129 TerrainRenderable(); 00130 ~TerrainRenderable(); 00131 00132 void deleteGeometry(); 00133 00134 enum Neighbor 00135 { 00136 NORTH = 0, 00137 SOUTH = 1, 00138 EAST = 2, 00139 WEST = 3, 00140 HERE = 4 00141 }; 00142 00146 void init( TerrainOptions &options ); 00147 00148 //movable object methods 00150 virtual const String& getName( void ) const 00151 { 00152 return mName; 00153 }; 00154 00156 virtual const String getMovableType( void ) const 00157 { 00158 return mType; 00159 }; 00160 00162 const AxisAlignedBox& getBoundingBox( void ) const 00163 { 00164 return mBounds; 00165 }; 00166 00168 virtual void _notifyCurrentCamera( Camera* cam ); 00169 00170 virtual void _updateRenderQueue( RenderQueue* queue ); 00171 00178 virtual void getRenderOperation( RenderOperation& rend ); 00179 00180 virtual Material* getMaterial( void ) const 00181 { 00182 return mMaterial; 00183 }; 00184 00185 virtual void getWorldTransforms( Matrix4* xform ); 00186 00188 inline int getSize() 00189 { 00190 return mSize; 00191 }; 00192 00194 inline int getRenderLevel() 00195 { 00196 return mRenderLevel; 00197 }; 00198 00200 inline void setForcedRenderLevel( int i ) 00201 { 00202 mForcedRenderLevel = i; 00203 } 00204 00206 inline int getNumMipMaps() 00207 { 00208 return mNumMipMaps; 00209 }; 00210 00212 void _getNormalAt( float x, float y, Vector3 * result ); 00213 00215 float getHeightAt( float x, float y ); 00216 00219 bool intersectSegment( const Vector3 & start, const Vector3 & end, Vector3 * result ); 00220 00224 void _setNeighbor( Neighbor n, TerrainRenderable *t ) 00225 { 00226 mNeighbors[ n ] = t; 00227 }; 00228 00231 TerrainRenderable * _getNeighbor( Neighbor n ) 00232 { 00233 return mNeighbors[ n ]; 00234 } 00235 00236 00237 void setMaterial( Material *m ) 00238 { 00239 mMaterial = m; 00240 }; 00241 00243 void _alignNeighbors(); 00245 void _calculateNormals(); 00246 00247 00248 00249 00252 void _generateVertexLighting( const Vector3 &sun, ColourValue ambient ); 00253 00254 00255 static int mRenderedTris; 00256 00258 Real getSquaredViewDepth(const Camera* cam) const; 00259 00260 00261 protected: 00262 00264 inline int _index( int x, int z ) 00265 { 00266 return ( x + z * mSize ); 00267 }; 00268 00270 inline float _vertex( int x, int z, int n ) 00271 { 00272 return mTerrain.pVertices[ x * 3 + z * mSize * 3 + n ]; 00273 }; 00274 00275 00276 inline int _numNeighbors() 00277 { 00278 int n = 0; 00279 00280 for ( int i = 0; i < 4; i++ ) 00281 { 00282 if ( mNeighbors[ i ] != 0 ) 00283 n++; 00284 } 00285 00286 return n; 00287 } 00288 00289 inline bool _hasNeighborRenderLevel( int i ) 00290 { 00291 for ( int j = 0; j < 4; j++ ) 00292 { 00293 if ( mNeighbors[ j ] != 0 && mNeighbors[ j ] ->mRenderLevel == i ) 00294 return true;; 00295 } 00296 00297 return false; 00298 00299 } 00300 00301 void _adjustRenderLevel( int i ); 00302 00303 void _initLevelIndexes(); 00304 00305 bool _checkSize( int n ); 00306 00307 void _calculateMinLevelDist2( Real C ); 00308 00309 Real _calculateCFactor(); 00310 00311 GeometryData mTerrain; 00312 00313 int mNumMipMaps; 00314 int mRenderLevel; 00315 00316 Real *mMinLevelDistSqr; 00317 00318 TerrainRenderable *mNeighbors [ 4 ]; 00319 00320 AxisAlignedBox mBounds; 00321 Vector3 mCenter; 00322 Vector3 mScale; 00323 00324 int mSize; 00325 int mWorldSize; 00326 00327 String mName; 00328 static String mType; 00329 00330 Material *mMaterial; 00331 00332 bool mRenderLevelChanged; 00333 bool mInit; 00334 00335 static LevelArray mLevelIndex; 00336 static bool mLevelInit; 00337 00338 int mNearPlane; 00339 int mMaxPixelError; 00340 int mVertResolution; 00341 Real mTopCoord; 00342 00343 Real old_L; 00344 00345 Real current_L; 00346 00347 bool mColored; 00348 bool mLit; 00349 00350 int mForcedRenderLevel; 00351 00352 }; 00353 00354 } 00355 00356 #endif
Copyright © 2002 by The OGRE Team