1 | #include <OgreNoMemoryMacros.h>
|
---|
2 | #include <CEGUI/CEGUI.h>
|
---|
3 | #include <../CEGUIRenderer/include/OgreCEGUIRenderer.h>
|
---|
4 | #include <../CEGUIRenderer/include/OgreCEGUIResourceProvider.h>
|
---|
5 | #include <../CEGUIRenderer/include/OgreCEGUITexture.h>
|
---|
6 | #include <OgreMemoryMacros.h>
|
---|
7 | #include "TerrainMouseQueryListener.h"
|
---|
8 |
|
---|
9 | // output file for frame info
|
---|
10 | const char* frames_out_filename = "frame.out";
|
---|
11 | // output file for object positions / orientations
|
---|
12 | const char* objects_out_filename = "objects.out";
|
---|
13 |
|
---|
14 |
|
---|
15 | #define KEY_PRESSED(_key, _timeDelay, _macro) \
|
---|
16 | { \
|
---|
17 | if (mInputDevice->isKeyDown(_key) && mTimeDelay <= 0) \
|
---|
18 | { \
|
---|
19 | mTimeDelay = _timeDelay; \
|
---|
20 | _macro ; \
|
---|
21 | } \
|
---|
22 | }
|
---|
23 |
|
---|
24 |
|
---|
25 | String mCurrentAlgorithmCaptions[GtpVisibility::VisibilityEnvironment::NUM_CULLING_MANAGERS] =
|
---|
26 | {
|
---|
27 | "View Frustum Culling",
|
---|
28 | "Stop and Wait Culling",
|
---|
29 | "Coherent Hierarchical Culling"
|
---|
30 | };
|
---|
31 |
|
---|
32 |
|
---|
33 | //-----------------------------------------------------------------------
|
---|
34 | TerrainMouseQueryListener::TerrainMouseQueryListener(RenderWindow* win, Camera* cam,
|
---|
35 | SceneManager *sceneManager,
|
---|
36 | CEGUI::Renderer *renderer,
|
---|
37 | TerrainContentGenerator *sceneGenerator,
|
---|
38 | Camera *vizCamera,
|
---|
39 | SceneNode *camNode):
|
---|
40 | mCamera(cam),
|
---|
41 | mWindow(win),
|
---|
42 | mNumScreenShots(0),
|
---|
43 | mTimeDelay(0),
|
---|
44 | mSceneDetailIndex(0),
|
---|
45 | mMoveScale(0.0f),
|
---|
46 | mRotScale(0.0f),
|
---|
47 | mTranslateVector(Vector3::ZERO),
|
---|
48 | mAniso(1),
|
---|
49 | mFiltering(TFO_BILINEAR),
|
---|
50 | mGUIRenderer(renderer),
|
---|
51 | mSceneMgr(sceneManager),
|
---|
52 | mCurrentObject(NULL),
|
---|
53 | mTerrainContentGenerator(sceneGenerator),
|
---|
54 | mVisibilityThreshold(0),
|
---|
55 | mCurrentAlgorithm(GtpVisibility::VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING),
|
---|
56 | //mCurrentAlgorithm(GtpVisibility::VisibilityEnvironment::FRUSTUM_CULLING),
|
---|
57 | mNodeVizMode(NODEVIZ_NONE),
|
---|
58 | mVizCameraHeight(Real(2500.0)),
|
---|
59 | mCamNode(camNode),
|
---|
60 | mAppState(WALKTHROUGH),
|
---|
61 | mCurrentFrame(0),
|
---|
62 | mTimeElapsed(0),
|
---|
63 | mRotateSpeed(72),
|
---|
64 | mMoveSpeed(50),
|
---|
65 | mVizCamera(vizCamera),
|
---|
66 | mStatsOn(true),
|
---|
67 | mShutdownRequested(false),
|
---|
68 | mLMouseDown(false),
|
---|
69 | mRMouseDown(false),
|
---|
70 | mShowOctree(false),
|
---|
71 | mUseDepthPass(false),
|
---|
72 | mUseOptimization(true),
|
---|
73 | mShowVisualization(false),
|
---|
74 | mCullCamera(false),
|
---|
75 | mRecord(false),
|
---|
76 | mUseShadows(false),
|
---|
77 | mShowHelp(false),
|
---|
78 | mDisplayCameraDetails(false),
|
---|
79 | mVisualizeCulledNodes(false)
|
---|
80 | {
|
---|
81 | mInputDevice = PlatformManager::getSingleton().createInputReader();
|
---|
82 | mInputDevice->initialise(win, true, true);
|
---|
83 |
|
---|
84 | /*
|
---|
85 | mEventProcessor->addMouseListener(this);
|
---|
86 | mEventProcessor->addMouseMotionListener(this);
|
---|
87 | mEventProcessor->addKeyListener(this);
|
---|
88 | */
|
---|
89 |
|
---|
90 | // create ray query executor, used to place objects in terrain
|
---|
91 | mRayQueryExecutor = new RayQueryExecutor(mSceneMgr);
|
---|
92 |
|
---|
93 | mHelpOverlay = OverlayManager::getSingleton().getByName("Example/Visibility/HelpOverlay");
|
---|
94 | mDebugOverlay = OverlayManager::getSingleton().getByName("Core/DebugOverlay");
|
---|
95 |
|
---|
96 |
|
---|
97 | //-- visibility culling stats overlay
|
---|
98 | mCullStatsOverlay = OverlayManager::getSingleton().getByName("Example/Visibility/DemoOverlay");
|
---|
99 |
|
---|
100 | mAlgorithmInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/AlgorithmInfo");
|
---|
101 | mThresholdInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/ThresholdInfo");
|
---|
102 |
|
---|
103 | mFrustumCulledNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/FrustumCulledNodesInfo");
|
---|
104 | mQueryCulledNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/QueryCulledNodesInfo");
|
---|
105 | mTraversedNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/TraversedNodesInfo");
|
---|
106 | mHierarchyNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/HierarchyNodesInfo");
|
---|
107 | mRenderedNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/RenderedNodesInfo");
|
---|
108 | mObjectsInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/ObjectsInfo");
|
---|
109 | mUseOptimizationInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/UseOptimizationInfo");
|
---|
110 | mUseDepthPassInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/UseDepthPassInfo");
|
---|
111 | mQueriesIssuedInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/QueriesIssuedInfo");
|
---|
112 |
|
---|
113 | mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
|
---|
114 | mThresholdInfo->setCaption(": 0");
|
---|
115 | mFrustumCulledNodesInfo->setCaption(": 0");
|
---|
116 | mQueryCulledNodesInfo->setCaption(": 0");
|
---|
117 | mTraversedNodesInfo->setCaption(": 0");
|
---|
118 | mHierarchyNodesInfo->setCaption(": 0");
|
---|
119 | mRenderedNodesInfo->setCaption(": 0");
|
---|
120 | mObjectsInfo->setCaption(": 0");
|
---|
121 | mUseOptimizationInfo->setCaption(": true");
|
---|
122 | mUseDepthPassInfo->setCaption(": false");
|
---|
123 | mQueriesIssuedInfo->setCaption(": 0");
|
---|
124 |
|
---|
125 | // show stats overlays
|
---|
126 | showStats(true);
|
---|
127 | //mHelpOverlay->show();
|
---|
128 |
|
---|
129 | // set culling algorithm type
|
---|
130 | setAlgorithm(mCurrentAlgorithm);
|
---|
131 |
|
---|
132 | // set scene manager options
|
---|
133 | mSceneMgr->setOption("UseOptimization", &mUseOptimization);
|
---|
134 |
|
---|
135 | // apply delayed rendering (i.e., transparents after hierarchical culling pass)
|
---|
136 | // only if optimization is not used
|
---|
137 | bool delayRenderTransparents = !mUseOptimization;
|
---|
138 |
|
---|
139 | mSceneMgr->setOption("DelayRenderTransparents", &delayRenderTransparents);
|
---|
140 | mSceneMgr->setOption("UseDepthPass", &mUseDepthPass);
|
---|
141 |
|
---|
142 | mSceneMgr->setOption("ShowOctree", &mShowOctree);
|
---|
143 | mSceneMgr->setOption("CullCamera", &mCullCamera);
|
---|
144 | mSceneMgr->setOption("ShowVisualization", &mShowVisualization);
|
---|
145 | }
|
---|
146 | //-----------------------------------------------------------------------
|
---|
147 | TerrainMouseQueryListener::~TerrainMouseQueryListener()
|
---|
148 | {
|
---|
149 | PlatformManager::getSingleton().destroyInputReader(mInputDevice);
|
---|
150 |
|
---|
151 | delete mRayQueryExecutor;
|
---|
152 | }
|
---|
153 | //-----------------------------------------------------------------------
|
---|
154 | void TerrainMouseQueryListener::mouseMoved(MouseEvent *e)
|
---|
155 | {
|
---|
156 | // Update CEGUI with the mouse motion
|
---|
157 | CEGUI::System::getSingleton().injectMouseMove(e->getRelX() *
|
---|
158 | mGUIRenderer->getWidth(), e->getRelY() * mGUIRenderer->getHeight());
|
---|
159 | }
|
---|
160 | //-----------------------------------------------------------------------
|
---|
161 | void TerrainMouseQueryListener::mousePressed(MouseEvent* e)
|
---|
162 | {
|
---|
163 | // Left mouse button down
|
---|
164 | if (e->getButtonID() & InputEvent::BUTTON0_MASK)
|
---|
165 | {
|
---|
166 | CEGUI::MouseCursor::getSingleton().hide();
|
---|
167 |
|
---|
168 | // Setup the ray scene query
|
---|
169 | Ray mouseRay = mCamera->getCameraToViewportRay(e->getX(), e->getY());
|
---|
170 |
|
---|
171 | Vector3 queryResult;
|
---|
172 |
|
---|
173 | // Get results, create a node/entity on the position
|
---|
174 | mCurrentObject = mTerrainContentGenerator->GenerateSceneObject(
|
---|
175 | mouseRay.getOrigin(), Vector3(0,0,0), "robot");
|
---|
176 |
|
---|
177 | mLMouseDown = true;
|
---|
178 | }
|
---|
179 | // Right mouse button down
|
---|
180 | else if (e->getButtonID() & InputEvent::BUTTON1_MASK)
|
---|
181 | {
|
---|
182 | CEGUI::MouseCursor::getSingleton().hide();
|
---|
183 | mRMouseDown = true;
|
---|
184 | }
|
---|
185 | } // mousePressed
|
---|
186 | //-----------------------------------------------------------------------
|
---|
187 | void TerrainMouseQueryListener::mouseReleased(MouseEvent* e)
|
---|
188 | {
|
---|
189 | // Left mouse button up
|
---|
190 | if (e->getButtonID() & InputEvent::BUTTON0_MASK)
|
---|
191 | {
|
---|
192 | CEGUI::MouseCursor::getSingleton().show();
|
---|
193 | mLMouseDown = false;
|
---|
194 | }
|
---|
195 | // Right mouse button up
|
---|
196 | else if (e->getButtonID() & InputEvent::BUTTON1_MASK)
|
---|
197 | {
|
---|
198 | CEGUI::MouseCursor::getSingleton().show();
|
---|
199 | mRMouseDown = false;
|
---|
200 | }
|
---|
201 | }
|
---|
202 | //-----------------------------------------------------------------------
|
---|
203 | void TerrainMouseQueryListener::mouseDragged(MouseEvent *e)
|
---|
204 | {
|
---|
205 | // If we are dragging the left mouse button.
|
---|
206 | if (mLMouseDown)
|
---|
207 | {
|
---|
208 | Vector3 queryResult;
|
---|
209 | Ray mouseRay = mCamera->getCameraToViewportRay(e->getX(), e->getY());
|
---|
210 |
|
---|
211 | if (mRayQueryExecutor->executeRayQuery(&queryResult, mouseRay))
|
---|
212 | {
|
---|
213 | if (mCurrentObject)
|
---|
214 | {
|
---|
215 | mCurrentObject->setPosition(queryResult);
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|
219 | // If we are dragging the right mouse button.
|
---|
220 | if (mRMouseDown)
|
---|
221 | {
|
---|
222 | //mCamera->yaw(-e->getRelX() * mRotateSpeed);
|
---|
223 | //mCamera->pitch(-e->getRelY() * mRotateSpeed);
|
---|
224 | mCamNode->yaw(-e->getRelX() * mRotateSpeed);
|
---|
225 | mCamNode->pitch(-e->getRelY() * mRotateSpeed);
|
---|
226 | }
|
---|
227 | }
|
---|
228 | //-----------------------------------------------------------------------
|
---|
229 | bool TerrainMouseQueryListener::frameStarted(const FrameEvent &evt)
|
---|
230 | {
|
---|
231 | if (mWindow->isClosed())
|
---|
232 | return false;
|
---|
233 |
|
---|
234 | mInputDevice->capture();
|
---|
235 |
|
---|
236 | //-- setup what is needed for immediate mouse/key movement
|
---|
237 | if (mTimeDelay >= 0)
|
---|
238 | mTimeDelay -= evt.timeSinceLastFrame;
|
---|
239 |
|
---|
240 | // If this is the first frame, pick a speed
|
---|
241 | if (evt.timeSinceLastFrame == 0)
|
---|
242 | {
|
---|
243 | mMoveScale = 1;
|
---|
244 | mRotScale = 0.1;
|
---|
245 | }
|
---|
246 | // Otherwise scale movement units by time passed since last frame
|
---|
247 | else
|
---|
248 | {
|
---|
249 | // Move about 100 units per second,
|
---|
250 | mMoveScale = mMoveSpeed * evt.timeSinceLastFrame;
|
---|
251 | // Take about 10 seconds for full rotation
|
---|
252 | mRotScale = mRotateSpeed * evt.timeSinceLastFrame;
|
---|
253 | }
|
---|
254 |
|
---|
255 | mRotX = 0;
|
---|
256 | mRotY = 0;
|
---|
257 | mTranslateVector = Vector3::ZERO;
|
---|
258 |
|
---|
259 | if (!processUnbufferedKeyInput(evt))
|
---|
260 | {
|
---|
261 | return false;
|
---|
262 | }
|
---|
263 | if (!processUnbufferedMouseInput(evt))
|
---|
264 | {
|
---|
265 | return false;
|
---|
266 | }
|
---|
267 |
|
---|
268 | if (mShowVisualization)
|
---|
269 | {
|
---|
270 | // important for visualization => draw octree bounding boxes
|
---|
271 | mSceneMgr->setOption("ShowOctree", &mShowVisualization);
|
---|
272 | // also render nodew content?
|
---|
273 | //mSceneMgr->setOption("RenderNodesForViz", &mRenderNodesForViz);
|
---|
274 |
|
---|
275 | // -- setup visualization camera
|
---|
276 | mVizCamera->setPosition(0, 0, 0);
|
---|
277 | mVizCamera->setOrientation(Quaternion::IDENTITY);
|
---|
278 |
|
---|
279 | Vector3 camPos = mCamNode->getPosition();
|
---|
280 | mVizCamera->setPosition(camPos.x, mVizCameraHeight, camPos.z);
|
---|
281 |
|
---|
282 | // point down -Z axis
|
---|
283 | mVizCamera->pitch(Radian(Degree(270.0)));
|
---|
284 |
|
---|
285 | // rotation arounnd X axis
|
---|
286 | mVizCamera->yaw(Math::ATan2(-mCamera->getDerivedDirection().x,
|
---|
287 | -mCamera->getDerivedDirection().z));
|
---|
288 |
|
---|
289 | // move by a constant so view plane is on bottom of viewport
|
---|
290 | mVizCamera->moveRelative(Vector3(0, 800, 0));
|
---|
291 | }
|
---|
292 |
|
---|
293 | switch (mAppState)
|
---|
294 | {
|
---|
295 | case REPLAY:
|
---|
296 | setCurrentFrameInfo(evt.timeSinceLastFrame);
|
---|
297 | break;
|
---|
298 | case WALKTHROUGH:
|
---|
299 | //-- if we are recording camera status per frame
|
---|
300 | if (mRecord)
|
---|
301 | {
|
---|
302 | addFrameInfo(mCamNode, evt.timeSinceLastFrame);
|
---|
303 | // print recording message
|
---|
304 | mWindow->setDebugText("Recording frame " +
|
---|
305 | StringConverter::toString(mFrameInfo.size() - 1));
|
---|
306 | }
|
---|
307 |
|
---|
308 | moveCamera();
|
---|
309 | Clamp2Terrain();
|
---|
310 |
|
---|
311 | break;
|
---|
312 | default:
|
---|
313 | break;
|
---|
314 | };
|
---|
315 |
|
---|
316 | return true;
|
---|
317 | }
|
---|
318 | //-----------------------------------------------------------------------
|
---|
319 | void TerrainMouseQueryListener::Clamp2Terrain()
|
---|
320 | {
|
---|
321 | // clamp to terrain
|
---|
322 | Vector3 camPos = mCamNode->getPosition();
|
---|
323 | Vector3 queryResult;
|
---|
324 |
|
---|
325 | if (mRayQueryExecutor->executeRayQuery(&queryResult,
|
---|
326 | Vector3(camPos.x, 5000.0f, camPos.z), Vector3::NEGATIVE_UNIT_Y))
|
---|
327 | {
|
---|
328 | mCamNode->setPosition(camPos.x, queryResult.y + 10, camPos.z);
|
---|
329 | }
|
---|
330 | }
|
---|
331 | //-----------------------------------------------------------------------
|
---|
332 | bool TerrainMouseQueryListener::frameEnded(const FrameEvent& evt)
|
---|
333 | {
|
---|
334 | if (mShutdownRequested)
|
---|
335 | return false;
|
---|
336 |
|
---|
337 | updateStats();
|
---|
338 |
|
---|
339 | return true;
|
---|
340 | }
|
---|
341 | //-----------------------------------------------------------------------
|
---|
342 | void TerrainMouseQueryListener::moveCamera()
|
---|
343 | {
|
---|
344 | // move node rather than camera so orientation is right in the visualization
|
---|
345 | mCamNode->yaw(mRotX);
|
---|
346 | mCamNode->pitch(mRotY);
|
---|
347 | mCamNode->translate(mCamNode->getLocalAxes(), mTranslateVector);
|
---|
348 | }
|
---|
349 | //-----------------------------------------------------------------------
|
---|
350 | void TerrainMouseQueryListener::writeFrames()
|
---|
351 | {
|
---|
352 | std::ofstream ofstr(frames_out_filename);
|
---|
353 |
|
---|
354 | std::vector<frame_info>::const_iterator it, it_end;
|
---|
355 |
|
---|
356 | it_end = mFrameInfo.end();
|
---|
357 | for(it = mFrameInfo.begin(); it < it_end; ++it)
|
---|
358 | {
|
---|
359 | ofstr << StringConverter::toString((*it).position) << " "
|
---|
360 | << StringConverter::toString((*it).orientation) << " "
|
---|
361 | << StringConverter::toString((*it).timeElapsed) << "\n";
|
---|
362 | }
|
---|
363 | ofstr.close();
|
---|
364 | }
|
---|
365 | //-----------------------------------------------------------------------
|
---|
366 | void TerrainMouseQueryListener::loadFrames()
|
---|
367 | {
|
---|
368 | std::ifstream ifstr(frames_out_filename);
|
---|
369 | char line[256];
|
---|
370 | frame_info info;
|
---|
371 |
|
---|
372 | // reset current values
|
---|
373 | mFrameInfo.clear();
|
---|
374 | mCurrentFrame = 0;
|
---|
375 |
|
---|
376 | while (!ifstr.eof())
|
---|
377 | {
|
---|
378 | ifstr.getline(line, 256);
|
---|
379 | sscanf(line, "%f %f %f %f %f %f %f %f", &info.position.x, &info.position.y, &info.position.z,
|
---|
380 | &info.orientation.w, &info.orientation.x, &info.orientation.y, &info.orientation.z,
|
---|
381 | &info.timeElapsed);
|
---|
382 |
|
---|
383 | mFrameInfo.push_back(info);
|
---|
384 |
|
---|
385 | /*
|
---|
386 | std::stringstream d;
|
---|
387 | d << StringConverter::toString(info.position) << " " << StringConverter::toString(info.orientation);
|
---|
388 | LogManager::getSingleton().logMessage(d.str());
|
---|
389 | */
|
---|
390 | }
|
---|
391 | ifstr.close();
|
---|
392 | }
|
---|
393 | //-----------------------------------------------------------------------
|
---|
394 | void TerrainMouseQueryListener::nextAppState()
|
---|
395 | {
|
---|
396 | mCurrentFrame = 0;
|
---|
397 |
|
---|
398 | // if last state was replay state
|
---|
399 | if (mAppState == REPLAY)
|
---|
400 | {
|
---|
401 | // reset debug text and write frame info to file
|
---|
402 | mWindow->setDebugText("");
|
---|
403 | writeFrames();
|
---|
404 |
|
---|
405 | //-- write out stats
|
---|
406 | std::stringstream d;
|
---|
407 | d << "Algorithm: " << mCurrentAlgorithmCaptions[mCurrentAlgorithm] << "\n"
|
---|
408 | << "avg. FPS: " << mWindow->getAverageFPS() << "\n"
|
---|
409 | << "best FPS: " << mWindow->getBestFPS() << "\n"
|
---|
410 | << "worst FPS: " << mWindow->getWorstFPS() << "\n"
|
---|
411 | << "best frame time: " << mWindow->getBestFrameTime() << "\n"
|
---|
412 | << "worst frame time: " << mWindow->getWorstFrameTime();
|
---|
413 |
|
---|
414 | LogManager::getSingleton().logMessage(d.str());
|
---|
415 | }
|
---|
416 |
|
---|
417 | //-- set the next státe
|
---|
418 | mAppState = (mAppState + 1) % STATE_NUM;
|
---|
419 |
|
---|
420 | // replay recorded walkthrough
|
---|
421 | if (mAppState == REPLAY)
|
---|
422 | {
|
---|
423 | // no recording during replay
|
---|
424 | mRecord = false;
|
---|
425 |
|
---|
426 | // load recorded walkthrough
|
---|
427 | if (mFrameInfo.size() == 0)
|
---|
428 | {
|
---|
429 | loadFrames();
|
---|
430 | }
|
---|
431 |
|
---|
432 | // if there are no recorded frames => set next state
|
---|
433 | if (mFrameInfo.size() == 0)
|
---|
434 | {
|
---|
435 | nextAppState();
|
---|
436 | }
|
---|
437 | else
|
---|
438 | {
|
---|
439 | mWindow->setDebugText("Replay");
|
---|
440 |
|
---|
441 | // reset, because we measure fps stats during walkthrough
|
---|
442 | mWindow->resetStatistics();
|
---|
443 |
|
---|
444 | //-- initialise frame data
|
---|
445 | mTimeElapsed = 0;
|
---|
446 |
|
---|
447 | mCamNode->setPosition(mFrameInfo[0].position);
|
---|
448 | mCamNode->setOrientation(mFrameInfo[0].orientation);
|
---|
449 | }
|
---|
450 | }
|
---|
451 |
|
---|
452 | }
|
---|
453 | //-----------------------------------------------------------------------
|
---|
454 | void TerrainMouseQueryListener::toggleRecord()
|
---|
455 | {
|
---|
456 | mRecord = !mRecord;
|
---|
457 |
|
---|
458 | // clear previous camera path
|
---|
459 | if (mRecord)
|
---|
460 | mFrameInfo.clear();
|
---|
461 | else
|
---|
462 | mWindow->setDebugText("");
|
---|
463 | }
|
---|
464 | //-----------------------------------------------------------------------
|
---|
465 | void TerrainMouseQueryListener::changeThreshold(int incr)
|
---|
466 | {
|
---|
467 | mVisibilityThreshold += incr;
|
---|
468 | if(mVisibilityThreshold < 0) mVisibilityThreshold = 0;
|
---|
469 |
|
---|
470 | char str[100]; sprintf(str,": %d", mVisibilityThreshold);
|
---|
471 |
|
---|
472 | mSceneMgr->setOption("Threshold", &mVisibilityThreshold);
|
---|
473 | mThresholdInfo->setCaption(str);
|
---|
474 | }
|
---|
475 | //-----------------------------------------------------------------------
|
---|
476 | void TerrainMouseQueryListener::zoomVizCamera(int zoom)
|
---|
477 | {
|
---|
478 | mVizCameraHeight += zoom;
|
---|
479 | if(mVizCameraHeight < 0) mVizCameraHeight = 0;
|
---|
480 | }
|
---|
481 | //-----------------------------------------------------------------------
|
---|
482 | void TerrainMouseQueryListener::nextAlgorithm()
|
---|
483 | {
|
---|
484 | mCurrentAlgorithm = (mCurrentAlgorithm + 1) %
|
---|
485 | GtpVisibility::VisibilityEnvironment::NUM_CULLING_MANAGERS,
|
---|
486 |
|
---|
487 | setAlgorithm(mCurrentAlgorithm);
|
---|
488 | }
|
---|
489 | //-----------------------------------------------------------------------
|
---|
490 | void TerrainMouseQueryListener::setAlgorithm(int algorithm)
|
---|
491 | {
|
---|
492 | mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
|
---|
493 | mSceneMgr->setOption("Algorithm", &mCurrentAlgorithm);
|
---|
494 | }
|
---|
495 | //-----------------------------------------------------------------------
|
---|
496 | void TerrainMouseQueryListener::updateStats()
|
---|
497 | {
|
---|
498 | unsigned int opt = 0;
|
---|
499 | char str[100];
|
---|
500 |
|
---|
501 | static String currFps = "Current FPS: ";
|
---|
502 | static String avgFps = "Average FPS: ";
|
---|
503 | static String bestFps = "Best FPS: ";
|
---|
504 | static String worstFps = "Worst FPS: ";
|
---|
505 | static String tris = "Triangle Count: ";
|
---|
506 |
|
---|
507 | // update stats when necessary
|
---|
508 | try {
|
---|
509 | OverlayElement* guiAvg = OverlayManager::getSingleton().getOverlayElement("Core/AverageFps");
|
---|
510 | OverlayElement* guiCurr = OverlayManager::getSingleton().getOverlayElement("Core/CurrFps");
|
---|
511 | OverlayElement* guiBest = OverlayManager::getSingleton().getOverlayElement("Core/BestFps");
|
---|
512 | OverlayElement* guiWorst = OverlayManager::getSingleton().getOverlayElement("Core/WorstFps");
|
---|
513 |
|
---|
514 | const RenderTarget::FrameStats& stats = mWindow->getStatistics();
|
---|
515 |
|
---|
516 | guiAvg->setCaption(avgFps + StringConverter::toString(stats.avgFPS));
|
---|
517 | guiCurr->setCaption(currFps + StringConverter::toString(stats.lastFPS));
|
---|
518 | guiBest->setCaption(bestFps + StringConverter::toString(stats.bestFPS)
|
---|
519 | +" "+StringConverter::toString(stats.bestFrameTime)+" ms");
|
---|
520 | guiWorst->setCaption(worstFps + StringConverter::toString(stats.worstFPS)
|
---|
521 | +" "+StringConverter::toString(stats.worstFrameTime)+" ms");
|
---|
522 |
|
---|
523 | OverlayElement* guiTris = OverlayManager::getSingleton().getOverlayElement("Core/NumTris");
|
---|
524 | guiTris->setCaption(tris + StringConverter::toString(stats.triangleCount));
|
---|
525 |
|
---|
526 | OverlayElement* guiDbg = OverlayManager::getSingleton().getOverlayElement("Core/DebugText");
|
---|
527 | guiDbg->setCaption(mWindow->getDebugText());
|
---|
528 |
|
---|
529 | //-- culling stats
|
---|
530 | mSceneMgr->getOption("NumFrustumCulledNodes", &opt); sprintf(str,": %d", opt);
|
---|
531 | mFrustumCulledNodesInfo->setCaption(str);
|
---|
532 |
|
---|
533 | mSceneMgr->getOption("NumQueriesIssued", &opt); sprintf(str,": %d", opt);
|
---|
534 | mQueriesIssuedInfo->setCaption(str);
|
---|
535 |
|
---|
536 | mSceneMgr->getOption("NumQueryCulledNodes", &opt); sprintf(str,": %d", opt);
|
---|
537 | mQueryCulledNodesInfo->setCaption(str);
|
---|
538 |
|
---|
539 | mSceneMgr->getOption("NumTraversedNodes", &opt); sprintf(str,": %d", opt);
|
---|
540 | mTraversedNodesInfo->setCaption(str);
|
---|
541 |
|
---|
542 | mSceneMgr->getOption("NumHierarchyNodes", &opt); sprintf(str,": %d", opt);
|
---|
543 | mHierarchyNodesInfo->setCaption(str);
|
---|
544 |
|
---|
545 | mSceneMgr->getOption("NumRenderedNodes", &opt); sprintf(str,": %d", opt);
|
---|
546 | mRenderedNodesInfo->setCaption(str);
|
---|
547 |
|
---|
548 | sprintf(str,": %d", mTerrainContentGenerator->GetObjectCount());
|
---|
549 | mObjectsInfo->setCaption(str);
|
---|
550 | }
|
---|
551 | catch(...)
|
---|
552 | {
|
---|
553 | // ignore
|
---|
554 | }
|
---|
555 | }
|
---|
556 | //-----------------------------------------------------------------------
|
---|
557 | void TerrainMouseQueryListener::toggleUseOptimization()
|
---|
558 | {
|
---|
559 | mUseOptimization = !mUseOptimization;
|
---|
560 |
|
---|
561 | mSceneMgr->setOption("UseOptimization", &mUseOptimization);
|
---|
562 | // delayed rendering (i.e., render transparents afterwards) allows
|
---|
563 | // no optimization which tests geometry instead of aabb
|
---|
564 | bool delayedRendering = !mUseOptimization;
|
---|
565 | mSceneMgr->setOption("DelayRenderTransparents", &delayedRendering);
|
---|
566 |
|
---|
567 | if (mUseOptimization)
|
---|
568 | mUseOptimizationInfo->setCaption(": true");
|
---|
569 | else
|
---|
570 | mUseOptimizationInfo->setCaption(": false");
|
---|
571 | }
|
---|
572 | //-----------------------------------------------------------------------
|
---|
573 | void TerrainMouseQueryListener::toggleShowOctree()
|
---|
574 | {
|
---|
575 | mShowOctree = !mShowOctree;
|
---|
576 |
|
---|
577 | mSceneMgr->setOption("ShowOctree", &mShowOctree);
|
---|
578 | }
|
---|
579 | //-----------------------------------------------------------------------
|
---|
580 | void TerrainMouseQueryListener::toggleUseDepthPass()
|
---|
581 | {
|
---|
582 | mUseDepthPass = !mUseDepthPass;
|
---|
583 |
|
---|
584 | mSceneMgr->setOption("UseDepthPass", &mUseDepthPass);
|
---|
585 |
|
---|
586 | if (mUseDepthPass)
|
---|
587 | {
|
---|
588 | mUseDepthPassInfo->setCaption(": true");
|
---|
589 | }
|
---|
590 | else
|
---|
591 | {
|
---|
592 | mUseDepthPassInfo->setCaption(": false");
|
---|
593 | }
|
---|
594 | }
|
---|
595 | //-----------------------------------------------------------------------
|
---|
596 | void TerrainMouseQueryListener::toggleShowViz()
|
---|
597 | {
|
---|
598 | mShowVisualization = !mShowVisualization;
|
---|
599 | mVisualizeCulledNodes = !mVisualizeCulledNodes;
|
---|
600 |
|
---|
601 | // create viewport with priority VIZ_VIEWPORT_Z_ORDER:
|
---|
602 | // will be rendered over standard viewport
|
---|
603 | if (mShowVisualization)
|
---|
604 | {
|
---|
605 | Viewport *vizvp = mWindow->addViewport(mVizCamera,
|
---|
606 | VIZ_VIEWPORT_Z_ORDER, 0.6, 0.6, 0.4, 0.4);
|
---|
607 |
|
---|
608 | vizvp->setBackgroundColour(ColourValue(0.0, 0.3, 0.2, 1));
|
---|
609 |
|
---|
610 | vizvp->setOverlaysEnabled(false);
|
---|
611 | // Alter the camera aspect ratio to match the viewport
|
---|
612 | mVizCamera->setAspectRatio(Real(vizvp->getActualWidth()) /
|
---|
613 | Real(vizvp->getActualHeight()));
|
---|
614 |
|
---|
615 | mSceneMgr->setOption("VisualizeCulledNodes", &mVisualizeCulledNodes);
|
---|
616 | //vizvp->setClearEveryFrame(false);
|
---|
617 |
|
---|
618 | // Create a skyplane (for visualization background)
|
---|
619 | /*
|
---|
620 | Plane plane;
|
---|
621 | plane.d = -1000;
|
---|
622 | plane.normal = Vector3::UNIT_Y;
|
---|
623 | mSceneMgr->setSkyPlane(true, plane, "Examples/TransparentTest", 4000, 75, false);
|
---|
624 | */
|
---|
625 |
|
---|
626 | }
|
---|
627 | else
|
---|
628 | {
|
---|
629 | mWindow->removeViewport(VIZ_VIEWPORT_Z_ORDER);
|
---|
630 | // if octree was enabled for visualization purpose, reset now
|
---|
631 | mSceneMgr->setOption("ShowOctree", &mShowOctree);
|
---|
632 | }
|
---|
633 | }
|
---|
634 | //-----------------------------------------------------------------------
|
---|
635 | void TerrainMouseQueryListener::toggleUseShadows()
|
---|
636 | {
|
---|
637 | mUseShadows = !mUseShadows;
|
---|
638 |
|
---|
639 | //mSunLight->setCastShadows(mUseShadows);
|
---|
640 |
|
---|
641 | if (mUseShadows)
|
---|
642 | {
|
---|
643 | mSceneMgr->setShadowTechnique(SHADOWTYPE_TEXTURE_MODULATIVE);
|
---|
644 | }
|
---|
645 | else
|
---|
646 | {
|
---|
647 | mSceneMgr->setShadowTechnique(SHADOWTYPE_NONE);
|
---|
648 | }
|
---|
649 |
|
---|
650 | }
|
---|
651 | //-----------------------------------------------------------------------
|
---|
652 | void TerrainMouseQueryListener::nextNodeVizMode()
|
---|
653 | {
|
---|
654 | mNodeVizMode = (mNodeVizMode + 1) % NODEVIZ_MODES_NUM;
|
---|
655 |
|
---|
656 | bool renderNodesForViz = (mNodeVizMode == NODEVIZ_RENDER_NODES) ||
|
---|
657 | (mNodeVizMode == NODEVIZ_RENDER_NODES_AND_CONTENT);
|
---|
658 | bool renderNodesContentForViz = (mNodeVizMode == NODEVIZ_RENDER_NODES_AND_CONTENT);
|
---|
659 | //bool renderNodesContentForViz = mNodeVizMode == NODEVIZ_RENDER_GEOMETRY;
|
---|
660 |
|
---|
661 | mSceneMgr->setOption("RenderNodesForViz", &renderNodesForViz);
|
---|
662 | mSceneMgr->setOption("RenderNodesContentForViz", &renderNodesContentForViz);
|
---|
663 | }
|
---|
664 | //-----------------------------------------------------------------------
|
---|
665 | void TerrainMouseQueryListener::keyPressed(KeyEvent* e)
|
---|
666 | {
|
---|
667 | if(e->getKey() == KC_ESCAPE)
|
---|
668 | {
|
---|
669 | mShutdownRequested = true;
|
---|
670 | e->consume();
|
---|
671 | return;
|
---|
672 | }
|
---|
673 |
|
---|
674 | CEGUI::System::getSingleton().injectKeyDown(e->getKey());
|
---|
675 | CEGUI::System::getSingleton().injectChar(e->getKeyChar());
|
---|
676 | e->consume();
|
---|
677 | }
|
---|
678 | //-----------------------------------------------------------------------
|
---|
679 | void TerrainMouseQueryListener::keyReleased(KeyEvent* e)
|
---|
680 | {
|
---|
681 | CEGUI::System::getSingleton().injectKeyUp(e->getKey());
|
---|
682 | e->consume();
|
---|
683 | }
|
---|
684 | //-----------------------------------------------------------------------
|
---|
685 | void TerrainMouseQueryListener::keyClicked(KeyEvent* e)
|
---|
686 | {
|
---|
687 | // Do nothing
|
---|
688 | e->consume();
|
---|
689 | }
|
---|
690 | //-----------------------------------------------------------------------
|
---|
691 | void TerrainMouseQueryListener::addFrameInfo(SceneNode *camNode, Real timeElapsed)
|
---|
692 | {
|
---|
693 | frame_info info;
|
---|
694 | info.orientation = mCamNode->getOrientation();
|
---|
695 | info.position = mCamNode->getPosition();
|
---|
696 | info.timeElapsed = timeElapsed;
|
---|
697 |
|
---|
698 | mFrameInfo.push_back(info);
|
---|
699 | }
|
---|
700 | //-----------------------------------------------------------------------
|
---|
701 | void TerrainMouseQueryListener::setCurrentFrameInfo(Real timeElapsed)
|
---|
702 | {
|
---|
703 | //-- find current frame relative to elapsed frame time
|
---|
704 | mTimeElapsed -= timeElapsed;
|
---|
705 |
|
---|
706 | while ((mTimeElapsed <= 0) && (mCurrentFrame < (int)mFrameInfo.size() - 1))
|
---|
707 | {
|
---|
708 | mTimeElapsed += mFrameInfo[mCurrentFrame ++].timeElapsed;
|
---|
709 | }
|
---|
710 |
|
---|
711 | frame_info new_frame = mFrameInfo[mCurrentFrame];
|
---|
712 | frame_info old_frame = mFrameInfo[mCurrentFrame - 1];
|
---|
713 |
|
---|
714 | //-- interpolate frames
|
---|
715 | Real factor = 1;
|
---|
716 |
|
---|
717 | if (old_frame.timeElapsed > 0)
|
---|
718 | factor = mTimeElapsed / old_frame.timeElapsed;
|
---|
719 |
|
---|
720 | Vector3 camPos = old_frame.position + factor * (new_frame.position - old_frame.position);
|
---|
721 | Quaternion camOrienation = Quaternion::Slerp(factor, old_frame.orientation,
|
---|
722 | new_frame.orientation, true);
|
---|
723 |
|
---|
724 | mCamNode->setPosition(camPos);
|
---|
725 | mCamNode->setOrientation(camOrienation);
|
---|
726 |
|
---|
727 | // stop replay after a full walkthrough
|
---|
728 | if (mCurrentFrame == (int)mFrameInfo.size() - 1)
|
---|
729 | {
|
---|
730 | nextAppState();
|
---|
731 | }
|
---|
732 | }
|
---|
733 | //-----------------------------------------------------------------------
|
---|
734 | bool TerrainMouseQueryListener::processUnbufferedKeyInput(const FrameEvent& evt)
|
---|
735 | {
|
---|
736 | if (mInputDevice->isKeyDown(KC_ESCAPE))
|
---|
737 | {
|
---|
738 | return false;
|
---|
739 | }
|
---|
740 |
|
---|
741 | /* Move camera forward by keypress. */
|
---|
742 | if (mInputDevice->isKeyDown(KC_UP))
|
---|
743 | {
|
---|
744 | mTranslateVector.z = -mMoveScale;
|
---|
745 | }
|
---|
746 |
|
---|
747 | /* Move camera backward by keypress. */
|
---|
748 | if (mInputDevice->isKeyDown(KC_DOWN))
|
---|
749 | {
|
---|
750 | mTranslateVector.z = mMoveScale;
|
---|
751 | }
|
---|
752 |
|
---|
753 | if (mInputDevice->isKeyDown(KC_RIGHT))
|
---|
754 | {
|
---|
755 | mCamNode->yaw(-mRotScale);
|
---|
756 | }
|
---|
757 |
|
---|
758 | if (mInputDevice->isKeyDown(KC_LEFT))
|
---|
759 | {
|
---|
760 | mCamNode->yaw(mRotScale);
|
---|
761 | }
|
---|
762 |
|
---|
763 | KEY_PRESSED(KC_SPACE, 0.3, nextAlgorithm());
|
---|
764 | KEY_PRESSED(KC_F, 0.3, nextFilter());
|
---|
765 | KEY_PRESSED(KC_R, 0.3, nextSceneDetailLevel());
|
---|
766 |
|
---|
767 | KEY_PRESSED(KC_P, 0.3, toggleDisplayCameraDetails());
|
---|
768 | KEY_PRESSED(KC_O, 0.3, toggleUseOptimization());
|
---|
769 | KEY_PRESSED(KC_T, 0.3, toggleShowOctree());
|
---|
770 | KEY_PRESSED(KC_X, 0.3, toggleUseDepthPass());
|
---|
771 | KEY_PRESSED(KC_S, 0.3, toggleUseShadows());
|
---|
772 |
|
---|
773 | KEY_PRESSED(KC_SUBTRACT, 0, changeThreshold(-10));
|
---|
774 | KEY_PRESSED(KC_ADD, 0, changeThreshold(10));
|
---|
775 |
|
---|
776 | //-- visualization
|
---|
777 | KEY_PRESSED(KC_1, 0.3, toggleShowViz());
|
---|
778 | KEY_PRESSED(KC_2, 0.3, nextNodeVizMode());
|
---|
779 | KEY_PRESSED(KC_3, 0, zoomVizCamera(50));
|
---|
780 | KEY_PRESSED(KC_4, 0, zoomVizCamera(-50));
|
---|
781 |
|
---|
782 | KEY_PRESSED(KC_F1, 0.3, toggleShowHelp());
|
---|
783 | KEY_PRESSED(KC_F2, 0.3, toggleShowStats());
|
---|
784 | KEY_PRESSED(KC_F3, 0.3, nextAppState());
|
---|
785 |
|
---|
786 | KEY_PRESSED(KC_F10, 0.3, toggleRecord());
|
---|
787 | KEY_PRESSED(KC_F11, 0.3, takeScreenShot());
|
---|
788 | KEY_PRESSED(KC_F12, 0.3, mTerrainContentGenerator->WriteObjects(objects_out_filename));
|
---|
789 | //KEY_PRESSED(KC_F3, 0.3, writeFrames());
|
---|
790 | //KEY_PRESSED(KC_F4, 0.3, loadFrames());
|
---|
791 |
|
---|
792 | if (mDisplayCameraDetails)
|
---|
793 | {
|
---|
794 | // Print camera details
|
---|
795 | mWindow->setDebugText("P: " + StringConverter::toString(mCamera->getDerivedPosition()) + " " +
|
---|
796 | "O: " + StringConverter::toString(mCamera->getDerivedOrientation()));
|
---|
797 | }
|
---|
798 |
|
---|
799 | // Return true to continue rendering
|
---|
800 | return true;
|
---|
801 | }
|
---|
802 | //-----------------------------------------------------------------------
|
---|
803 | void TerrainMouseQueryListener::nextFilter()
|
---|
804 | {
|
---|
805 | switch(mFiltering)
|
---|
806 | {
|
---|
807 | case TFO_BILINEAR:
|
---|
808 | mFiltering = TFO_TRILINEAR;
|
---|
809 | mAniso = 1;
|
---|
810 | break;
|
---|
811 | case TFO_TRILINEAR:
|
---|
812 | mFiltering = TFO_ANISOTROPIC;
|
---|
813 | mAniso = 8;
|
---|
814 | break;
|
---|
815 | case TFO_ANISOTROPIC:
|
---|
816 | mFiltering = TFO_BILINEAR;
|
---|
817 | mAniso = 1;
|
---|
818 | break;
|
---|
819 | default:
|
---|
820 | break;
|
---|
821 | }
|
---|
822 |
|
---|
823 | MaterialManager::getSingleton().setDefaultTextureFiltering(mFiltering);
|
---|
824 | MaterialManager::getSingleton().setDefaultAnisotropy(mAniso);
|
---|
825 |
|
---|
826 | showStats(mStatsOn);
|
---|
827 | }
|
---|
828 | //-----------------------------------------------------------------------
|
---|
829 | void TerrainMouseQueryListener::nextSceneDetailLevel()
|
---|
830 | {
|
---|
831 | mSceneDetailIndex = (mSceneDetailIndex+1)%3 ;
|
---|
832 | switch(mSceneDetailIndex)
|
---|
833 | {
|
---|
834 | case 0 : mCamera->setDetailLevel(SDL_SOLID) ; break ;
|
---|
835 | case 1 : mCamera->setDetailLevel(SDL_WIREFRAME) ; break ;
|
---|
836 | case 2 : mCamera->setDetailLevel(SDL_POINTS) ; break ;
|
---|
837 | }
|
---|
838 | }
|
---|
839 | //-----------------------------------------------------------------------
|
---|
840 | void TerrainMouseQueryListener::takeScreenShot()
|
---|
841 | {
|
---|
842 | char tmp[20];
|
---|
843 | sprintf(tmp, "screenshot_%d.png", ++mNumScreenShots);
|
---|
844 | mWindow->writeContentsToFile(tmp);
|
---|
845 | mWindow->setDebugText(String("Wrote ") + tmp);
|
---|
846 | }
|
---|
847 | //-----------------------------------------------------------------------
|
---|
848 | void TerrainMouseQueryListener::toggleDisplayCameraDetails()
|
---|
849 | {
|
---|
850 | mDisplayCameraDetails = !mDisplayCameraDetails;
|
---|
851 |
|
---|
852 | if (!mDisplayCameraDetails)
|
---|
853 | mWindow->setDebugText("");
|
---|
854 | }
|
---|
855 | //-----------------------------------------------------------------------
|
---|
856 | bool TerrainMouseQueryListener::processUnbufferedMouseInput(const FrameEvent& evt)
|
---|
857 | {
|
---|
858 | /* Rotation factors, may not be used if the second mouse button is pressed. */
|
---|
859 |
|
---|
860 | /* If the second mouse button is pressed, then the mouse movement results in
|
---|
861 | sliding the camera, otherwise we rotate. */
|
---|
862 | if(mInputDevice->getMouseButton(1))
|
---|
863 | {
|
---|
864 | mTranslateVector.x += mInputDevice->getMouseRelativeX() * 0.13;
|
---|
865 | mTranslateVector.y -= mInputDevice->getMouseRelativeY() * 0.13;
|
---|
866 | }
|
---|
867 | else
|
---|
868 | {
|
---|
869 | mRotX = Degree(-mInputDevice->getMouseRelativeX() * 0.13);
|
---|
870 | mRotY = Degree(-mInputDevice->getMouseRelativeY() * 0.13);
|
---|
871 | }
|
---|
872 |
|
---|
873 |
|
---|
874 | return true;
|
---|
875 | }
|
---|
876 | //-----------------------------------------------------------------------
|
---|
877 | void TerrainMouseQueryListener::showStats(bool show)
|
---|
878 | {
|
---|
879 | if (mDebugOverlay && mCullStatsOverlay)
|
---|
880 | {
|
---|
881 | if (show)
|
---|
882 | {
|
---|
883 | mDebugOverlay->show();
|
---|
884 | mCullStatsOverlay->show();
|
---|
885 | }
|
---|
886 | else
|
---|
887 | {
|
---|
888 | mDebugOverlay->hide();
|
---|
889 | mCullStatsOverlay->hide();
|
---|
890 | }
|
---|
891 | }
|
---|
892 | }
|
---|
893 | //-----------------------------------------------------------------------
|
---|
894 | void TerrainMouseQueryListener::toggleShowStats()
|
---|
895 | {
|
---|
896 | mStatsOn = !mStatsOn;
|
---|
897 |
|
---|
898 | showStats(mStatsOn);
|
---|
899 | }
|
---|
900 | //-----------------------------------------------------------------------
|
---|
901 | void TerrainMouseQueryListener::toggleShowHelp()
|
---|
902 | {
|
---|
903 | mShowHelp = !mShowHelp;
|
---|
904 |
|
---|
905 | if (mShowHelp)
|
---|
906 | {
|
---|
907 | mHelpOverlay->show();
|
---|
908 | }
|
---|
909 | else
|
---|
910 | {
|
---|
911 | mHelpOverlay->hide();
|
---|
912 | }
|
---|
913 | }
|
---|
914 |
|
---|
915 | |
---|