source: trunk/VUT/work/TestCulling/TestCullingApplication.cpp @ 115

Revision 115, 13.1 KB checked in by mattausch, 19 years ago (diff)

added depth pass algorithm + delayed transparent object rendering (so depth ordering is right)

RevLine 
[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]41TestCullingApplication::~TestCullingApplication()
42{
43        delete mSceneContentGenerator;
44}
45//-----------------------------------------------------------------------
[61]46void 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]67void 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]81void TestCullingApplication::createFrameListener()
[61]82{
[115]83        mFrameListener= new TerrainMouseQueryListener(mWindow, mCamera, mSceneMgr,
[85]84                mGUIRenderer, mSceneContentGenerator);
[61]85        mFrameListener->showDebugOverlay(true);
86        mRoot->addFrameListener(mFrameListener);
87}
88//-----------------------------------------------------------------------
89void TestCullingApplication::chooseSceneManager(void)
90{
91    mSceneMgr = mRoot->getSceneManager(ST_GENERIC);
92}
93
94
95/***********************************************/
[115]96/* TerrainMouseQueryListener implementation           */
[61]97/***********************************************/
98//-----------------------------------------------------------------------
[115]99TerrainMouseQueryListener::TerrainMouseQueryListener(RenderWindow* win, Camera* cam,
[85]100                                                                           SceneManager *sceneManager,
101                                                                           CEGUI::Renderer *renderer,
102                                                                           SceneContentGenerator *sceneContentGenerator)
[86]103: ExampleFrameListener(win, cam, false, true),
104mSceneMgr(sceneManager),
105mGUIRenderer(renderer),
106mShutdownRequested(false),
107mLMouseDown(false),
108mRMouseDown(false),
109mSceneContentGenerator(sceneContentGenerator),
110mVisibilityThreshold(0),
[87]111mCurrentAlgorithm(GtpVisibility::VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING),
[93]112mShowOctree(false),
[115]113mUseDepthPass(false),
[94]114mUseOptimization(true),
[112]115mShowVisualization(false),
116mVisualizeCulledNodes(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//-----------------------------------------------------------------------
[115]163TerrainMouseQueryListener::~TerrainMouseQueryListener( )
[61]164{
165}
166//-----------------------------------------------------------------------
[115]167void TerrainMouseQueryListener::mouseMoved (MouseEvent *e)
[61]168{
169        // Update CEGUI with the mouse motion
170    CEGUI::System::getSingleton().injectMouseMove(e->getRelX() * mGUIRenderer->getWidth(), e->getRelY() * mGUIRenderer->getHeight());
171}
172//-----------------------------------------------------------------------
[115]173void TerrainMouseQueryListener::mousePressed(MouseEvent* e)
[61]174{
175     // Left mouse button down
176     if (e->getButtonID() & InputEvent::BUTTON0_MASK)
177     {
178                mLMouseDown = true;
179     }
180     // Right mouse button down
181     else if (e->getButtonID() & InputEvent::BUTTON1_MASK)
182     {
183         CEGUI::MouseCursor::getSingleton().hide( );
184         mRMouseDown = true;
185     } // else if
186} // mousePressed
187
188 //-----------------------------------------------------------------------
[115]189void TerrainMouseQueryListener::mouseReleased(MouseEvent* e)
[61]190{
191    // Left mouse button up
192    if (e->getButtonID() & InputEvent::BUTTON0_MASK)
193    {
[85]194                CEGUI::MouseCursor::getSingleton().show();
[61]195        mLMouseDown = false;
196    }
197    // Right mouse button up
198    else if (e->getButtonID() & InputEvent::BUTTON1_MASK)
199    {
[85]200        CEGUI::MouseCursor::getSingleton().show();
[61]201        mRMouseDown = false;
202    }
203}
204//-----------------------------------------------------------------------
[115]205void TerrainMouseQueryListener::mouseDragged (MouseEvent *e)
[61]206 {
207         // If we are dragging the left mouse button.           
208         if ( mLMouseDown )
209     {
210               
211     }
212         // If we are dragging the right mouse button.
213         if ( mRMouseDown )
214         {
[107]215                 mCamera->yaw(-e->getRelX() * mRotateSpeed);
216                 mCamera->pitch(-e->getRelY() * mRotateSpeed);
[61]217         }
218}
219//-----------------------------------------------------------------------
[115]220bool TerrainMouseQueryListener::frameStarted(const FrameEvent &evt)
[61]221{       
222        return ExampleFrameListener::frameStarted(evt);
223}
224//-----------------------------------------------------------------------
[115]225bool TerrainMouseQueryListener::frameEnded(const FrameEvent& evt)
[61]226{
227        if (mShutdownRequested)
228                return false;
229
230    if (timeDelay >= 0)
231        timeDelay -= evt.timeSinceLastFrame;
232
[85]233    KEY_PRESSED(KC_SPACE, 0.3, nextAlgorithm());
[61]234
235        KEY_PRESSED(KC_SUBTRACT, 0, changeThreshold(-10));
236        KEY_PRESSED(KC_ADD, 0, changeThreshold(10));
[86]237        KEY_PRESSED(KC_O, 0.3, toggleUseOptimization());
[115]238        KEY_PRESSED(KC_C, 0.3, toggleUseDepthPass());
[99]239        KEY_PRESSED(KC_V, 0.3, toggleShowViz());
[93]240
[87]241        updateStats();
[61]242
243    return ExampleFrameListener::frameStarted(evt) && ExampleFrameListener::frameEnded(evt);       
244}
245//-----------------------------------------------------------------------
[115]246void TerrainMouseQueryListener::changeThreshold(int incr)
[61]247{
[85]248        mVisibilityThreshold += incr;
249        if(mVisibilityThreshold < 0) mVisibilityThreshold = 0;
[61]250       
[85]251        char str[100]; sprintf(str,": %d", mVisibilityThreshold);
[61]252
[85]253        mSceneMgr->setOption("Threshold", &mVisibilityThreshold);
[61]254        mThresholdInfo->setCaption(str);
255}
256//-----------------------------------------------------------------------
[115]257void TerrainMouseQueryListener::nextAlgorithm()
[61]258{
[85]259        mCurrentAlgorithm = ++mCurrentAlgorithm %
260                GtpVisibility::VisibilityEnvironment::NUM_CULLING_MANAGERS,
[61]261
[85]262        setAlgorithm(mCurrentAlgorithm);
263}
264//-----------------------------------------------------------------------
[115]265void TerrainMouseQueryListener::toggleUseOptimization()
[86]266{
267        mUseOptimization = !mUseOptimization;
268
269        mSceneMgr->setOption("UseOptimization", &mUseOptimization);
270
271        if(mUseOptimization)
272                mUseOptimizationInfo->setCaption(": true");
273        else
274                mUseOptimizationInfo->setCaption(": false");
275}
276//-----------------------------------------------------------------------
[115]277void TerrainMouseQueryListener::toggleShowOctree()
[86]278{
279        mShowOctree = !mShowOctree;
280
281        mSceneMgr->setOption("ShowOctree", &mShowOctree);
282}
283//-----------------------------------------------------------------------
[115]284void TerrainMouseQueryListener::toggleUseDepthPass()
[87]285{
[115]286        mUseDepthPass = !mUseDepthPass;
[87]287
[115]288        mSceneMgr->setOption("UseDepthPass", &mUseDepthPass);
[87]289}
290//-----------------------------------------------------------------------
[115]291void TerrainMouseQueryListener::toggleShowViz()
[93]292{
[99]293        mShowVisualization = !mShowVisualization;
[93]294
[100]295        mSceneMgr->setOption("ShowVisualization", &mShowVisualization);
[112]296        mSceneMgr->setOption("VisualizeCulledNodes", &mVisualizeCulledNodes);
[93]297}
298//-----------------------------------------------------------------------
[115]299void TerrainMouseQueryListener::setAlgorithm(int algorithm)
[85]300{
[61]301        mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
302        mSceneMgr->setOption("Algorithm", &mCurrentAlgorithm);
303}
304//-----------------------------------------------------------------------
[115]305void TerrainMouseQueryListener::updateStats()
[61]306{
307        unsigned int opt = 0;
308        char str[100];
309       
310        mSceneMgr->getOption("NumFrustumCulledNodes", &opt); sprintf(str,": %d", opt);
311        mFrustumCulledNodesInfo->setCaption(str);
312       
[87]313        mSceneMgr->getOption("NumQueriesIssued", &opt); sprintf(str,": %d", opt);
314        mQueriesIssuedInfo->setCaption(str);
315
[61]316        mSceneMgr->getOption("NumQueryCulledNodes", &opt); sprintf(str,": %d", opt);
317        mQueryCulledNodesInfo->setCaption(str);
318       
319        mSceneMgr->getOption("NumTraversedNodes", &opt); sprintf(str,": %d", opt);
320        mTraversedNodesInfo->setCaption(str);
321
[74]322        mSceneMgr->getOption("NumHierarchyNodes", &opt); sprintf(str,": %d", opt);
[61]323        mHierarchyNodesInfo->setCaption(str);
324
325        mSceneMgr->getOption("NumRenderedNodes", &opt); sprintf(str,": %d", opt);
326        mRenderedNodesInfo->setCaption(str);
[84]327
[85]328        sprintf(str,": %d", mSceneContentGenerator->GetObjectCount());
[87]329        mObjectsInfo->setCaption(str);
[61]330}
331//-----------------------------------------------------------------------
[115]332void TerrainMouseQueryListener::keyPressed(KeyEvent* e)
[61]333{
334        if(e->getKey() == KC_ESCAPE)
335    {
336                mShutdownRequested = true;
337                e->consume();
338                return;
339        }
340
341        CEGUI::System::getSingleton().injectKeyDown(e->getKey());
342        CEGUI::System::getSingleton().injectChar(e->getKeyChar());
343        e->consume();
344}
345//-----------------------------------------------------------------------
[115]346void TerrainMouseQueryListener::keyReleased(KeyEvent* e)
[61]347{
348        CEGUI::System::getSingleton().injectKeyUp(e->getKey());
349        e->consume();
350}
351//-----------------------------------------------------------------------
[115]352void TerrainMouseQueryListener::keyClicked(KeyEvent* e)
[61]353{
354        // Do nothing
355        e->consume();
356}
357//-----------------------------------------------------------------------
358INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
359{
360    // Create application object
361    TestCullingApplication app;
362
363        try
364        {
365        app.go();
366    }
367        catch( Ogre::Exception& e )
368        {
369        MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
370    }   
371
372    return 0;
373}
Note: See TracBrowser for help on using the repository browser.