[61] | 1 | /**
|
---|
| 2 | \file
|
---|
| 3 | TestCullingApplication.cpp
|
---|
| 4 | \brief
|
---|
[74] | 5 | Tests the visibility culling algorithm
|
---|
[61] | 6 | */
|
---|
| 7 |
|
---|
| 8 | #include <OgreNoMemoryMacros.h>
|
---|
| 9 | #include <CEGUI/CEGUI.h>
|
---|
| 10 | #include <../CEGUIRenderer/include/OgreCEGUIRenderer.h>
|
---|
| 11 | #include <../CEGUIRenderer/include/OgreCEGUIResourceProvider.h>
|
---|
| 12 | #include <../CEGUIRenderer/include/OgreCEGUITexture.h>
|
---|
| 13 | #include <OgreMemoryMacros.h>
|
---|
[79] | 14 | #include <Ogre.h>
|
---|
[61] | 15 | #include "TestCullingApplication.h"
|
---|
[107] | 16 | #include "OgreSceneContentGenerator.h"
|
---|
[61] | 17 |
|
---|
| 18 | #define WIN32_LEAN_AND_MEAN
|
---|
[94] | 19 | #include <windows.h>
|
---|
[61] | 20 |
|
---|
| 21 |
|
---|
| 22 | /***********************************************/
|
---|
| 23 | /* TestCullingApplication implementation */
|
---|
| 24 | /***********************************************/
|
---|
| 25 |
|
---|
| 26 | //-----------------------------------------------------------------------
|
---|
| 27 | /*void TestCullingApplication::createCamera( void )
|
---|
| 28 | {
|
---|
| 29 | // Create the camera
|
---|
[94] | 30 | mCamera = mSceneMgr->createCamera("CullCamera");
|
---|
[61] | 31 |
|
---|
| 32 | // Position it at 500 in Z direction
|
---|
[160] | 33 | mCamera->setPosition(Vector3(128, 25, 128));
|
---|
[61] | 34 |
|
---|
| 35 | // Look back along -Z
|
---|
[160] | 36 | mCamera->lookAt(Vector3(0, 0, -300));
|
---|
| 37 | mCamera->setNearClipDistance(1);
|
---|
| 38 | mCamera->setFarClipDistance(1000);
|
---|
[61] | 39 | }*/
|
---|
| 40 | //-----------------------------------------------------------------------
|
---|
[85] | 41 | TestCullingApplication::~TestCullingApplication()
|
---|
| 42 | {
|
---|
| 43 | delete mSceneContentGenerator;
|
---|
| 44 | }
|
---|
| 45 | //-----------------------------------------------------------------------
|
---|
[61] | 46 | void TestCullingApplication::createScene(void)
|
---|
| 47 | {
|
---|
| 48 | // Set ambient light
|
---|
| 49 | mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
|
---|
| 50 |
|
---|
| 51 | // Create a light
|
---|
| 52 | Light* l = mSceneMgr->createLight("MainLight");
|
---|
| 53 | //l->setPosition(20,80,50);
|
---|
| 54 |
|
---|
[85] | 55 | mSceneContentGenerator = new SceneContentGenerator(mSceneMgr);
|
---|
[86] | 56 | mSceneContentGenerator->GenerateScene(3000, "sphere.mesh");
|
---|
[61] | 57 |
|
---|
| 58 | // Create a skybox
|
---|
[111] | 59 | mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox", 2000, true);
|
---|
[61] | 60 | ColourValue fadeColour(0.1, 0.1, 0.6);
|
---|
| 61 | mWindow->getViewport(0)->setBackgroundColour(fadeColour);
|
---|
| 62 |
|
---|
| 63 | // CEGUI setup
|
---|
| 64 | setupGui();
|
---|
| 65 | }
|
---|
| 66 | //-----------------------------------------------------------------------
|
---|
[85] | 67 | void TestCullingApplication::setupGui()
|
---|
[61] | 68 | {
|
---|
[160] | 69 | mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow,
|
---|
| 70 | Ogre::RENDER_QUEUE_OVERLAY, false, 3000, ST_EXTERIOR_CLOSE);
|
---|
[61] | 71 | mGUISystem = new CEGUI::System(mGUIRenderer);
|
---|
| 72 |
|
---|
| 73 | // Mouse
|
---|
| 74 | CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLook.scheme");
|
---|
| 75 | CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");
|
---|
| 76 | mGUISystem->setDefaultMouseCursor(
|
---|
| 77 | (CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");
|
---|
| 78 |
|
---|
| 79 | CEGUI::MouseCursor::getSingleton().show( );
|
---|
| 80 | }
|
---|
| 81 | //-----------------------------------------------------------------------
|
---|
[85] | 82 | void TestCullingApplication::createFrameListener()
|
---|
[61] | 83 | {
|
---|
[133] | 84 | mFrameListener= new TerrainFrameListener(mWindow, mCamera, mSceneMgr,
|
---|
[85] | 85 | mGUIRenderer, mSceneContentGenerator);
|
---|
[61] | 86 | mFrameListener->showDebugOverlay(true);
|
---|
| 87 | mRoot->addFrameListener(mFrameListener);
|
---|
| 88 | }
|
---|
| 89 | //-----------------------------------------------------------------------
|
---|
| 90 | void TestCullingApplication::chooseSceneManager(void)
|
---|
| 91 | {
|
---|
| 92 | mSceneMgr = mRoot->getSceneManager(ST_GENERIC);
|
---|
[139] | 93 | }
|
---|
| 94 |
|
---|
| 95 |
|
---|
[160] | 96 | /*****************************************************/
|
---|
| 97 | /* TerrainFrameListener implementation */
|
---|
| 98 | /*****************************************************/
|
---|
[61] | 99 | //-----------------------------------------------------------------------
|
---|
[133] | 100 | TerrainFrameListener::TerrainFrameListener(RenderWindow* win, Camera* cam,
|
---|
[85] | 101 | SceneManager *sceneManager,
|
---|
| 102 | CEGUI::Renderer *renderer,
|
---|
[160] | 103 | SceneContentGenerator *sceneContentGenerator):
|
---|
| 104 | ExampleFrameListener(win, cam, false, true),
|
---|
[86] | 105 | mSceneMgr(sceneManager),
|
---|
| 106 | mGUIRenderer(renderer),
|
---|
| 107 | mShutdownRequested(false),
|
---|
| 108 | mLMouseDown(false),
|
---|
| 109 | mRMouseDown(false),
|
---|
| 110 | mSceneContentGenerator(sceneContentGenerator),
|
---|
| 111 | mVisibilityThreshold(0),
|
---|
[87] | 112 | mCurrentAlgorithm(GtpVisibility::VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING),
|
---|
[93] | 113 | mShowOctree(false),
|
---|
[115] | 114 | mUseDepthPass(false),
|
---|
[155] | 115 | mTestGeometryForVisibleLeaves(true),
|
---|
[112] | 116 | mShowVisualization(false),
|
---|
| 117 | mVisualizeCulledNodes(false)
|
---|
[61] | 118 | {
|
---|
| 119 | // Reduce move speed
|
---|
| 120 | mMoveSpeed = 50;
|
---|
| 121 | mRotateSpeed *= 2;
|
---|
[86] | 122 |
|
---|
[61] | 123 | // Register this so that we get mouse events.
|
---|
| 124 | mEventProcessor->addMouseListener(this);
|
---|
| 125 | mEventProcessor->addMouseMotionListener(this);
|
---|
| 126 | mEventProcessor->addKeyListener(this);
|
---|
| 127 |
|
---|
| 128 | // show overlay
|
---|
| 129 | Overlay* pOver = OverlayManager::getSingleton().getByName("Example/VisibilityDemoOverlay");
|
---|
| 130 |
|
---|
[74] | 131 | mAlgorithmInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/AlgorithmInfo");
|
---|
| 132 | mThresholdInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/ThresholdInfo");
|
---|
[84] | 133 |
|
---|
[74] | 134 | mFrustumCulledNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/FrustumCulledNodesInfo");
|
---|
| 135 | mQueryCulledNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/QueryCulledNodesInfo");
|
---|
| 136 | mTraversedNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/TraversedNodesInfo");
|
---|
| 137 | mHierarchyNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/HierarchyNodesInfo");
|
---|
| 138 | mRenderedNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/RenderedNodesInfo");
|
---|
[160] | 139 | mObjectsCountInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/ObjectsInfo");
|
---|
[155] | 140 | mTestGeometryForVisibleLeavesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/TestGeometryForVisibleLeavesInfo");
|
---|
[87] | 141 | mQueriesIssuedInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/QueriesIssuedInfo");
|
---|
[155] | 142 |
|
---|
[61] | 143 |
|
---|
| 144 | mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
|
---|
| 145 | mThresholdInfo->setCaption(": 0");
|
---|
| 146 | mFrustumCulledNodesInfo->setCaption(": 0");
|
---|
| 147 | mQueryCulledNodesInfo->setCaption(": 0");
|
---|
| 148 | mTraversedNodesInfo->setCaption(": 0");
|
---|
| 149 | mHierarchyNodesInfo->setCaption(": 0");
|
---|
| 150 | mRenderedNodesInfo->setCaption(": 0");
|
---|
[160] | 151 | mObjectsCountInfo->setCaption(": 0");
|
---|
[155] | 152 | mTestGeometryForVisibleLeavesInfo->setCaption(": true");
|
---|
[87] | 153 | mQueriesIssuedInfo->setCaption(": 0");
|
---|
[155] | 154 |
|
---|
[61] | 155 |
|
---|
[85] | 156 | setAlgorithm(mCurrentAlgorithm);
|
---|
[93] | 157 |
|
---|
[155] | 158 | mSceneMgr->setOption("TestGeometryForVisibleLeaves", &mTestGeometryForVisibleLeaves);
|
---|
[115] | 159 | mSceneMgr->setOption("UseDepthPass", &mUseDepthPass);
|
---|
[99] | 160 | mSceneMgr->setOption("ShowVizualisation", &mShowVisualization);
|
---|
[93] | 161 | mSceneMgr->setOption("ShowOctree", &mShowOctree);
|
---|
[85] | 162 |
|
---|
[61] | 163 | pOver->show();
|
---|
| 164 | }
|
---|
| 165 | //-----------------------------------------------------------------------
|
---|
[133] | 166 | TerrainFrameListener::~TerrainFrameListener()
|
---|
[61] | 167 | {
|
---|
| 168 | }
|
---|
| 169 | //-----------------------------------------------------------------------
|
---|
[133] | 170 | void TerrainFrameListener::mouseMoved(MouseEvent *e)
|
---|
[61] | 171 | {
|
---|
| 172 | // Update CEGUI with the mouse motion
|
---|
[130] | 173 | CEGUI::System::getSingleton().injectMouseMove(e->getRelX() *
|
---|
| 174 | mGUIRenderer->getWidth(), e->getRelY() * mGUIRenderer->getHeight());
|
---|
[61] | 175 | }
|
---|
| 176 | //-----------------------------------------------------------------------
|
---|
[133] | 177 | void TerrainFrameListener::mousePressed(MouseEvent* e)
|
---|
[61] | 178 | {
|
---|
| 179 | // Left mouse button down
|
---|
| 180 | if (e->getButtonID() & InputEvent::BUTTON0_MASK)
|
---|
| 181 | {
|
---|
| 182 | mLMouseDown = true;
|
---|
| 183 | }
|
---|
| 184 | // Right mouse button down
|
---|
| 185 | else if (e->getButtonID() & InputEvent::BUTTON1_MASK)
|
---|
| 186 | {
|
---|
| 187 | CEGUI::MouseCursor::getSingleton().hide( );
|
---|
| 188 | mRMouseDown = true;
|
---|
| 189 | } // else if
|
---|
| 190 | } // mousePressed
|
---|
| 191 |
|
---|
| 192 | //-----------------------------------------------------------------------
|
---|
[133] | 193 | void TerrainFrameListener::mouseReleased(MouseEvent* e)
|
---|
[61] | 194 | {
|
---|
| 195 | // Left mouse button up
|
---|
| 196 | if (e->getButtonID() & InputEvent::BUTTON0_MASK)
|
---|
| 197 | {
|
---|
[85] | 198 | CEGUI::MouseCursor::getSingleton().show();
|
---|
[61] | 199 | mLMouseDown = false;
|
---|
| 200 | }
|
---|
| 201 | // Right mouse button up
|
---|
| 202 | else if (e->getButtonID() & InputEvent::BUTTON1_MASK)
|
---|
| 203 | {
|
---|
[85] | 204 | CEGUI::MouseCursor::getSingleton().show();
|
---|
[61] | 205 | mRMouseDown = false;
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 | //-----------------------------------------------------------------------
|
---|
[133] | 209 | void TerrainFrameListener::mouseDragged (MouseEvent *e)
|
---|
[61] | 210 | {
|
---|
| 211 | // If we are dragging the left mouse button.
|
---|
| 212 | if ( mLMouseDown )
|
---|
| 213 | {
|
---|
| 214 |
|
---|
| 215 | }
|
---|
| 216 | // If we are dragging the right mouse button.
|
---|
| 217 | if ( mRMouseDown )
|
---|
| 218 | {
|
---|
[107] | 219 | mCamera->yaw(-e->getRelX() * mRotateSpeed);
|
---|
| 220 | mCamera->pitch(-e->getRelY() * mRotateSpeed);
|
---|
[61] | 221 | }
|
---|
| 222 | }
|
---|
| 223 | //-----------------------------------------------------------------------
|
---|
[133] | 224 | bool TerrainFrameListener::frameStarted(const FrameEvent &evt)
|
---|
[61] | 225 | {
|
---|
| 226 | return ExampleFrameListener::frameStarted(evt);
|
---|
| 227 | }
|
---|
| 228 | //-----------------------------------------------------------------------
|
---|
[133] | 229 | bool TerrainFrameListener::frameEnded(const FrameEvent& evt)
|
---|
[61] | 230 | {
|
---|
| 231 | if (mShutdownRequested)
|
---|
| 232 | return false;
|
---|
| 233 |
|
---|
| 234 | if (timeDelay >= 0)
|
---|
| 235 | timeDelay -= evt.timeSinceLastFrame;
|
---|
| 236 |
|
---|
[85] | 237 | KEY_PRESSED(KC_SPACE, 0.3, nextAlgorithm());
|
---|
[61] | 238 |
|
---|
| 239 | KEY_PRESSED(KC_SUBTRACT, 0, changeThreshold(-10));
|
---|
| 240 | KEY_PRESSED(KC_ADD, 0, changeThreshold(10));
|
---|
[155] | 241 | KEY_PRESSED(KC_O, 0.3, toggleTestGeometryForVisibleLeaves());
|
---|
[115] | 242 | KEY_PRESSED(KC_C, 0.3, toggleUseDepthPass());
|
---|
[99] | 243 | KEY_PRESSED(KC_V, 0.3, toggleShowViz());
|
---|
[93] | 244 |
|
---|
[87] | 245 | updateStats();
|
---|
[61] | 246 |
|
---|
| 247 | return ExampleFrameListener::frameStarted(evt) && ExampleFrameListener::frameEnded(evt);
|
---|
| 248 | }
|
---|
| 249 | //-----------------------------------------------------------------------
|
---|
[133] | 250 | void TerrainFrameListener::changeThreshold(int incr)
|
---|
[61] | 251 | {
|
---|
[85] | 252 | mVisibilityThreshold += incr;
|
---|
| 253 | if(mVisibilityThreshold < 0) mVisibilityThreshold = 0;
|
---|
[61] | 254 |
|
---|
[85] | 255 | char str[100]; sprintf(str,": %d", mVisibilityThreshold);
|
---|
[61] | 256 |
|
---|
[85] | 257 | mSceneMgr->setOption("Threshold", &mVisibilityThreshold);
|
---|
[61] | 258 | mThresholdInfo->setCaption(str);
|
---|
| 259 | }
|
---|
| 260 | //-----------------------------------------------------------------------
|
---|
[133] | 261 | void TerrainFrameListener::nextAlgorithm()
|
---|
[61] | 262 | {
|
---|
[85] | 263 | mCurrentAlgorithm = ++mCurrentAlgorithm %
|
---|
| 264 | GtpVisibility::VisibilityEnvironment::NUM_CULLING_MANAGERS,
|
---|
[61] | 265 |
|
---|
[85] | 266 | setAlgorithm(mCurrentAlgorithm);
|
---|
| 267 | }
|
---|
| 268 | //-----------------------------------------------------------------------
|
---|
[155] | 269 | void TerrainFrameListener::toggleTestGeometryForVisibleLeaves()
|
---|
[86] | 270 | {
|
---|
[155] | 271 | mTestGeometryForVisibleLeaves = !mTestGeometryForVisibleLeaves;
|
---|
[86] | 272 |
|
---|
[155] | 273 | mSceneMgr->setOption("TestGeometryForVisibleLeaves", &mTestGeometryForVisibleLeaves);
|
---|
[86] | 274 |
|
---|
[155] | 275 | if(mTestGeometryForVisibleLeaves)
|
---|
| 276 | mTestGeometryForVisibleLeavesInfo->setCaption(": true");
|
---|
[86] | 277 | else
|
---|
[155] | 278 | mTestGeometryForVisibleLeavesInfo->setCaption(": false");
|
---|
[86] | 279 | }
|
---|
| 280 | //-----------------------------------------------------------------------
|
---|
[133] | 281 | void TerrainFrameListener::toggleShowOctree()
|
---|
[86] | 282 | {
|
---|
| 283 | mShowOctree = !mShowOctree;
|
---|
| 284 |
|
---|
| 285 | mSceneMgr->setOption("ShowOctree", &mShowOctree);
|
---|
| 286 | }
|
---|
| 287 | //-----------------------------------------------------------------------
|
---|
[133] | 288 | void TerrainFrameListener::toggleUseDepthPass()
|
---|
[87] | 289 | {
|
---|
[115] | 290 | mUseDepthPass = !mUseDepthPass;
|
---|
[87] | 291 |
|
---|
[115] | 292 | mSceneMgr->setOption("UseDepthPass", &mUseDepthPass);
|
---|
[87] | 293 | }
|
---|
| 294 | //-----------------------------------------------------------------------
|
---|
[133] | 295 | void TerrainFrameListener::toggleShowViz()
|
---|
[93] | 296 | {
|
---|
[99] | 297 | mShowVisualization = !mShowVisualization;
|
---|
[93] | 298 |
|
---|
[139] | 299 | mSceneMgr->setOption("PrepareVisualization", &mShowVisualization);
|
---|
[112] | 300 | mSceneMgr->setOption("VisualizeCulledNodes", &mVisualizeCulledNodes);
|
---|
[93] | 301 | }
|
---|
| 302 | //-----------------------------------------------------------------------
|
---|
[133] | 303 | void TerrainFrameListener::setAlgorithm(int algorithm)
|
---|
[85] | 304 | {
|
---|
[61] | 305 | mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
|
---|
| 306 | mSceneMgr->setOption("Algorithm", &mCurrentAlgorithm);
|
---|
| 307 | }
|
---|
| 308 | //-----------------------------------------------------------------------
|
---|
[133] | 309 | void TerrainFrameListener::updateStats()
|
---|
[61] | 310 | {
|
---|
| 311 | unsigned int opt = 0;
|
---|
| 312 | char str[100];
|
---|
| 313 |
|
---|
| 314 | mSceneMgr->getOption("NumFrustumCulledNodes", &opt); sprintf(str,": %d", opt);
|
---|
| 315 | mFrustumCulledNodesInfo->setCaption(str);
|
---|
| 316 |
|
---|
[87] | 317 | mSceneMgr->getOption("NumQueriesIssued", &opt); sprintf(str,": %d", opt);
|
---|
| 318 | mQueriesIssuedInfo->setCaption(str);
|
---|
| 319 |
|
---|
[61] | 320 | mSceneMgr->getOption("NumQueryCulledNodes", &opt); sprintf(str,": %d", opt);
|
---|
| 321 | mQueryCulledNodesInfo->setCaption(str);
|
---|
| 322 |
|
---|
| 323 | mSceneMgr->getOption("NumTraversedNodes", &opt); sprintf(str,": %d", opt);
|
---|
| 324 | mTraversedNodesInfo->setCaption(str);
|
---|
| 325 |
|
---|
[74] | 326 | mSceneMgr->getOption("NumHierarchyNodes", &opt); sprintf(str,": %d", opt);
|
---|
[61] | 327 | mHierarchyNodesInfo->setCaption(str);
|
---|
| 328 |
|
---|
| 329 | mSceneMgr->getOption("NumRenderedNodes", &opt); sprintf(str,": %d", opt);
|
---|
| 330 | mRenderedNodesInfo->setCaption(str);
|
---|
[84] | 331 |
|
---|
[85] | 332 | sprintf(str,": %d", mSceneContentGenerator->GetObjectCount());
|
---|
[160] | 333 | mObjectsCountInfo->setCaption(str);
|
---|
[61] | 334 | }
|
---|
| 335 | //-----------------------------------------------------------------------
|
---|
[133] | 336 | void TerrainFrameListener::keyPressed(KeyEvent* e)
|
---|
[61] | 337 | {
|
---|
| 338 | if(e->getKey() == KC_ESCAPE)
|
---|
| 339 | {
|
---|
| 340 | mShutdownRequested = true;
|
---|
| 341 | e->consume();
|
---|
| 342 | return;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | CEGUI::System::getSingleton().injectKeyDown(e->getKey());
|
---|
| 346 | CEGUI::System::getSingleton().injectChar(e->getKeyChar());
|
---|
| 347 | e->consume();
|
---|
| 348 | }
|
---|
| 349 | //-----------------------------------------------------------------------
|
---|
[133] | 350 | void TerrainFrameListener::keyReleased(KeyEvent* e)
|
---|
[61] | 351 | {
|
---|
| 352 | CEGUI::System::getSingleton().injectKeyUp(e->getKey());
|
---|
| 353 | e->consume();
|
---|
| 354 | }
|
---|
| 355 | //-----------------------------------------------------------------------
|
---|
[133] | 356 | void TerrainFrameListener::keyClicked(KeyEvent* e)
|
---|
[61] | 357 | {
|
---|
| 358 | // Do nothing
|
---|
| 359 | e->consume();
|
---|
| 360 | }
|
---|
| 361 | //-----------------------------------------------------------------------
|
---|
| 362 | INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
|
---|
| 363 | {
|
---|
| 364 | // Create application object
|
---|
| 365 | TestCullingApplication app;
|
---|
| 366 |
|
---|
| 367 | try
|
---|
| 368 | {
|
---|
| 369 | app.go();
|
---|
| 370 | }
|
---|
| 371 | catch( Ogre::Exception& e )
|
---|
| 372 | {
|
---|
| 373 | MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | return 0;
|
---|
| 377 | } |
---|