source: GTP/trunk/App/Demos/Geom/OgreStuff/include/opt/OgreTerrainSceneManager.h @ 1092

Revision 1092, 16.7 KB checked in by gumbau, 18 years ago (diff)

LodStrips? and LODTrees demos

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/***************************************************************************
26terrainscenemanager.h  -  description
27---------------------
28  begin                : Mon Sep 23 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 TERRAINSCENEMANAGER_H
37#define TERRAINSCENEMANAGER_H
38
39#include "OgreTerrainPrerequisites.h"
40#include "OgreOctreeSceneManager.h"
41#include "OgreOctreeSceneQuery.h"
42#include "OgreTerrainRenderable.h"
43#include "OgreTerrainPageSource.h"
44#include "OgreIteratorWrappers.h"
45
46
47namespace Ogre
48{
49
50class Image;
51
52typedef std::vector < TerrainPage * > TerrainPageRow;
53typedef std::vector < TerrainPageRow > TerrainPage2D;
54
55/** Default implementation of RaySceneQuery. */
56class _OgreTerrainExport TerrainRaySceneQuery : public OctreeRaySceneQuery
57{
58public:
59    TerrainRaySceneQuery(SceneManager* creator);
60    ~TerrainRaySceneQuery();
61
62    /** See RayScenQuery. */
63    void execute(RaySceneQueryListener* listener);
64};
65
66
67
68/** This is a basic SceneManager for organizing TerrainRenderables into a total landscape.
69  * It loads a terrain from a .cfg file that specifices what textures/scale/mipmaps/etc to use.
70  *@author Jon Anderson
71  */
72
73class _OgreTerrainExport TerrainSceneManager : public OctreeSceneManager
74{
75public:
76    TerrainSceneManager( );
77    virtual ~TerrainSceneManager( );
78
79    /** Loads the terrain using parameters int he given config file. */
80    void setWorldGeometry( const String& filename );
81
82    /** Aligns TerrainRenderable neighbors, and renders them. */
83    virtual void _renderVisibleObjects( void );
84
85    /** Returns the height at the given terrain coordinates. */
86    float getHeightAt( float x, float y );
87
88
89    bool intersectSegment( const Vector3 & start, const Vector3 & end, Vector3 * result );
90
91    /** Sets the texture to use for the main world texture. */
92    void setWorldTexture(const String& textureName);
93    /** Sets the texture to use for the detail texture. */
94    void setDetailTexture(const String& textureName);
95    /** Sets the number of times per tile the detail texture should be repeated. */
96    void setDetailTextureRepeat(int repeat);
97    /** Sets the dimensions of each tile (must be power of 2 + 1) */
98    void setTileSize(int size);
99    /** Sets the dimensions of each page (must be power of 2 + 1) */
100    void setPageSize(int size);
101    /** Sets the maximum screen space pixel error.  */
102    void setMaxPixelError(int pixelError);
103    /** Sets how to scale the terrain data. */
104    void setScale(const Vector3& scale);
105    /** Sets the maximum geomipmap level to allow. */
106    void setMaxGeoMipMapLevel(int maxMip);
107   
108    /** Gets the texture to use for the main world texture. */
109    const String& getWorldTexture(void) { return mWorldTextureName; }
110    /** Gets the texture to use for the detail texture. */
111    const String& getDetailTexture(void) { return mDetailTextureName; }
112    /** Gets the number of times per tile the detail texture should be repeated. */
113    int setDetailTextureRepeat(void);
114    /** Gets the dimensions of each tile (must be power of 2 + 1) */
115    int getTileSize(void);
116    /** Gets the dimensions of each page (must be power of 2 + 1) */
117    int getPageSize(void);
118    /** Gets the maximum screen space pixel error.  */
119    int getMaxPixelError(void);
120    /** Sets how to scale the terrain data. */
121    const Vector3& getScale(void);
122    /** Gets the maximum geomipmap level to allow. */
123    int getMaxGeoMipMapLevel(void);
124
125
126
127    /** Sets whether the terrain should use triangle strips or not.
128    @remarks
129        The default is not, in which case it uses triangle lists.
130    */
131    void setUseTriStrips(bool useStrips);
132    /** Sets whether or not terrain tiles should be morphed between LODs
133    (NB requires vertex program support). */
134    void setUseLODMorph(bool useMorph);
135    /** Sets whether vertex normals will be generated for the terrain. */
136    void setUseVertexNormals(bool useNormals);
137    /** Sets whether vertex colours will be used. */
138    void setUseVertexColours(bool useColours);
139
140    /** Sets the name of a custom material to use to shade the landcape.
141    @remarks
142        This method allows you to provide a custom material which will be
143        used to render the landscape instead of the standard internal
144        material. This gives you a great deal of flexibility and allows you
145        to perform special effects if you wish. Note that because you determine
146        every aspect of the material, this setting makes the use of setWorldTexture
147        and setDetailTexture redundant.
148    @par
149        In your custom material, you can use all the usual features of Ogre's
150        materials, including multiple passes if you wish. You can also use
151        the programmable pipeline to hook vertex and fragment programs into the
152        terrain rendering. The plugin provides the following vertex components:
153        <ul>
154            <li>positions</li>
155            <li>2 sets of texture coordinates (index 0 is world texture,
156            index 1 is detail texture)</li>
157            <li>Normals, if enabled</li>
158            <li>Per-vertex delta values, for morphing a higher LOD tile into
159                a lower LOD tile. This is one float per vertex bound as 'blend
160                weight'. If you want to use this you also have to provide the
161                name or index of the parameter you wish to receive the morph factor
162                (@see setCustomMaterialMorphFactorParam)</li>
163        </ul>
164    */
165    void setCustomMaterial(const String& materialName);
166    /** Sets the name of the vertex program parameter to which to pass the
167        LOD morph factor.
168    @remarks
169        When LOD morphing is enabled, and you are using a custom material to
170        shade the terrain, you need to inform this class of the parameter you
171        wish the current LOD morph factor to be passed to. This is a simple
172        float parameter value that the plugin will set from 0 to 1, depending on
173        the morph stage of a tile. 0 represents no morphing, ie the vertices are
174        all in the original position. 1 represents a complete morph such that
175        the height of the vertices is the same as they are at the next lower LOD
176        level. The vertex program must use this factor, in conjunction with the
177        per-vertex height delta values (bound as 'blend weight'), to displace
178        vertices.
179    @note This version of the method lets you specify a parameter name, compatible
180        with high-level vertex programs. There is an alternative signature method
181        which allows you to set the parameter index for low-level assembler programs.
182    @param paramName The name of the parameter which will receive the morph factor
183    */
184    void setCustomMaterialMorphFactorParam(const String& paramName);
185    /** Sets the index of the vertex program parameter to which to pass the
186        LOD morph factor.
187    @remarks
188        When LOD morphing is enabled, and you are using a custom material to
189        shade the terrain, you need to inform this class of the parameter you
190        wish the current LOD morph factor to be passed to. This is a simple
191        float parameter value that the plugin will set from 0 to 1, depending on
192        the morph stage of a tile. 0 represents no morphing, ie the vertices are
193        all in the original position. 1 represents a complete morph such that
194        the height of the vertices is the same as they are at the next lower LOD
195        level. The vertex program must use this factor, in conjunction with the
196        per-vertex height delta values (bound as 'blend weight'), to displace
197        vertices.
198    @note This version of the method lets you specify a parameter index, compatible
199        with low-level assembler vertex programs. There is an alternative signature method
200        which allows you to set the parameter name for high-level programs.
201    @param paramName The name of the parameter which will receive the morph factor
202    */
203    void setCustomMaterialMorphFactorParam(size_t paramIndex);
204    /** Sets the distance at which the LOD will start to morph downwards, as
205    a proportion of the distance between the LODs. */
206    void setLODMorphStart(Real morphStart);
207
208    /** Returns the TerrainRenderable that contains the given pt.
209        If no tile exists at the point, it returns 0;
210    */
211    virtual TerrainRenderable * getTerrainTile( const Vector3 & pt );
212
213    /** Returns the TerrainPage that contains the given pt.
214    If no page exists at the point, it returns 0;
215    */
216    virtual TerrainPage* getTerrainPage( const Vector3 & pt );
217
218    /** Creates a RaySceneQuery for this scene manager.
219    @remarks
220        This method creates a new instance of a query object for this scene manager,
221        looking for objects which fall along a ray. See SceneQuery and RaySceneQuery
222        for full details.
223    @par
224        The instance returned from this method must be destroyed by calling
225        SceneManager::destroyQuery when it is no longer required.
226    @param ray Details of the ray which describes the region for this query.
227    @param mask The query mask to apply to this query; can be used to filter out
228        certain objects; see SceneQuery for details.
229    */
230    RaySceneQuery*
231        createRayQuery(const Ray& ray, unsigned long mask = 0xFFFFFFFF);
232
233    /** Overridden in order to store the first camera created as the primary
234        one, for determining error metrics and the 'home' terrain page.
235    */
236    Camera* createCamera( const String &name );
237    /// Gets the terrain options
238    static const TerrainOptions& getOptions(void) { return mOptions; }
239
240    /** Sets the given option for the SceneManager.
241    @remarks
242        Options are (in addition to those supported by superclasses):
243        "PageSize", int*;
244        "TileSize", int*;
245        "PrimaryCamera, Camera*;
246        "MaxMipMapLevel", int*;
247        "Scale", Vector3 *;
248        "MaxPixelError", int*;
249        "UseTriStrips", bool*;
250        "VertexProgramMorph", bool*;
251        "DetailTile", int*;
252        "LodMorphStart", Real*;
253        "VertexNormals", bool*;
254        "VertexColours", bool*;
255        "MorphLODFactorParamName", String*;
256        "MorphLODFactorParamIndex", size_t*;
257        "CustomMaterialName", String*;
258        "WorldTexture", String*;
259        "DetailTexture", String*;
260    */
261    virtual bool setOption( const String &, const void * );
262
263    /** Sets the 'primary' camera, i.e. the one which will be used to determine
264        the 'home' terrain page, and to calculate the error metrics.
265    */
266    virtual void setPrimaryCamera(const Camera* cam);
267    /// Internal map of page source name to page source
268    typedef std::map<String, TerrainPageSource*> PageSourceMap;
269
270    /// Iterator over all page sources
271    typedef ConstMapIterator<PageSourceMap> PageSourceIterator;
272    /// Get an iterator over all page sources
273    PageSourceIterator getPageSourceIterator(void);
274    /** Registers a TerrainPageSource class and associates it with a named type
275        of source.
276    @remarks
277        This function allows external classes to register themselves as providers
278        of terrain pages of a particular type. Only one page source can be
279        active at once, and the active one is selected by calling
280        selectPageSource(typeName), which is part of plugin configuration.
281    @note The terrain engine comes with a default page source which loads
282        greyscale heightmap images, registered under the type name "Heightmap".
283    @param typeName A unique String to associate with this type of source
284    @param source Pointer to the class which will implement this source.
285    */
286    virtual void registerPageSource(const String& typeName, TerrainPageSource* source);
287    /** Selects a given page source based on its type name.
288    @remarks
289        This method activates a single page source based on its typename,
290        e.g. "Heightmap". It also passes to it a number of custom options
291        which the source is able to interpret however it likes.
292    @param typeName The type name of the page source to activate
293    @param optionList A list of string parameters, which are expected to begin
294        with 'typeName.' (e.g. "Heightmap.image"), with their appropriate
295        values.
296    */
297    virtual void selectPageSource(const String& typeName,
298        TerrainPageSourceOptionList& optionList);
299
300    /** Attaches a previously built page to the list of available pages.
301    @remarks
302        TerrainPageSource subclasses will call this method once they have
303        pages available to be added to the working set. Note that whilst you
304        can build TerrainPage instances in another thread if you like, this
305        method must be called in the same thread as the main rendering loop
306        in order to avoid concurrency issues.
307    @param pageX, pageZ The page index at which to attach the page
308    @param page The page to attach
309    */
310    virtual void attachPage(ushort pageX, ushort pageZ, TerrainPage* page);
311    /// Get a pointer to the material being used for the terrain
312    MaterialPtr& getTerrainMaterial(void);
313    // Overridden from basic scene manager
314    void _renderScene(Camera *cam, Viewport *vp, bool includeOverlays);
315
316    /// Get the SceneNode under which all terrain nodes are attached.
317    SceneNode* getTerrainRootNode(void) const { return mTerrainRoot; }
318    /** Overridden from SceneManager */
319    void clearScene(void);
320        /** Overridden from SceneManager */
321        void setWorldGeometryRenderQueue(RenderQueueGroupID qid);
322
323        /// Shutdown cleanly before we get destroyed
324        void shutdown(void);
325
326
327protected:
328
329    /// Validates that the size picked for the terrain is acceptable
330    bool _checkSize( int s )
331    {
332        for ( int i = 0; i < 16; i++ )
333        {
334            printf( "Checking...%d\n", ( 1 << i ) + 1 );
335
336            if ( s == ( 1 << i ) + 1 )
337                return true;
338        }
339
340        return false;
341
342    }
343
344    /// The node to which all terrain tiles are attached
345    SceneNode * mTerrainRoot;
346    /// Terrain size, detail etc
347    static TerrainOptions mOptions;
348    /// Should we use an externally-defined custom material?
349    bool mUseCustomMaterial;
350    /// The name of the custom material to use
351    String mCustomMaterialName;
352    /// The name of the world texture
353    String mWorldTextureName;
354    /// The name of the detail texture
355    String mDetailTextureName;
356    /// Are we using a named parameter to hook up LOD morph?
357    bool mUseNamedParameterLodMorph;
358    /// The name of the parameter to send the LOD morph to
359    String mLodMorphParamName;
360    /// The index of the parameter to send the LOD morph to
361    size_t mLodMorphParamIndex;
362    /// Whether paging is enabled, or whether a single page will be used
363    bool mPagingEnabled;
364    /// The number of pages to render outside the 'home' page
365    unsigned short mLivePageMargin;
366    /// The number of pages to keep loaded outside the 'home' page
367    unsigned short mBufferedPageMargin;
368    /// Grid of buffered pages
369    TerrainPage2D mTerrainPages;
370   
371    /// Internal method for loading configurations settings
372    void loadConfig(const String& filename);
373
374    /// Sets up the terrain material
375    void setupTerrainMaterial(void);
376    /// Sets up the terrain page slots
377    void setupTerrainPages(void);
378
379
380    /// Map of source type -> TerrainPageSource
381    PageSourceMap mPageSources;
382    /// The currently active page source
383    TerrainPageSource* mActivePageSource;
384
385};
386
387}
388
389#endif
Note: See TracBrowser for help on using the repository browser.