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