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