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