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

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