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

Revision 2359, 12.7 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        void setTestGeometryForVisibleLeaves(bool testGeometryForVisibleLeaves);
227        /** Shows visualization of octree hierarchy nodes.
228        */
229        void toggleShowOctree();
230        /** Shows visualization of the view cells.
231        */
232        void toggleShowViewCells();
233        /** Toggles between view cells / no view cells.
234        */
235        void toggleUseViewCells();
236
237        void toggleUseVisibilityFilter();
238
239        /** Toggles whether we use the initial depth pass.
240        */
241        void toggleUseDepthPass();
242
243        void toggleFlushQueue();
244
245        /** Toggles whether the visualization is shown.
246        */
247        void toggleShowViz();
248
249        /** Toggles whether the frames are recorded.
250        */
251        void toggleRecord();
252
253        void changeFloorDist(const float incr);
254
255        /** Applies visibility query. Collects the visible objects
256                and their visibility information.
257                @param fromPoint if query should be from point or from camera
258                @param relativeVisibility if query should return number of visible pixels or
259                the ratio visible / projected pixels.
260                @param useItemBuffer if item buffer should be used or occlusion queries
261        */
262        void applyVisibilityQuery(bool fromPoint, bool relativeVisibility, bool useItemBuffer);
263
264        /** Shows result from visibility queries.
265        */
266        void toggleShowQueryStats();
267
268        /** Sets the type of the objects generated in the scene.
269        */
270        void applyObjectType();
271
272        void setAlgorithm(const int algorithm);
273
274
275        static String msAlgorithmCaptions[];
276        static String msApprevAlgorithmCaptions[];
277        static String msQueryTypeCaptions[];
278        static String msQueryRelativeVisCaptions[];
279        static String msQueryMethodCaptions[];
280        static Real msObjectTerrainOffsets[];
281        static Real msObjectScales[];
282        static String msObjectCaptions[];
283
284protected:
285
286        void initHelpOverlayElement(String name, int top);
287        void initOverlayElement(OverlayElement **elInfo, String ext, String name,
288                                                        int top, String caption);
289
290        //-- initialise overlays
291        void initHelpOverlay();
292        void initVisStatsOverlay();
293        void initQueryOverlay();
294       
295        SceneManager *mSceneMgr;           // A pointer to the scene manager
296   
297        CEGUI::Renderer *mGUIRenderer;     // cegui renderer
298       
299        int mCurrentAlgorithm;
300        int mVisibilityThreshold;
301        int mAssumedVisibility;
302        /// the current frame number
303        int mCurrentFrame;
304        /// the current application state
305        int mAppState;
306        /// The number of objects on the screen
307        int mObjectCount;           
308        /// visualization mode
309        int mNodeVizMode;
310        /// number of video frames
311        int mNumVideoFrames;
312        /// the newly created object
313        int m_iShowBoundingBoxes;
314        int m_iBoundingBoxLevel;
315        SceneNode *mCurrentObject;   
316
317        WalkthroughStats mWalkthroughStats;
318
319        /// visualization scale
320        float mVizScale;
321
322        ////////////////////////////////////////////////////////////////////////
323
324        /** HACK for recording demo:
325                1) the walkthrough must be recorded.
326                2) the walktrough must be replayed using the different options, e.g., CHC
327                   with the mSavePrecomputedFps (key "E") option. This saves the frame rates
328                   for the different methods.
329            3) the walkthrough must then be replayed with the mRecordDemo (KEY "M") options turned on.
330                   for the video recording we are using FRAPS.
331        */
332
333        /// the currently interpolated FPS
334        float mPrecomputedFps;
335        /// saves the precomputed fps for the different methods.
336        bool mSavePrecomputedFps;
337        /// displayes the precomputed fps
338        bool mRecordDemo;
339       
340        ////////////////////////////////////////////////////////////////////////////
341
342
343        OverlayElement *mAlgorithmInfo;
344        OverlayElement *mThresholdInfo;
345        OverlayElement *mAssumedVisibilityInfo;
346        OverlayElement *mFrustumCulledNodesInfo;
347        OverlayElement *mQueryCulledNodesInfo;
348    OverlayElement *mTraversedNodesInfo;
349        OverlayElement *mHierarchyNodesInfo;
350        OverlayElement *mTestGeometryForVisibleLeavesInfo;
351        OverlayElement *mUseDepthPassInfo;
352        OverlayElement *mRenderedNodesInfo;
353        OverlayElement *mObjectsCountInfo;
354        OverlayElement *mQueriesIssuedInfo;
355        OverlayElement *mDelayedQueriesIssuedInfo;
356        OverlayElement *mDelayedTraversedNodesInfo;
357        OverlayElement *mCurrentObjectTypeInfo;
358        OverlayElement *mViewCellsInfo;
359
360        OverlayElement *mMyStatsAlgorithmInfo;
361        OverlayElement *mMyStatsFpsInfo;
362
363        OverlayElement *mMyLoadingInfo;
364       
365        OverlayElement *mQueryTypeInfo;
366        OverlayElement *mQueryVisibleNodesInfo;
367        OverlayElement *mQueryVisibleGeometryInfo;
368        OverlayElement *mQueryVisiblePatchInfo;
369
370        OverlayElement *mQueryVisiblityInfo;
371        OverlayElement *mQueryNodeVisibilityInfo;
372        OverlayElement *mQueryGeometryVisibilityInfo;
373        OverlayElement *mQueryPatchVisibilityInfo;
374        //OverlayElement *mHelpInfo;
375
376        RayQueryExecutor *mRayQueryExecutor;
377        TerrainContentGenerator *mTerrainContentGenerator;
378
379        bool mTestGeometryForVisibleLeaves;
380        bool mShowOctree;
381        bool mShowViewCells;
382        bool mUseDepthPass;
383        bool mFlushQueue;
384        bool mShowVisualization;
385        bool mCullCamera;
386        bool mRecordFrames;
387        bool mShowShadows;
388        bool mVisualizeCulledNodes;
389        bool mShowHelp;
390        bool mStatsOn;
391        bool mLMouseDown;
392        // True if the mouse buttons are down
393        bool mRMouseDown;   
394        bool mShutdownRequested;
395        bool mDisplayCameraDetails;
396        bool mUseItemBuffer;
397        bool mUseAnimation;
398       
399        int mItemBufferMode;
400
401        Real mVizCameraHeight;
402
403        Camera *mVizCamera;
404        SceneNode *mCamNode;
405       
406        /// Per frame data stored for a walkthrough
407        FrameInfoContainer mFrameInfo;
408        /// HACK for demo
409        FrameInfoContainer mPrecomputedFpsFrameInfo;
410       
411        Real mReplayTimeElapsed;
412        //EventProcessor* mEventProcessor;
413    InputReader* mInputDevice;
414    Camera* mCamera;
415
416    Vector3 mTranslateVector;
417    RenderWindow* mWindow;
418   
419        unsigned int mNumScreenShots;
420    int mSceneDetailIndex;
421        int mAniso;
422
423        float mMoveScale;
424    Degree mRotScale;
425
426    // just to stop toggles flipping too fast
427    Real mTimeDelay;
428    Radian mRotX, mRotY;
429    TextureFilterOptions mFiltering;
430 
431        Real mMoveSpeed;
432    Degree mRotateSpeed;
433   
434        Overlay* mDebugOverlay;
435        Overlay* mHelpOverlay;
436        Overlay* mCullStatsOverlay;
437        Overlay* mQueryOverlay;
438        Overlay* mMyStatsOverlay;
439        Overlay *mLoadingOverlay;
440
441        Light *mSunLight;
442        GtpVisibility::VisibilityManager *mVisibilityManager;
443        EventProcessor* mEventProcessor;
444       
445        bool mShiftPressed;
446        bool mShowQueryStats;
447       
448        //bool mUseBufferedInputKeys, mUseBufferedInputMouse, mInputTypeSwitchingOn;
449        PlatformQueryManager *mQueryManager;
450
451        float mDelayedQueriesIssued;
452        float mDelayedTraversedNodes;
453
454        int mCurrentObjectType;
455       
456        bool mDeleteObjects;
457        bool mRecordVideo;
458
459        Timer *mTimer;
460        long mTimeFrameEnded;
461        long mTimeFrameStarted;
462        float mPureRenderTimeFps;
463        TestCullingTerrainApplication *mApplication;
464
465        bool mUseBufferedInputMouse;
466       
467        bool mUseViewCells;
468        bool mViewCellsLoaded;
469        bool mUseVisibilityFilter;
470
471        float mFloorDist;
472};
473
474
475#endif //_TerrainFrameListener_H__
Note: See TracBrowser for help on using the repository browser.