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

Revision 103, 12.9 KB checked in by mattausch, 20 years ago (diff)

fixed bug with shadow maps

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