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