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