source: trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/include/OgreTerrainRenderable.h @ 316

Revision 316, 14.9 KB checked in by mattausch, 19 years ago (diff)

queries are realized as templates

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