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)

Line 
1/**
2    \file
3        TestCullingApplication.cpp
4    \brief
5        Tests the visibility culling algorithm
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>
14#include <Ogre.h>
15#include "TestCullingApplication.h"
16#include "OgreSceneContentGenerator.h"
17
18#define WIN32_LEAN_AND_MEAN
19#include <windows.h>
20
21
22/***********************************************/
23/* TestCullingApplication implementation       */
24/***********************************************/
25
26//-----------------------------------------------------------------------
27/*void TestCullingApplication::createCamera( void )
28{
29        // Create the camera
30        mCamera = mSceneMgr->createCamera("CullCamera");
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//-----------------------------------------------------------------------
41TestCullingApplication::~TestCullingApplication()
42{
43        delete mSceneContentGenerator;
44}
45//-----------------------------------------------------------------------
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
55        mSceneContentGenerator = new SceneContentGenerator(mSceneMgr);
56        mSceneContentGenerator->GenerateScene(3000, "sphere.mesh");
57
58        // Create a skybox
59    mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox", 2000, true);
60    ColourValue fadeColour(0.1, 0.1, 0.6);
61        mWindow->getViewport(0)->setBackgroundColour(fadeColour);
62
63        // CEGUI setup
64        setupGui();
65}
66//-----------------------------------------------------------------------
67void TestCullingApplication::setupGui()
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//-----------------------------------------------------------------------
81void TestCullingApplication::createFrameListener()
82{
83        mFrameListener= new TerrainMouseQueryListener(mWindow, mCamera, mSceneMgr,
84                mGUIRenderer, mSceneContentGenerator);
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/***********************************************/
96/* TerrainMouseQueryListener implementation           */
97/***********************************************/
98//-----------------------------------------------------------------------
99TerrainMouseQueryListener::TerrainMouseQueryListener(RenderWindow* win, Camera* cam,
100                                                                           SceneManager *sceneManager,
101                                                                           CEGUI::Renderer *renderer,
102                                                                           SceneContentGenerator *sceneContentGenerator)
103: ExampleFrameListener(win, cam, false, true),
104mSceneMgr(sceneManager),
105mGUIRenderer(renderer),
106mShutdownRequested(false),
107mLMouseDown(false),
108mRMouseDown(false),
109mSceneContentGenerator(sceneContentGenerator),
110mVisibilityThreshold(0),
111mCurrentAlgorithm(GtpVisibility::VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING),
112mShowOctree(false),
113mUseDepthPass(false),
114mUseOptimization(true),
115mShowVisualization(false),
116mVisualizeCulledNodes(false)
117{
118    // Reduce move speed
119        mMoveSpeed = 50;
120        mRotateSpeed *= 2;
121 
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
130        mAlgorithmInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/AlgorithmInfo");
131        mThresholdInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/ThresholdInfo");
132       
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");
138        mObjectsInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/ObjectsInfo");
139        mUseOptimizationInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/UseOptimizationInfo");
140        mQueriesIssuedInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/QueriesIssuedInfo");
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");
149        mObjectsInfo->setCaption(": 0");
150        mUseOptimizationInfo->setCaption(": true");
151        mQueriesIssuedInfo->setCaption(": 0");
152
153        setAlgorithm(mCurrentAlgorithm);
154       
155        mSceneMgr->setOption("UseOptimization", &mUseOptimization);
156        mSceneMgr->setOption("UseDepthPass", &mUseDepthPass);
157        mSceneMgr->setOption("ShowVizualisation", &mShowVisualization);
158        mSceneMgr->setOption("ShowOctree", &mShowOctree);
159
160    pOver->show();
161}
162//-----------------------------------------------------------------------
163TerrainMouseQueryListener::~TerrainMouseQueryListener( )
164{
165}
166//-----------------------------------------------------------------------
167void TerrainMouseQueryListener::mouseMoved (MouseEvent *e)
168{
169        // Update CEGUI with the mouse motion
170    CEGUI::System::getSingleton().injectMouseMove(e->getRelX() * mGUIRenderer->getWidth(), e->getRelY() * mGUIRenderer->getHeight());
171}
172//-----------------------------------------------------------------------
173void TerrainMouseQueryListener::mousePressed(MouseEvent* e)
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 //-----------------------------------------------------------------------
189void TerrainMouseQueryListener::mouseReleased(MouseEvent* e)
190{
191    // Left mouse button up
192    if (e->getButtonID() & InputEvent::BUTTON0_MASK)
193    {
194                CEGUI::MouseCursor::getSingleton().show();
195        mLMouseDown = false;
196    }
197    // Right mouse button up
198    else if (e->getButtonID() & InputEvent::BUTTON1_MASK)
199    {
200        CEGUI::MouseCursor::getSingleton().show();
201        mRMouseDown = false;
202    }
203}
204//-----------------------------------------------------------------------
205void TerrainMouseQueryListener::mouseDragged (MouseEvent *e)
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         {
215                 mCamera->yaw(-e->getRelX() * mRotateSpeed);
216                 mCamera->pitch(-e->getRelY() * mRotateSpeed);
217         }
218}
219//-----------------------------------------------------------------------
220bool TerrainMouseQueryListener::frameStarted(const FrameEvent &evt)
221{       
222        return ExampleFrameListener::frameStarted(evt);
223}
224//-----------------------------------------------------------------------
225bool TerrainMouseQueryListener::frameEnded(const FrameEvent& evt)
226{
227        if (mShutdownRequested)
228                return false;
229
230    if (timeDelay >= 0)
231        timeDelay -= evt.timeSinceLastFrame;
232
233    KEY_PRESSED(KC_SPACE, 0.3, nextAlgorithm());
234
235        KEY_PRESSED(KC_SUBTRACT, 0, changeThreshold(-10));
236        KEY_PRESSED(KC_ADD, 0, changeThreshold(10));
237        KEY_PRESSED(KC_O, 0.3, toggleUseOptimization());
238        KEY_PRESSED(KC_C, 0.3, toggleUseDepthPass());
239        KEY_PRESSED(KC_V, 0.3, toggleShowViz());
240
241        updateStats();
242
243    return ExampleFrameListener::frameStarted(evt) && ExampleFrameListener::frameEnded(evt);       
244}
245//-----------------------------------------------------------------------
246void TerrainMouseQueryListener::changeThreshold(int incr)
247{
248        mVisibilityThreshold += incr;
249        if(mVisibilityThreshold < 0) mVisibilityThreshold = 0;
250       
251        char str[100]; sprintf(str,": %d", mVisibilityThreshold);
252
253        mSceneMgr->setOption("Threshold", &mVisibilityThreshold);
254        mThresholdInfo->setCaption(str);
255}
256//-----------------------------------------------------------------------
257void TerrainMouseQueryListener::nextAlgorithm()
258{
259        mCurrentAlgorithm = ++mCurrentAlgorithm %
260                GtpVisibility::VisibilityEnvironment::NUM_CULLING_MANAGERS,
261
262        setAlgorithm(mCurrentAlgorithm);
263}
264//-----------------------------------------------------------------------
265void TerrainMouseQueryListener::toggleUseOptimization()
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//-----------------------------------------------------------------------
277void TerrainMouseQueryListener::toggleShowOctree()
278{
279        mShowOctree = !mShowOctree;
280
281        mSceneMgr->setOption("ShowOctree", &mShowOctree);
282}
283//-----------------------------------------------------------------------
284void TerrainMouseQueryListener::toggleUseDepthPass()
285{
286        mUseDepthPass = !mUseDepthPass;
287
288        mSceneMgr->setOption("UseDepthPass", &mUseDepthPass);
289}
290//-----------------------------------------------------------------------
291void TerrainMouseQueryListener::toggleShowViz()
292{
293        mShowVisualization = !mShowVisualization;
294
295        mSceneMgr->setOption("ShowVisualization", &mShowVisualization);
296        mSceneMgr->setOption("VisualizeCulledNodes", &mVisualizeCulledNodes);
297}
298//-----------------------------------------------------------------------
299void TerrainMouseQueryListener::setAlgorithm(int algorithm)
300{
301        mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
302        mSceneMgr->setOption("Algorithm", &mCurrentAlgorithm);
303}
304//-----------------------------------------------------------------------
305void TerrainMouseQueryListener::updateStats()
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       
313        mSceneMgr->getOption("NumQueriesIssued", &opt); sprintf(str,": %d", opt);
314        mQueriesIssuedInfo->setCaption(str);
315
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
322        mSceneMgr->getOption("NumHierarchyNodes", &opt); sprintf(str,": %d", opt);
323        mHierarchyNodesInfo->setCaption(str);
324
325        mSceneMgr->getOption("NumRenderedNodes", &opt); sprintf(str,": %d", opt);
326        mRenderedNodesInfo->setCaption(str);
327
328        sprintf(str,": %d", mSceneContentGenerator->GetObjectCount());
329        mObjectsInfo->setCaption(str);
330}
331//-----------------------------------------------------------------------
332void TerrainMouseQueryListener::keyPressed(KeyEvent* e)
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//-----------------------------------------------------------------------
346void TerrainMouseQueryListener::keyReleased(KeyEvent* e)
347{
348        CEGUI::System::getSingleton().injectKeyUp(e->getKey());
349        e->consume();
350}
351//-----------------------------------------------------------------------
352void TerrainMouseQueryListener::keyClicked(KeyEvent* e)
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.