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