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