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

Revision 74, 11.7 KB checked in by mattausch, 19 years ago (diff)

added support for release mode

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