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

Revision 7535, 10.6 kB (checked in by robert, 10 years ago)

Merged the threading set up and rendering code from Viewer and CompositeViewer?
into ViewerBase? to allow CompositeViewer? to inherit the same theading models
previously just supported by osgViewer::Viewer

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);
75       
76        /** Get the threading model the rendering traversals will use.*/
77        ThreadingModel getThreadingModel() const { return _threadingModel; }
78
79        /** Let the viewer suggest the best threading model for the viewers camera/window setup and the hardware available.*/
80        virtual ThreadingModel suggestBestThreadingModel();
81
82        /** Set up the threading and processor affinity as per the viewers threading model.*/
83        virtual void setUpThreading();     
84
85        /** Return true if viewer threads are running. */
86        bool areThreadsRunning() const { return _threadsRunning; }
87
88        /** Stop any threads begin run by viewer.*/
89        virtual void stopThreading();
90
91        /** Start any threads required by the viewer.*/
92        virtual void startThreading();       
93
94        enum BarrierPosition
95        {
96            BeforeSwapBuffers,
97            AfterSwapBuffers
98        };
99       
100         /** Set the position of the end barrier.
101          * AfterSwapBuffers will may result is slightly higher framerates, by may
102          * lead to inconcistent swapping between different windows.
103          * BeforeSwapBuffers may lead to slightly lower framerate, but improve consistency in timing of swap buffers,
104          * especially important if you are likely to consistently break frame.*/
105        void setEndBarrierPosition(BarrierPosition bp);
106
107        /** Get the end barrier position.*/
108        BarrierPosition getEndBarrierPosition() const { return _endBarrierPosition; }
109
110 
111 
112        /** Set the done flag to singnal the viewer's work is done and should exit the frame loop.*/
113        void setDone(bool done) { _done = done; }
114
115        /** Reurn true if  viewer's work is done and should exit the frame loop.*/
116        bool done() const { return _done; }
117
118        /** Set the EventVisitor. */
119        void setEventVisitor(osgGA::EventVisitor* eventVisitor) { _eventVisitor = eventVisitor; }
120
121        /** Get the EventVisitor. */
122        osgGA::EventVisitor* getEventVisitor() { return _eventVisitor.get(); }
123
124        /** Get the const EventVisitor. */
125        const osgGA::EventVisitor* getEventVisitor() const { return _eventVisitor.get(); }
126
127        /** Set the key event that the viewer checks on each frame to see if the viewer's done flag should be set to
128          * signal end of viewers main loop.
129          * Default value is Escape (osgGA::GUIEVentAdapter::KEY_Escape).
130          * Setting to 0 switches off the feature.*/
131        void setKeyEventSetsDone(int key) { _keyEventSetsDone = key; }
132
133        /** get the key event that the viewer checks on each frame to see if the viewer's done flag.*/
134        int getKeyEventSetsDone() const { return _keyEventSetsDone; }
135       
136        /** if the flag is true, the viewer set its done flag when a QUIT_APPLICATION is received, false disables this feature */
137        void setQuitEventSetsDone(bool flag) { _quitEventSetsDone = flag; }
138       
139        /** @return true if the viewer respond to the QUIT_APPLICATION-event */
140        bool getQuitEventSetsDone() const { return _quitEventSetsDone; }
141
142
143
144        /** Set the UpdateVisitor. */
145        void setUpdateVisitor(osgUtil::UpdateVisitor* updateVisitor) { _updateVisitor = updateVisitor; }
146
147        /** Get the UpdateVisitor. */
148        osgUtil::UpdateVisitor* getUpdateVisitor() { return _updateVisitor.get(); }
149
150        /** Get the const UpdateVisitor. */
151        const osgUtil::UpdateVisitor* getUpdateVisitor() const { return _updateVisitor.get(); }
152
153
154        /** Set the Update OperationQueue. */
155        void setUpdateOperations(osg::OperationQueue* operations) { _updateOperations = operations; }
156
157        /** Get the Update OperationQueue. */
158        osg::OperationQueue* getUpdateOperations() { return _updateOperations.get(); }
159       
160        /** Get the const Update OperationQueue. */
161        const osg::OperationQueue* getUpdateOperations() const { return _updateOperations.get(); }
162       
163        /** Add an update operation.*/
164        void addUpdateOperation(osg::Operation* operation);
165
166        /** Remove an update operation.*/
167        void removeUpdateOperation(osg::Operation* operation);
168
169
170        /** Set the graphics operation to call on realization of the viewers graphics windows.*/
171        void setRealizeOperation(osg::Operation* op) { _realizeOperation = op; }
172
173        /** Get the graphics operation to call on realization of the viewers graphics windows.*/
174        osg::Operation* getRealizeOperation() { return _realizeOperation.get(); }
175
176
177        /** Check to see if windows are still open, if not set viewer done to true. */
178        void checkWindowStatus();
179
180
181
182        /** Execute a main frame loop.
183          * Equivialant to while (!viewer.done()) viewer.frame();
184          * Also calls realize() if the viewer is not already realized,
185          * and installs trackball manipulator if one is not already assigned.
186          */
187        virtual int run() = 0;
188
189        /** Render a complete new frame.
190          * Calls advance(), eventTraversal(), updateTraversal(), renderingTraversals(). */
191        virtual void frame(double simulationTime=USE_REFERENCE_TIME);
192
193        virtual void advance(double simulationTime=USE_REFERENCE_TIME) = 0;
194
195        virtual void eventTraversal() = 0;
196
197        virtual void updateTraversal() = 0;
198       
199        virtual void renderingTraversals();
200
201        typedef std::vector<osg::Camera*> Cameras;
202        virtual void getCameras(Cameras& cameras, bool onlyActive=true) = 0;
203
204        typedef std::vector<osg::GraphicsContext*> Contexts;
205        virtual void getContexts(Contexts& contexts, bool onlyValid=true) = 0;
206
207        typedef std::vector<osgViewer::GraphicsWindow*> Windows;
208        virtual void getWindows(Windows& windows, bool onlyValid=true) = 0;
209
210        typedef std::vector<OpenThreads::Thread*> Threads;
211        virtual void getAllThreads(Threads& threads, bool onlyActive=true) = 0;
212
213        typedef std::vector<osg::OperationThread*> OperationThreads;
214        virtual void getOperationThreads(OperationThreads& threads, bool onlyActive=true) = 0;
215
216        typedef std::vector<osgViewer::Scene*> Scenes;
217        virtual void getScenes(Scenes& scenes, bool onlyValid=true) = 0;
218
219        typedef std::vector<osgViewer::View*> Views;
220        virtual void getViews(Views& views, bool onlyValid=true) = 0;
221
222        virtual double elapsedTime() = 0;
223
224        virtual osg::FrameStamp* getViewerFrameStamp() = 0;
225
226        /** Get the keyboard and mouse usage of this viewer.*/
227        virtual void getUsage(osg::ApplicationUsage& usage) const = 0;
228
229    protected:
230   
231        inline void makeCurrent(osg::GraphicsContext* gc)
232        {
233            if (_currentContext==gc) return;
234           
235            releaseContext();
236           
237            if (gc && gc->valid() && gc->makeCurrent()) _currentContext = gc;
238        }
239       
240        inline void releaseContext()
241        {
242            if (_currentContext.valid() && _currentContext->valid())
243            {
244                _currentContext->releaseContext();
245            }
246            _currentContext = 0;
247        }
248
249        virtual void viewerInit() = 0;
250       
251
252        osg::ref_ptr<osg::Stats>                    _stats;
253
254        bool                                        _firstFrame;
255        bool                                        _done;
256        int                                         _keyEventSetsDone;
257        bool                                        _quitEventSetsDone;
258       
259        ThreadingModel                              _threadingModel;
260        bool                                        _threadsRunning;
261
262        BarrierPosition                             _endBarrierPosition;
263
264        osg::ref_ptr<osg::BarrierOperation>         _startRenderingBarrier;
265        osg::ref_ptr<osg::BarrierOperation>         _endRenderingDispatchBarrier;
266        osg::ref_ptr<osg::EndOfDynamicDrawBlock>    _endDynamicDrawBlock;
267       
268        osg::ref_ptr<osgGA::EventVisitor>           _eventVisitor;
269       
270        osg::ref_ptr<osg::OperationQueue>           _updateOperations;
271        osg::ref_ptr<osgUtil::UpdateVisitor>        _updateVisitor;
272       
273        osg::ref_ptr<osg::Operation>                _realizeOperation;
274
275        osg::observer_ptr<osg::GraphicsContext>     _currentContext;
276};
277
278}
279
280#endif
Note: See TracBrowser for help on using the browser.