source: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreKdTerrainSceneManager.h @ 1273

Revision 1273, 11.2 KB checked in by szydlowski, 18 years ago (diff)

Added the KdTerrainSceneManager?, a subclass of the KdTreeSceneManager? capable of rendering terrain like the TerrainSceneManager? from Ogre.
All the *Kd*Terrain* classes are identical to their octree counterparts, save prefixing all classes and structures with Kd to avoid namespace clashes.
This was necessary, since the TerrainSceneManager? was hard coded in these classes, and all references had to be replaced with the KdTerrainSceneManager?.
Also added a comprehensive README for the demo application.

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of the GameTools Project
4http://www.gametools.org
5
6Author: Martin Szydlowski
7-----------------------------------------------------------------------------
8*/
9
10#ifndef _KdTreeTerrainSceneManager_H__
11#define _KdTreeTerrainSceneManager_H__
12
13#include <OgreIteratorWrappers.h>
14
15#include "OgreKdTerrainPrerequisites.h"
16#include "OgreKdTerrainPageSource.h"
17#include "OgreKdTerrainRenderable.h"
18
19#include "OgreKdTreeSceneManager.h"
20
21
22namespace Ogre
23{
24
25typedef std::vector < KdTerrainPage * > KdTerrainPageRow;
26typedef std::vector < KdTerrainPageRow > KdTerrainPage2D;
27
28/** Default implementation of RaySceneQuery. */
29// TODO: all scene queries in kd-tree
30class _OgreKdTerrainExport KdTerrainRaySceneQuery : public DefaultRaySceneQuery
31{
32protected:
33        WorldFragment mWorldFrag;
34public:
35        KdTerrainRaySceneQuery(SceneManager* creator);
36        ~KdTerrainRaySceneQuery();
37
38        /** See RayScenQuery. */
39        void execute(RaySceneQueryListener* listener);
40};
41
42class KdTerrainSceneManager : public KdTreeSceneManager
43{
44public:
45        KdTerrainSceneManager(const String& name, GtpVisibility::VisibilityManager *vm);
46        ~KdTerrainSceneManager(void);
47
48        virtual const String& getTypeName(void) const;
49
50        /************************************************************************/
51        /* Functions overriden from (KdTree)SceneManager                        */
52        /************************************************************************/
53
54        /** Loads the terrain using parameters int he given config file. */
55        void setWorldGeometry( const String& filename );
56        /** Loads the terrain using parameters in the given config file (contained
57        in a stream). */
58        virtual void setWorldGeometry(DataStreamPtr& stream,
59                const String& typeName = StringUtil::BLANK);
60
61        /** Sets the given option for the SceneManager.
62        @remarks
63        Options are (in addition to those supported by superclasses):
64        "PageSize", int*;
65        "TileSize", int*;
66        "PrimaryCamera, Camera*;
67        "MaxMipMapLevel", int*;
68        "Scale", Vector3 *;
69        "MaxPixelError", int*;
70        "UseTriStrips", bool*;
71        "VertexProgramMorph", bool*;
72        "DetailTile", int*;
73        "LodMorphStart", Real*;
74        "VertexNormals", bool*;
75        "VertexColours", bool*;
76        "MorphLODFactorParamName", String*;
77        "MorphLODFactorParamIndex", size_t*;
78        "CustomMaterialName", String*;
79        "WorldTexture", String*;
80        "DetailTexture", String*;
81        */
82        virtual bool setOption(const String& strKey, const void* pValue);
83
84
85        // Overridden from kdtree scene mananger
86        //void _findVisibleObjects(Camera *cam, bool onlyShadowCasters);
87        // Overridden from kdtree scene manager
88        void _renderScene(Camera *cam, Viewport *vp, bool includeOverlays);
89        /** Overridden to user _renderVisibleObjects from KdTreeSceneManager */
90        //virtual void _renderVisibleObjects( void );
91        /** Overridden from SceneManager */
92        void setWorldGeometryRenderQueue(uint8 qid);
93
94        /** Overridden in order to store the first camera created as the primary
95        one, for determining error metrics and the 'home' terrain page.
96        */
97        Camera* createCamera( const String &name );
98
99
100        /** Creates a RaySceneQuery for this scene manager. */
101        RaySceneQuery*
102                createRayQuery(const Ray& ray, unsigned long mask = 0xFFFFFFFF);
103
104        /** Overide from scene manager to destroy kdtree properly (before the scene graph is destroyed)
105        */
106        virtual void clearScene();
107
108        /************************************************************************/
109        /* Functions for terrain rendering                                      */
110        /************************************************************************/
111        /** Returns the height at the given terrain coordinates. */
112        float getHeightAt( float x, float y );
113
114        bool intersectSegment( const Vector3 & start, const Vector3 & end, Vector3 * result );
115
116        /** Sets the texture to use for the main world texture. */
117        void setWorldTexture(const String& textureName);
118        /** Sets the texture to use for the detail texture. */
119        void setDetailTexture(const String& textureName);
120        /** Sets the number of times per tile the detail texture should be repeated. */
121        void setDetailTextureRepeat(int repeat);
122        /** Sets the dimensions of each tile (must be power of 2 + 1) */
123        void setTileSize(int size);
124        /** Sets the dimensions of each page (must be power of 2 + 1) */
125        void setPageSize(int size);
126        /** Sets the maximum screen space pixel error.  */
127        void setMaxPixelError(int pixelError);
128        /** Sets how to scale the terrain data. */
129        void setScale(const Vector3& scale);
130        /** Sets the maximum geomipmap level to allow. */
131        void setMaxGeoMipMapLevel(int maxMip);
132
133        /** Gets the texture to use for the main world texture. */
134        const String& getWorldTexture(void) { return mWorldTextureName; }
135        /** Gets the texture to use for the detail texture. */
136        const String& getDetailTexture(void) { return mDetailTextureName; }
137        /** Gets the number of times per tile the detail texture should be repeated. */
138        int getDetailTextureRepeat(void);
139        /** Gets the dimensions of each tile (must be power of 2 + 1) */
140        int getTileSize(void);
141        /** Gets the dimensions of each page (must be power of 2 + 1) */
142        int getPageSize(void);
143        /** Gets the maximum screen space pixel error.  */
144        int getMaxPixelError(void);
145        /** Gets how to scale the terrain data. */
146        const Vector3& getScale(void);
147        /** Gets the maximum geomipmap level to allow. */
148        int getMaxGeoMipMapLevel(void);
149
150
151        /** Sets whether the terrain should use triangle strips or not. */
152        void setUseTriStrips(bool useStrips);
153        /** Sets whether or not terrain tiles should be morphed between LODs
154        (NB requires vertex program support). */
155        void setUseLODMorph(bool useMorph);
156        /** Sets whether vertex normals will be generated for the terrain. */
157        void setUseVertexNormals(bool useNormals);
158        /** Sets whether vertex colours will be used. */
159        void setUseVertexColours(bool useColours);
160
161        /** Sets the name of a custom material to use to shade the landcape. */
162        void setCustomMaterial(const String& materialName);
163        /** Sets the name of the vertex program parameter to which to pass the
164        LOD morph factor. */
165        void setCustomMaterialMorphFactorParam(const String& paramName);
166        /** Sets the index of the vertex program parameter to which to pass the
167        LOD morph factor. */
168        void setCustomMaterialMorphFactorParam(uint paramIndex);
169        /** Sets the distance at which the LOD will start to morph downwards, as
170        a proportion of the distance between the LODs. */
171        void setLODMorphStart(Real morphStart);
172
173        /** Returns the TerrainRenderable that contains the given pt.
174        If no tile exists at the point, it returns 0;
175        */
176        virtual KdTerrainRenderable * getTerrainTile( const Vector3 & pt );
177
178        /** Returns the TerrainPage that contains the given pt.
179        If no page exists at the point, it returns 0;
180        */
181        virtual KdTerrainPage* getTerrainPage( const Vector3 & pt );
182
183        /// Gets the terrain options
184        const KdTerrainOptions& getOptions(void) { return mOptions; }
185
186        /** Sets the 'primary' camera, i.e. the one which will be used to determine
187        the 'home' terrain page, and to calculate the error metrics.
188        */
189        virtual void setPrimaryCamera(const Camera* cam);
190        /// Internal map of page source name to page source
191        typedef std::map<String, KdTerrainPageSource*> KdPageSourceMap;
192
193        /// Iterator over all page sources
194        typedef ConstMapIterator<KdPageSourceMap> KdPageSourceIterator;
195        /// Get an iterator over all page sources
196        KdPageSourceIterator getPageSourceIterator(void);
197        /** Registers a TerrainPageSource class and associates it with a named type
198        of source. */
199        virtual void registerPageSource(const String& typeName, KdTerrainPageSource* source);
200        /** Selects a given page source based on its type name. */
201        virtual void selectPageSource(const String& typeName,
202                KdTerrainPageSourceOptionList& optionList);
203
204        /** Attaches a previously built page to the list of available pages. */
205        virtual void attachPage(ushort pageX, ushort pageZ, KdTerrainPage* page);
206        /// Get a pointer to the material being used for the terrain
207        MaterialPtr& getTerrainMaterial(void);
208
209        /// Get the SceneNode under which all terrain nodes are attached.
210        SceneNode* getTerrainRootNode(void) const { return mTerrainRoot; }
211
212        /// Get the shared list of indexes cached (internal use only)
213        KdTerrainBufferCache& _getIndexCache(void) {return mIndexCache;}
214
215        /// Get the shared level index list (internal use only)
216        LevelArray& _getLevelIndex(void) { return mLevelIndex; }
217
218        /// Get the current page count (internal use only)
219        uint _getPageCount(void) { return (uint)mTerrainPages.size(); }
220
221        /// Shutdown cleanly before we get destroyed
222        void shutdown(void);
223
224protected:
225        /// Validates that the size picked for the terrain is acceptable
226        bool _checkSize( int s )
227        {
228                for ( int i = 0; i < 16; i++ )
229                {
230                        printf( "Checking...%d\n", ( 1 << i ) + 1 );
231
232                        if ( s == ( 1 << i ) + 1 )
233                                return true;
234                }
235
236                return false;
237
238        }
239
240        /// The node to which all terrain tiles are attached
241        SceneNode * mTerrainRoot;
242        /// Terrain size, detail etc
243        KdTerrainOptions mOptions;
244        /// Should we use an externally-defined custom material?
245        bool mUseCustomMaterial;
246        /// The name of the custom material to use
247        String mCustomMaterialName;
248        /// The name of the world texture
249        String mWorldTextureName;
250        /// The name of the detail texture
251        String mDetailTextureName;
252        /// Are we using a named parameter to hook up LOD morph?
253        bool mUseNamedParameterLodMorph;
254        /// The name of the parameter to send the LOD morph to
255        String mLodMorphParamName;
256        /// The index of the parameter to send the LOD morph to
257        uint mLodMorphParamIndex;
258        /// Whether paging is enabled, or whether a single page will be used
259        bool mPagingEnabled;
260        /// The number of pages to render outside the 'home' page
261        unsigned short mLivePageMargin;
262        /// The number of pages to keep loaded outside the 'home' page
263        unsigned short mBufferedPageMargin;
264        /// Grid of buffered pages
265        KdTerrainPage2D mTerrainPages;
266        //-- attributes to share across tiles
267        /// Shared list of index buffers
268        KdTerrainBufferCache mIndexCache;
269        /// Shared array of IndexData (reuse indexes across tiles)
270        LevelArray mLevelIndex;
271
272        /// Internal method for loading configurations settings
273        void loadConfig(DataStreamPtr& stream);
274
275        /// Sets up the terrain material
276        void setupTerrainMaterial(void);
277        /// Sets up the terrain page slots
278        void setupTerrainPages(void);
279        /// Initialise level indexes
280        void initLevelIndexes(void);
281        /// Destroy level indexes
282        void destroyLevelIndexes(void);
283
284
285        /// Map of source type -> TerrainPageSource
286        KdPageSourceMap mPageSources;
287        /// The currently active page source
288        KdTerrainPageSource* mActivePageSource;
289};
290
291/// Factory for KdTreeSceneManager
292class KdTerrainSceneManagerFactory : public SceneManagerFactory
293{
294protected:
295        typedef std::vector<KdTerrainPageSource*> KdTerrainPageSources;
296        KdTerrainPageSources mTerrainPageSources;
297        GtpVisibility::VisibilityManager *visManager;
298        void initMetaData(void) const;
299public:
300        KdTerrainSceneManagerFactory(GtpVisibility::VisibilityManager * vm);
301        ~KdTerrainSceneManagerFactory(void);
302        /// Factory type name
303        static const String FACTORY_TYPE_NAME;
304        SceneManager* createInstance(const String& instanceName);
305        void destroyInstance(SceneManager* instance);
306};
307
308} // namespace Ogre
309
310#endif // _KdTreeTerrainSceneManager_H__
Note: See TracBrowser for help on using the repository browser.