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

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