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