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

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