[692] | 1 | /*
|
---|
| 2 | -----------------------------------------------------------------------------
|
---|
| 3 | This source file is part of OGRE
|
---|
| 4 | (Object-oriented Graphics Rendering Engine)
|
---|
| 5 | For the latest info, see http://www.ogre3d.org/
|
---|
| 6 |
|
---|
| 7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
| 8 | Also see acknowledgements in Readme.html
|
---|
| 9 |
|
---|
| 10 | This program is free software; you can redistribute it and/or modify it under
|
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
| 13 | version.
|
---|
| 14 |
|
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
| 18 |
|
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
| 23 | -----------------------------------------------------------------------------
|
---|
| 24 | */
|
---|
| 25 | /***************************************************************************
|
---|
| 26 | terrainrenderable.h - description
|
---|
| 27 | -------------------
|
---|
| 28 | begin : Sat Oct 5 2002
|
---|
| 29 | copyright : (C) 2002 by Jon Anderson
|
---|
| 30 | email : janders@users.sf.net
|
---|
| 31 |
|
---|
| 32 | Enhancements 2003 - 2004 (C) The OGRE Team
|
---|
| 33 |
|
---|
| 34 | ***************************************************************************/
|
---|
| 35 |
|
---|
| 36 | #ifndef TERRAINRENDERABLE_H
|
---|
| 37 | #define TERRAINRENDERABLE_H
|
---|
| 38 |
|
---|
| 39 | #include "OgreTerrainPrerequisites.h"
|
---|
| 40 | #include <OgreRenderable.h>
|
---|
| 41 | #include <OgreMovableObject.h>
|
---|
| 42 | #include <OgreAxisAlignedBox.h>
|
---|
| 43 | #include <OgreString.h>
|
---|
| 44 | #include <OgreHardwareBufferManager.h>
|
---|
| 45 |
|
---|
| 46 | #include <vector>
|
---|
| 47 |
|
---|
| 48 | #define MORPH_CUSTOM_PARAM_ID 77
|
---|
| 49 |
|
---|
| 50 | namespace Ogre
|
---|
| 51 | {
|
---|
| 52 |
|
---|
| 53 | typedef std::map <unsigned int, IndexData* > IndexMap;
|
---|
| 54 | typedef std::vector < IndexData* > IndexArray;
|
---|
| 55 | typedef std::vector < IndexMap* > LevelArray;
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * A cache of TerrainIndexBuffers. Used to keep track of the buffers, and
|
---|
| 59 | * delete them when the program finishes.
|
---|
| 60 | */
|
---|
| 61 | class TerrainBufferCache
|
---|
| 62 | {
|
---|
| 63 | public:
|
---|
| 64 | void shutdown(void)
|
---|
| 65 | {
|
---|
| 66 | for( size_t i=0; i<mCache.size(); i++ )
|
---|
| 67 | {
|
---|
| 68 | delete mCache[i];
|
---|
| 69 | }
|
---|
| 70 | mCache.clear();
|
---|
| 71 | }
|
---|
| 72 | ~TerrainBufferCache()
|
---|
| 73 | {
|
---|
| 74 | shutdown();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | IndexArray mCache;
|
---|
| 78 | };
|
---|
| 79 |
|
---|
| 80 | inline Real _max( Real x, Real y )
|
---|
| 81 | {
|
---|
| 82 | return ( x > y ) ? x : y;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | /** A simple class for encapsulating parameters which are commonly needed by
|
---|
| 86 | both TerrainSceneManager and TerrainRenderable.
|
---|
| 87 | */
|
---|
| 88 | class TerrainOptions
|
---|
| 89 | {
|
---|
| 90 | public:
|
---|
| 91 | TerrainOptions()
|
---|
| 92 | {
|
---|
| 93 | pageSize = 0;
|
---|
| 94 | tileSize = 0;
|
---|
| 95 | tilesPerPage = 0;
|
---|
| 96 | maxGeoMipMapLevel = 0;
|
---|
| 97 | scale = Vector3::UNIT_SCALE;
|
---|
| 98 | maxPixelError = 4;
|
---|
| 99 | detailTile = 1;
|
---|
| 100 | lit = false;
|
---|
| 101 | coloured = false;
|
---|
| 102 | lodMorph = false;
|
---|
| 103 | lodMorphStart = 0.5;
|
---|
| 104 | useTriStrips = false;
|
---|
| 105 | primaryCamera = 0;
|
---|
| 106 | terrainMaterial.setNull();
|
---|
| 107 | };
|
---|
| 108 | /// The size of one edge of a terrain page, in vertices
|
---|
| 109 | size_t pageSize;
|
---|
| 110 | /// The size of one edge of a terrain tile, in vertices
|
---|
| 111 | size_t tileSize;
|
---|
| 112 | /// Precalculated number of tiles per page
|
---|
| 113 | size_t tilesPerPage;
|
---|
| 114 | /// The primary camera, used for error metric calculation and page choice
|
---|
| 115 | const Camera* primaryCamera;
|
---|
| 116 | /// The maximum terrain geo-mipmap level
|
---|
| 117 | size_t maxGeoMipMapLevel;
|
---|
| 118 | /// The scale factor to apply to the terrain (each vertex is 1 unscaled unit
|
---|
| 119 | /// away from the next, and height is from 0 to 1)
|
---|
| 120 | Vector3 scale;
|
---|
| 121 | /// The maximum pixel error allowed
|
---|
| 122 | size_t maxPixelError;
|
---|
| 123 | /// Whether we should use triangle strips
|
---|
| 124 | bool useTriStrips;
|
---|
| 125 | /// The number of times to repeat a detail texture over a tile
|
---|
| 126 | size_t detailTile;
|
---|
| 127 | /// Whether LOD morphing is enabled
|
---|
| 128 | bool lodMorph;
|
---|
| 129 | /// At what point (parametric) should LOD morphing start
|
---|
| 130 | Real lodMorphStart;
|
---|
| 131 | /// Whether dynamic lighting is enabled
|
---|
| 132 | bool lit;
|
---|
| 133 | /// Whether vertex colours are enabled
|
---|
| 134 | bool coloured;
|
---|
| 135 | /// Pointer to the material to use to render the terrain
|
---|
| 136 | MaterialPtr terrainMaterial;
|
---|
| 137 |
|
---|
| 138 | };
|
---|
| 139 |
|
---|
| 140 | #define STITCH_NORTH_SHIFT 0
|
---|
| 141 | #define STITCH_SOUTH_SHIFT 8
|
---|
| 142 | #define STITCH_WEST_SHIFT 16
|
---|
| 143 | #define STITCH_EAST_SHIFT 24
|
---|
| 144 |
|
---|
| 145 | #define STITCH_NORTH 128 << STITCH_NORTH_SHIFT
|
---|
| 146 | #define STITCH_SOUTH 128 << STITCH_SOUTH_SHIFT
|
---|
| 147 | #define STITCH_WEST 128 << STITCH_WEST_SHIFT
|
---|
| 148 | #define STITCH_EAST 128 << STITCH_EAST_SHIFT
|
---|
| 149 |
|
---|
| 150 | /**
|
---|
| 151 | Represents a terrain tile.
|
---|
| 152 | @remarks
|
---|
| 153 | A TerrainRenderable represents a tile used to render a block of terrain using the geomipmap approach
|
---|
| 154 | for LOD.
|
---|
| 155 | *@author Jon Anderson
|
---|
| 156 | */
|
---|
| 157 |
|
---|
| 158 | class _OgreTerrainExport TerrainRenderable : public Renderable, public MovableObject
|
---|
| 159 | {
|
---|
| 160 | public:
|
---|
| 161 |
|
---|
| 162 | TerrainRenderable(const String& name, TerrainSceneManager* tsm);
|
---|
| 163 | ~TerrainRenderable();
|
---|
| 164 |
|
---|
| 165 | void deleteGeometry();
|
---|
| 166 |
|
---|
| 167 | enum Neighbor
|
---|
| 168 | {
|
---|
| 169 | NORTH = 0,
|
---|
| 170 | SOUTH = 1,
|
---|
| 171 | EAST = 2,
|
---|
| 172 | WEST = 3,
|
---|
| 173 | HERE = 4
|
---|
| 174 | };
|
---|
| 175 |
|
---|
| 176 | /** Initializes the TerrainRenderable.
|
---|
| 177 | @param startx, startz
|
---|
| 178 | The starting points of the top-left of this tile, in terms of the
|
---|
| 179 | number of vertices.
|
---|
| 180 | @param pageHeightData The source height data for the entire parent page
|
---|
| 181 | */
|
---|
| 182 | void initialise(int startx, int startz, Real* pageHeightData);
|
---|
| 183 |
|
---|
| 184 | //movable object methods
|
---|
| 185 |
|
---|
| 186 | /** Returns the type of the movable. */
|
---|
| 187 | virtual const String& getMovableType( void ) const
|
---|
| 188 | {
|
---|
| 189 | return mType;
|
---|
| 190 | };
|
---|
| 191 |
|
---|
| 192 | /** Returns the bounding box of this TerrainRenderable */
|
---|
| 193 | const AxisAlignedBox& getBoundingBox( void ) const
|
---|
| 194 | {
|
---|
| 195 | return mBounds;
|
---|
| 196 | };
|
---|
| 197 |
|
---|
| 198 | /** Updates the level of detail to be used for rendering this TerrainRenderable based on the passed in Camera */
|
---|
| 199 | virtual void _notifyCurrentCamera( Camera* cam );
|
---|
| 200 |
|
---|
| 201 | virtual void _updateRenderQueue( RenderQueue* queue );
|
---|
| 202 |
|
---|
| 203 | /**
|
---|
| 204 | Constructs a RenderOperation to render the TerrainRenderable.
|
---|
| 205 | @remarks
|
---|
| 206 | Each TerrainRenderable has a block of vertices that represent the terrain. Index arrays are dynamically
|
---|
| 207 | created for mipmap level, and then cached.
|
---|
| 208 | */
|
---|
| 209 | virtual void getRenderOperation( RenderOperation& rend );
|
---|
| 210 |
|
---|
| 211 | virtual const MaterialPtr& getMaterial( void ) const
|
---|
| 212 | {
|
---|
| 213 | return mMaterial;
|
---|
| 214 | };
|
---|
| 215 |
|
---|
| 216 | virtual void getWorldTransforms( Matrix4* xform ) const;
|
---|
| 217 |
|
---|
| 218 | virtual const Quaternion& getWorldOrientation(void) const;
|
---|
| 219 | virtual const Vector3& getWorldPosition(void) const;
|
---|
| 220 |
|
---|
| 221 | /** Returns the mipmap level that will be rendered for this frame. */
|
---|
| 222 | inline int getRenderLevel() const
|
---|
| 223 | {
|
---|
| 224 | return mRenderLevel;
|
---|
| 225 | };
|
---|
| 226 |
|
---|
| 227 | /** Forces the LOD to the given level from this point on. */
|
---|
| 228 | inline void setForcedRenderLevel( int i )
|
---|
| 229 | {
|
---|
| 230 | mForcedRenderLevel = i;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | /** Calculates the normal at the given location */
|
---|
| 234 | void _getNormalAt( float x, float y, Vector3 * result );
|
---|
| 235 |
|
---|
| 236 | /** Returns the terrain height at the given coordinates */
|
---|
| 237 | float getHeightAt( float x, float y );
|
---|
| 238 |
|
---|
| 239 | /** Intersects the segment witht he terrain tile
|
---|
| 240 | */
|
---|
| 241 | bool intersectSegment( const Vector3 & start, const Vector3 & end, Vector3 * result );
|
---|
| 242 |
|
---|
| 243 | /** Sets the appropriate neighbor for this TerrainRenderable. Neighbors are necessary
|
---|
| 244 | to know when to bridge between LODs.
|
---|
| 245 | */
|
---|
| 246 | void _setNeighbor( Neighbor n, TerrainRenderable *t )
|
---|
| 247 | {
|
---|
| 248 | mNeighbors[ n ] = t;
|
---|
| 249 | };
|
---|
| 250 |
|
---|
| 251 | /** Returns the neighbor TerrainRenderable.
|
---|
| 252 | */
|
---|
| 253 | TerrainRenderable * _getNeighbor( Neighbor n )
|
---|
| 254 | {
|
---|
| 255 | return mNeighbors[ n ];
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 |
|
---|
| 259 | void setMaterial(const MaterialPtr& m )
|
---|
| 260 | {
|
---|
| 261 | mMaterial = m;
|
---|
| 262 | };
|
---|
| 263 |
|
---|
| 264 | /** Calculates static normals for lighting the terrain. */
|
---|
| 265 | void _calculateNormals();
|
---|
| 266 |
|
---|
| 267 |
|
---|
| 268 |
|
---|
| 269 |
|
---|
| 270 | /** Generates terrain shadows and lighting using vertex colors
|
---|
| 271 | */
|
---|
| 272 | void _generateVertexLighting( const Vector3 &sun, ColourValue ambient );
|
---|
| 273 |
|
---|
| 274 |
|
---|
| 275 | /** Overridden, see Renderable */
|
---|
| 276 | Real getSquaredViewDepth(const Camera* cam) const;
|
---|
| 277 |
|
---|
| 278 | /** Overridden from MovableObject */
|
---|
| 279 | Real getBoundingRadius(void) const { return mBoundingRadius; }
|
---|
| 280 |
|
---|
| 281 | /** @copydoc Renderable::getLights */
|
---|
| 282 | const LightList& getLights(void) const;
|
---|
| 283 |
|
---|
| 284 | /// Overridden from Renderable to allow the morph LOD entry to be set
|
---|
| 285 | void _updateCustomGpuParameter(
|
---|
| 286 | const GpuProgramParameters::AutoConstantEntry& constantEntry,
|
---|
| 287 | GpuProgramParameters* params) const;
|
---|
| 288 | /// @see MovableObject
|
---|
| 289 | uint32 getTypeFlags(void) const;
|
---|
| 290 | protected:
|
---|
| 291 | /// Parent SceneManager
|
---|
| 292 | TerrainSceneManager* mSceneManager;
|
---|
| 293 | /// Link to shared options
|
---|
| 294 | const TerrainOptions* mOptions;
|
---|
| 295 |
|
---|
| 296 | /** Returns the index into the height array for the given coords. */
|
---|
| 297 | inline size_t _index( int x, int z ) const
|
---|
| 298 | {
|
---|
| 299 | return ( x + z * mOptions->tileSize );
|
---|
| 300 | };
|
---|
| 301 |
|
---|
| 302 | /** Returns the vertex coord for the given coordinates */
|
---|
| 303 | inline float _vertex( int x, int z, int n )
|
---|
| 304 | {
|
---|
| 305 | return mPositionBuffer[x * 3 + z * mOptions->tileSize * 3 + n];
|
---|
| 306 | };
|
---|
| 307 |
|
---|
| 308 |
|
---|
| 309 | inline int _numNeighbors() const
|
---|
| 310 | {
|
---|
| 311 | int n = 0;
|
---|
| 312 |
|
---|
| 313 | for ( int i = 0; i < 4; i++ )
|
---|
| 314 | {
|
---|
| 315 | if ( mNeighbors[ i ] != 0 )
|
---|
| 316 | n++;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | return n;
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | inline bool _hasNeighborRenderLevel( int i ) const
|
---|
| 323 | {
|
---|
| 324 | for ( int j = 0; j < 4; j++ )
|
---|
| 325 | {
|
---|
| 326 | if ( mNeighbors[ j ] != 0 && mNeighbors[ j ] ->mRenderLevel == i )
|
---|
| 327 | return true;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | return false;
|
---|
| 331 |
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | void _adjustRenderLevel( int i );
|
---|
| 335 |
|
---|
| 336 | bool _checkSize( int n );
|
---|
| 337 |
|
---|
| 338 | void _calculateMinLevelDist2( Real C );
|
---|
| 339 |
|
---|
| 340 | Real _calculateCFactor();
|
---|
| 341 |
|
---|
| 342 | VertexData* mTerrain;
|
---|
| 343 |
|
---|
| 344 | /// The current LOD level
|
---|
| 345 | int mRenderLevel;
|
---|
| 346 | /// The previous 'next' LOD level down, for frame coherency
|
---|
| 347 | int mLastNextLevel;
|
---|
| 348 | /// The morph factor between this and the next LOD level down
|
---|
| 349 | Real mLODMorphFactor;
|
---|
| 350 | /// List of squared distances at which LODs change
|
---|
| 351 | Real *mMinLevelDistSqr;
|
---|
| 352 | /// Connection to tiles four neighbours
|
---|
| 353 | TerrainRenderable *mNeighbors [ 4 ];
|
---|
| 354 | /// Whether light list need to re-calculate
|
---|
| 355 | mutable bool mLightListDirty;
|
---|
| 356 | /// Cached light list
|
---|
| 357 | mutable LightList mLightList;
|
---|
| 358 | /// The bounding radius of this tile
|
---|
| 359 | Real mBoundingRadius;
|
---|
| 360 | /// Bounding box of this tile
|
---|
| 361 | AxisAlignedBox mBounds;
|
---|
| 362 | /// The center point of this tile
|
---|
| 363 | Vector3 mCenter;
|
---|
| 364 | /// The MovableObject type
|
---|
| 365 | static String mType;
|
---|
| 366 | /// Current material used by this tile
|
---|
| 367 | MaterialPtr mMaterial;
|
---|
| 368 | /// Whether this tile has been initialised
|
---|
| 369 | bool mInit;
|
---|
| 370 | /// The buffer with all the renderable geometry in it
|
---|
| 371 | HardwareVertexBufferSharedPtr mMainBuffer;
|
---|
| 372 | /// Optional set of delta buffers, used to morph from one LOD to the next
|
---|
| 373 | HardwareVertexBufferSharedPtr* mDeltaBuffers;
|
---|
| 374 | /// System-memory buffer with just positions in it, for CPU operations
|
---|
| 375 | float* mPositionBuffer;
|
---|
| 376 | /// Forced rendering LOD level, optional
|
---|
| 377 | int mForcedRenderLevel;
|
---|
| 378 | /// Array of LOD indexes specifying which LOD is the next one down
|
---|
| 379 | /// (deals with clustered error metrics which cause LODs to be skipped)
|
---|
| 380 | int mNextLevelDown[10];
|
---|
| 381 | /// Gets the index data for this tile based on current settings
|
---|
| 382 | IndexData* getIndexData(void);
|
---|
| 383 | /// Internal method for generating stripified terrain indexes
|
---|
| 384 | IndexData* generateTriStripIndexes(unsigned int stitchFlags);
|
---|
| 385 | /// Internal method for generating triangle list terrain indexes
|
---|
| 386 | IndexData* generateTriListIndexes(unsigned int stitchFlags);
|
---|
| 387 | /** Utility method to generate stitching indexes on the edge of a tile
|
---|
| 388 | @param neighbor The neighbor direction to stitch
|
---|
| 389 | @param hiLOD The LOD of this tile
|
---|
| 390 | @param loLOD The LOD of the neighbor
|
---|
| 391 | @param omitFirstTri Whether the first tri of the stitch (always clockwise
|
---|
| 392 | relative to the centre of this tile) is to be omitted because an
|
---|
| 393 | adjoining edge is also being stitched
|
---|
| 394 | @param omitLastTri Whether the last tri of the stitch (always clockwise
|
---|
| 395 | relative to the centre of this tile) is to be omitted because an
|
---|
| 396 | adjoining edge is also being stitched
|
---|
| 397 | @param pIdx Pointer to a pointer to the index buffer to push the results
|
---|
| 398 | into (this pointer will be updated)
|
---|
| 399 | @returns The number of indexes added
|
---|
| 400 | */
|
---|
| 401 | int stitchEdge(Neighbor neighbor, int hiLOD, int loLOD,
|
---|
| 402 | bool omitFirstTri, bool omitLastTri, unsigned short** ppIdx);
|
---|
| 403 |
|
---|
| 404 | /// Create a blank delta buffer for usein morphing
|
---|
| 405 | HardwareVertexBufferSharedPtr createDeltaBuffer(void);
|
---|
| 406 |
|
---|
| 407 | };
|
---|
| 408 |
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 | #endif
|
---|