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

Revision 13041, 14.1 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
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#include <osgUtil/IncrementalCompileOperation>
21
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        virtual void setViewerStats(osg::Stats* stats) = 0;
45
46        /** Get the Viewers Stats object.*/
47        virtual osg::Stats* getViewerStats() = 0;
48
49        /** Get the Viewers Stats object.*/
50        virtual const osg::Stats* getViewerStats() const = 0;
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        /** Set the end barrier operation. \c op may be one of GL_FLUSH, GL_FINISH,
111         * or NO_OPERATION. NO_OPERATION is the default. Per BarrierOperation::operator()(),
112         * a glFlush() command, glFinish() command, or no additional OpenGL command will be
113         * issued before entering the end barrier. */
114        void setEndBarrierOperation(osg::BarrierOperation::PreBlockOp op);
115
116        /** Get the end barrier operation. */
117        osg::BarrierOperation::PreBlockOp getEndBarrierOperation() const { return _endBarrierOperation; }
118
119
120
121        /** Set the done flag to signal the viewer's work is done and should exit the frame loop.*/
122        void setDone(bool done) { _done = done; }
123
124        /** Return true if  viewer's work is done and should exit the frame loop.*/
125        bool done() const { return _done; }
126
127        /** Set the EventVisitor. */
128        void setEventVisitor(osgGA::EventVisitor* eventVisitor) { _eventVisitor = eventVisitor; }
129
130        /** Get the EventVisitor. */
131        osgGA::EventVisitor* getEventVisitor() { return _eventVisitor.get(); }
132
133        /** Get the const EventVisitor. */
134        const osgGA::EventVisitor* getEventVisitor() const { return _eventVisitor.get(); }
135
136        /** Set the key event that the viewer checks on each frame to see if the viewer's done flag should be set to
137          * signal end of viewers main loop.
138          * Default value is Escape (osgGA::GUIEVentAdapter::KEY_Escape).
139          * Setting to 0 switches off the feature.*/
140        void setKeyEventSetsDone(int key) { _keyEventSetsDone = key; }
141
142        /** get the key event that the viewer checks on each frame to see if the viewer's done flag.*/
143        int getKeyEventSetsDone() const { return _keyEventSetsDone; }
144
145        /** if the flag is true, the viewer set its done flag when a QUIT_APPLICATION is received, false disables this feature */
146        void setQuitEventSetsDone(bool flag) { _quitEventSetsDone = flag; }
147
148        /** @return true if the viewer respond to the QUIT_APPLICATION-event */
149        bool getQuitEventSetsDone() const { return _quitEventSetsDone; }
150
151
152        /** Hint to tell the renderingTraversals() method whether to call relaseContext() on the last
153          * context that was made current by the thread calling renderingTraverals().  Note, when
154          * running multi-threaded viewer no threads will be made current or release current.
155          * Setting this hint to false can enable the frame loop to be lazy about calling makeCurrent
156          * and releaseContext on each new frame, helping performance.  However, if you frame loop
157          * is managing multiple graphics context all from the main frame thread then this hint must
158          * be left on, otherwise the wrong context could be left active, introducing errors in rendering.*/
159        void setReleaseContextAtEndOfFrameHint(bool hint) { _releaseContextAtEndOfFrameHint = hint; }
160
161        /** Hint to tell the renderingTraversals() method whether to call relaseContext().*/
162        bool getReleaseContextAtEndOfFrameHint() const { return _releaseContextAtEndOfFrameHint; }
163
164
165        /** Set the UpdateVisitor. */
166        void setUpdateVisitor(osgUtil::UpdateVisitor* updateVisitor) { _updateVisitor = updateVisitor; }
167
168        /** Get the UpdateVisitor. */
169        osgUtil::UpdateVisitor* getUpdateVisitor() { return _updateVisitor.get(); }
170
171        /** Get the const UpdateVisitor. */
172        const osgUtil::UpdateVisitor* getUpdateVisitor() const { return _updateVisitor.get(); }
173
174
175        /** Set the Update OperationQueue. */
176        void setUpdateOperations(osg::OperationQueue* operations) { _updateOperations = operations; }
177
178        /** Get the Update OperationQueue. */
179        osg::OperationQueue* getUpdateOperations() { return _updateOperations.get(); }
180
181        /** Get the const Update OperationQueue. */
182        const osg::OperationQueue* getUpdateOperations() const { return _updateOperations.get(); }
183
184        /** Add an update operation.*/
185        void addUpdateOperation(osg::Operation* operation);
186
187        /** Remove an update operation.*/
188        void removeUpdateOperation(osg::Operation* operation);
189
190
191        /** Set the graphics operation to call on realization of the viewers graphics windows.*/
192        void setRealizeOperation(osg::Operation* op) { _realizeOperation = op; }
193
194        /** Get the graphics operation to call on realization of the viewers graphics windows.*/
195        osg::Operation* getRealizeOperation() { return _realizeOperation.get(); }
196
197
198        /** Set the incremental compile operation.
199          * Used to manage the OpenGL object compilation and merging of subgraphs in a way that avoids overloading
200          * the rendering of frame with too many new objects in one frame. */
201        void setIncrementalCompileOperation(osgUtil::IncrementalCompileOperation* ico);
202
203        /** Get the incremental compile operation. */
204        osgUtil::IncrementalCompileOperation* getIncrementalCompileOperation() { return _incrementalCompileOperation.get(); }
205
206
207        enum FrameScheme
208        {
209            ON_DEMAND,
210            CONTINUOUS
211        };
212
213        void setRunFrameScheme(FrameScheme fs) { _runFrameScheme = fs; }
214        FrameScheme getRunFrameScheme() const { return _runFrameScheme; }
215
216        void setRunMaxFrameRate(double frameRate) { _runMaxFrameRate = frameRate; }
217        double getRunMaxFrameRate() const { return _runMaxFrameRate; }
218
219        /** Execute a main frame loop.
220          * Equivalent to while (!viewer.done()) viewer.frame();
221          * Also calls realize() if the viewer is not already realized,
222          * and installs trackball manipulator if one is not already assigned.
223          */
224        virtual int run();
225
226        /** check to see if the new frame is required, called by run(..) when FrameScheme is set to ON_DEMAND.*/
227        virtual bool checkNeedToDoFrame() = 0;
228
229        /** Render a complete new frame.
230          * Calls advance(), eventTraversal(), updateTraversal(), renderingTraversals(). */
231        virtual void frame(double simulationTime=USE_REFERENCE_TIME);
232
233        virtual void advance(double simulationTime=USE_REFERENCE_TIME) = 0;
234
235        virtual void eventTraversal() = 0;
236
237        virtual void updateTraversal() = 0;
238
239        virtual void renderingTraversals();
240
241        typedef std::vector<osg::Camera*> Cameras;
242        virtual void getCameras(Cameras& cameras, bool onlyActive=true) = 0;
243
244        typedef std::vector<osg::GraphicsContext*> Contexts;
245        virtual void getContexts(Contexts& contexts, bool onlyValid=true) = 0;
246
247        typedef std::vector<osgViewer::GraphicsWindow*> Windows;
248        virtual void getWindows(Windows& windows, bool onlyValid=true);
249
250        typedef std::vector<OpenThreads::Thread*> Threads;
251        virtual void getAllThreads(Threads& threads, bool onlyActive=true) = 0;
252
253        typedef std::vector<osg::OperationThread*> OperationThreads;
254        virtual void getOperationThreads(OperationThreads& threads, bool onlyActive=true) = 0;
255
256        typedef std::vector<osgViewer::Scene*> Scenes;
257        virtual void getScenes(Scenes& scenes, bool onlyValid=true) = 0;
258
259        typedef std::vector<osgViewer::View*> Views;
260        virtual void getViews(Views& views, bool onlyValid=true) = 0;
261
262        /** Check to see if any windows are still open. If not, set viewer done to true. */
263        void checkWindowStatus();
264
265        /** Check to see if windows are still open using the list of contexts given as a parameter.
266         *  If no windows are open, stop rendering threads and set viewer done to true.
267         *  This function is more effective than checkWindowStatus() as it does not query
268         *  the context list and should be used whenever context list is already available in your code.*/
269        void checkWindowStatus(const Contexts& contexts);
270
271        virtual double elapsedTime() = 0;
272
273        virtual osg::FrameStamp* getViewerFrameStamp() = 0;
274
275        /** Get the keyboard and mouse usage of this viewer.*/
276        virtual void getUsage(osg::ApplicationUsage& usage) const = 0;
277
278    protected:
279
280        void viewerBaseInit();
281
282        friend class osgViewer::View;
283
284        inline void makeCurrent(osg::GraphicsContext* gc)
285        {
286            if (_currentContext==gc) return;
287
288            releaseContext();
289
290            if (gc && gc->valid() && gc->makeCurrent()) _currentContext = gc;
291        }
292
293        inline void releaseContext()
294        {
295            if (_currentContext.valid() && _currentContext->valid())
296            {
297                _currentContext->releaseContext();
298            }
299            _currentContext = 0;
300        }
301
302        virtual void viewerInit() = 0;
303
304        bool                                                _firstFrame;
305        bool                                                _done;
306        int                                                 _keyEventSetsDone;
307        bool                                                _quitEventSetsDone;
308        bool                                                _releaseContextAtEndOfFrameHint;
309
310        ThreadingModel                                      _threadingModel;
311        bool                                                _threadsRunning;
312
313        bool                                                _requestRedraw;
314        bool                                                _requestContinousUpdate;
315
316        FrameScheme                                         _runFrameScheme;
317        double                                              _runMaxFrameRate;
318
319
320        BarrierPosition                                     _endBarrierPosition;
321        osg::BarrierOperation::PreBlockOp                   _endBarrierOperation;
322
323        osg::ref_ptr<osg::BarrierOperation>                 _startRenderingBarrier;
324        osg::ref_ptr<osg::BarrierOperation>                 _endRenderingDispatchBarrier;
325        osg::ref_ptr<osg::EndOfDynamicDrawBlock>            _endDynamicDrawBlock;
326
327        osg::ref_ptr<osgGA::EventVisitor>                   _eventVisitor;
328
329        osg::ref_ptr<osg::OperationQueue>                   _updateOperations;
330        osg::ref_ptr<osgUtil::UpdateVisitor>                _updateVisitor;
331
332        osg::ref_ptr<osg::Operation>                        _realizeOperation;
333        osg::ref_ptr<osgUtil::IncrementalCompileOperation>  _incrementalCompileOperation;
334
335        osg::observer_ptr<osg::GraphicsContext>             _currentContext;
336};
337
338}
339
340#endif
Note: See TracBrowser for help on using the browser.