root/OpenSceneGraph/trunk/include/osgViewer/ViewerBase @ 7507

Revision 7507, 8.5 kB (checked in by robert, 10 years ago)

Moved ViewerBase? class out into its own header and source file.

Improved the support for stats collection in CompositeViewer/StatsHandler?.

Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under 
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef OSGVIEWER_VIEWERBASE
15#define OSGVIEWER_VIEWERBASE 1
16
17#include <osg/Stats>
18
19#include <osgUtil/UpdateVisitor>
20
21#include <osgGA/MatrixManipulator>
22#include <osgGA/EventVisitor>
23#include <osgGA/EventQueue>
24
25#include <osgViewer/Scene>
26#include <osgViewer/GraphicsWindow>
27
28namespace osgViewer {
29
30#define USE_REFERENCE_TIME DBL_MAX
31
32class View;
33
34/** ViewerBase is the view base class that is inhertied by both Viewer and CompositeViewer.*/
35class OSGVIEWER_EXPORT ViewerBase : public virtual osg::Object
36{
37    public:
38   
39        ViewerBase();
40        ViewerBase(const ViewerBase& vb);
41       
42       
43        /** Set the Stats object used for collect various frame related timing and scene graph stats.*/
44        void setStats(osg::Stats* stats) { _stats = stats; }
45
46        /** Get the Viewers Stats object.*/
47        osg::Stats* getStats() { return _stats.get(); }
48
49        /** Get the Viewers Stats object.*/
50        const osg::Stats* getStats() const { return _stats.get(); }
51
52           
53         /** read the viewer configuration from a configuration file.*/
54        virtual bool readConfiguration(const std::string& filename) = 0;
55       
56        /** Get whether at least of one of this viewers windows are realized.*/
57        virtual bool isRealized() const = 0;
58
59        /** set up windows and associated threads.*/
60        virtual void realize() = 0;
61
62        enum ThreadingModel
63        {
64            SingleThreaded,
65            CullDrawThreadPerContext,
66            ThreadPerContext = CullDrawThreadPerContext,
67            DrawThreadPerContext,
68            CullThreadPerCameraDrawThreadPerContext,
69            ThreadPerCamera = CullThreadPerCameraDrawThreadPerContext,
70            AutomaticSelection
71        };
72
73        /** Set the threading model the rendering traversals will use.*/
74        virtual void setThreadingModel(ThreadingModel threadingModel) = 0;
75       
76        /** Get the threading model the rendering traversals will use.*/
77        ThreadingModel getThreadingModel() const { return _threadingModel; }
78       
79 
80        /** Set the done flag to singnal the viewer's work is done and should exit the frame loop.*/
81        void setDone(bool done) { _done = done; }
82
83        /** Reurn true if  viewer's work is done and should exit the frame loop.*/
84        bool done() const { return _done; }
85
86        /** Set the EventVisitor. */
87        void setEventVisitor(osgGA::EventVisitor* eventVisitor) { _eventVisitor = eventVisitor; }
88
89        /** Get the EventVisitor. */
90        osgGA::EventVisitor* getEventVisitor() { return _eventVisitor.get(); }
91
92        /** Get the const EventVisitor. */
93        const osgGA::EventVisitor* getEventVisitor() const { return _eventVisitor.get(); }
94
95        /** Set the key event that the viewer checks on each frame to see if the viewer's done flag should be set to
96          * signal end of viewers main loop.
97          * Default value is Escape (osgGA::GUIEVentAdapter::KEY_Escape).
98          * Setting to 0 switches off the feature.*/
99        void setKeyEventSetsDone(int key) { _keyEventSetsDone = key; }
100
101        /** get the key event that the viewer checks on each frame to see if the viewer's done flag.*/
102        int getKeyEventSetsDone() const { return _keyEventSetsDone; }
103       
104        /** if the flag is true, the viewer set its done flag when a QUIT_APPLICATION is received, false disables this feature */
105        void setQuitEventSetsDone(bool flag) { _quitEventSetsDone = flag; }
106       
107        /** @return true if the viewer respond to the QUIT_APPLICATION-event */
108        bool getQuitEventSetsDone() const { return _quitEventSetsDone; }
109
110
111
112        /** Set the UpdateVisitor. */
113        void setUpdateVisitor(osgUtil::UpdateVisitor* updateVisitor) { _updateVisitor = updateVisitor; }
114
115        /** Get the UpdateVisitor. */
116        osgUtil::UpdateVisitor* getUpdateVisitor() { return _updateVisitor.get(); }
117
118        /** Get the const UpdateVisitor. */
119        const osgUtil::UpdateVisitor* getUpdateVisitor() const { return _updateVisitor.get(); }
120
121
122        /** Set the Update OperationQueue. */
123        void setUpdateOperations(osg::OperationQueue* operations) { _updateOperations = operations; }
124
125        /** Get the Update OperationQueue. */
126        osg::OperationQueue* getUpdateOperations() { return _updateOperations.get(); }
127       
128        /** Get the const Update OperationQueue. */
129        const osg::OperationQueue* getUpdateOperations() const { return _updateOperations.get(); }
130       
131        /** Add an update operation.*/
132        void addUpdateOperation(osg::Operation* operation);
133
134        /** Remove an update operation.*/
135        void removeUpdateOperation(osg::Operation* operation);
136
137
138        /** Execute a main frame loop.
139          * Equivialant to while (!viewer.done()) viewer.frame();
140          * Also calls realize() if the viewer is not already realized,
141          * and installs trackball manipulator if one is not already assigned.
142          */
143        virtual int run() = 0;
144
145        /** Render a complete new frame.
146          * Calls advance(), eventTraversal(), updateTraversal(), renderingTraversals(). */
147        virtual void frame(double simulationTime=USE_REFERENCE_TIME) = 0;
148
149        virtual void advance(double simulationTime=USE_REFERENCE_TIME) = 0;
150
151        virtual void eventTraversal() = 0;
152
153        virtual void updateTraversal() = 0;
154       
155        virtual void renderingTraversals() = 0;
156
157        typedef std::vector<osg::Camera*> Cameras;
158        virtual void getCameras(Cameras& cameras, bool onlyActive=true) = 0;
159
160        typedef std::vector<osg::GraphicsContext*> Contexts;
161        virtual void getContexts(Contexts& contexts, bool onlyValid=true) = 0;
162
163        typedef std::vector<osgViewer::GraphicsWindow*> Windows;
164        virtual void getWindows(Windows& windows, bool onlyValid=true) = 0;
165
166        typedef std::vector<OpenThreads::Thread*> Threads;
167        virtual void getAllThreads(Threads& threads, bool onlyActive=true) = 0;
168
169        typedef std::vector<osg::OperationThread*> OperationThreads;
170        virtual void getOperationThreads(OperationThreads& threads, bool onlyActive=true) = 0;
171
172        typedef std::vector<osgViewer::Scene*> Scenes;
173        virtual void getScenes(Scenes& scenes, bool onlyValid=true) = 0;
174
175        typedef std::vector<osgViewer::View*> Views;
176        virtual void getViews(Views& views, bool onlyValid=true) = 0;
177
178
179        /** Set the graphics operation to call on realization of the viewers graphics windows.*/
180        void setRealizeOperation(osg::Operation* op) { _realizeOperation = op; }
181
182        /** Get the graphics operation to call on realization of the viewers graphics windows.*/
183        osg::Operation* getRealizeOperation() { return _realizeOperation.get(); }
184
185        /** Set up the threading and processor affinity as per the viewers threading model.*/
186        virtual void setUpThreading() = 0;
187
188        /** Return true if viewer threads are running. */
189        bool areThreadsRunning() const { return _threadsRunning; }
190
191        /** Stop any threads begin run by viewer.*/
192        virtual void stopThreading() = 0;
193
194        /** Start any threads required by the viewer.*/
195        virtual void startThreading() = 0;       
196
197        /** Get the keyboard and mouse usage of this viewer.*/
198        virtual void getUsage(osg::ApplicationUsage& usage) const = 0;
199
200    protected:
201   
202        osg::ref_ptr<osg::Stats>                    _stats;
203
204        bool                                        _done;
205        int                                         _keyEventSetsDone;
206        bool                                        _quitEventSetsDone;
207       
208        ThreadingModel                              _threadingModel;
209        bool                                        _threadsRunning;
210       
211        osg::ref_ptr<osgGA::EventVisitor>           _eventVisitor;
212       
213        osg::ref_ptr<osg::OperationQueue>           _updateOperations;
214        osg::ref_ptr<osgUtil::UpdateVisitor>        _updateVisitor;
215       
216        osg::ref_ptr<osg::Operation>                _realizeOperation;
217
218};
219}
220
221#endif
Note: See TracBrowser for help on using the browser.