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

Revision 13172, 14.1 kB (checked in by robert, 10 hours ago)

From Jason Beverage, "It looks like the Callback header got accidentally removed from the CMakeLists.txt in the submission yesterday for the geometry instancing example."

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