1 | #include <OgreNoMemoryMacros.h>
|
---|
2 | #include <CEGUI/CEGUI.h>
|
---|
3 | #include <../CEGUIRenderer/include/OgreCEGUIRenderer.h>
|
---|
4 | #include <../CEGUIRenderer/include/OgreCEGUIResourceProvider.h>
|
---|
5 | #include <../CEGUIRenderer/include/OgreCEGUITexture.h>
|
---|
6 | #include <OgreMemoryMacros.h>
|
---|
7 | #include <OgreIteratorWrappers.h>
|
---|
8 | #include <Ogre.h>
|
---|
9 | #include "TestCullingTerrainApplication.h"
|
---|
10 | #include "TerrainFrameListener.h"
|
---|
11 |
|
---|
12 | #if COLLADA
|
---|
13 | #include "OgreColladaPrerequisites.h"
|
---|
14 | #include "OgreColladaDocument.h"
|
---|
15 | #include "OgreColladaScene.h"
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | #include "IVReader.h"
|
---|
19 |
|
---|
20 | #define WIN32_LEAN_AND_MEAN
|
---|
21 | #include <windows.h>
|
---|
22 |
|
---|
23 | const static bool USE_STATIC_GEOMETRY = false;
|
---|
24 | bool TestCullingTerrainApplication::msShowHillyTerrain = false;
|
---|
25 |
|
---|
26 |
|
---|
27 | //using namespace GtpVisibility;
|
---|
28 |
|
---|
29 | /*************************************************************/
|
---|
30 | /* EntityState implementation */
|
---|
31 | /*************************************************************/
|
---|
32 |
|
---|
33 |
|
---|
34 | Vector3 EntityState::msMinPos = Vector3::ZERO;
|
---|
35 | Vector3 EntityState::msMaxPos = Vector3::ZERO;
|
---|
36 |
|
---|
37 | EntityState::EntityState(Entity *ent, State entityState, Real speed):
|
---|
38 | mEntity(ent), mState(entityState), mAnimationSpeed(speed)
|
---|
39 | {
|
---|
40 | switch(entityState)
|
---|
41 | {
|
---|
42 | case MOVING:
|
---|
43 | mAnimationState = mEntity->getAnimationState("Walk");
|
---|
44 | break;
|
---|
45 | case WAITING:
|
---|
46 | mAnimationState = mEntity->getAnimationState("Idle");
|
---|
47 | break;
|
---|
48 | case STOP:
|
---|
49 | mAnimationState = NULL;
|
---|
50 | break;
|
---|
51 | default:
|
---|
52 | break;
|
---|
53 | }
|
---|
54 | // enable animation state
|
---|
55 | if (mAnimationState)
|
---|
56 | {
|
---|
57 | mAnimationState->setLoop(true);
|
---|
58 | mAnimationState->setEnabled(true);
|
---|
59 | }
|
---|
60 |
|
---|
61 | mTimeElapsed = Math::RangeRandom(1, 5);
|
---|
62 | }
|
---|
63 | //-----------------------------------------------------------------------
|
---|
64 | EntityState::~EntityState()
|
---|
65 | {
|
---|
66 | mAnimationState = NULL;
|
---|
67 | mEntity = NULL;
|
---|
68 | }
|
---|
69 | //-----------------------------------------------------------------------
|
---|
70 | Entity *EntityState::GetEntity()
|
---|
71 | {
|
---|
72 | return mEntity;
|
---|
73 | }
|
---|
74 | //-----------------------------------------------------------------------
|
---|
75 | EntityState::State EntityState::GetState()
|
---|
76 | {
|
---|
77 | return mState;
|
---|
78 | }
|
---|
79 | //-----------------------------------------------------------------------
|
---|
80 | void EntityState::update(Real timeSinceLastFrame)
|
---|
81 | {
|
---|
82 | mTimeElapsed -= timeSinceLastFrame * mAnimationSpeed;
|
---|
83 |
|
---|
84 | if (!mEntity || !mAnimationState)
|
---|
85 | return;
|
---|
86 |
|
---|
87 | if (mState == MOVING) // toggle between moving (longer) and waiting (short)
|
---|
88 | {
|
---|
89 | SceneNode *parent = mEntity->getParentSceneNode();
|
---|
90 |
|
---|
91 | if (!parent)
|
---|
92 | return;
|
---|
93 |
|
---|
94 | if (mTimeElapsed <= 0) // toggle animation state
|
---|
95 | {
|
---|
96 | if (mAnimationState->getAnimationName() == "Idle")
|
---|
97 | {
|
---|
98 | SetAnimationState("Walk", true);
|
---|
99 |
|
---|
100 | mTimeElapsed = walk_duration; // walk for mTimeElapsed units
|
---|
101 |
|
---|
102 | // choose random rotation
|
---|
103 | Radian rnd = Radian(Math::UnitRandom() * Math::PI * rotate_factor);
|
---|
104 |
|
---|
105 | //mEntity->getParentSceneNode()->rotate();
|
---|
106 | parent->yaw(rnd);
|
---|
107 | }
|
---|
108 | else
|
---|
109 | {
|
---|
110 | SetAnimationState("Idle", true);
|
---|
111 | mTimeElapsed = wait_duration; // wait for mTimeElapsed seconds
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (mAnimationState->getAnimationName() == "Walk") // move forward
|
---|
116 | {
|
---|
117 | // store old position, just in case we get out of bounds
|
---|
118 | Vector3 oldPos = parent->getPosition();
|
---|
119 | parent->translate(parent->getLocalAxes(), Vector3(move_factor * mAnimationSpeed, 0, 0));
|
---|
120 |
|
---|
121 | // HACK: if out of bounds => reset to old position and set animationstate to idle
|
---|
122 | if (OutOfBounds(parent))
|
---|
123 | {
|
---|
124 | parent->setPosition(oldPos);
|
---|
125 | SetAnimationState("Idle", true);
|
---|
126 |
|
---|
127 | mTimeElapsed = wait_duration;
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | // add time to drive animation
|
---|
133 | mAnimationState->addTime(timeSinceLastFrame * mAnimationSpeed);
|
---|
134 | }
|
---|
135 | //-----------------------------------------------------------------------
|
---|
136 | void EntityState::SetAnimationState(String stateStr, bool loop)
|
---|
137 | {
|
---|
138 | if (!mEntity)
|
---|
139 | return;
|
---|
140 |
|
---|
141 | mAnimationState = mEntity->getAnimationState(stateStr);
|
---|
142 | mAnimationState->setLoop(loop);
|
---|
143 | mAnimationState->setEnabled(true);
|
---|
144 | }
|
---|
145 | //-----------------------------------------------------------------------
|
---|
146 | bool EntityState::OutOfBounds(SceneNode *node)
|
---|
147 | {
|
---|
148 | Vector3 pos = node->getPosition();
|
---|
149 |
|
---|
150 | if ((pos > msMinPos) && (pos < msMaxPos))
|
---|
151 | return false;
|
---|
152 |
|
---|
153 | return true;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 |
|
---|
158 | /********************************************************************/
|
---|
159 | /* TestCullingTerrainApplication implementation */
|
---|
160 | /********************************************************************/
|
---|
161 |
|
---|
162 |
|
---|
163 | TestCullingTerrainApplication::TestCullingTerrainApplication():
|
---|
164 | mTerrainContentGenerator(NULL),
|
---|
165 | mRayQueryExecutor(NULL),
|
---|
166 | mIVReader(NULL),
|
---|
167 | mFilename("terrain"),
|
---|
168 | mViewCellsFilename(""),
|
---|
169 | mAlgorithm(GtpVisibility::VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING)
|
---|
170 | {
|
---|
171 | }
|
---|
172 | //-------------------------------------------------------------------------
|
---|
173 | void TestCullingTerrainApplication::loadConfig(const String& filename)
|
---|
174 | {
|
---|
175 | // Set up the options
|
---|
176 | ConfigFile config;
|
---|
177 | String val;
|
---|
178 |
|
---|
179 | config.load(filename);
|
---|
180 |
|
---|
181 | std::stringstream d; d << "reading the config file from: " << filename;
|
---|
182 | LogManager::getSingleton().logMessage(d.str());
|
---|
183 |
|
---|
184 | val = config.getSetting("Scene");
|
---|
185 |
|
---|
186 | if (!val.empty())
|
---|
187 | {
|
---|
188 | mFilename = val.c_str();
|
---|
189 | d << "\nloading scene from file: " << mFilename;
|
---|
190 | LogManager::getSingleton().logMessage(d.str());
|
---|
191 | }
|
---|
192 | }
|
---|
193 | //-----------------------------------------------------------------------
|
---|
194 | TestCullingTerrainApplication::~TestCullingTerrainApplication()
|
---|
195 | {
|
---|
196 | OGRE_DELETE(mTerrainContentGenerator);
|
---|
197 | OGRE_DELETE(mRayQueryExecutor);
|
---|
198 | OGRE_DELETE(mIVReader);
|
---|
199 |
|
---|
200 | deleteEntityStates();
|
---|
201 | }
|
---|
202 | //-----------------------------------------------------------------------
|
---|
203 | void TestCullingTerrainApplication::deleteEntityStates()
|
---|
204 | {
|
---|
205 | for (int i = 0; i < (int)mEntityStates.size(); ++i)
|
---|
206 | {
|
---|
207 | OGRE_DELETE(mEntityStates[i]);
|
---|
208 | }
|
---|
209 |
|
---|
210 | mEntityStates.clear();
|
---|
211 | }
|
---|
212 | //-----------------------------------------------------------------------
|
---|
213 | void TestCullingTerrainApplication::createCamera()
|
---|
214 | {
|
---|
215 | // create the camera
|
---|
216 | mCamera = mSceneMgr->createCamera("PlayerCam");
|
---|
217 |
|
---|
218 | /** set a nice viewpoint
|
---|
219 | * we use a camera node here and apply all transformations on it instead
|
---|
220 | * of applying all transformations directly to the camera
|
---|
221 | * because then the camera is displayed correctly in the visualization
|
---|
222 | */
|
---|
223 | // hack: vienna view point
|
---|
224 | Vector3 viewPoint(830, 300, -540);
|
---|
225 |
|
---|
226 | mCamNode = mSceneMgr->getRootSceneNode()->
|
---|
227 | createChildSceneNode("CamNode1", viewPoint);
|
---|
228 |
|
---|
229 | mCamNode->setOrientation(Quaternion(-0.3486, 0.0122, 0.9365, 0.0329));
|
---|
230 | mCamNode->attachObject(mCamera);
|
---|
231 |
|
---|
232 | ///////////////////
|
---|
233 | //-- create visualization camera
|
---|
234 |
|
---|
235 | mVizCamera = mSceneMgr->createCamera("VizCam");
|
---|
236 | mVizCamera->setPosition(mCamNode->getPosition());
|
---|
237 |
|
---|
238 | mVizCamera->setNearClipDistance(1);
|
---|
239 | mCamera->setNearClipDistance(1);
|
---|
240 |
|
---|
241 | // infinite far plane?
|
---|
242 | if (mRoot->getRenderSystem()->getCapabilities()->hasCapability(RSC_INFINITE_FAR_PLANE))
|
---|
243 | {
|
---|
244 | mVizCamera->setFarClipDistance(0);
|
---|
245 | mCamera->setFarClipDistance(0);
|
---|
246 | }
|
---|
247 | else
|
---|
248 | {
|
---|
249 | mVizCamera->setFarClipDistance(20000);
|
---|
250 | mCamera->setFarClipDistance(20000);
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | //-----------------------------------------------------------------------
|
---|
255 | bool TestCullingTerrainApplication::setup()
|
---|
256 | {
|
---|
257 | bool carryOn = ExampleApplication::setup();
|
---|
258 |
|
---|
259 | if (carryOn)
|
---|
260 | createRenderTargetListener();
|
---|
261 |
|
---|
262 | return carryOn;
|
---|
263 | }
|
---|
264 | //-----------------------------------------------------------------------
|
---|
265 | void TestCullingTerrainApplication::createRenderTargetListener()
|
---|
266 | {
|
---|
267 | mWindow->addListener(new VisualizationRenderTargetListener(mSceneMgr));
|
---|
268 | }
|
---|
269 | //-----------------------------------------------------------------------
|
---|
270 | bool TestCullingTerrainApplication::LoadSceneCollada(const String &filename,
|
---|
271 | SceneNode *root,
|
---|
272 | const int index)
|
---|
273 | {
|
---|
274 | #if COLLADA
|
---|
275 | // Collada
|
---|
276 | ColladaDocument *daeDoc = new ColladaDocument(mSceneMgr);
|
---|
277 |
|
---|
278 | String daeName = filename;
|
---|
279 | //if (daeName.empty())
|
---|
280 | // daeName = "City_1500.dae";
|
---|
281 |
|
---|
282 | // default directory
|
---|
283 | //daeName.insert(0, "../../media/models/collada/City");
|
---|
284 | LogManager::getSingleton().logMessage("ColladaDocument - import started");
|
---|
285 |
|
---|
286 | // full import
|
---|
287 | if (daeDoc->doImport(daeName))
|
---|
288 | {
|
---|
289 | LogManager::getSingleton().logMessage("loading collada");
|
---|
290 | /**
|
---|
291 | * build up scene
|
---|
292 | * if you only want a specific part of the scene graph, you must fetch a scene node
|
---|
293 | * by its unique node name and give it as parameter
|
---|
294 | * e.g.
|
---|
295 | * ColladaSceneNode *box = mScene->getNode("Box2");
|
---|
296 | * if (box != NULL) box->createOgreInstance(NULL);
|
---|
297 | */
|
---|
298 | daeDoc->getScene()->createOgreInstance(NULL);
|
---|
299 |
|
---|
300 | // everything is loaded, we do not need the collada document anymore
|
---|
301 | delete daeDoc;
|
---|
302 |
|
---|
303 | return true;
|
---|
304 | }
|
---|
305 | else
|
---|
306 | {
|
---|
307 | LogManager::getSingleton().logMessage("ColladaDocument - import failed");
|
---|
308 | return false;
|
---|
309 | }
|
---|
310 | #endif
|
---|
311 | return true;
|
---|
312 | }
|
---|
313 | //-----------------------------------------------------------------------
|
---|
314 | bool TestCullingTerrainApplication::LoadSceneIV(const String &filename,
|
---|
315 | SceneNode *root,
|
---|
316 | const int index)
|
---|
317 | {
|
---|
318 | mIVReader = new IVReader();
|
---|
319 |
|
---|
320 | Timer *timer = PlatformManager::getSingleton().createTimer();
|
---|
321 | timer->reset();
|
---|
322 |
|
---|
323 | if (1)
|
---|
324 | {
|
---|
325 | std::string logFilename = "IVLog" + Ogre::StringConverter().toString(index) + ".log";
|
---|
326 |
|
---|
327 | Log *log = LogManager::getSingleton().createLog(logFilename);
|
---|
328 | mIVReader->setLog(log);
|
---|
329 | }
|
---|
330 |
|
---|
331 | //viennaNode->translate(Vector3(-300, -300, 0));
|
---|
332 |
|
---|
333 | if (mIVReader->loadFile(filename.c_str()))
|
---|
334 | {
|
---|
335 | SceneNode *node = root->createChildSceneNode("IVSceneNode" + index);
|
---|
336 |
|
---|
337 | mIVReader->buildTree(mSceneMgr, node);
|
---|
338 |
|
---|
339 | mIVReader->collapse();
|
---|
340 | OGRE_DELETE(mIVReader);
|
---|
341 |
|
---|
342 | std::stringstream d;
|
---|
343 | d << "loaded " << filename << " in " << timer->getMilliseconds() * 1e-3 << " secs";
|
---|
344 | LogManager::getSingleton().logMessage(d.str());
|
---|
345 |
|
---|
346 | PlatformManager::getSingleton().destroyTimer(timer);
|
---|
347 |
|
---|
348 | //-- bake into static geometry
|
---|
349 | if (USE_STATIC_GEOMETRY)
|
---|
350 | {
|
---|
351 | BakeSceneIntoStaticGeometry("staticVienna", "Vienna");
|
---|
352 | }
|
---|
353 |
|
---|
354 | return true;
|
---|
355 | }
|
---|
356 |
|
---|
357 | return false;
|
---|
358 | }
|
---|
359 | //--------------------------------------------------------
|
---|
360 | void TestCullingTerrainApplication::BakeSceneIntoStaticGeometry(const String &staticGeomName,
|
---|
361 | const String &nodeName)
|
---|
362 | {
|
---|
363 | #if OGRE_103
|
---|
364 | // note: different static geom for roofs, roads, ..., becazse they have same material
|
---|
365 | StaticGeometry *staticGeom = mSceneMgr->createStaticGeometry(staticGeomName);
|
---|
366 |
|
---|
367 | // note: looping over entities here. why does scene node not work?
|
---|
368 | SceneManager::EntityIterator it = mSceneMgr->getEntityIterator();
|
---|
369 | while (it.hasMoreElements())
|
---|
370 | {
|
---|
371 | Entity *ent = it.getNext();
|
---|
372 | ent->setVisible(false);
|
---|
373 | staticGeom->addEntity(ent, ent->getParentSceneNode()->getPosition());
|
---|
374 | }
|
---|
375 |
|
---|
376 | staticGeom->setRegionDimensions(Vector3(100,100,100));
|
---|
377 | staticGeom->build();
|
---|
378 |
|
---|
379 | // cleanup node
|
---|
380 | //wallsNode->detachAllObjects();
|
---|
381 |
|
---|
382 | //roofsNode->detachAllObjects();
|
---|
383 | //roadsNode->detachAllObjects();
|
---|
384 | //planeNode->detachAllObjects();
|
---|
385 |
|
---|
386 | //viennaNode->removeChild("Walls");
|
---|
387 | mSceneMgr->destroySceneNode(nodeName);
|
---|
388 | #endif
|
---|
389 | }
|
---|
390 | //--------------------------------------------------------
|
---|
391 | void TestCullingTerrainApplication::createScene()
|
---|
392 | {
|
---|
393 | /////////
|
---|
394 | //-- load parameters & scene
|
---|
395 | //loadConfig("terrainCulling.cfg");
|
---|
396 |
|
---|
397 | /////////////////////////////////////
|
---|
398 |
|
---|
399 | // Set ambient light
|
---|
400 | mAmbientLight = ColourValue(0.5, 0.5, 0.5);
|
---|
401 | mSceneMgr->setAmbientLight(mAmbientLight);
|
---|
402 |
|
---|
403 | //-- create light
|
---|
404 | mSunLight = mSceneMgr->createLight("SunLight");
|
---|
405 | mSunLight->setType(Light::LT_DIRECTIONAL);
|
---|
406 | //mSunLight->setType(Light::LT_SPOTLIGHT);
|
---|
407 | //mSunLight->setSpotlightRange(Degree(30), Degree(50));
|
---|
408 |
|
---|
409 | mSunLight->setPosition(707, 2000, 500);
|
---|
410 | mSunLight->setCastShadows(true);
|
---|
411 |
|
---|
412 | // set light angle not too small over the surface,
|
---|
413 | // otherwise shadows textures will be broken
|
---|
414 | Vector3 dir(0.5, 1, 0.5);
|
---|
415 | dir.normalise();
|
---|
416 | mSunLight->setDirection(dir);
|
---|
417 | //mSunLight->setDirection(Vector3::NEGATIVE_UNIT_Y);
|
---|
418 | mSunLight->setDiffuseColour(1, 1, 1);
|
---|
419 | mSunLight->setSpecularColour(1, 1, 1);
|
---|
420 |
|
---|
421 | //-- Fog
|
---|
422 |
|
---|
423 | // NB it's VERY important to set this before calling setWorldGeometry
|
---|
424 | // because the vertex program picked will be different
|
---|
425 | ColourValue fadeColour(0.93, 0.86, 0.76);
|
---|
426 | mWindow->getViewport(0)->setBackgroundColour(fadeColour);
|
---|
427 | //mSceneMgr->setFog(FOG_LINEAR, fadeColour, .001, 500, 1000);
|
---|
428 |
|
---|
429 | // Create a skybox
|
---|
430 | mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox", 50000, true);
|
---|
431 |
|
---|
432 | // terrain creation
|
---|
433 | std::string terrain_cfg("terrainCulling.cfg");
|
---|
434 | mSceneMgr->setWorldGeometry(terrain_cfg);
|
---|
435 |
|
---|
436 | // was terrain loaded?
|
---|
437 | mSceneMgr->getOption("ShowTerrain", &msShowHillyTerrain);
|
---|
438 |
|
---|
439 | // hack view point for terrain
|
---|
440 | if (msShowHillyTerrain)
|
---|
441 | {
|
---|
442 | Vector3 viewPoint(707, 5000, 528);
|
---|
443 | mCamNode->setPosition(viewPoint);
|
---|
444 | }
|
---|
445 |
|
---|
446 |
|
---|
447 | //-- CEGUI setup
|
---|
448 | setupGui();
|
---|
449 |
|
---|
450 | // Warning: In GL since we can't go higher than the window res
|
---|
451 | mSceneMgr->setShadowTextureSettings(1024, 2);
|
---|
452 | mSceneMgr->setShadowColour(ColourValue(0.5, 0.5, 0.5));
|
---|
453 |
|
---|
454 |
|
---|
455 | //////////////
|
---|
456 | //-- terrain content setup
|
---|
457 |
|
---|
458 | // HACK: necessary to call once here for terrain initialisation
|
---|
459 | mSceneMgr->_renderScene(mCamera, mWindow->getViewport(0), true);
|
---|
460 |
|
---|
461 | // ray query executor: needed to clamp to terrain
|
---|
462 | mRayQueryExecutor = new RayQueryExecutor(mSceneMgr);
|
---|
463 |
|
---|
464 | mTerrainMinPos = EntityState::msMinPos = Vector3(0, 0, 0);
|
---|
465 | mTerrainMaxPos = EntityState::msMaxPos = Vector3(5000, 5000, 5000);
|
---|
466 |
|
---|
467 | mTerrainContentGenerator = new TerrainContentGenerator(mSceneMgr);
|
---|
468 |
|
---|
469 | // if no objects in file, we generate new objects
|
---|
470 | if (msShowHillyTerrain && !mTerrainContentGenerator->LoadObjects("objects.out"))
|
---|
471 | {
|
---|
472 | // the objects are generated randomly distributed over the terrain
|
---|
473 | if (1) generateScene(1500, 0); // create robots
|
---|
474 | if (0) generateScene(100, 1); // create ninjas
|
---|
475 | if (0) generateScene(1000, 2); // create trees
|
---|
476 | }
|
---|
477 | }
|
---|
478 | //-----------------------------------------------------------------------
|
---|
479 | void TestCullingTerrainApplication::generateScene(int num, int objectType)
|
---|
480 | {
|
---|
481 | const float val = TerrainFrameListener::msObjectScales[objectType];
|
---|
482 | Vector3 scale(val, val, val);
|
---|
483 | const float maxHeight = 75;
|
---|
484 |
|
---|
485 | // In order to provide much occlusion,
|
---|
486 | // height is restricted to maxHeight => no objects are created on peaks
|
---|
487 | mTerrainContentGenerator->SetMinPos(Vector3(mTerrainMinPos));
|
---|
488 | mTerrainContentGenerator->SetMaxPos(Vector3(mTerrainMaxPos.x, maxHeight, mTerrainMaxPos.z));
|
---|
489 |
|
---|
490 | mTerrainContentGenerator->SetScale(scale);
|
---|
491 | mTerrainContentGenerator->SetOffset(TerrainFrameListener::msObjectTerrainOffsets[objectType]);
|
---|
492 | mTerrainContentGenerator->GenerateScene(num, TerrainFrameListener::msObjectCaptions[objectType]);
|
---|
493 |
|
---|
494 | if (objectType != 0) // from our objects, only robot has animation phases
|
---|
495 | return;
|
---|
496 |
|
---|
497 | EntityList *entList = mTerrainContentGenerator->GetGeneratedEntities();
|
---|
498 |
|
---|
499 | /////////////
|
---|
500 | //-- add animation state for new robots (located at the end of the list)
|
---|
501 |
|
---|
502 | for (int i = (int)entList->size() - num; i < (int)entList->size(); ++i)
|
---|
503 | {
|
---|
504 | mEntityStates.push_back(new EntityState((*entList)[i],
|
---|
505 | EntityState::WAITING, Math::RangeRandom(0.5, 1.5)));
|
---|
506 | }
|
---|
507 |
|
---|
508 | // release limitations on height => it is possible for the user to put single
|
---|
509 | // objects on peaks of the terrain (will be only few, not relevant for occlusion)
|
---|
510 | mTerrainContentGenerator->SetMaxPos(mTerrainMaxPos);
|
---|
511 | }
|
---|
512 | //-----------------------------------------------------------------------
|
---|
513 | void TestCullingTerrainApplication::updateAnimations(Real timeSinceLastFrame)
|
---|
514 | {
|
---|
515 | for (int i = 0; i < (int)mEntityStates.size(); ++i)
|
---|
516 | {
|
---|
517 | SceneNode *sn = mEntityStates[i]->GetEntity()->getParentSceneNode();
|
---|
518 |
|
---|
519 | mEntityStates[i]->update(timeSinceLastFrame);
|
---|
520 |
|
---|
521 | if (mEntityStates[i]->GetState() == EntityState::MOVING)
|
---|
522 | {
|
---|
523 | Clamp2Terrain(sn, 0); //sn->setNodeVisible(false);
|
---|
524 | }
|
---|
525 | }
|
---|
526 | }
|
---|
527 | //-----------------------------------------------------------------------
|
---|
528 | EntityStates &TestCullingTerrainApplication::getEntityStates()
|
---|
529 | {
|
---|
530 | return mEntityStates;
|
---|
531 | }
|
---|
532 | //-----------------------------------------------------------------------
|
---|
533 | void TestCullingTerrainApplication::setupGui()
|
---|
534 | {
|
---|
535 | #if OGRE103
|
---|
536 | mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY,
|
---|
537 | false, 3000, ST_EXTERIOR_CLOSE);
|
---|
538 | #else
|
---|
539 | mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY,
|
---|
540 | false, 3000, mSceneMgr);
|
---|
541 | #endif
|
---|
542 | mGUISystem = new CEGUI::System(mGUIRenderer);
|
---|
543 |
|
---|
544 | // Mouse
|
---|
545 | CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLook.scheme");
|
---|
546 | CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");
|
---|
547 | mGUISystem->setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook",
|
---|
548 | (CEGUI::utf8*)"MouseArrow");
|
---|
549 |
|
---|
550 | CEGUI::MouseCursor::getSingleton().hide();
|
---|
551 | }
|
---|
552 | //-----------------------------------------------------------------------
|
---|
553 | void TestCullingTerrainApplication::createFrameListener()
|
---|
554 | {
|
---|
555 | mTerrainFrameListener =
|
---|
556 | new TerrainFrameListener(mWindow,
|
---|
557 | mCamera,
|
---|
558 | mSceneMgr,
|
---|
559 | mGUIRenderer,
|
---|
560 | mTerrainContentGenerator,
|
---|
561 | mVizCamera,
|
---|
562 | mCamNode,
|
---|
563 | mSunLight,
|
---|
564 | this);
|
---|
565 |
|
---|
566 | mTerrainFrameListener->setPriority(10);
|
---|
567 | mTerrainFrameListener->setPriority(10);
|
---|
568 | mRoot->addFrameListener(mTerrainFrameListener);
|
---|
569 | }
|
---|
570 | //-----------------------------------------------------------------------
|
---|
571 | void TestCullingTerrainApplication::chooseSceneManager()
|
---|
572 | {
|
---|
573 | #ifdef OGRE_103
|
---|
574 | if (msShowHillyTerrain)
|
---|
575 | {
|
---|
576 | // Terrain scene manager
|
---|
577 | mSceneMgr = mRoot->getSceneManager(ST_EXTERIOR_CLOSE);
|
---|
578 | }
|
---|
579 | else
|
---|
580 | { // octree scene manager
|
---|
581 | mSceneMgr = mRoot->getSceneManager(ST_GENERIC);
|
---|
582 | }
|
---|
583 | #else
|
---|
584 |
|
---|
585 | mSceneMgr = mRoot->createSceneManager("OcclusionCullingSceneManager");
|
---|
586 |
|
---|
587 | //mSceneMgr = mRoot->createSceneManager("TerrainSceneManager");
|
---|
588 | //mSceneMgr = mRoot->createSceneManager("OctreeSceneManager");
|
---|
589 |
|
---|
590 | //mSceneMgr = mRoot->createSceneManager("KdTreeSceneManager");
|
---|
591 |
|
---|
592 | #endif
|
---|
593 | }
|
---|
594 | //-----------------------------------------------------------------------
|
---|
595 | bool TestCullingTerrainApplication::Clamp2Terrain(SceneNode *node, int terrainOffs)
|
---|
596 | {
|
---|
597 | // clamp scene node to terrain
|
---|
598 | Vector3 pos = node->getPosition();
|
---|
599 | Vector3 queryResult;
|
---|
600 |
|
---|
601 | if (mRayQueryExecutor->executeRayQuery(&queryResult,
|
---|
602 | Vector3(pos.x, MAX_HEIGHT, pos.z), Vector3::NEGATIVE_UNIT_Y))
|
---|
603 | {
|
---|
604 | node->setPosition(pos.x, queryResult.y + terrainOffs, pos.z);
|
---|
605 | return true;
|
---|
606 | }
|
---|
607 |
|
---|
608 | return false;
|
---|
609 | }
|
---|
610 |
|
---|
611 | //-----------------------------------------------------------------------
|
---|
612 | bool TestCullingTerrainApplication::Clamp2FloorPlane(const float dist)
|
---|
613 | {
|
---|
614 | // clamp to floor plane
|
---|
615 | RaySceneQuery *raySceneQuery = mSceneMgr->createRayQuery(
|
---|
616 | Ray(mCamNode->getPosition(), Vector3::NEGATIVE_UNIT_Y));
|
---|
617 |
|
---|
618 | RaySceneQueryResult& qryResult = raySceneQuery->execute();
|
---|
619 |
|
---|
620 | RaySceneQueryResult::iterator rit = qryResult.begin();
|
---|
621 | bool success = false;
|
---|
622 |
|
---|
623 | float yVal = 0;
|
---|
624 | float minVal = 999999999999;
|
---|
625 |
|
---|
626 | while (rit != qryResult.end() && rit->movable)
|
---|
627 | {
|
---|
628 | if (rit->movable->getName() != "PlayerCam")
|
---|
629 | {
|
---|
630 | // place on the ground object
|
---|
631 | yVal = rit->movable->getWorldBoundingBox().getCenter().y;
|
---|
632 | if (yVal < minVal)
|
---|
633 | minVal = yVal;
|
---|
634 |
|
---|
635 | //std::stringstream d; d << "dist: " << dist << endl;
|
---|
636 | //Ogre::LogManager()
|
---|
637 | success = true;
|
---|
638 | }
|
---|
639 |
|
---|
640 | ++ rit;
|
---|
641 | }
|
---|
642 |
|
---|
643 | // place on the ground object
|
---|
644 | if (success)
|
---|
645 | mCamNode->setPosition(
|
---|
646 | mCamNode->getPosition().x,
|
---|
647 | minVal + dist,
|
---|
648 | mCamNode->getPosition().z);
|
---|
649 |
|
---|
650 | OGRE_DELETE(raySceneQuery);
|
---|
651 | return success;
|
---|
652 | }
|
---|
653 |
|
---|
654 | //-----------------------------------------------------------------------
|
---|
655 | // splits strings containing multiple file names
|
---|
656 | static int SplitFilenames(const std::string str, std::vector<std::string> &filenames)
|
---|
657 | {
|
---|
658 | int pos = 0;
|
---|
659 |
|
---|
660 | while(1)
|
---|
661 | {
|
---|
662 | int npos = (int)str.find(';', pos);
|
---|
663 |
|
---|
664 | if (npos < 0 || npos - pos < 1)
|
---|
665 | break;
|
---|
666 | filenames.push_back(std::string(str, pos, npos - pos));
|
---|
667 | pos = npos + 1;
|
---|
668 | }
|
---|
669 |
|
---|
670 | filenames.push_back(std::string(str, pos, str.size() - pos));
|
---|
671 | return (int)filenames.size();
|
---|
672 | }
|
---|
673 | //-----------------------------------------------------------------------
|
---|
674 | bool TestCullingTerrainApplication::LoadScene(const String &filename)
|
---|
675 | {
|
---|
676 | using namespace std;
|
---|
677 | // use leaf nodes of the original spatial hierarchy as occludees
|
---|
678 | vector<string> filenames;
|
---|
679 | int files = SplitFilenames(filename, filenames);
|
---|
680 |
|
---|
681 | std::stringstream d;
|
---|
682 | d << "number of input files: " << files << "\n";
|
---|
683 | LogManager::getSingleton().logMessage(d.str());
|
---|
684 |
|
---|
685 | bool result = false;
|
---|
686 | vector<string>::const_iterator fit, fit_end = filenames.end();
|
---|
687 | int i = 0;
|
---|
688 |
|
---|
689 | for (fit = filenames.begin(); fit != fit_end; ++ fit, ++ i)
|
---|
690 | {
|
---|
691 | const string fn = *fit;
|
---|
692 |
|
---|
693 | if (strstr(fn.c_str(), ".iv") || strstr(fn.c_str(), ".wrl"))
|
---|
694 | {
|
---|
695 | // load iv files
|
---|
696 | if (!LoadSceneIV(fn, mSceneMgr->getRootSceneNode(), i))
|
---|
697 | {
|
---|
698 | // terrain hack
|
---|
699 | //msShowHillyTerrain = true;
|
---|
700 | LogManager::getSingleton().logMessage("error loading scene => load terrain");
|
---|
701 | }
|
---|
702 | }
|
---|
703 | else if (strstr(filename.c_str(), ".dae"))
|
---|
704 | {
|
---|
705 | // load collada files
|
---|
706 | LoadSceneCollada(fn, mSceneMgr->getRootSceneNode(), i);
|
---|
707 | }
|
---|
708 | else //if (filename == "terrain")
|
---|
709 | {
|
---|
710 | // terrain hack
|
---|
711 | msShowHillyTerrain = true;
|
---|
712 | LogManager::getSingleton().logMessage("loading terrain");
|
---|
713 | }
|
---|
714 |
|
---|
715 | result = true;
|
---|
716 | }
|
---|
717 |
|
---|
718 | return result;
|
---|
719 | }
|
---|
720 | //-----------------------------------------------------------------------
|
---|
721 | bool TestCullingTerrainApplication::LoadViewCells(const String &filename)
|
---|
722 | {
|
---|
723 | // if not already loaded,
|
---|
724 | // the scene manager will load the view cells
|
---|
725 | return mSceneMgr->setOption("UseViewCells", filename.c_str());
|
---|
726 | }
|
---|
727 |
|
---|
728 |
|
---|
729 | /**********************************************************************/
|
---|
730 | /* VisualizationRenderTargetListener implementation */
|
---|
731 | /**********************************************************************/
|
---|
732 |
|
---|
733 |
|
---|
734 | //-----------------------------------------------------------------------
|
---|
735 | VisualizationRenderTargetListener::VisualizationRenderTargetListener(SceneManager *sceneMgr)
|
---|
736 | :RenderTargetListener(), mSceneMgr(sceneMgr)
|
---|
737 | {
|
---|
738 | }
|
---|
739 | //-----------------------------------------------------------------------
|
---|
740 | void VisualizationRenderTargetListener::preViewportUpdate(const RenderTargetViewportEvent &evt)
|
---|
741 | {
|
---|
742 | // visualization viewport
|
---|
743 | const bool showViz = evt.source->getZOrder() == VIZ_VIEWPORT_Z_ORDER;
|
---|
744 | const bool nShowViz = !showViz;
|
---|
745 |
|
---|
746 | mSavedShadowTechnique = mSceneMgr->getShadowTechnique();
|
---|
747 | mSavedAmbientLight = mSceneMgr->getAmbientLight();
|
---|
748 |
|
---|
749 | // -- ambient light must be full for visualization, shadows disabled
|
---|
750 | if (showViz)
|
---|
751 | {
|
---|
752 | mSceneMgr->setAmbientLight(ColourValue(1, 1, 1));
|
---|
753 | mSceneMgr->setShadowTechnique(SHADOWTYPE_NONE);
|
---|
754 | }
|
---|
755 |
|
---|
756 | mSceneMgr->setOption("PrepareVisualization", &showViz);
|
---|
757 | mSceneMgr->setOption("SkyBoxEnabled", &nShowViz);
|
---|
758 |
|
---|
759 | RenderTargetListener::preViewportUpdate(evt);
|
---|
760 | }
|
---|
761 | //-----------------------------------------------------------------------
|
---|
762 | void VisualizationRenderTargetListener::postRenderTargetUpdate(const RenderTargetEvent &evt)
|
---|
763 | {
|
---|
764 | // reset values
|
---|
765 | mSceneMgr->setShadowTechnique(mSavedShadowTechnique);
|
---|
766 | mSceneMgr->setAmbientLight(mSavedAmbientLight);
|
---|
767 |
|
---|
768 | RenderTargetListener::postRenderTargetUpdate(evt);
|
---|
769 | }
|
---|
770 | //-----------------------------------------------------------------------
|
---|
771 | INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
|
---|
772 | {
|
---|
773 | // Create application object
|
---|
774 | TestCullingTerrainApplication app;
|
---|
775 |
|
---|
776 | try
|
---|
777 | {
|
---|
778 | app.go();
|
---|
779 | }
|
---|
780 | catch( Ogre::Exception& e )
|
---|
781 | {
|
---|
782 | MessageBox( NULL, e.getFullDescription().c_str(),
|
---|
783 | "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
|
---|
784 | }
|
---|
785 |
|
---|
786 | return 0;
|
---|
787 | }
|
---|