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 | //-----------------------------------------------------------------------
|
---|
41 | void 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 | SceneContentGenerator contentGenerator(mSceneMgr);
|
---|
51 | contentGenerator.GenerateScene(330, "robot.mesh");
|
---|
52 |
|
---|
53 | // Create a skybox
|
---|
54 | //mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox", 1000, false);
|
---|
55 | ColourValue fadeColour(0.1, 0.1, 0.6);
|
---|
56 | mWindow->getViewport(0)->setBackgroundColour(fadeColour);
|
---|
57 |
|
---|
58 | // CEGUI setup
|
---|
59 | setupGui();
|
---|
60 | }
|
---|
61 | //-----------------------------------------------------------------------
|
---|
62 | void TestCullingApplication::setupGui( void )
|
---|
63 | {
|
---|
64 | mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, ST_EXTERIOR_CLOSE);
|
---|
65 | mGUISystem = new CEGUI::System(mGUIRenderer);
|
---|
66 |
|
---|
67 | // Mouse
|
---|
68 | CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLook.scheme");
|
---|
69 | CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");
|
---|
70 | mGUISystem->setDefaultMouseCursor(
|
---|
71 | (CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");
|
---|
72 |
|
---|
73 | CEGUI::MouseCursor::getSingleton().show( );
|
---|
74 | }
|
---|
75 | //-----------------------------------------------------------------------
|
---|
76 | void TestCullingApplication::createFrameListener(void)
|
---|
77 | {
|
---|
78 | mFrameListener= new MouseQueryListener(mWindow, mCamera, mSceneMgr, mGUIRenderer);
|
---|
79 | mFrameListener->showDebugOverlay(true);
|
---|
80 | mRoot->addFrameListener(mFrameListener);
|
---|
81 | }
|
---|
82 | //-----------------------------------------------------------------------
|
---|
83 | void TestCullingApplication::chooseSceneManager(void)
|
---|
84 | {
|
---|
85 | mSceneMgr = mRoot->getSceneManager(ST_GENERIC);
|
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | /***********************************************/
|
---|
90 | /* MouseQueryListener implementation */
|
---|
91 | /***********************************************/
|
---|
92 | //-----------------------------------------------------------------------
|
---|
93 | MouseQueryListener::MouseQueryListener(RenderWindow* win, Camera* cam, SceneManager *sceneManager, CEGUI::Renderer *renderer)
|
---|
94 | : ExampleFrameListener(win, cam, false, true), mGUIRenderer(renderer),
|
---|
95 | mShutdownRequested(false)
|
---|
96 | {
|
---|
97 |
|
---|
98 | // Setup default variables
|
---|
99 | //mOgreHead = NULL;
|
---|
100 | mLMouseDown = false;
|
---|
101 | mRMouseDown = false;
|
---|
102 | mSceneMgr = sceneManager;
|
---|
103 |
|
---|
104 | // Reduce move speed
|
---|
105 | mMoveSpeed = 50;
|
---|
106 | mRotateSpeed *= 2;
|
---|
107 |
|
---|
108 | mCurrentAlgorithm = GtpVisibility::VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING;
|
---|
109 | mThreshold = 0;
|
---|
110 |
|
---|
111 | // Register this so that we get mouse events.
|
---|
112 | mEventProcessor->addMouseListener(this);
|
---|
113 | mEventProcessor->addMouseMotionListener(this);
|
---|
114 | mEventProcessor->addKeyListener(this);
|
---|
115 |
|
---|
116 | // show overlay
|
---|
117 | Overlay* pOver = OverlayManager::getSingleton().getByName("Example/VisibilityDemoOverlay");
|
---|
118 |
|
---|
119 | mAlgorithmInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/AlgorithmInfo");
|
---|
120 | mThresholdInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/ThresholdInfo");
|
---|
121 | mFrustumCulledNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/FrustumCulledNodesInfo");
|
---|
122 | mQueryCulledNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/QueryCulledNodesInfo");
|
---|
123 | mTraversedNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/TraversedNodesInfo");
|
---|
124 | mHierarchyNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/HierarchyNodesInfo");
|
---|
125 | mRenderedNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/RenderedNodesInfo");
|
---|
126 |
|
---|
127 | mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
|
---|
128 | mThresholdInfo->setCaption(": 0");
|
---|
129 | mFrustumCulledNodesInfo->setCaption(": 0");
|
---|
130 | mQueryCulledNodesInfo->setCaption(": 0");
|
---|
131 | mTraversedNodesInfo->setCaption(": 0");
|
---|
132 | mHierarchyNodesInfo->setCaption(": 0");
|
---|
133 | mRenderedNodesInfo->setCaption(": 0");
|
---|
134 |
|
---|
135 | pOver->show();
|
---|
136 | }
|
---|
137 | //-----------------------------------------------------------------------
|
---|
138 | MouseQueryListener::~MouseQueryListener( )
|
---|
139 | {
|
---|
140 | }
|
---|
141 | //-----------------------------------------------------------------------
|
---|
142 | void MouseQueryListener::mouseMoved (MouseEvent *e)
|
---|
143 | {
|
---|
144 | // Update CEGUI with the mouse motion
|
---|
145 | CEGUI::System::getSingleton().injectMouseMove(e->getRelX() * mGUIRenderer->getWidth(), e->getRelY() * mGUIRenderer->getHeight());
|
---|
146 | }
|
---|
147 | //-----------------------------------------------------------------------
|
---|
148 | void MouseQueryListener::mousePressed(MouseEvent* e)
|
---|
149 | {
|
---|
150 | // Left mouse button down
|
---|
151 | if (e->getButtonID() & InputEvent::BUTTON0_MASK)
|
---|
152 | {
|
---|
153 | mLMouseDown = true;
|
---|
154 | }
|
---|
155 | // Right mouse button down
|
---|
156 | else if (e->getButtonID() & InputEvent::BUTTON1_MASK)
|
---|
157 | {
|
---|
158 | CEGUI::MouseCursor::getSingleton().hide( );
|
---|
159 | mRMouseDown = true;
|
---|
160 | } // else if
|
---|
161 | } // mousePressed
|
---|
162 |
|
---|
163 | //-----------------------------------------------------------------------
|
---|
164 | void MouseQueryListener::mouseReleased(MouseEvent* e)
|
---|
165 | {
|
---|
166 | // Left mouse button up
|
---|
167 | if (e->getButtonID() & InputEvent::BUTTON0_MASK)
|
---|
168 | {
|
---|
169 | CEGUI::MouseCursor::getSingleton().show( );
|
---|
170 | mLMouseDown = false;
|
---|
171 | }
|
---|
172 | // Right mouse button up
|
---|
173 | else if (e->getButtonID() & InputEvent::BUTTON1_MASK)
|
---|
174 | {
|
---|
175 | CEGUI::MouseCursor::getSingleton().show( );
|
---|
176 | mRMouseDown = false;
|
---|
177 | }
|
---|
178 | }
|
---|
179 | //-----------------------------------------------------------------------
|
---|
180 | void MouseQueryListener::mouseDragged (MouseEvent *e)
|
---|
181 | {
|
---|
182 | // If we are dragging the left mouse button.
|
---|
183 | if ( mLMouseDown )
|
---|
184 | {
|
---|
185 |
|
---|
186 | }
|
---|
187 | // If we are dragging the right mouse button.
|
---|
188 | if ( mRMouseDown )
|
---|
189 | {
|
---|
190 | mCamera->yaw( -e->getRelX() * mRotateSpeed );
|
---|
191 | mCamera->pitch( -e->getRelY() * mRotateSpeed );
|
---|
192 | }
|
---|
193 | }
|
---|
194 | //-----------------------------------------------------------------------
|
---|
195 | bool MouseQueryListener::frameStarted(const FrameEvent &evt)
|
---|
196 | {
|
---|
197 | return ExampleFrameListener::frameStarted(evt);
|
---|
198 | }
|
---|
199 | //-----------------------------------------------------------------------
|
---|
200 | bool MouseQueryListener::frameEnded(const FrameEvent& evt)
|
---|
201 | {
|
---|
202 | if (mShutdownRequested)
|
---|
203 | return false;
|
---|
204 |
|
---|
205 | if (timeDelay >= 0)
|
---|
206 | timeDelay -= evt.timeSinceLastFrame;
|
---|
207 |
|
---|
208 | KEY_PRESSED(KC_SPACE, 0.3, changeAlgorithm());
|
---|
209 |
|
---|
210 | KEY_PRESSED(KC_SUBTRACT, 0, changeThreshold(-10));
|
---|
211 | KEY_PRESSED(KC_ADD, 0, changeThreshold(10));
|
---|
212 | //KEY_PRESSED(KC_T, 1, change);
|
---|
213 |
|
---|
214 | changeStats();
|
---|
215 |
|
---|
216 | return ExampleFrameListener::frameStarted(evt) && ExampleFrameListener::frameEnded(evt);
|
---|
217 | }
|
---|
218 | //-----------------------------------------------------------------------
|
---|
219 | void MouseQueryListener::changeThreshold(int incr)
|
---|
220 | {
|
---|
221 | mThreshold += incr; if(mThreshold < 0) mThreshold = 0;
|
---|
222 |
|
---|
223 | char str[100]; sprintf(str,": %d", mThreshold);
|
---|
224 |
|
---|
225 | mSceneMgr->setOption("Threshold", &mThreshold);
|
---|
226 | mThresholdInfo->setCaption(str);
|
---|
227 | }
|
---|
228 | //-----------------------------------------------------------------------
|
---|
229 | void MouseQueryListener::changeAlgorithm()
|
---|
230 | {
|
---|
231 | mCurrentAlgorithm = ++mCurrentAlgorithm % GtpVisibility::VisibilityEnvironment::NUM_CULLING_MANAGERS,
|
---|
232 |
|
---|
233 | mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
|
---|
234 | mSceneMgr->setOption("Algorithm", &mCurrentAlgorithm);
|
---|
235 | }
|
---|
236 | //-----------------------------------------------------------------------
|
---|
237 | void MouseQueryListener::changeStats()
|
---|
238 | {
|
---|
239 | unsigned int opt = 0;
|
---|
240 | char str[100];
|
---|
241 |
|
---|
242 | mSceneMgr->getOption("NumFrustumCulledNodes", &opt); sprintf(str,": %d", opt);
|
---|
243 | mFrustumCulledNodesInfo->setCaption(str);
|
---|
244 |
|
---|
245 | mSceneMgr->getOption("NumQueryCulledNodes", &opt); sprintf(str,": %d", opt);
|
---|
246 | mQueryCulledNodesInfo->setCaption(str);
|
---|
247 |
|
---|
248 | mSceneMgr->getOption("NumTraversedNodes", &opt); sprintf(str,": %d", opt);
|
---|
249 | mTraversedNodesInfo->setCaption(str);
|
---|
250 |
|
---|
251 | mSceneMgr->getOption("NumHierarchyNodes", &opt); sprintf(str,": %d", opt);
|
---|
252 | mHierarchyNodesInfo->setCaption(str);
|
---|
253 |
|
---|
254 | mSceneMgr->getOption("NumRenderedNodes", &opt); sprintf(str,": %d", opt);
|
---|
255 | mRenderedNodesInfo->setCaption(str);
|
---|
256 | }
|
---|
257 | //-----------------------------------------------------------------------
|
---|
258 | void MouseQueryListener::keyPressed(KeyEvent* e)
|
---|
259 | {
|
---|
260 | if(e->getKey() == KC_ESCAPE)
|
---|
261 | {
|
---|
262 | mShutdownRequested = true;
|
---|
263 | e->consume();
|
---|
264 | return;
|
---|
265 | }
|
---|
266 |
|
---|
267 | CEGUI::System::getSingleton().injectKeyDown(e->getKey());
|
---|
268 | CEGUI::System::getSingleton().injectChar(e->getKeyChar());
|
---|
269 | e->consume();
|
---|
270 | }
|
---|
271 | //-----------------------------------------------------------------------
|
---|
272 | void MouseQueryListener::keyReleased(KeyEvent* e)
|
---|
273 | {
|
---|
274 | CEGUI::System::getSingleton().injectKeyUp(e->getKey());
|
---|
275 | e->consume();
|
---|
276 | }
|
---|
277 | //-----------------------------------------------------------------------
|
---|
278 | void MouseQueryListener::keyClicked(KeyEvent* e)
|
---|
279 | {
|
---|
280 | // Do nothing
|
---|
281 | e->consume();
|
---|
282 | }
|
---|
283 | //-----------------------------------------------------------------------
|
---|
284 | INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
|
---|
285 | {
|
---|
286 | // Create application object
|
---|
287 | TestCullingApplication app;
|
---|
288 |
|
---|
289 | try
|
---|
290 | {
|
---|
291 | app.go();
|
---|
292 | }
|
---|
293 | catch( Ogre::Exception& e )
|
---|
294 | {
|
---|
295 | MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
|
---|
296 | }
|
---|
297 |
|
---|
298 | return 0;
|
---|
299 | } |
---|