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 | mVizCamera->setNearClipDistance(1);
|
---|
238 | mCamera->setNearClipDistance(1);
|
---|
239 |
|
---|
240 | // infinite far plane?
|
---|
241 | if (mRoot->getRenderSystem()->getCapabilities()->hasCapability(RSC_INFINITE_FAR_PLANE))
|
---|
242 | {
|
---|
243 | mVizCamera->setFarClipDistance(0);
|
---|
244 | mCamera->setFarClipDistance(0);
|
---|
245 | }
|
---|
246 | else
|
---|
247 | {
|
---|
248 | mVizCamera->setFarClipDistance(20000);
|
---|
249 | mCamera->setFarClipDistance(20000);
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | //-----------------------------------------------------------------------
|
---|
254 | bool TestCullingTerrainApplication::setup()
|
---|
255 | {
|
---|
256 | bool carryOn = ExampleApplication::setup();
|
---|
257 |
|
---|
258 | if (carryOn)
|
---|
259 | createRenderTargetListener();
|
---|
260 |
|
---|
261 | return carryOn;
|
---|
262 | }
|
---|
263 | //-----------------------------------------------------------------------
|
---|
264 | void TestCullingTerrainApplication::createRenderTargetListener()
|
---|
265 | {
|
---|
266 | mWindow->addListener(new VisualizationRenderTargetListener(mSceneMgr));
|
---|
267 | }
|
---|
268 | //-----------------------------------------------------------------------
|
---|
269 | bool TestCullingTerrainApplication::LoadSceneCollada(const String &filename,
|
---|
270 | SceneNode *root,
|
---|
271 | const int index)
|
---|
272 | {
|
---|
273 | #if COLLADA
|
---|
274 | // Collada
|
---|
275 | ColladaDocument *daeDoc = new ColladaDocument(mSceneMgr);
|
---|
276 |
|
---|
277 | String daeName = filename;
|
---|
278 | //if (daeName.empty())
|
---|
279 | // daeName = "City_1500.dae";
|
---|
280 |
|
---|
281 | // default directory
|
---|
282 | //daeName.insert(0, "../../media/models/collada/City");
|
---|
283 | LogManager::getSingleton().logMessage("ColladaDocument - import started");
|
---|
284 |
|
---|
285 | // full import
|
---|
286 | if (daeDoc->doImport(daeName))
|
---|
287 | {
|
---|
288 | LogManager::getSingleton().logMessage("loading collada");
|
---|
289 | /**
|
---|
290 | * build up scene
|
---|
291 | * if you only want a specific part of the scene graph, you must fetch a scene node
|
---|
292 | * by its unique node name and give it as parameter
|
---|
293 | * e.g.
|
---|
294 | * ColladaSceneNode *box = mScene->getNode("Box2");
|
---|
295 | * if (box != NULL) box->createOgreInstance(NULL);
|
---|
296 | */
|
---|
297 | daeDoc->getScene()->createOgreInstance(NULL);
|
---|
298 |
|
---|
299 | // everything is loaded, we do not need the collada document anymore
|
---|
300 | delete daeDoc;
|
---|
301 |
|
---|
302 | return true;
|
---|
303 | }
|
---|
304 | else
|
---|
305 | {
|
---|
306 | LogManager::getSingleton().logMessage("ColladaDocument - import failed");
|
---|
307 | return false;
|
---|
308 | }
|
---|
309 | #endif
|
---|
310 | return true;
|
---|
311 | }
|
---|
312 | //-----------------------------------------------------------------------
|
---|
313 | bool TestCullingTerrainApplication::LoadSceneIV(const String &filename,
|
---|
314 | SceneNode *root,
|
---|
315 | const int index)
|
---|
316 | {
|
---|
317 | mIVReader = new IVReader();
|
---|
318 |
|
---|
319 | Timer *timer = PlatformManager::getSingleton().createTimer();
|
---|
320 | timer->reset();
|
---|
321 |
|
---|
322 | if (1)
|
---|
323 | {
|
---|
324 | std::string logFilename = "IVLog" + Ogre::StringConverter().toString(index) + ".log";
|
---|
325 |
|
---|
326 | Log *log = LogManager::getSingleton().createLog(logFilename);
|
---|
327 | mIVReader->setLog(log);
|
---|
328 | }
|
---|
329 |
|
---|
330 | //viennaNode->translate(Vector3(-300, -300, 0));
|
---|
331 |
|
---|
332 | if (mIVReader->loadFile(filename.c_str()))
|
---|
333 | {
|
---|
334 | SceneNode *node = root->createChildSceneNode("IVSceneNode" + index);
|
---|
335 |
|
---|
336 | mIVReader->buildTree(mSceneMgr, node);
|
---|
337 |
|
---|
338 | mIVReader->collapse();
|
---|
339 | OGRE_DELETE(mIVReader);
|
---|
340 |
|
---|
341 | std::stringstream d;
|
---|
342 | d << "loaded " << filename << " in " << timer->getMilliseconds() * 1e-3 << " secs";
|
---|
343 | LogManager::getSingleton().logMessage(d.str());
|
---|
344 |
|
---|
345 | PlatformManager::getSingleton().destroyTimer(timer);
|
---|
346 |
|
---|
347 | //-- bake into static geometry
|
---|
348 | if (USE_STATIC_GEOMETRY)
|
---|
349 | {
|
---|
350 | BakeSceneIntoStaticGeometry("staticVienna", "Vienna");
|
---|
351 | }
|
---|
352 |
|
---|
353 | return true;
|
---|
354 | }
|
---|
355 |
|
---|
356 | return false;
|
---|
357 | }
|
---|
358 | //--------------------------------------------------------
|
---|
359 | void TestCullingTerrainApplication::BakeSceneIntoStaticGeometry(const String &staticGeomName,
|
---|
360 | const String &nodeName)
|
---|
361 | {
|
---|
362 | #if OGRE_103
|
---|
363 | // note: different static geom for roofs, roads, ..., becazse they have same material
|
---|
364 | StaticGeometry *staticGeom = mSceneMgr->createStaticGeometry(staticGeomName);
|
---|
365 |
|
---|
366 | // note: looping over entities here. why does scene node not work?
|
---|
367 | SceneManager::EntityIterator it = mSceneMgr->getEntityIterator();
|
---|
368 | while (it.hasMoreElements())
|
---|
369 | {
|
---|
370 | Entity *ent = it.getNext();
|
---|
371 | ent->setVisible(false);
|
---|
372 | staticGeom->addEntity(ent, ent->getParentSceneNode()->getPosition());
|
---|
373 | }
|
---|
374 |
|
---|
375 | staticGeom->setRegionDimensions(Vector3(100,100,100));
|
---|
376 | staticGeom->build();
|
---|
377 |
|
---|
378 | // cleanup node
|
---|
379 | //wallsNode->detachAllObjects();
|
---|
380 |
|
---|
381 | //roofsNode->detachAllObjects();
|
---|
382 | //roadsNode->detachAllObjects();
|
---|
383 | //planeNode->detachAllObjects();
|
---|
384 |
|
---|
385 | //viennaNode->removeChild("Walls");
|
---|
386 | mSceneMgr->destroySceneNode(nodeName);
|
---|
387 | #endif
|
---|
388 | }
|
---|
389 | //--------------------------------------------------------
|
---|
390 | void TestCullingTerrainApplication::createScene()
|
---|
391 | {
|
---|
392 | /////////
|
---|
393 | //-- load parameters & scene
|
---|
394 | //loadConfig("terrainCulling.cfg");
|
---|
395 |
|
---|
396 | /////////////////////////////////////
|
---|
397 |
|
---|
398 | // Set ambient light
|
---|
399 | mAmbientLight = ColourValue(0.5, 0.5, 0.5);
|
---|
400 | mSceneMgr->setAmbientLight(mAmbientLight);
|
---|
401 |
|
---|
402 | //-- create light
|
---|
403 | mSunLight = mSceneMgr->createLight("SunLight");
|
---|
404 | mSunLight->setType(Light::LT_DIRECTIONAL);
|
---|
405 | //mSunLight->setType(Light::LT_SPOTLIGHT);
|
---|
406 | //mSunLight->setSpotlightRange(Degree(30), Degree(50));
|
---|
407 |
|
---|
408 | mSunLight->setPosition(707, 2000, 500);
|
---|
409 | mSunLight->setCastShadows(true);
|
---|
410 |
|
---|
411 | // set light angle not too small over the surface,
|
---|
412 | // otherwise shadows textures will be broken
|
---|
413 | Vector3 dir(0.5, 1, 0.5);
|
---|
414 | dir.normalise();
|
---|
415 | mSunLight->setDirection(dir);
|
---|
416 | //mSunLight->setDirection(Vector3::NEGATIVE_UNIT_Y);
|
---|
417 | mSunLight->setDiffuseColour(1, 1, 1);
|
---|
418 | mSunLight->setSpecularColour(1, 1, 1);
|
---|
419 |
|
---|
420 | //-- Fog
|
---|
421 |
|
---|
422 | // NB it's VERY important to set this before calling setWorldGeometry
|
---|
423 | // because the vertex program picked will be different
|
---|
424 | ColourValue fadeColour(0.93, 0.86, 0.76);
|
---|
425 | mWindow->getViewport(0)->setBackgroundColour(fadeColour);
|
---|
426 | //mSceneMgr->setFog(FOG_LINEAR, fadeColour, .001, 500, 1000);
|
---|
427 |
|
---|
428 | // Create a skybox
|
---|
429 | mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox", 50000, true);
|
---|
430 |
|
---|
431 | // terrain creation
|
---|
432 | std::string terrain_cfg("terrainCulling.cfg");
|
---|
433 |
|
---|
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(500, 0); // create robots
|
---|
474 | if (0) generateScene(300, 1); // create ninjas
|
---|
475 | if (0) generateScene(1000, 2); // create trees
|
---|
476 | }
|
---|
477 |
|
---|
478 | int maxDepth = 30;
|
---|
479 |
|
---|
480 | mSceneMgr->setOption("BiHierarchyMaxDepth", &maxDepth);
|
---|
481 |
|
---|
482 | int mode = 1;
|
---|
483 |
|
---|
484 | mSceneMgr->setOption("EnhancedVisibility", &mode);
|
---|
485 | mSceneMgr->setOption("RebuildBiHierarchy", NULL);
|
---|
486 | mSceneMgr->setOption("RebuildKdTree", NULL);
|
---|
487 | }
|
---|
488 | //-----------------------------------------------------------------------
|
---|
489 | void TestCullingTerrainApplication::generateScene(int num, int objectType)
|
---|
490 | {
|
---|
491 | const float val = TerrainFrameListener::msObjectScales[objectType];
|
---|
492 | Vector3 scale(val, val, val);
|
---|
493 | const float maxHeight = 75;
|
---|
494 |
|
---|
495 | // In order to provide much occlusion,
|
---|
496 | // height is restricted to maxHeight => no objects are created on peaks
|
---|
497 | mTerrainContentGenerator->SetMinPos(Vector3(mTerrainMinPos));
|
---|
498 | mTerrainContentGenerator->SetMaxPos(Vector3(mTerrainMaxPos.x, maxHeight, mTerrainMaxPos.z));
|
---|
499 |
|
---|
500 | mTerrainContentGenerator->SetScale(scale);
|
---|
501 | mTerrainContentGenerator->SetOffset(TerrainFrameListener::msObjectTerrainOffsets[objectType]);
|
---|
502 | mTerrainContentGenerator->GenerateScene(num, TerrainFrameListener::msObjectCaptions[objectType]);
|
---|
503 |
|
---|
504 | if (objectType != 0) // from our objects, only robot has animation phases
|
---|
505 | return;
|
---|
506 |
|
---|
507 | EntityList *entList = mTerrainContentGenerator->GetGeneratedEntities();
|
---|
508 |
|
---|
509 | /////////////
|
---|
510 | //-- add animation state for new robots (located at the end of the list)
|
---|
511 |
|
---|
512 | for (size_t i = entList->size() - (unsigned int)num; i < entList->size(); ++ i)
|
---|
513 | {
|
---|
514 | mEntityStates.push_back(new EntityState((*entList)[i],
|
---|
515 | EntityState::WAITING, Math::RangeRandom(0.5, 1.5)));
|
---|
516 | }
|
---|
517 |
|
---|
518 | // release limitations on height => it is possible for the user to put single
|
---|
519 | // objects on peaks of the terrain (will be only few, not relevant for occlusion)
|
---|
520 | mTerrainContentGenerator->SetMaxPos(mTerrainMaxPos);
|
---|
521 | }
|
---|
522 | //-----------------------------------------------------------------------
|
---|
523 | void TestCullingTerrainApplication::updateAnimations(Real timeSinceLastFrame)
|
---|
524 | {
|
---|
525 | for (int i = 0; i < (int)mEntityStates.size(); ++i)
|
---|
526 | {
|
---|
527 | SceneNode *sn = mEntityStates[i]->GetEntity()->getParentSceneNode();
|
---|
528 |
|
---|
529 | mEntityStates[i]->update(timeSinceLastFrame);
|
---|
530 |
|
---|
531 | if (mEntityStates[i]->GetState() == EntityState::MOVING)
|
---|
532 | {
|
---|
533 | Clamp2Terrain(sn, 0); //sn->setNodeVisible(false);
|
---|
534 | }
|
---|
535 | }
|
---|
536 | }
|
---|
537 | //-----------------------------------------------------------------------
|
---|
538 | EntityStates &TestCullingTerrainApplication::getEntityStates()
|
---|
539 | {
|
---|
540 | return mEntityStates;
|
---|
541 | }
|
---|
542 | //-----------------------------------------------------------------------
|
---|
543 | void TestCullingTerrainApplication::setupGui()
|
---|
544 | {
|
---|
545 | #if OGRE103
|
---|
546 | mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY,
|
---|
547 | false, 3000, ST_EXTERIOR_CLOSE);
|
---|
548 | #else
|
---|
549 | mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY,
|
---|
550 | false, 3000, mSceneMgr);
|
---|
551 | #endif
|
---|
552 | mGUISystem = new CEGUI::System(mGUIRenderer);
|
---|
553 |
|
---|
554 | // Mouse
|
---|
555 | CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLook.scheme");
|
---|
556 | CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");
|
---|
557 | mGUISystem->setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook",
|
---|
558 | (CEGUI::utf8*)"MouseArrow");
|
---|
559 |
|
---|
560 | CEGUI::MouseCursor::getSingleton().hide();
|
---|
561 | }
|
---|
562 | //-----------------------------------------------------------------------
|
---|
563 | void TestCullingTerrainApplication::createFrameListener()
|
---|
564 | {
|
---|
565 | mTerrainFrameListener =
|
---|
566 | new TerrainFrameListener(mWindow,
|
---|
567 | mCamera,
|
---|
568 | mSceneMgr,
|
---|
569 | mGUIRenderer,
|
---|
570 | mTerrainContentGenerator,
|
---|
571 | mVizCamera,
|
---|
572 | mCamNode,
|
---|
573 | mSunLight,
|
---|
574 | this);
|
---|
575 | #ifdef GAMETOOLS_ILLUMINATION_MODULE
|
---|
576 | mTerrainFrameListener->setPriority(10);
|
---|
577 | #endif
|
---|
578 | mRoot->addFrameListener(mTerrainFrameListener);
|
---|
579 | }
|
---|
580 | //-----------------------------------------------------------------------
|
---|
581 | void TestCullingTerrainApplication::chooseSceneManager()
|
---|
582 | {
|
---|
583 | #ifdef OGRE_103
|
---|
584 | if (msShowHillyTerrain)
|
---|
585 | {
|
---|
586 | // Terrain scene manager
|
---|
587 | mSceneMgr = mRoot->getSceneManager(ST_EXTERIOR_CLOSE);
|
---|
588 | }
|
---|
589 | else
|
---|
590 | { // octree scene manager
|
---|
591 | mSceneMgr = mRoot->getSceneManager(ST_GENERIC);
|
---|
592 | }
|
---|
593 | #else
|
---|
594 |
|
---|
595 | mSceneMgr = mRoot->createSceneManager("OcclusionCullingSceneManager");
|
---|
596 |
|
---|
597 | //mSceneMgr = mRoot->createSceneManager("TerrainSceneManager");
|
---|
598 | //mSceneMgr = mRoot->createSceneManager("OctreeSceneManager");
|
---|
599 | //mSceneMgr = mRoot->createSceneManager("BiHierarchySceneManager");
|
---|
600 | //mSceneMgr = mRoot->createSceneManager("BiTreeTerrainSceneManager");
|
---|
601 |
|
---|
602 | bool useEnhancedVisibility = true;
|
---|
603 | mSceneMgr->setOption("EnhancedVisibility", &useEnhancedVisibility);
|
---|
604 | #endif
|
---|
605 | }
|
---|
606 | //-----------------------------------------------------------------------
|
---|
607 | bool TestCullingTerrainApplication::Clamp2Terrain(SceneNode *node, int terrainOffs)
|
---|
608 | {
|
---|
609 | // clamp scene node to terrain
|
---|
610 | Vector3 pos = node->getPosition();
|
---|
611 | Vector3 queryResult;
|
---|
612 |
|
---|
613 | if (mRayQueryExecutor->executeRayQuery(&queryResult,
|
---|
614 | Vector3(pos.x, MAX_HEIGHT, pos.z), Vector3::NEGATIVE_UNIT_Y))
|
---|
615 | {
|
---|
616 | node->setPosition(pos.x, queryResult.y + terrainOffs, pos.z);
|
---|
617 | return true;
|
---|
618 | }
|
---|
619 |
|
---|
620 | return false;
|
---|
621 | }
|
---|
622 |
|
---|
623 | //-----------------------------------------------------------------------
|
---|
624 | bool TestCullingTerrainApplication::Clamp2FloorPlane(const float dist)
|
---|
625 | {
|
---|
626 | // clamp to floor plane
|
---|
627 | RaySceneQuery *raySceneQuery = mSceneMgr->createRayQuery(
|
---|
628 | Ray(mCamNode->getPosition(), Vector3::NEGATIVE_UNIT_Y));
|
---|
629 |
|
---|
630 | RaySceneQueryResult& qryResult = raySceneQuery->execute();
|
---|
631 |
|
---|
632 | RaySceneQueryResult::iterator rit = qryResult.begin();
|
---|
633 | bool success = false;
|
---|
634 |
|
---|
635 | float yVal = 0;
|
---|
636 | float minVal = 999999999999;
|
---|
637 |
|
---|
638 | // find next valid intersection
|
---|
639 | while (rit != qryResult.end() && rit->movable)
|
---|
640 | {
|
---|
641 | // camera is not a real world object
|
---|
642 | if (rit->movable->getName() != "PlayerCam")
|
---|
643 | {
|
---|
644 | // get the next intersection with a movable object
|
---|
645 | yVal = rit->movable->getWorldBoundingBox().getCenter().y;
|
---|
646 |
|
---|
647 | if (yVal < minVal)
|
---|
648 | minVal = yVal;
|
---|
649 |
|
---|
650 | success = true;
|
---|
651 | }
|
---|
652 |
|
---|
653 | ++ rit;
|
---|
654 | }
|
---|
655 |
|
---|
656 | // place player on the ground object
|
---|
657 | if (success)
|
---|
658 | {
|
---|
659 | mCamNode->setPosition(mCamNode->getPosition().x,
|
---|
660 | minVal + dist,
|
---|
661 | mCamNode->getPosition().z);
|
---|
662 | }
|
---|
663 |
|
---|
664 | OGRE_DELETE(raySceneQuery);
|
---|
665 | return success;
|
---|
666 | }
|
---|
667 |
|
---|
668 | //-----------------------------------------------------------------------
|
---|
669 | // splits strings containing multiple file names
|
---|
670 | static int SplitFilenames(const std::string str, std::vector<std::string> &filenames)
|
---|
671 | {
|
---|
672 | int pos = 0;
|
---|
673 |
|
---|
674 | while(1)
|
---|
675 | {
|
---|
676 | int npos = (int)str.find(';', pos);
|
---|
677 |
|
---|
678 | if (npos < 0 || npos - pos < 1)
|
---|
679 | break;
|
---|
680 | filenames.push_back(std::string(str, pos, npos - pos));
|
---|
681 | pos = npos + 1;
|
---|
682 | }
|
---|
683 |
|
---|
684 | filenames.push_back(std::string(str, pos, str.size() - pos));
|
---|
685 | return (int)filenames.size();
|
---|
686 | }
|
---|
687 | //-----------------------------------------------------------------------
|
---|
688 | bool TestCullingTerrainApplication::LoadScene(const String &filename)
|
---|
689 | {
|
---|
690 | using namespace std;
|
---|
691 | // use leaf nodes of the original spatial hierarchy as occludees
|
---|
692 | vector<string> filenames;
|
---|
693 | int files = SplitFilenames(filename, filenames);
|
---|
694 |
|
---|
695 | std::stringstream d;
|
---|
696 | d << "number of input files: " << files << "\n";
|
---|
697 | LogManager::getSingleton().logMessage(d.str());
|
---|
698 |
|
---|
699 | bool result = false;
|
---|
700 | vector<string>::const_iterator fit, fit_end = filenames.end();
|
---|
701 | int i = 0;
|
---|
702 |
|
---|
703 | for (fit = filenames.begin(); fit != fit_end; ++ fit, ++ i)
|
---|
704 | {
|
---|
705 | const string fn = *fit;
|
---|
706 |
|
---|
707 | if (strstr(fn.c_str(), ".iv") || strstr(fn.c_str(), ".wrl"))
|
---|
708 | {
|
---|
709 | // load iv files
|
---|
710 | if (!LoadSceneIV(fn, mSceneMgr->getRootSceneNode(), i))
|
---|
711 | {
|
---|
712 | // terrain hack
|
---|
713 | //msShowHillyTerrain = true;
|
---|
714 | LogManager::getSingleton().logMessage("error loading scene => load terrain");
|
---|
715 | }
|
---|
716 | }
|
---|
717 | else if (strstr(filename.c_str(), ".dae"))
|
---|
718 | {
|
---|
719 | // load collada files
|
---|
720 | LoadSceneCollada(fn, mSceneMgr->getRootSceneNode(), i);
|
---|
721 | }
|
---|
722 | else //if (filename == "terrain")
|
---|
723 | {
|
---|
724 | // terrain hack
|
---|
725 | msShowHillyTerrain = true;
|
---|
726 | LogManager::getSingleton().logMessage("loading terrain");
|
---|
727 | }
|
---|
728 |
|
---|
729 | result = true;
|
---|
730 | }
|
---|
731 |
|
---|
732 | return result;
|
---|
733 | }
|
---|
734 | //-----------------------------------------------------------------------
|
---|
735 | bool TestCullingTerrainApplication::LoadViewCells(const String &filename)
|
---|
736 | {
|
---|
737 | // if not already loaded, the scene manager will load the view cells
|
---|
738 | return mSceneMgr->setOption("UseViewCells", filename.c_str());
|
---|
739 | }
|
---|
740 |
|
---|
741 |
|
---|
742 |
|
---|
743 | /**********************************************************************/
|
---|
744 | /* VisualizationRenderTargetListener implementation */
|
---|
745 | /**********************************************************************/
|
---|
746 |
|
---|
747 | //-----------------------------------------------------------------------
|
---|
748 | VisualizationRenderTargetListener::VisualizationRenderTargetListener(SceneManager *sceneMgr)
|
---|
749 | :RenderTargetListener(), mSceneMgr(sceneMgr)
|
---|
750 | {
|
---|
751 | }
|
---|
752 | //-----------------------------------------------------------------------
|
---|
753 | void VisualizationRenderTargetListener::preViewportUpdate(const RenderTargetViewportEvent &evt)
|
---|
754 | {
|
---|
755 | // visualization viewport
|
---|
756 | const bool showViz = evt.source->getZOrder() == VIZ_VIEWPORT_Z_ORDER;
|
---|
757 | const bool nShowViz = !showViz;
|
---|
758 |
|
---|
759 | mSavedShadowTechnique = mSceneMgr->getShadowTechnique();
|
---|
760 | mSavedAmbientLight = mSceneMgr->getAmbientLight();
|
---|
761 |
|
---|
762 | //-- ambient light must be full for visualization, shadows disabled
|
---|
763 | if (showViz)
|
---|
764 | {
|
---|
765 | mSceneMgr->setAmbientLight(ColourValue(1, 1, 1));
|
---|
766 | mSceneMgr->setShadowTechnique(SHADOWTYPE_NONE);
|
---|
767 | }
|
---|
768 |
|
---|
769 | mSceneMgr->setOption("PrepareVisualization", &showViz);
|
---|
770 | mSceneMgr->setOption("SkyBoxEnabled", &nShowViz);
|
---|
771 |
|
---|
772 | RenderTargetListener::preViewportUpdate(evt);
|
---|
773 | }
|
---|
774 | //-----------------------------------------------------------------------
|
---|
775 | void VisualizationRenderTargetListener::postRenderTargetUpdate(const RenderTargetEvent &evt)
|
---|
776 | {
|
---|
777 | // reset values
|
---|
778 | mSceneMgr->setShadowTechnique(mSavedShadowTechnique);
|
---|
779 | mSceneMgr->setAmbientLight(mSavedAmbientLight);
|
---|
780 |
|
---|
781 | RenderTargetListener::postRenderTargetUpdate(evt);
|
---|
782 | }
|
---|
783 | //-----------------------------------------------------------------------
|
---|
784 | INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
|
---|
785 | {
|
---|
786 | // Create application object
|
---|
787 | TestCullingTerrainApplication app;
|
---|
788 |
|
---|
789 | try
|
---|
790 | {
|
---|
791 | app.go();
|
---|
792 | }
|
---|
793 | catch( Ogre::Exception& e )
|
---|
794 | {
|
---|
795 | MessageBox( NULL, e.getFullDescription().c_str(),
|
---|
796 | "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
|
---|
797 | }
|
---|
798 |
|
---|
799 | return 0;
|
---|
800 | } |
---|