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

Revision 9186, 11.7 kB (checked in by robert, 6 years ago)

Reverted Kyle Centers mistaken patch that removed a _currentContext->valid() that was actually required.

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 inherited 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 may result in slightly higher framerates, but may
102          * lead to inconsistent 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 signal the viewer's work is done and should exit the frame loop.*/
113        void setDone(bool done) { _done = done; }
114
115        /** Return 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        /** Hint to tell the renderingTraversals() method whether to call relaseContext() on the last
144          * context that was made current by the thread calling renderingTraverals().  Note, when
145          * running multi-threaded viewer no threads will be made current or release current.
146          * Setting this hint to false can enable the frame loop to be lazy about calling makeCurrent
147          * and releaseContext on each new frame, helping performance.  However, if you frame loop
148          * is managing multiple graphics context all from the main frame thread then this hint must
149          * be left on, otherwise the wrong context could be left active, introducing errors in rendering.*/
150        void setReleaseContextAtEndOfFrameHint(bool hint) { _releaseContextAtEndOfFrameHint = hint; }
151       
152        /** Hint to tell the renderingTraversals() method whether to call relaseContext().*/
153        bool getReleaseContextAtEndOfFrameHint() const { return _releaseContextAtEndOfFrameHint; }
154
155
156        /** Set the UpdateVisitor. */
157        void setUpdateVisitor(osgUtil::UpdateVisitor* updateVisitor) { _updateVisitor = updateVisitor; }
158
159        /** Get the UpdateVisitor. */
160        osgUtil::UpdateVisitor* getUpdateVisitor() { return _updateVisitor.get(); }
161
162        /** Get the const UpdateVisitor. */
163        const osgUtil::UpdateVisitor* getUpdateVisitor() const { return _updateVisitor.get(); }
164
165
166        /** Set the Update OperationQueue. */
167        void setUpdateOperations(osg::OperationQueue* operations) { _updateOperations = operations; }
168
169        /** Get the Update OperationQueue. */
170        osg::OperationQueue* getUpdateOperations() { return _updateOperations.get(); }
171       
172        /** Get the const Update OperationQueue. */
173        const osg::OperationQueue* getUpdateOperations() const { return _updateOperations.get(); }
174       
175        /** Add an update operation.*/
176        void addUpdateOperation(osg::Operation* operation);
177
178        /** Remove an update operation.*/
179        void removeUpdateOperation(osg::Operation* operation);
180
181
182        /** Set the graphics operation to call on realization of the viewers graphics windows.*/
183        void setRealizeOperation(osg::Operation* op) { _realizeOperation = op; }
184
185        /** Get the graphics operation to call on realization of the viewers graphics windows.*/
186        osg::Operation* getRealizeOperation() { return _realizeOperation.get(); }
187
188
189        /** Check to see if windows are still open, if not set viewer done to true. */
190        void checkWindowStatus();
191
192
193
194        /** Execute a main frame loop.
195          * Equivalent to while (!viewer.done()) viewer.frame();
196          * Also calls realize() if the viewer is not already realized,
197          * and installs trackball manipulator if one is not already assigned.
198          */
199        virtual int run() = 0;
200
201        /** Render a complete new frame.
202          * Calls advance(), eventTraversal(), updateTraversal(), renderingTraversals(). */
203        virtual void frame(double simulationTime=USE_REFERENCE_TIME);
204
205        virtual void advance(double simulationTime=USE_REFERENCE_TIME) = 0;
206
207        virtual void eventTraversal() = 0;
208
209        virtual void updateTraversal() = 0;
210       
211        virtual void renderingTraversals();
212
213        typedef std::vector<osg::Camera*> Cameras;
214        virtual void getCameras(Cameras& cameras, bool onlyActive=true) = 0;
215
216        typedef std::vector<osg::GraphicsContext*> Contexts;
217        virtual void getContexts(Contexts& contexts, bool onlyValid=true) = 0;
218
219        typedef std::vector<osgViewer::GraphicsWindow*> Windows;
220        virtual void getWindows(Windows& windows, bool onlyValid=true);
221
222        typedef std::vector<OpenThreads::Thread*> Threads;
223        virtual void getAllThreads(Threads& threads, bool onlyActive=true) = 0;
224
225        typedef std::vector<osg::OperationThread*> OperationThreads;
226        virtual void getOperationThreads(OperationThreads& threads, bool onlyActive=true) = 0;
227
228        typedef std::vector<osgViewer::Scene*> Scenes;
229        virtual void getScenes(Scenes& scenes, bool onlyValid=true) = 0;
230
231        typedef std::vector<osgViewer::View*> Views;
232        virtual void getViews(Views& views, bool onlyValid=true) = 0;
233
234        virtual double elapsedTime() = 0;
235
236        virtual osg::FrameStamp* getViewerFrameStamp() = 0;
237
238        /** Get the keyboard and mouse usage of this viewer.*/
239        virtual void getUsage(osg::ApplicationUsage& usage) const = 0;
240       
241    protected:
242   
243        inline void makeCurrent(osg::GraphicsContext* gc)
244        {
245            if (_currentContext==gc) return;
246           
247            releaseContext();
248           
249            if (gc && gc->valid() && gc->makeCurrent()) _currentContext = gc;
250        }
251       
252        inline void releaseContext()
253        {
254            if (_currentContext.valid() && _currentContext->valid())
255            {
256                _currentContext->releaseContext();
257            }
258            _currentContext = 0;
259        }
260
261        virtual void viewerInit() = 0;
262       
263
264        osg::ref_ptr<osg::Stats>                    _stats;
265
266        bool                                        _firstFrame;
267        bool                                        _done;
268        int                                         _keyEventSetsDone;
269        bool                                        _quitEventSetsDone;
270        bool                                        _releaseContextAtEndOfFrameHint;
271       
272        ThreadingModel                              _threadingModel;
273        bool                                        _threadsRunning;
274
275        BarrierPosition                             _endBarrierPosition;
276
277        osg::ref_ptr<osg::BarrierOperation>         _startRenderingBarrier;
278        osg::ref_ptr<osg::BarrierOperation>         _endRenderingDispatchBarrier;
279        osg::ref_ptr<osg::EndOfDynamicDrawBlock>    _endDynamicDrawBlock;
280       
281        osg::ref_ptr<osgGA::EventVisitor>           _eventVisitor;
282       
283        osg::ref_ptr<osg::OperationQueue>           _updateOperations;
284        osg::ref_ptr<osgUtil::UpdateVisitor>        _updateVisitor;
285       
286        osg::ref_ptr<osg::Operation>                _realizeOperation;
287
288        osg::observer_ptr<osg::GraphicsContext>     _currentContext;
289};
290
291}
292
293#endif
Note: See TracBrowser for help on using the browser.