source: branches/VUT/0.2/work/TestCulling/TestCullingApplication.cpp @ 160

Revision 160, 13.2 KB checked in by mattausch, 19 years ago (diff)

added animation

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