source: GTP/trunk/App/Demos/Vis/HillyTerrain/OGRE/TerrainFrameListener.h @ 2455

Revision 2455, 12.6 KB checked in by mattausch, 17 years ago (diff)
Line 
1#ifndef _TerrainFrameListener_H__
2#define _TerrainFrameListener_H__
3
4#include "CEGUIForwardRefs.h"
5#include <Ogre.h>
6#include <OgreKeyEvent.h>
7#include <OgreEventListeners.h>
8#include <OgreStringConverter.h>
9#include <OgreException.h>
10#include "VisibilityEnvironment.h"
11#include "VisibilityManager.h"
12#include "OgreTerrainContentGenerator.h"
13#include "OgrePlatformQueryManager.h"
14
15
16class TestCullingTerrainApplication;
17
18
19using namespace Ogre;
20
21/** Struct storing walktrough statistics
22*/
23struct WalkthroughStats
24{
25public:
26
27        unsigned long mAccFps;
28        unsigned long mBestFps;
29        unsigned long mWorstFps;
30        unsigned long mAccTris;
31        unsigned long mAccQueryCulledNodes;
32        unsigned long mAccFrustumCulledNodes;
33        unsigned long mFrameCount;
34        unsigned long mAccRenderedNodes;
35       
36        WalkthroughStats():
37        mAccFps(0), mBestFps(0), mWorstFps(0), mAccTris(0),
38        mAccQueryCulledNodes(0), mAccFrustumCulledNodes(0),
39        mFrameCount(0), mAccRenderedNodes(0)
40        {}
41
42        void Reset()
43        {
44                mAccFps = 0;
45                mBestFps = 0;
46                mWorstFps = 0;
47                mAccTris = 0;
48                mAccQueryCulledNodes = 0;
49                mAccFrustumCulledNodes = 0;
50                mFrameCount = mAccRenderedNodes = 0;
51        }
52
53        void UpdateFrame(int currentFps,
54                                         int bestFps,
55                                         int worstFps,
56                                         int renderedTris,
57                                         int renderedNodes,
58                                         int queryCulledNodes,
59                                         int frustumCulledNodes)
60        {
61                mAccFps += currentFps;
62                mBestFps = bestFps;
63                mWorstFps = worstFps;
64
65                // accumulated #triangles (M)
66                mAccTris += renderedTris / 1000;
67                mAccRenderedNodes += renderedNodes;
68                mAccQueryCulledNodes += queryCulledNodes;
69                mAccFrustumCulledNodes += frustumCulledNodes;
70       
71                ++ mFrameCount;
72        }
73
74        void Print(std::ostream &d, const std::string &algorithmName) const
75        {
76                // compuate average fps and triangle count
77                float avgFps = (float)mAccFps / (float)mFrameCount;
78                float avgTris = (float)mAccTris / (float)mFrameCount;
79                float avgFrustumCulledNodes = 0;
80                float avgRenderedNodes = 0;
81                float avgQueryCulledNodes = 0;
82               
83                if (mFrameCount != 0)
84                {
85                        avgFrustumCulledNodes = (float)mAccFrustumCulledNodes / (float)mFrameCount;
86                        avgQueryCulledNodes  = (float)mAccQueryCulledNodes / (float)mFrameCount;
87                        avgRenderedNodes = (float)mAccRenderedNodes / (float) mFrameCount;
88                }
89
90                //-- write out stats for recorded walkthrough
91                d << "Algorithm: " << algorithmName << "\n"
92                  << "avg. FPS: " << avgFps << "\n"
93                  << "best FPS: " << mBestFps << "\n"
94                  << "worst FPS: " << mWorstFps << "\n"
95                  << "#frames: " << mFrameCount << "\n"
96                  << "avg. #triangles: " << avgTris << " M\n"
97                  << "avg. #query culled nodes: " << avgFrustumCulledNodes << "\n"
98                  << "avg. #frustum culled nodes: " << avgQueryCulledNodes << "\n"
99                  << "avg. #rendered nodes: " << avgRenderedNodes << "\n";
100        }
101
102};
103
104/** Frame listener specialised for terrains.
105*/
106class TerrainFrameListener: public FrameListener, public MouseListener,
107                                                        public MouseMotionListener, public KeyListener
108{
109public:
110       
111   TerrainFrameListener(RenderWindow* win,
112                                                Camera* cam,
113                                                SceneManager *sceneManager,
114                                                CEGUI::Renderer *renderer,
115                                                TerrainContentGenerator *contentGenerator,
116                                                Camera *vizCamera,
117                                                SceneNode *camNode,
118                                                Light *sunLight,
119                                                TestCullingTerrainApplication *app);
120
121   ~TerrainFrameListener();
122
123   bool frameStarted(const FrameEvent& evt);
124   bool frameEnded(const FrameEvent& evt);
125     
126
127   /* MouseListener callbacks. */
128   virtual void mouseClicked(MouseEvent* e) { }
129   virtual void mouseEntered(MouseEvent* e) { }
130   virtual void mouseExited(MouseEvent* e)  { }
131
132   // This is when the mouse button goes DOWN.
133   void mousePressed(MouseEvent* e);
134
135   // This is when the mouse button is let UP.
136   void mouseReleased(MouseEvent* e);
137
138   /* MouseMotionListener callbacks */
139   void mouseMoved(MouseEvent *e);
140   
141   // This is when the mouse is clicked, held and dragged.
142   void mouseDragged(MouseEvent *e);
143
144   void mouseDragDropped(MouseEvent *e);
145   void keyPressed(KeyEvent* e);
146
147   void keyReleased(KeyEvent* e);
148   void keyClicked(KeyEvent* e);
149 
150   /** The information about camera position and orienation per frame.
151   */
152   typedef struct
153   {
154           Vector3 position;
155           Quaternion orientation;
156           Real timeElapsed;
157           Real fps;
158   } frame_info;
159
160   typedef std::vector<frame_info> FrameInfoContainer;
161
162   enum  {WALKTHROUGH, REPLAY, STATE_NUM};
163
164   // visualization modes for scene nodes
165   enum {
166                 NODEVIZ_NONE,
167         NODEVIZ_RENDER_NODES,
168                 NODEVIZ_RENDER_NODES_AND_CONTENT,
169                 NODEVIZ_RENDER_PVS,
170                 NODEVIZ_MODES_NUM};
171
172   //enum {NODEVIZ_NONE, NODEVIZ_RENDER_GEOMETRY, NODEVIZ_MODES_NUM};
173
174   void zoomVizCamera(int zoom);
175       
176   void addFrameInfo(FrameInfoContainer &frameInfos, SceneNode *camNode, Real timeElapsed);
177   void setCurrentFrameInfo(Real timeElapsed);
178
179   void applyCurrentAlgorithm();
180
181   void moveCamera();
182
183   void writeFrames(const std::string filename, const FrameInfoContainer &frameInfo) const;
184   void loadFrames(const std::string filename, FrameInfoContainer &frameInfo);
185
186    bool processUnbufferedKeyInput(const FrameEvent& evt);
187    bool processUnbufferedMouseInput(const FrameEvent& evt);
188
189        void switchMouseMode();
190       
191        /** Shows the frame / algorithm statistics.
192        */
193    void showStats(bool show);
194
195        /** Updates frame statistics and the frame statistics display.
196        */
197        void updateStats();
198
199        /** Toggles whether help screen is shown or not.
200        */
201        void toggleShowHelp();
202       
203        /** Toggles whether shadows are shown or not.
204        */
205        void toggleShowShadows();
206        /** if camera stats (e.g, location, orientation, ...) should be displayed-
207        */
208        void toggleDisplayCameraDetails();
209        /** Takes screenshot of the current scene.
210        */
211        void takeScreenshot();
212        /** Takes one video frame.
213        */
214
215        void showBoundingBoxes();
216        void takeVideoFrame(std::ofstream &ofstr);
217
218        void nextSceneDetailLevel();
219        void nextFilter();
220        void nextAlgorithm();
221        void nextNodeVizMode();
222        void nextAppState();
223    void changeThreshold(int incr);
224        void changeAssumedVisibility(int incr);
225        void changeVizScale(const int incr);
226       
227        void setTestGeometryForVisibleLeaves(bool testGeometryForVisibleLeaves);
228       
229        void setTestGeometryBounds(bool testGeometryBounds);
230
231        /** Shows visualization of octree hierarchy nodes.
232        */
233        void toggleShowOctree();
234
235        /** Shows visualization of the view cells.
236        */
237        void toggleShowViewCells();
238       
239        /** Toggles between view cells / no view cells.
240        */
241        void toggleUseViewCells();
242
243        /** Toggles whether we use the initial depth pass.
244        */
245        void toggleUseDepthPass();
246
247        void toggleFlushQueue();
248
249        /** Toggles whether the visualization is shown.
250        */
251        void toggleShowViz();
252
253        /** Toggles whether the frames are recorded.
254        */
255        void toggleRecord();
256
257        void changeFloorDist(const float incr);
258
259        /** Applies visibility query. Collects the visible objects
260                and their visibility information.
261                @param fromPoint if query should be from point or from camera
262                @param relativeVisibility if query should return number of visible pixels or
263                the ratio visible / projected pixels.
264                @param useItemBuffer if item buffer should be used or occlusion queries
265        */
266        void applyVisibilityQuery(bool fromPoint, bool relativeVisibility, bool useItemBuffer);
267
268        /** Shows result from visibility queries.
269        */
270        void toggleShowQueryStats();
271
272        /** Sets the type of the objects generated in the scene.
273        */
274        void applyObjectType();
275
276        void setAlgorithm(const int algorithm);
277
278
279        static String msAlgorithmCaptions[];
280        static String msApprevAlgorithmCaptions[];
281        static String msQueryTypeCaptions[];
282        static String msQueryRelativeVisCaptions[];
283        static String msQueryMethodCaptions[];
284        static Real msObjectTerrainOffsets[];
285        static Real msObjectScales[];
286        static String msObjectCaptions[];
287
288protected:
289
290        void initHelpOverlayElement(String name, int top);
291        void initOverlayElement(OverlayElement **elInfo, String ext, String name,
292                                                        int top, String caption);
293
294        //-- initialise overlays
295        void initHelpOverlay();
296        void initVisStatsOverlay();
297        void initQueryOverlay();
298       
299        SceneManager *mSceneMgr;           // A pointer to the scene manager
300   
301        CEGUI::Renderer *mGUIRenderer;     // cegui renderer
302       
303        int mCurrentAlgorithm;
304        int mVisibilityThreshold;
305        int mAssumedVisibility;
306        /// the current frame number
307        int mCurrentFrame;
308        /// the current application state
309        int mAppState;
310        /// The number of objects on the screen
311        int mObjectCount;           
312        /// visualization mode
313        int mNodeVizMode;
314        /// number of video frames
315        int mNumVideoFrames;
316        /// the newly created object
317        int m_iShowBoundingBoxes;
318        int m_iBoundingBoxLevel;
319        SceneNode *mCurrentObject;   
320
321        WalkthroughStats mWalkthroughStats;
322
323        /// visualization scale
324        float mVizScale;
325
326        ////////////////////////////////////////////////////////////////////////
327
328        /** HACK for recording demo:
329                1) the walkthrough must be recorded.
330                2) the walktrough must be replayed using the different options, e.g., CHC
331                   with the mSavePrecomputedFps (key "E") option. This saves the frame rates
332                   for the different methods.
333            3) the walkthrough must then be replayed with the mRecordDemo (KEY "M") options turned on.
334                   for the video recording we are using FRAPS.
335        */
336
337        /// the currently interpolated FPS
338        float mPrecomputedFps;
339        /// saves the precomputed fps for the different methods.
340        bool mSavePrecomputedFps;
341        /// displayes the precomputed fps
342        bool mRecordDemo;
343       
344        ////////////////////////////////////////////////////////////////////////////
345
346
347        OverlayElement *mAlgorithmInfo;
348        OverlayElement *mThresholdInfo;
349        OverlayElement *mAssumedVisibilityInfo;
350        OverlayElement *mFrustumCulledNodesInfo;
351        OverlayElement *mQueryCulledNodesInfo;
352    OverlayElement *mTraversedNodesInfo;
353        OverlayElement *mHierarchyNodesInfo;
354        OverlayElement *mTestGeometryForVisibleLeavesInfo;
355        OverlayElement *mUseDepthPassInfo;
356        OverlayElement *mRenderedNodesInfo;
357        OverlayElement *mObjectsCountInfo;
358        OverlayElement *mQueriesIssuedInfo;
359        OverlayElement *mDelayedQueriesIssuedInfo;
360        OverlayElement *mDelayedTraversedNodesInfo;
361        OverlayElement *mCurrentObjectTypeInfo;
362        OverlayElement *mViewCellsInfo;
363
364        OverlayElement *mMyStatsAlgorithmInfo;
365        OverlayElement *mMyStatsFpsInfo;
366
367        OverlayElement *mMyLoadingInfo;
368       
369        OverlayElement *mQueryTypeInfo;
370        OverlayElement *mQueryVisibleNodesInfo;
371        OverlayElement *mQueryVisibleGeometryInfo;
372        OverlayElement *mQueryVisiblePatchInfo;
373
374        OverlayElement *mQueryVisiblityInfo;
375        OverlayElement *mQueryNodeVisibilityInfo;
376        OverlayElement *mQueryGeometryVisibilityInfo;
377        OverlayElement *mQueryPatchVisibilityInfo;
378
379        RayQueryExecutor *mRayQueryExecutor;
380        TerrainContentGenerator *mTerrainContentGenerator;
381
382        bool mTestGeometryForVisibleLeaves;
383        bool mTestGeometryBounds;
384
385        bool mShowOctree;
386        bool mShowViewCells;
387        bool mUseDepthPass;
388        bool mFlushQueue;
389        bool mShowVisualization;
390        bool mCullCamera;
391        bool mRecordFrames;
392        bool mShowShadows;
393        bool mVisualizeCulledNodes;
394        bool mShowHelp;
395        bool mStatsOn;
396       
397
398        ////////
399        //-- true if the mouse buttons are down
400       
401        bool mLMouseDown;
402        bool mRMouseDown;   
403        bool mShutdownRequested;
404        bool mDisplayCameraDetails;
405        bool mUseItemBuffer;
406        bool mUseAnimation;
407       
408
409        int mItemBufferMode;
410
411        Real mVizCameraHeight;
412
413        Camera *mVizCamera;
414        SceneNode *mCamNode;
415       
416        /// Per frame data stored for a walkthrough
417        FrameInfoContainer mFrameInfo;
418        /// HACK for demo
419        FrameInfoContainer mPrecomputedFpsFrameInfo;
420       
421        Real mReplayTimeElapsed;
422    InputReader* mInputDevice;
423    Camera* mCamera;
424
425    Vector3 mTranslateVector;
426    RenderWindow* mWindow;
427   
428        unsigned int mNumScreenShots;
429    int mSceneDetailIndex;
430        int mAniso;
431
432        float mMoveScale;
433    Degree mRotScale;
434
435    // just to stop toggles flipping too fast
436    Real mTimeDelay;
437    Radian mRotX, mRotY;
438    TextureFilterOptions mFiltering;
439 
440        Real mMoveSpeed;
441    Degree mRotateSpeed;
442   
443        Overlay* mDebugOverlay;
444        Overlay* mHelpOverlay;
445        Overlay* mCullStatsOverlay;
446        Overlay* mQueryOverlay;
447        Overlay* mMyStatsOverlay;
448        Overlay *mLoadingOverlay;
449
450        Light *mSunLight;
451        GtpVisibility::VisibilityManager *mVisibilityManager;
452        EventProcessor* mEventProcessor;
453       
454        bool mShiftPressed;
455        bool mShowQueryStats;
456       
457        PlatformQueryManager *mQueryManager;
458
459        float mDelayedQueriesIssued;
460        float mDelayedTraversedNodes;
461
462        int mCurrentObjectType;
463       
464        bool mDeleteObjects;
465        bool mRecordVideo;
466
467        Timer *mTimer;
468        long mTimeFrameEnded;
469        long mTimeFrameStarted;
470        float mPureRenderTimeFps;
471        TestCullingTerrainApplication *mApplication;
472
473        bool mUseBufferedInputMouse;
474       
475        bool mUseViewCells;
476        bool mViewCellsLoaded;
477
478        float mFloorDist;
479};
480
481
482#endif //_TerrainFrameListener_H__
Note: See TracBrowser for help on using the repository browser.