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

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