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 | terrainscenemanager.cpp - 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 | #include <OgreTerrainSceneManager.h>
|
---|
36 | #include <OgreImage.h>
|
---|
37 | #include <OgreConfigFile.h>
|
---|
38 | #include <OgreMaterial.h>
|
---|
39 | #include <OgreTechnique.h>
|
---|
40 | #include <OgrePass.h>
|
---|
41 | #include <OgreCamera.h>
|
---|
42 | #include "OgreException.h"
|
---|
43 | #include "OgreStringConverter.h"
|
---|
44 | #include "OgreRenderSystem.h"
|
---|
45 | #include "OgreRenderSystemCapabilities.h"
|
---|
46 | #include "OgreGpuProgram.h"
|
---|
47 | #include "OgreGpuProgramManager.h"
|
---|
48 | #include "OgreTerrainVertexProgram.h"
|
---|
49 | #include "OgreTerrainPage.h"
|
---|
50 | #include "OgreLogManager.h"
|
---|
51 |
|
---|
52 | #define TERRAIN_MATERIAL_NAME "TerrainSceneManager/Terrain"
|
---|
53 |
|
---|
54 | namespace Ogre
|
---|
55 | {
|
---|
56 | //-------------------------------------------------------------------------
|
---|
57 | TerrainOptions TerrainSceneManager::mOptions;
|
---|
58 | //-------------------------------------------------------------------------
|
---|
59 | //-------------------------------------------------------------------------
|
---|
60 | TerrainSceneManager::TerrainSceneManager() : OctreeSceneManager( )
|
---|
61 | {
|
---|
62 | //setDisplaySceneNodes( true );
|
---|
63 | //setShowBoxes( true );
|
---|
64 |
|
---|
65 | mUseCustomMaterial = false;
|
---|
66 | mUseNamedParameterLodMorph = false;
|
---|
67 | mLodMorphParamIndex = 3;
|
---|
68 | mTerrainRoot = 0;
|
---|
69 | mActivePageSource = 0;
|
---|
70 | mPagingEnabled = false;
|
---|
71 | mLivePageMargin = 0;
|
---|
72 | mBufferedPageMargin = 0;
|
---|
73 | // Construct listener manager singleton
|
---|
74 | new TerrainPageSourceListenerManager();
|
---|
75 |
|
---|
76 |
|
---|
77 |
|
---|
78 | }
|
---|
79 | //-------------------------------------------------------------------------
|
---|
80 | void TerrainSceneManager::shutdown(void)
|
---|
81 | {
|
---|
82 | // Make sure the indexes are destroyed during orderly shutdown
|
---|
83 | // and not when statics are destroyed (may be too late)
|
---|
84 | TerrainRenderable::_getIndexCache().shutdown();
|
---|
85 |
|
---|
86 | // Make sure we free up material (static)
|
---|
87 | mOptions.terrainMaterial.setNull();
|
---|
88 |
|
---|
89 | // destroy listener manager
|
---|
90 | delete TerrainPageSourceListenerManager::getSingletonPtr();
|
---|
91 | }
|
---|
92 | //-------------------------------------------------------------------------
|
---|
93 | TerrainSceneManager::~TerrainSceneManager()
|
---|
94 | {
|
---|
95 | }
|
---|
96 | //-------------------------------------------------------------------------
|
---|
97 | void TerrainSceneManager::loadConfig(const String& filename)
|
---|
98 | {
|
---|
99 | /* Set up the options */
|
---|
100 | ConfigFile config;
|
---|
101 | String val;
|
---|
102 |
|
---|
103 | config.load( filename );
|
---|
104 |
|
---|
105 | val = config.getSetting( "DetailTile" );
|
---|
106 | if ( !val.empty() )
|
---|
107 | setDetailTextureRepeat(atoi(val.c_str()));
|
---|
108 |
|
---|
109 | val = config.getSetting( "MaxMipMapLevel" );
|
---|
110 | if ( !val.empty() )
|
---|
111 | setMaxGeoMipMapLevel(atoi( val.c_str() ));
|
---|
112 |
|
---|
113 |
|
---|
114 | val = config.getSetting( "PageSize" );
|
---|
115 | if ( !val.empty() )
|
---|
116 | setPageSize(atoi( val.c_str() ));
|
---|
117 | else
|
---|
118 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Missing option 'PageSize'",
|
---|
119 | "TerrainSceneManager::loadConfig");
|
---|
120 |
|
---|
121 |
|
---|
122 | val = config.getSetting( "TileSize" );
|
---|
123 | if ( !val.empty() )
|
---|
124 | setTileSize(atoi( val.c_str() ));
|
---|
125 | else
|
---|
126 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Missing option 'TileSize'",
|
---|
127 | "TerrainSceneManager::loadConfig");
|
---|
128 |
|
---|
129 | Vector3 v = Vector3::UNIT_SCALE;
|
---|
130 |
|
---|
131 | val = config.getSetting( "PageWorldX" );
|
---|
132 | if ( !val.empty() )
|
---|
133 | v.x = atof( val.c_str() );
|
---|
134 |
|
---|
135 | val = config.getSetting( "MaxHeight" );
|
---|
136 | if ( !val.empty() )
|
---|
137 | v.y = atof( val.c_str() );
|
---|
138 |
|
---|
139 | val = config.getSetting( "PageWorldZ" );
|
---|
140 | if ( !val.empty() )
|
---|
141 | v.z = atof( val.c_str() );
|
---|
142 |
|
---|
143 | // Scale x/z relative to pagesize
|
---|
144 | v.x /= mOptions.pageSize;
|
---|
145 | v.z /= mOptions.pageSize;
|
---|
146 | setScale(v);
|
---|
147 |
|
---|
148 | val = config.getSetting( "MaxPixelError" );
|
---|
149 | if ( !val.empty() )
|
---|
150 | setMaxPixelError(atoi( val.c_str() ));
|
---|
151 |
|
---|
152 | mDetailTextureName = config.getSetting( "DetailTexture" );
|
---|
153 |
|
---|
154 | mWorldTextureName = config.getSetting( "WorldTexture" );
|
---|
155 |
|
---|
156 | if ( config.getSetting( "VertexColours" ) == "yes" )
|
---|
157 | mOptions.coloured = true;
|
---|
158 |
|
---|
159 | if ( config.getSetting( "VertexNormals" ) == "yes" )
|
---|
160 | mOptions.lit = true;
|
---|
161 |
|
---|
162 | if ( config.getSetting( "UseTriStrips" ) == "yes" )
|
---|
163 | setUseTriStrips(true);
|
---|
164 |
|
---|
165 | if ( config.getSetting( "VertexProgramMorph" ) == "yes" )
|
---|
166 | setUseLODMorph(true);
|
---|
167 |
|
---|
168 | val = config.getSetting( "LODMorphStart");
|
---|
169 | if ( !val.empty() )
|
---|
170 | setLODMorphStart(atof(val.c_str()));
|
---|
171 |
|
---|
172 | val = config.getSetting( "CustomMaterialName" );
|
---|
173 | if ( !val.empty() )
|
---|
174 | setCustomMaterial(val);
|
---|
175 |
|
---|
176 | val = config.getSetting( "MorphLODFactorParamName" );
|
---|
177 | if ( !val.empty() )
|
---|
178 | setCustomMaterialMorphFactorParam(val);
|
---|
179 |
|
---|
180 | val = config.getSetting( "MorphLODFactorParamIndex" );
|
---|
181 | if ( !val.empty() )
|
---|
182 | setCustomMaterialMorphFactorParam(atoi(val.c_str()));
|
---|
183 |
|
---|
184 | // Now scan through the remaining settings, looking for any PageSource
|
---|
185 | // prefixed items
|
---|
186 | String pageSourceName = config.getSetting("PageSource");
|
---|
187 | if (pageSourceName == "")
|
---|
188 | {
|
---|
189 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Missing option 'PageSource'",
|
---|
190 | "TerrainSceneManager::loadConfig");
|
---|
191 | }
|
---|
192 | TerrainPageSourceOptionList optlist;
|
---|
193 | ConfigFile::SettingsIterator setIt = config.getSettingsIterator();
|
---|
194 | while (setIt.hasMoreElements())
|
---|
195 | {
|
---|
196 | String name = setIt.peekNextKey();
|
---|
197 | String value = setIt.getNext();
|
---|
198 | if (StringUtil::startsWith(name, pageSourceName, false))
|
---|
199 | {
|
---|
200 | optlist.push_back(TerrainPageSourceOption(name, value));
|
---|
201 | }
|
---|
202 | }
|
---|
203 | // set the page source
|
---|
204 | selectPageSource(pageSourceName, optlist);
|
---|
205 |
|
---|
206 |
|
---|
207 | }
|
---|
208 | //-------------------------------------------------------------------------
|
---|
209 | /*
|
---|
210 | void TerrainSceneManager::loadHeightmap(void)
|
---|
211 | {
|
---|
212 | Image image;
|
---|
213 |
|
---|
214 | image.load( mHeightmapName );
|
---|
215 |
|
---|
216 | //check to make sure it's 2^n + 1 size.
|
---|
217 | if ( image.getWidth() != image.getHeight() ||
|
---|
218 | ! _checkSize( image.getWidth() ) )
|
---|
219 | {
|
---|
220 | String err = "Error: Invalid heightmap size : " +
|
---|
221 | StringConverter::toString( image.getWidth() ) +
|
---|
222 | "," + StringConverter::toString( image.getHeight() ) +
|
---|
223 | ". Should be 2^n+1, 2^n+1";
|
---|
224 | OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS, err, "TerrainSceneManager::loadHeightmap" );
|
---|
225 | }
|
---|
226 |
|
---|
227 | int upperRange = 0;
|
---|
228 | int size = image.getWidth();
|
---|
229 |
|
---|
230 | if ( image.getFormat() == PF_L8 )
|
---|
231 | {
|
---|
232 | upperRange = 255;
|
---|
233 |
|
---|
234 | // Parse the char data into floats
|
---|
235 | mOptions.heightData = new Real[size*size];
|
---|
236 | const uchar* pSrc = image. getData();
|
---|
237 | Real* pDest = mOptions.heightData;
|
---|
238 | for (int i = 0; i < size*size; ++i)
|
---|
239 | {
|
---|
240 | *pDest++ = *pSrc++ * mOptions.scale.y;
|
---|
241 | }
|
---|
242 | }
|
---|
243 | else
|
---|
244 | {
|
---|
245 | OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS, "Error: Image is not a grayscale image.",
|
---|
246 | "TerrainSceneManager::setWorldGeometry" );
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | // set up the octree size.
|
---|
251 | float max_x = mOptions.scale.x * size;
|
---|
252 |
|
---|
253 | float max_y = upperRange * mOptions.scale.y;
|
---|
254 |
|
---|
255 | float max_z = mOptions.scale.z * size;
|
---|
256 |
|
---|
257 | resize( AxisAlignedBox( 0, 0, 0, max_x, max_y, max_z ) );
|
---|
258 |
|
---|
259 | mOptions.pageSize = size;
|
---|
260 |
|
---|
261 | }
|
---|
262 | */
|
---|
263 | //-------------------------------------------------------------------------
|
---|
264 | void TerrainSceneManager::setupTerrainMaterial(void)
|
---|
265 | {
|
---|
266 | if (mCustomMaterialName == "")
|
---|
267 | {
|
---|
268 | // define our own material
|
---|
269 | mOptions.terrainMaterial =
|
---|
270 | MaterialManager::getSingleton().getByName(TERRAIN_MATERIAL_NAME);
|
---|
271 | if (mOptions.terrainMaterial.isNull())
|
---|
272 | {
|
---|
273 | mOptions.terrainMaterial = MaterialManager::getSingleton().create(
|
---|
274 | "TerrainSceneManager/Terrain",
|
---|
275 | ResourceGroupManager::getSingleton().getWorldResourceGroupName());
|
---|
276 |
|
---|
277 | }
|
---|
278 | else
|
---|
279 | {
|
---|
280 | mOptions.terrainMaterial->getTechnique(0)->getPass(0)->removeAllTextureUnitStates();
|
---|
281 | }
|
---|
282 |
|
---|
283 | Pass* pass = mOptions.terrainMaterial->getTechnique(0)->getPass(0);
|
---|
284 |
|
---|
285 | if ( mWorldTextureName != "" )
|
---|
286 | {
|
---|
287 | pass->createTextureUnitState( mWorldTextureName, 0 );
|
---|
288 | }
|
---|
289 | if ( mDetailTextureName != "" )
|
---|
290 | {
|
---|
291 | pass->createTextureUnitState( mDetailTextureName, 1 );
|
---|
292 | }
|
---|
293 |
|
---|
294 | mOptions.terrainMaterial -> setLightingEnabled( mOptions.lit );
|
---|
295 |
|
---|
296 | if (mOptions.lodMorph &&
|
---|
297 | mDestRenderSystem->getCapabilities()->hasCapability(RSC_VERTEX_PROGRAM) &&
|
---|
298 | GpuProgramManager::getSingleton().getByName("Terrain/VertexMorph").isNull())
|
---|
299 | {
|
---|
300 | // Create & assign LOD morphing vertex program
|
---|
301 | String syntax;
|
---|
302 | if (GpuProgramManager::getSingleton().isSyntaxSupported("arbvp1"))
|
---|
303 | {
|
---|
304 | syntax = "arbvp1";
|
---|
305 | }
|
---|
306 | else
|
---|
307 | {
|
---|
308 | syntax = "vs_1_1";
|
---|
309 | }
|
---|
310 |
|
---|
311 | // Get source, and take into account current fog mode
|
---|
312 | FogMode fm = getFogMode();
|
---|
313 | const String& source = TerrainVertexProgram::getProgramSource(
|
---|
314 | fm, syntax);
|
---|
315 |
|
---|
316 | GpuProgramPtr prog = GpuProgramManager::getSingleton().createProgramFromString(
|
---|
317 | "Terrain/VertexMorph", ResourceGroupManager::getSingleton().getWorldResourceGroupName(),
|
---|
318 | source, GPT_VERTEX_PROGRAM, syntax);
|
---|
319 |
|
---|
320 | // Attach
|
---|
321 | pass->setVertexProgram("Terrain/VertexMorph");
|
---|
322 |
|
---|
323 | // Get params
|
---|
324 | GpuProgramParametersSharedPtr params = pass->getVertexProgramParameters();
|
---|
325 |
|
---|
326 | // worldviewproj
|
---|
327 | params->setAutoConstant(0, GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
|
---|
328 | // morph factor
|
---|
329 | params->setAutoConstant(4, GpuProgramParameters::ACT_CUSTOM, MORPH_CUSTOM_PARAM_ID);
|
---|
330 | // fog exp density(if relevant)
|
---|
331 | if (fm == FOG_EXP || fm == FOG_EXP2)
|
---|
332 | {
|
---|
333 | params->setConstant(5, Vector3(getFogDensity(), 0, 0));
|
---|
334 | // Override scene fog since otherwise it's applied twice
|
---|
335 | // Set to linear and we derive [0,1] fog value in the shader
|
---|
336 | pass->setFog(true, FOG_LINEAR, getFogColour(), 0, 1, 0);
|
---|
337 | }
|
---|
338 |
|
---|
339 | // Also set shadow receiver program
|
---|
340 | const String& source2 = TerrainVertexProgram::getProgramSource(
|
---|
341 | fm, syntax, true);
|
---|
342 |
|
---|
343 | prog = GpuProgramManager::getSingleton().createProgramFromString(
|
---|
344 | "Terrain/VertexMorphShadowReceive",
|
---|
345 | ResourceGroupManager::getSingleton().getWorldResourceGroupName(),
|
---|
346 | source2, GPT_VERTEX_PROGRAM, syntax);
|
---|
347 | pass->setShadowReceiverVertexProgram("Terrain/VertexMorphShadowReceive");
|
---|
348 | params = pass->getShadowReceiverVertexProgramParameters();
|
---|
349 | // worldviewproj
|
---|
350 | params->setAutoConstant(0, GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
|
---|
351 | // world
|
---|
352 | params->setAutoConstant(4, GpuProgramParameters::ACT_WORLD_MATRIX);
|
---|
353 | // texture view / proj
|
---|
354 | params->setAutoConstant(8, GpuProgramParameters::ACT_TEXTURE_VIEWPROJ_MATRIX);
|
---|
355 | // morph factor
|
---|
356 | params->setAutoConstant(12, GpuProgramParameters::ACT_CUSTOM, MORPH_CUSTOM_PARAM_ID);
|
---|
357 |
|
---|
358 |
|
---|
359 | // Set param index
|
---|
360 | mLodMorphParamName = "";
|
---|
361 | mLodMorphParamIndex = 4;
|
---|
362 | }
|
---|
363 |
|
---|
364 | mOptions.terrainMaterial->load();
|
---|
365 |
|
---|
366 | }
|
---|
367 | else
|
---|
368 | {
|
---|
369 | // Custom material
|
---|
370 | mOptions.terrainMaterial =
|
---|
371 | MaterialManager::getSingleton().getByName(mCustomMaterialName);
|
---|
372 | mOptions.terrainMaterial->load();
|
---|
373 |
|
---|
374 | }
|
---|
375 |
|
---|
376 | // now set up the linkage between vertex program and LOD morph param
|
---|
377 | if (mOptions.lodMorph)
|
---|
378 | {
|
---|
379 | Technique* t = mOptions.terrainMaterial->getBestTechnique();
|
---|
380 | for (ushort i = 0; i < t->getNumPasses(); ++i)
|
---|
381 | {
|
---|
382 | Pass* p = t->getPass(i);
|
---|
383 | if (p->hasVertexProgram())
|
---|
384 | {
|
---|
385 | // we have to assume vertex program includes LOD morph capability
|
---|
386 | GpuProgramParametersSharedPtr params =
|
---|
387 | p->getVertexProgramParameters();
|
---|
388 | // Check to see if custom param is already there
|
---|
389 | GpuProgramParameters::AutoConstantIterator aci = params->getAutoConstantIterator();
|
---|
390 | bool found = false;
|
---|
391 | while (aci.hasMoreElements())
|
---|
392 | {
|
---|
393 | const GpuProgramParameters::AutoConstantEntry& ace = aci.getNext();
|
---|
394 | if (ace.paramType == GpuProgramParameters::ACT_CUSTOM &&
|
---|
395 | ace.data == MORPH_CUSTOM_PARAM_ID)
|
---|
396 | {
|
---|
397 | found = true;
|
---|
398 | }
|
---|
399 | }
|
---|
400 | if (!found)
|
---|
401 | {
|
---|
402 | if(mLodMorphParamName != "")
|
---|
403 | {
|
---|
404 | params->setNamedAutoConstant(mLodMorphParamName,
|
---|
405 | GpuProgramParameters::ACT_CUSTOM, MORPH_CUSTOM_PARAM_ID);
|
---|
406 | }
|
---|
407 | else
|
---|
408 | {
|
---|
409 | params->setAutoConstant(mLodMorphParamIndex,
|
---|
410 | GpuProgramParameters::ACT_CUSTOM, MORPH_CUSTOM_PARAM_ID);
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | }
|
---|
415 | }
|
---|
416 | }
|
---|
417 |
|
---|
418 | }
|
---|
419 | //-------------------------------------------------------------------------
|
---|
420 | void TerrainSceneManager::setupTerrainPages(void)
|
---|
421 | {
|
---|
422 |
|
---|
423 | //create a root terrain node.
|
---|
424 | if (!mTerrainRoot)
|
---|
425 | mTerrainRoot = getRootSceneNode() -> createChildSceneNode( "Terrain" );
|
---|
426 |
|
---|
427 | //setup the page array.
|
---|
428 | unsigned short pageSlots = 1 + (mBufferedPageMargin * 2);
|
---|
429 | unsigned short i, j;
|
---|
430 | for (i = 0; i < pageSlots; ++i)
|
---|
431 | {
|
---|
432 | mTerrainPages.push_back(TerrainPageRow());
|
---|
433 | for (j = 0; j < pageSlots; ++j)
|
---|
434 | {
|
---|
435 | mTerrainPages[i].push_back(0);
|
---|
436 | }
|
---|
437 | }
|
---|
438 |
|
---|
439 | // If we're not paging, load immediate for convenience
|
---|
440 | if ( mActivePageSource && !mPagingEnabled )
|
---|
441 | mActivePageSource->requestPage(0,0);
|
---|
442 |
|
---|
443 |
|
---|
444 | }
|
---|
445 | //-------------------------------------------------------------------------
|
---|
446 | void TerrainSceneManager::setWorldGeometry( const String& filename )
|
---|
447 | {
|
---|
448 | // Clear out any existing world resources (if not default)
|
---|
449 | if (ResourceGroupManager::getSingleton().getWorldResourceGroupName() !=
|
---|
450 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME)
|
---|
451 | {
|
---|
452 | ResourceGroupManager::getSingleton().clearResourceGroup(
|
---|
453 | ResourceGroupManager::getSingleton().getWorldResourceGroupName());
|
---|
454 | }
|
---|
455 | mTerrainPages.clear();
|
---|
456 | // Load the configuration
|
---|
457 | loadConfig(filename);
|
---|
458 |
|
---|
459 | // Resize the octree, allow for 1 page for now
|
---|
460 | float max_x = mOptions.scale.x * mOptions.pageSize;
|
---|
461 | float max_y = mOptions.scale.y;
|
---|
462 | float max_z = mOptions.scale.z * mOptions.pageSize;
|
---|
463 | resize( AxisAlignedBox( 0, 0, 0, max_x, max_y, max_z ) );
|
---|
464 |
|
---|
465 | setupTerrainMaterial();
|
---|
466 |
|
---|
467 | setupTerrainPages();
|
---|
468 |
|
---|
469 | }
|
---|
470 | //-------------------------------------------------------------------------
|
---|
471 | void TerrainSceneManager::clearScene(void)
|
---|
472 | {
|
---|
473 | OctreeSceneManager::clearScene();
|
---|
474 | mTerrainPages.clear();
|
---|
475 | // Octree has destroyed our root
|
---|
476 | mTerrainRoot = 0;
|
---|
477 | }
|
---|
478 | //-------------------------------------------------------------------------
|
---|
479 | void TerrainSceneManager::_renderScene(Camera* cam, Viewport *vp, bool includeOverlays)
|
---|
480 | {
|
---|
481 | // For now, no paging and expect immediate response
|
---|
482 | if (!mTerrainPages.empty() && mTerrainPages[0][0] == 0)
|
---|
483 | {
|
---|
484 | mActivePageSource->requestPage(0, 0);
|
---|
485 | }
|
---|
486 | SceneManager::_renderScene(cam, vp, includeOverlays);
|
---|
487 |
|
---|
488 | }
|
---|
489 | //-------------------------------------------------------------------------
|
---|
490 | void TerrainSceneManager::attachPage(ushort pageX, ushort pageZ, TerrainPage* page)
|
---|
491 | {
|
---|
492 | assert(pageX == 0 && pageZ == 0 && "Multiple pages not yet supported");
|
---|
493 |
|
---|
494 | assert(mTerrainPages[pageX][pageZ] == 0 && "Page at that index not yet expired!");
|
---|
495 | // Insert page into list
|
---|
496 | mTerrainPages[pageX][pageZ] = page;
|
---|
497 | // Attach page to terrain root
|
---|
498 | mTerrainRoot->addChild(page->pageSceneNode);
|
---|
499 |
|
---|
500 | }
|
---|
501 | //-------------------------------------------------------------------------
|
---|
502 | void TerrainSceneManager::_renderVisibleObjects( void )
|
---|
503 | {
|
---|
504 |
|
---|
505 | mDestRenderSystem -> setLightingEnabled( false );
|
---|
506 |
|
---|
507 | OctreeSceneManager::_renderVisibleObjects();
|
---|
508 |
|
---|
509 | }
|
---|
510 | //-------------------------------------------------------------------------
|
---|
511 | float TerrainSceneManager::getHeightAt( float x, float z )
|
---|
512 | {
|
---|
513 |
|
---|
514 |
|
---|
515 | Vector3 pt( x, 0, z );
|
---|
516 |
|
---|
517 | TerrainRenderable * t = getTerrainTile( pt );
|
---|
518 |
|
---|
519 | if ( t == 0 )
|
---|
520 | {
|
---|
521 | // printf( "No tile found for point\n" );
|
---|
522 | return -1;
|
---|
523 | }
|
---|
524 |
|
---|
525 | float h = t -> getHeightAt( x, z );
|
---|
526 |
|
---|
527 | // printf( "Height is %f\n", h );
|
---|
528 | return h;
|
---|
529 |
|
---|
530 | }
|
---|
531 | //-------------------------------------------------------------------------
|
---|
532 | TerrainPage* TerrainSceneManager::getTerrainPage( const Vector3 & pt )
|
---|
533 | {
|
---|
534 | if (mPagingEnabled)
|
---|
535 | {
|
---|
536 | // TODO
|
---|
537 | return 0;
|
---|
538 | }
|
---|
539 | else
|
---|
540 | {
|
---|
541 | // Single page
|
---|
542 | if (mTerrainPages.empty() || mTerrainPages[0].empty())
|
---|
543 | return 0;
|
---|
544 | return mTerrainPages[0][0];
|
---|
545 | }
|
---|
546 | }
|
---|
547 | //-------------------------------------------------------------------------
|
---|
548 | TerrainRenderable * TerrainSceneManager::getTerrainTile( const Vector3 & pt )
|
---|
549 | {
|
---|
550 | TerrainPage* tp = getTerrainPage(pt);
|
---|
551 | if (!tp)
|
---|
552 | return NULL;
|
---|
553 | else
|
---|
554 | return tp->getTerrainTile(pt);
|
---|
555 | }
|
---|
556 | //-------------------------------------------------------------------------
|
---|
557 | bool TerrainSceneManager::intersectSegment( const Vector3 & start,
|
---|
558 | const Vector3 & end, Vector3 * result )
|
---|
559 | {
|
---|
560 | TerrainRenderable * t = getTerrainTile( start );
|
---|
561 |
|
---|
562 | if ( t == 0 )
|
---|
563 | {
|
---|
564 | *result = Vector3( -1, -1, -1 );
|
---|
565 | return false;
|
---|
566 | }
|
---|
567 |
|
---|
568 | return t -> intersectSegment( start, end, result );
|
---|
569 | }
|
---|
570 | //-------------------------------------------------------------------------
|
---|
571 | void TerrainSceneManager::setUseTriStrips(bool useStrips)
|
---|
572 | {
|
---|
573 | mOptions.useTriStrips = useStrips;
|
---|
574 | }
|
---|
575 | //-------------------------------------------------------------------------
|
---|
576 | void TerrainSceneManager::setUseLODMorph(bool morph)
|
---|
577 | {
|
---|
578 | // Set true only if vertex programs are supported
|
---|
579 | mOptions.lodMorph = morph &&
|
---|
580 | mDestRenderSystem->getCapabilities()->hasCapability(RSC_VERTEX_PROGRAM);
|
---|
581 | }
|
---|
582 | //-------------------------------------------------------------------------
|
---|
583 | void TerrainSceneManager::setUseVertexNormals(bool useNormals)
|
---|
584 | {
|
---|
585 | mOptions.lit = useNormals;
|
---|
586 | }
|
---|
587 | //-------------------------------------------------------------------------
|
---|
588 | void TerrainSceneManager::setUseVertexColours(bool useColours)
|
---|
589 | {
|
---|
590 | mOptions.coloured = useColours;
|
---|
591 | }
|
---|
592 | //-------------------------------------------------------------------------
|
---|
593 | void TerrainSceneManager::setWorldTexture(const String& textureName)
|
---|
594 | {
|
---|
595 | mWorldTextureName = textureName;
|
---|
596 | }
|
---|
597 | //-------------------------------------------------------------------------
|
---|
598 | void TerrainSceneManager::setDetailTexture(const String& textureName)
|
---|
599 | {
|
---|
600 | mDetailTextureName = textureName;
|
---|
601 |
|
---|
602 | }
|
---|
603 | //-------------------------------------------------------------------------
|
---|
604 | void TerrainSceneManager::setDetailTextureRepeat(int repeat)
|
---|
605 | {
|
---|
606 | mOptions.detailTile = repeat;
|
---|
607 | }
|
---|
608 | //-------------------------------------------------------------------------
|
---|
609 | void TerrainSceneManager::setTileSize(int size)
|
---|
610 | {
|
---|
611 | mOptions.tileSize = size;
|
---|
612 | }
|
---|
613 | //-------------------------------------------------------------------------
|
---|
614 | void TerrainSceneManager::setPageSize(int size)
|
---|
615 | {
|
---|
616 | mOptions.pageSize = size;
|
---|
617 | }
|
---|
618 | //-------------------------------------------------------------------------
|
---|
619 | void TerrainSceneManager::setMaxPixelError(int pixelError)
|
---|
620 | {
|
---|
621 | mOptions.maxPixelError = pixelError;
|
---|
622 | }
|
---|
623 | //-------------------------------------------------------------------------
|
---|
624 | void TerrainSceneManager::setScale(const Vector3& scale)
|
---|
625 | {
|
---|
626 | mOptions.scale = scale;
|
---|
627 | }
|
---|
628 | //-------------------------------------------------------------------------
|
---|
629 | void TerrainSceneManager::setMaxGeoMipMapLevel(int maxMip)
|
---|
630 | {
|
---|
631 | mOptions.maxGeoMipMapLevel = maxMip;
|
---|
632 | }
|
---|
633 | //-------------------------------------------------------------------------
|
---|
634 | void TerrainSceneManager::setCustomMaterial(const String& materialName)
|
---|
635 | {
|
---|
636 | mCustomMaterialName = materialName;
|
---|
637 | if (materialName != "")
|
---|
638 | mUseCustomMaterial = true;
|
---|
639 | else
|
---|
640 | mUseCustomMaterial = false;
|
---|
641 | }
|
---|
642 | //-------------------------------------------------------------------------
|
---|
643 | void TerrainSceneManager::setCustomMaterialMorphFactorParam(const String& paramName)
|
---|
644 | {
|
---|
645 | mUseNamedParameterLodMorph = true;
|
---|
646 | mLodMorphParamName = paramName;
|
---|
647 |
|
---|
648 | }
|
---|
649 | //-------------------------------------------------------------------------
|
---|
650 | void TerrainSceneManager::setCustomMaterialMorphFactorParam(size_t paramIndex)
|
---|
651 | {
|
---|
652 | mUseNamedParameterLodMorph = false;
|
---|
653 | mLodMorphParamIndex = paramIndex;
|
---|
654 | }
|
---|
655 | //-------------------------------------------------------------------------
|
---|
656 | void TerrainSceneManager::setLODMorphStart(Real morphStart)
|
---|
657 | {
|
---|
658 | mOptions.lodMorphStart = morphStart;
|
---|
659 | }
|
---|
660 | //-------------------------------------------------------------------------
|
---|
661 | Camera* TerrainSceneManager::createCamera( const String &name )
|
---|
662 | {
|
---|
663 | Camera* c = OctreeSceneManager::createCamera(name);
|
---|
664 |
|
---|
665 | // Set primary camera, if none
|
---|
666 | if (!mOptions.primaryCamera)
|
---|
667 | setPrimaryCamera(c);
|
---|
668 |
|
---|
669 | return c;
|
---|
670 |
|
---|
671 | }
|
---|
672 | //-------------------------------------------------------------------------
|
---|
673 | void TerrainSceneManager::setPrimaryCamera(const Camera* cam)
|
---|
674 | {
|
---|
675 | mOptions.primaryCamera = cam;
|
---|
676 | }
|
---|
677 | //-------------------------------------------------------------------------
|
---|
678 | bool TerrainSceneManager::setOption( const String & name, const void *value )
|
---|
679 | {
|
---|
680 | if (name == "PageSize")
|
---|
681 | {
|
---|
682 | setPageSize(*static_cast<const int*>(value));
|
---|
683 | return true;
|
---|
684 | }
|
---|
685 | else if (name == "TileSize")
|
---|
686 | {
|
---|
687 | setTileSize(*static_cast<const int*>(value));
|
---|
688 | return true;
|
---|
689 | }
|
---|
690 | else if (name == "PrimaryCamera")
|
---|
691 | {
|
---|
692 | setPrimaryCamera(static_cast<const Camera*>(value));
|
---|
693 | return true;
|
---|
694 | }
|
---|
695 | else if (name == "MaxMipMapLevel")
|
---|
696 | {
|
---|
697 | setMaxGeoMipMapLevel(*static_cast<const int*>(value));
|
---|
698 | return true;
|
---|
699 | }
|
---|
700 | else if (name == "Scale")
|
---|
701 | {
|
---|
702 | setScale(*static_cast<const Vector3*>(value));
|
---|
703 | return true;
|
---|
704 | }
|
---|
705 | else if (name == "MaxPixelError")
|
---|
706 | {
|
---|
707 | setMaxPixelError(*static_cast<const int*>(value));
|
---|
708 | return true;
|
---|
709 | }
|
---|
710 | else if (name == "UseTriStrips")
|
---|
711 | {
|
---|
712 | setUseTriStrips(*static_cast<const bool*>(value));
|
---|
713 | return true;
|
---|
714 | }
|
---|
715 | else if (name == "VertexProgramMorph")
|
---|
716 | {
|
---|
717 | setUseLODMorph(*static_cast<const bool*>(value));
|
---|
718 | return true;
|
---|
719 | }
|
---|
720 | else if (name == "DetailTile")
|
---|
721 | {
|
---|
722 | setDetailTextureRepeat(*static_cast<const int*>(value));
|
---|
723 | return true;
|
---|
724 | }
|
---|
725 | else if (name == "LodMorphStart")
|
---|
726 | {
|
---|
727 | setLODMorphStart(*static_cast<const Real*>(value));
|
---|
728 | return true;
|
---|
729 | }
|
---|
730 | else if (name == "VertexNormals")
|
---|
731 | {
|
---|
732 | setUseVertexNormals(*static_cast<const bool*>(value));
|
---|
733 | return true;
|
---|
734 | }
|
---|
735 | else if (name == "VertexColours")
|
---|
736 | {
|
---|
737 | setUseVertexColours(*static_cast<const bool*>(value));
|
---|
738 | return true;
|
---|
739 | }
|
---|
740 | else if (name == "MorphLODFactorParamName")
|
---|
741 | {
|
---|
742 | setCustomMaterialMorphFactorParam(*static_cast<const String*>(value));
|
---|
743 | return true;
|
---|
744 | }
|
---|
745 | else if (name == "MorphLODFactorParamIndex")
|
---|
746 | {
|
---|
747 | setCustomMaterialMorphFactorParam(*static_cast<const size_t*>(value));
|
---|
748 | return true;
|
---|
749 | }
|
---|
750 | else if (name == "CustomMaterialName")
|
---|
751 | {
|
---|
752 | setCustomMaterial(*static_cast<const String*>(value));
|
---|
753 | return true;
|
---|
754 | }
|
---|
755 | else if (name == "WorldTexture")
|
---|
756 | {
|
---|
757 | setWorldTexture(*static_cast<const String*>(value));
|
---|
758 | return true;
|
---|
759 | }
|
---|
760 | else if (name == "DetailTexture")
|
---|
761 | {
|
---|
762 | setDetailTexture(*static_cast<const String*>(value));
|
---|
763 | return true;
|
---|
764 | }
|
---|
765 | else
|
---|
766 | {
|
---|
767 | return OctreeSceneManager::setOption(name, value);
|
---|
768 | }
|
---|
769 |
|
---|
770 | return false;
|
---|
771 | }
|
---|
772 | //-------------------------------------------------------------------------
|
---|
773 | void TerrainSceneManager::registerPageSource(const String& typeName,
|
---|
774 | TerrainPageSource* source)
|
---|
775 | {
|
---|
776 | std::pair<PageSourceMap::iterator, bool> retPair =
|
---|
777 | mPageSources.insert(
|
---|
778 | PageSourceMap::value_type(typeName, source));
|
---|
779 | if (!retPair.second)
|
---|
780 | {
|
---|
781 | OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM,
|
---|
782 | "The page source " + typeName + " is already registered",
|
---|
783 | "TerrainSceneManager::registerPageSource");
|
---|
784 | }
|
---|
785 | LogManager::getSingleton().logMessage(
|
---|
786 | "TerrainSceneManager: Registered a new PageSource for "
|
---|
787 | "type " + typeName);
|
---|
788 | }
|
---|
789 | //-------------------------------------------------------------------------
|
---|
790 | void TerrainSceneManager::selectPageSource(const String& typeName,
|
---|
791 | TerrainPageSourceOptionList& optionList)
|
---|
792 | {
|
---|
793 | PageSourceMap::iterator i = mPageSources.find(typeName);
|
---|
794 | if (i == mPageSources.end())
|
---|
795 | {
|
---|
796 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
|
---|
797 | "Cannot locate a TerrainPageSource for type " + typeName,
|
---|
798 | "TerrainSceneManager::selectPageSource");
|
---|
799 | }
|
---|
800 |
|
---|
801 | if (mActivePageSource)
|
---|
802 | {
|
---|
803 | mActivePageSource->shutdown();
|
---|
804 | }
|
---|
805 | mActivePageSource = i->second;
|
---|
806 | mActivePageSource->initialise(this, mOptions.tileSize, mOptions.pageSize,
|
---|
807 | mPagingEnabled, optionList);
|
---|
808 |
|
---|
809 | LogManager::getSingleton().logMessage(
|
---|
810 | "TerrainSceneManager: Activated PageSource " + typeName);
|
---|
811 |
|
---|
812 | }
|
---|
813 | //-------------------------------------------------------------------------
|
---|
814 | int TerrainSceneManager::getDetailTextureRepeat(void)
|
---|
815 | {
|
---|
816 | return mOptions.detailTile;
|
---|
817 | }
|
---|
818 | //-------------------------------------------------------------------------
|
---|
819 | int TerrainSceneManager::getTileSize(void)
|
---|
820 | {
|
---|
821 | return mOptions.tileSize;
|
---|
822 | }
|
---|
823 | //-------------------------------------------------------------------------
|
---|
824 | int TerrainSceneManager::getPageSize(void)
|
---|
825 | {
|
---|
826 | return mOptions.pageSize;
|
---|
827 | }
|
---|
828 | //-------------------------------------------------------------------------
|
---|
829 | int TerrainSceneManager::getMaxPixelError(void)
|
---|
830 | {
|
---|
831 | return mOptions.maxPixelError;
|
---|
832 | }
|
---|
833 | //-------------------------------------------------------------------------
|
---|
834 | const Vector3& TerrainSceneManager::getScale(void)
|
---|
835 | {
|
---|
836 | return mOptions.scale;
|
---|
837 | }
|
---|
838 | //-------------------------------------------------------------------------
|
---|
839 | int TerrainSceneManager::getMaxGeoMipMapLevel(void)
|
---|
840 | {
|
---|
841 | return mOptions.maxGeoMipMapLevel;
|
---|
842 | }
|
---|
843 | //-------------------------------------------------------------------------
|
---|
844 | //-------------------------------------------------------------------------
|
---|
845 | RaySceneQuery*
|
---|
846 | TerrainSceneManager::createRayQuery(const Ray& ray, unsigned long mask)
|
---|
847 | {
|
---|
848 | TerrainRaySceneQuery *trsq = new TerrainRaySceneQuery(this);
|
---|
849 | trsq->setRay(ray);
|
---|
850 | trsq->setQueryMask(mask);
|
---|
851 | return trsq;
|
---|
852 | }
|
---|
853 | //-------------------------------------------------------------------------
|
---|
854 | TerrainRaySceneQuery::TerrainRaySceneQuery(SceneManager* creator)
|
---|
855 | :OctreeRaySceneQuery(creator)
|
---|
856 | {
|
---|
857 | mSupportedWorldFragments.insert(SceneQuery::WFT_SINGLE_INTERSECTION);
|
---|
858 | }
|
---|
859 | //-------------------------------------------------------------------------
|
---|
860 | TerrainRaySceneQuery::~TerrainRaySceneQuery()
|
---|
861 | {
|
---|
862 | }
|
---|
863 | //-------------------------------------------------------------------------
|
---|
864 | void TerrainRaySceneQuery::execute(RaySceneQueryListener* listener)
|
---|
865 | {
|
---|
866 | static WorldFragment worldFrag;
|
---|
867 | worldFrag.fragmentType = SceneQuery::WFT_SINGLE_INTERSECTION;
|
---|
868 |
|
---|
869 | const Vector3& dir = mRay.getDirection();
|
---|
870 | const Vector3& origin = mRay.getOrigin();
|
---|
871 | // Straight up / down?
|
---|
872 | if (dir == Vector3::UNIT_Y || dir == Vector3::NEGATIVE_UNIT_Y)
|
---|
873 | {
|
---|
874 | Real height = static_cast<TerrainSceneManager*>(mParentSceneMgr)->getHeightAt(
|
---|
875 | origin.x, origin.z);
|
---|
876 | if (height != -1 && (height <= origin.y && dir.y < 0) || (height >= origin.y && dir.y > 0))
|
---|
877 | {
|
---|
878 | worldFrag.singleIntersection.x = origin.x;
|
---|
879 | worldFrag.singleIntersection.z = origin.z;
|
---|
880 | worldFrag.singleIntersection.y = height;
|
---|
881 | if (!listener->queryResult(&worldFrag,
|
---|
882 | (worldFrag.singleIntersection - origin).length()))
|
---|
883 | return;
|
---|
884 | }
|
---|
885 | }
|
---|
886 | else
|
---|
887 | {
|
---|
888 | // Perform arbitrary query
|
---|
889 | if (static_cast<TerrainSceneManager*>(mParentSceneMgr)->intersectSegment(
|
---|
890 | origin, origin + (dir * 100000), &worldFrag.singleIntersection))
|
---|
891 | {
|
---|
892 | if (!listener->queryResult(&worldFrag,
|
---|
893 | (worldFrag.singleIntersection - origin).length()))
|
---|
894 | return;
|
---|
895 | }
|
---|
896 |
|
---|
897 |
|
---|
898 | }
|
---|
899 | OctreeRaySceneQuery::execute(listener);
|
---|
900 |
|
---|
901 | }
|
---|
902 | //-------------------------------------------------------------------------
|
---|
903 | MaterialPtr& TerrainSceneManager::getTerrainMaterial(void)
|
---|
904 | {
|
---|
905 | return mOptions.terrainMaterial;
|
---|
906 | }
|
---|
907 | //-------------------------------------------------------------------------
|
---|
908 | TerrainSceneManager::PageSourceIterator TerrainSceneManager::getPageSourceIterator(void)
|
---|
909 | {
|
---|
910 | return PageSourceIterator(mPageSources.begin(), mPageSources.end());
|
---|
911 | }
|
---|
912 | //-------------------------------------------------------------------------
|
---|
913 | void TerrainSceneManager::setWorldGeometryRenderQueue(RenderQueueGroupID qid)
|
---|
914 | {
|
---|
915 | for (TerrainPage2D::iterator pi = mTerrainPages.begin();
|
---|
916 | pi != mTerrainPages.end(); ++pi)
|
---|
917 | {
|
---|
918 | TerrainPageRow& row = *pi;
|
---|
919 | for (TerrainPageRow::iterator ri = row.begin(); ri != row.end(); ++ri)
|
---|
920 | {
|
---|
921 | TerrainPage* page = *ri;
|
---|
922 | if (page)
|
---|
923 | {
|
---|
924 | page->setRenderQueue(qid);
|
---|
925 | }
|
---|
926 | }
|
---|
927 | }
|
---|
928 |
|
---|
929 | }
|
---|
930 |
|
---|
931 |
|
---|
932 | } //namespace
|
---|