source: branches/VUT/DummyModules/obsolete/OcclusionCullingSceneManager/TestCulling/TestCullingApplication.cpp @ 52

Revision 52, 12.0 KB checked in by mattausch, 20 years ago (diff)
Line 
1/**
2    \file
3        TestCullingApplication.cpp
4    \brief
5        Tests the occlusion 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
15#include "Ogre.h"
16#include "TestCullingApplication.h"
17#include "OgreOcclusionCullingSceneTraverser.h"
18
19#define WIN32_LEAN_AND_MEAN
20#include "windows.h"
21
22
23/***********************************************/
24/* TestCullingApplication implementation       */
25/***********************************************/
26TestCullingApplication::~TestCullingApplication()
27{
28}
29//-----------------------------------------------------------------------
30/*void TestCullingApplication::createCamera( void )
31{
32        // Create the camera
33        mCamera = mSceneMgr->createCamera("PlayerCam");
34
35        // Position it at 500 in Z direction
36        mCamera->setPosition(Vector3(128,25,128));
37
38        // Look back along -Z
39    mCamera->lookAt(Vector3(0,0,-300));
40    mCamera->setNearClipDistance( 1 );
41    mCamera->setFarClipDistance( 1000 );
42}*/
43//-----------------------------------------------------------------------
44void TestCullingApplication::createScene(void)
45{
46        // Set ambient light
47        mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
48       
49        // Create a light
50        Light* l = mSceneMgr->createLight("MainLight");
51        //l->setPosition(20,80,50);
52
53        mMinTranslation = Vector3(-70.0f, -70.0f, 0.0f);
54        mMaxTranslation = Vector3(70.0f, 70.0f, 600.0f);
55
56        mMinAngle = Vector3(0, 0, 0);
57        mMaxAngle = Vector3(360, 360, 360);
58
59        generateScene(330);
60
61        // Create a skybox
62    //mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox", 1000, false);
63    ColourValue fadeColour(0.1, 0.1, 0.6);
64        mWindow->getViewport(0)->setBackgroundColour(fadeColour);
65
66        // CEGUI setup
67        setupGui();
68}
69//-----------------------------------------------------------------------
70void TestCullingApplication::setupGui( void )
71{
72         mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, ST_EXTERIOR_CLOSE);
73     mGUISystem = new CEGUI::System(mGUIRenderer);
74
75         // Mouse
76     CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLook.scheme");
77     CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");
78         mGUISystem->setDefaultMouseCursor(
79                (CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");
80
81         CEGUI::MouseCursor::getSingleton().show( );
82}
83//-----------------------------------------------------------------------
84void TestCullingApplication::createFrameListener(void)
85{
86        mFrameListener= new MouseQueryListener(mWindow, mCamera, mSceneMgr, mGUIRenderer);
87        mFrameListener->showDebugOverlay(true);
88        mRoot->addFrameListener(mFrameListener);
89}
90//-----------------------------------------------------------------------
91void TestCullingApplication::chooseSceneManager(void)
92{
93    mSceneMgr = mRoot->getSceneManager(ST_GENERIC);
94}
95//-----------------------------------------------------------------------
96void TestCullingApplication::generateSceneObject(const Vector3 &translationRatio,
97                                                                                                 const Vector3 &rotationRatio, const int idx, const String& entName)
98{
99        Vector3 rotation = mMinAngle + rotationRatio * (mMaxAngle - mMinAngle);
100        Vector3 translation = mMinTranslation + translationRatio * (mMaxTranslation - mMinTranslation);
101
102        char name[16];
103    sprintf( name, "object%d", idx );
104
105    Entity *ent = mSceneMgr->createEntity( name, entName );
106        SceneNode *node = mSceneMgr->getRootSceneNode()->createChildSceneNode( String(name) + "Node", translation);
107        node->attachObject(ent);
108        node->scale(0.1,0.1,0.1);
109
110        node->yaw(Degree(rotation.x));
111    node->pitch(Degree(rotation.y));
112        node->roll(Degree(rotation.z));
113}
114//-----------------------------------------------------------------------
115void TestCullingApplication::generateScene( int numObjects )
116{
117        srand (time (0));
118       
119        Vector3 rotationRatio;
120        Vector3 translationRatio;
121
122        for(int i=0; i < numObjects; i++)
123        {
124                rotationRatio.x = rand() / (float) RAND_MAX;
125                rotationRatio.y = rand() / (float) RAND_MAX;
126                rotationRatio.z = rand() / (float) RAND_MAX;
127
128                translationRatio.x = rand() / (float) RAND_MAX;
129                translationRatio.y = rand() / (float) RAND_MAX;
130                translationRatio.z = rand() / (float) RAND_MAX;
131
132                generateSceneObject(translationRatio, rotationRatio, i, "sphere.mesh");
133        }
134}
135/***********************************************/
136/* MouseQueryListener implementation           */
137/***********************************************/
138//-----------------------------------------------------------------------
139MouseQueryListener::MouseQueryListener(RenderWindow* win, Camera* cam, SceneManager *sceneManager, CEGUI::Renderer *renderer)
140        : ExampleFrameListener(win, cam, false, true), mGUIRenderer(renderer),
141                mShutdownRequested(false)
142{
143
144        // Setup default variables
145        //mOgreHead = NULL;
146        mLMouseDown = false;
147        mRMouseDown = false;
148        mSceneMgr = sceneManager;
149
150    // Reduce move speed
151        mMoveSpeed = 50;
152        mRotateSpeed *= 2;
153
154        mCurrentAlgorithm = OcclusionCullingSceneTraverser::RENDER_COHERENT;
155        mThreshold = 0;
156   
157        // Register this so that we get mouse events.
158        mEventProcessor->addMouseListener(this);
159        mEventProcessor->addMouseMotionListener(this);
160        mEventProcessor->addKeyListener(this);
161
162        // show overlay
163        Overlay* pOver = OverlayManager::getSingleton().getByName("Example/OcclusionCullingDemoOverlay");
164
165        mAlgorithmInfo = OverlayManager::getSingleton().getOverlayElement("Example/Occlusion/AlgorithmInfo");
166        mThresholdInfo = OverlayManager::getSingleton().getOverlayElement("Example/Occlusion/ThresholdInfo");
167        mFrustumCulledNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Occlusion/FrustumCulledNodesInfo");
168        mQueryCulledNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Occlusion/QueryCulledNodesInfo");
169    mTraversedNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Occlusion/TraversedNodesInfo");
170        mSceneNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Occlusion/SceneNodesInfo");
171        mHierarchyNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Occlusion/HierarchyNodesInfo");
172        mRenderedNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Occlusion/RenderedNodesInfo");
173
174        mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
175        mThresholdInfo->setCaption(": 0");
176        mFrustumCulledNodesInfo->setCaption(": 0");
177        mQueryCulledNodesInfo->setCaption(": 0");
178        mTraversedNodesInfo->setCaption(": 0");
179        mSceneNodesInfo->setCaption(": 0");
180        mHierarchyNodesInfo->setCaption(": 0");
181        mRenderedNodesInfo->setCaption(": 0");
182
183    pOver->show();
184}
185//-----------------------------------------------------------------------
186MouseQueryListener::~MouseQueryListener( )
187{
188}
189//-----------------------------------------------------------------------
190void MouseQueryListener::mouseMoved (MouseEvent *e)
191{
192        // Update CEGUI with the mouse motion
193    CEGUI::System::getSingleton().injectMouseMove(e->getRelX() * mGUIRenderer->getWidth(), e->getRelY() * mGUIRenderer->getHeight());
194}
195//-----------------------------------------------------------------------
196void MouseQueryListener::mousePressed(MouseEvent* e)
197{
198     // Left mouse button down
199     if (e->getButtonID() & InputEvent::BUTTON0_MASK)
200     {
201                mLMouseDown = true;
202     }
203     // Right mouse button down
204     else if (e->getButtonID() & InputEvent::BUTTON1_MASK)
205     {
206         CEGUI::MouseCursor::getSingleton().hide( );
207         mRMouseDown = true;
208     } // else if
209} // mousePressed
210
211 //-----------------------------------------------------------------------
212void MouseQueryListener::mouseReleased(MouseEvent* e)
213{
214    // Left mouse button up
215    if (e->getButtonID() & InputEvent::BUTTON0_MASK)
216    {
217                CEGUI::MouseCursor::getSingleton().show( );
218        mLMouseDown = false;
219    }
220    // Right mouse button up
221    else if (e->getButtonID() & InputEvent::BUTTON1_MASK)
222    {
223        CEGUI::MouseCursor::getSingleton().show( );
224        mRMouseDown = false;
225    }
226}
227//-----------------------------------------------------------------------
228void MouseQueryListener::mouseDragged (MouseEvent *e)
229 {
230         // If we are dragging the left mouse button.           
231         if ( mLMouseDown )
232     {
233               
234     }
235         // If we are dragging the right mouse button.
236         if ( mRMouseDown )
237         {
238                 mCamera->yaw( -e->getRelX() * mRotateSpeed );
239                 mCamera->pitch( -e->getRelY() * mRotateSpeed );
240         }
241}
242//-----------------------------------------------------------------------
243bool MouseQueryListener::frameStarted(const FrameEvent &evt)
244{       
245        return ExampleFrameListener::frameStarted(evt);
246}
247//-----------------------------------------------------------------------
248bool MouseQueryListener::frameEnded(const FrameEvent& evt)
249{
250        if (mShutdownRequested)
251                return false;
252
253    if (timeDelay >= 0)
254        timeDelay -= evt.timeSinceLastFrame;
255
256    KEY_PRESSED(KC_SPACE, 0.3, changeAlgorithm());
257
258        KEY_PRESSED(KC_SUBTRACT, 0, changeThreshold(-10));
259        KEY_PRESSED(KC_ADD, 0, changeThreshold(10));
260        //KEY_PRESSED(KC_T, 1, change);
261     
262        changeStats();
263
264    return ExampleFrameListener::frameStarted(evt) && ExampleFrameListener::frameEnded(evt);       
265}
266//-----------------------------------------------------------------------
267void MouseQueryListener::changeThreshold(int incr)
268{
269        mThreshold += incr; if(mThreshold < 0) mThreshold = 0;
270       
271        char str[100]; sprintf(str,": %d", mThreshold);
272
273        mSceneMgr->setOption("Threshold", &mThreshold);
274        mThresholdInfo->setCaption(str);
275}
276//-----------------------------------------------------------------------
277void MouseQueryListener::changeAlgorithm()
278{
279    mCurrentAlgorithm = ++mCurrentAlgorithm % OcclusionCullingSceneTraverser::NUM_RENDERMODES;
280
281        mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
282        mSceneMgr->setOption("Algorithm", &mCurrentAlgorithm);
283}
284//-----------------------------------------------------------------------
285void MouseQueryListener::changeStats()
286{
287        unsigned int opt = 0;
288        char str[100];
289       
290        mSceneMgr->getOption("NumFrustumCulledNodes", &opt); sprintf(str,": %d", opt);
291        mFrustumCulledNodesInfo->setCaption(str);
292       
293        mSceneMgr->getOption("NumQueryCulledNodes", &opt); sprintf(str,": %d", opt);
294        mQueryCulledNodesInfo->setCaption(str);
295       
296        mSceneMgr->getOption("NumTraversedNodes", &opt); sprintf(str,": %d", opt);
297        mTraversedNodesInfo->setCaption(str);
298
299        mSceneMgr->getOption("NumSceneNodes", &opt); sprintf(str,": %d", opt);
300        mSceneNodesInfo->setCaption(str);
301
302        mSceneMgr->getOption("NumOctreeNodes", &opt); sprintf(str,": %d", opt);
303        mHierarchyNodesInfo->setCaption(str);
304
305        mSceneMgr->getOption("NumRenderedNodes", &opt); sprintf(str,": %d", opt);
306        mRenderedNodesInfo->setCaption(str);
307}
308//-----------------------------------------------------------------------
309void MouseQueryListener::keyPressed(KeyEvent* e)
310{
311        if(e->getKey() == KC_ESCAPE)
312    {
313                mShutdownRequested = true;
314                e->consume();
315                return;
316        }
317
318        CEGUI::System::getSingleton().injectKeyDown(e->getKey());
319        CEGUI::System::getSingleton().injectChar(e->getKeyChar());
320        e->consume();
321}
322//-----------------------------------------------------------------------
323void MouseQueryListener::keyReleased(KeyEvent* e)
324{
325        CEGUI::System::getSingleton().injectKeyUp(e->getKey());
326        e->consume();
327}
328//-----------------------------------------------------------------------
329void MouseQueryListener::keyClicked(KeyEvent* e)
330{
331        // Do nothing
332        e->consume();
333}
334//-----------------------------------------------------------------------
335INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
336{
337    // Create application object
338    TestCullingApplication app;
339
340        try
341        {
342        app.go();
343    }
344        catch( Ogre::Exception& e )
345        {
346        MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
347    }   
348
349    return 0;
350}
Note: See TracBrowser for help on using the repository browser.