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

Revision 2352, 12.6 KB checked in by vizrt_christian_seidl, 17 years ago (diff)

FEATURE: Changes for BITerrainManager

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