openscenegraph
ViewerBase
Go to the documentation of this file.
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 to 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
63 /** Set whether the setUpThreading() method should call configureAffinity() to set up up the processor affinity of viewer threads.*/
64 void setUseConfigureAffinity(bool flag) { _useConfigureAffinity = flag; }
65
66 /** get whether the setUpThreading() method should call configureAffinity() to set up up the processor affinity of viewer threads.*/
67 bool getUseConfigureAffinity() const { return _useConfigureAffinity; }
68
69 /** analyse the viewer configuration and select an appropriate Affinity for main thread, and any graphics, camera threads and database pagers that are required.*/
70 virtual void configureAffinity();
71
72 /** Set the processor affinity of main thread.*/
73 virtual void setProcessorAffinity(const OpenThreads::Affinity& affinity) { _affinity = affinity; }
74 OpenThreads::Affinity& getProcessorAffinity() { return _affinity; }
75 const OpenThreads::Affinity& getProcessorAffinity() const { return _affinity; }
76
77 enum ThreadingModel
78 {
79 SingleThreaded,
80 CullDrawThreadPerContext,
81 ThreadPerContext = CullDrawThreadPerContext,
82 DrawThreadPerContext,
83 CullThreadPerCameraDrawThreadPerContext,
84 ThreadPerCamera = CullThreadPerCameraDrawThreadPerContext,
85 AutomaticSelection
86 };
87
88 /** Set the threading model the rendering traversals will use.*/
89 virtual void setThreadingModel(ThreadingModel threadingModel);
90
91 /** Get the threading model the rendering traversals will use.*/
92 ThreadingModel getThreadingModel() const { return _threadingModel; }
93
94 /** Let the viewer suggest the best threading model for the viewers camera/window setup and the hardware available.*/
95 virtual ThreadingModel suggestBestThreadingModel();
96
97 /** Set up the threading and processor affinity as per the viewers threading model.*/
98 virtual void setUpThreading();
99
100 /** Return true if viewer threads are running. */
101 bool areThreadsRunning() const { return _threadsRunning; }
102
103 /** Stop any threads being run by viewer.*/
104 virtual void stopThreading();
105
106 /** Start any threads required by the viewer.*/
107 virtual void startThreading();
108
109 enum BarrierPosition
110 {
111 BeforeSwapBuffers,
112 AfterSwapBuffers
113 };
114
115 /** Set the position of the end barrier.
116 * AfterSwapBuffers may result in slightly higher framerates, but may
117 * lead to inconsistent swapping between different windows.
118 * BeforeSwapBuffers may lead to slightly lower framerate, but improve consistency in timing of swap buffers,
119 * especially important if you are likely to consistently break frame.*/
120 void setEndBarrierPosition(BarrierPosition bp);
121
122 /** Get the end barrier position.*/
123 BarrierPosition getEndBarrierPosition() const { return _endBarrierPosition; }
124
125 /** Set the end barrier operation. \c op may be one of GL_FLUSH, GL_FINISH,
126 * or NO_OPERATION. NO_OPERATION is the default. Per BarrierOperation::operator()(),
127 * a glFlush() command, glFinish() command, or no additional OpenGL command will be
128 * issued before entering the end barrier. */
129 void setEndBarrierOperation(osg::BarrierOperation::PreBlockOp op);
130
131 /** Get the end barrier operation. */
132 osg::BarrierOperation::PreBlockOp getEndBarrierOperation() const { return _endBarrierOperation; }
133
134
135 /** Set the done flag to signal the viewer's work is done and should exit the frame loop.*/
136 void setDone(bool done) { _done = done; }
137
138 /** Return true if viewer's work is done and should exit the frame loop.*/
139 bool done() const { return _done; }
140
141 /** Set the EventVisitor. */
142 void setEventVisitor(osgGA::EventVisitor* eventVisitor) { _eventVisitor = eventVisitor; }
143
144 /** Get the EventVisitor. */
145 osgGA::EventVisitor* getEventVisitor() { return _eventVisitor.get(); }
146
147 /** Get the const EventVisitor. */
148 const osgGA::EventVisitor* getEventVisitor() const { return _eventVisitor.get(); }
149
150 /** Set the key event that the viewer checks on each frame to see if the viewer's done flag should be set to
151 * signal end of viewers main loop.
152 * Default value is Escape (osgGA::GUIEVentAdapter::KEY_Escape).
153 * Setting to 0 switches off the feature.*/
154 void setKeyEventSetsDone(int key) { _keyEventSetsDone = key; }
155
156 /** get the key event that the viewer checks on each frame to see if the viewer's done flag.*/
157 int getKeyEventSetsDone() const { return _keyEventSetsDone; }
158
159 /** if the flag is true, the viewer set its done flag when a QUIT_APPLICATION is received, false disables this feature */
160 void setQuitEventSetsDone(bool flag) { _quitEventSetsDone = flag; }
161
162 /** @return true if the viewer respond to the QUIT_APPLICATION-event */
163 bool getQuitEventSetsDone() const { return _quitEventSetsDone; }
164
165
166 /** Hint to tell the renderingTraversals() method whether to call releaseContext() on the last
167 * context that was made current by the thread calling renderingTraverals(). Note, when
168 * running multi-threaded viewer no threads will be made current or release current.
169 * Setting this hint to false can enable the frame loop to be lazy about calling makeCurrent
170 * and releaseContext on each new frame, helping performance. However, if you frame loop
171 * is managing multiple graphics context all from the main frame thread then this hint must
172 * be left on, otherwise the wrong context could be left active, introducing errors in rendering.*/
173 void setReleaseContextAtEndOfFrameHint(bool hint) { _releaseContextAtEndOfFrameHint = hint; }
174
175 /** Hint to tell the renderingTraversals() method whether to call releaseContext().*/
176 bool getReleaseContextAtEndOfFrameHint() const { return _releaseContextAtEndOfFrameHint; }
177
178
179 /** Set the UpdateVisitor. */
180 void setUpdateVisitor(osgUtil::UpdateVisitor* updateVisitor) { _updateVisitor = updateVisitor; }
181
182 /** Get the UpdateVisitor. */
183 osgUtil::UpdateVisitor* getUpdateVisitor() { return _updateVisitor.get(); }
184
185 /** Get the const UpdateVisitor. */
186 const osgUtil::UpdateVisitor* getUpdateVisitor() const { return _updateVisitor.get(); }
187
188
189 /** Set the Update OperationQueue. */
190 void setUpdateOperations(osg::OperationQueue* operations) { _updateOperations = operations; }
191
192 /** Get the Update OperationQueue. */
193 osg::OperationQueue* getUpdateOperations() { return _updateOperations.get(); }
194
195 /** Get the const Update OperationQueue. */
196 const osg::OperationQueue* getUpdateOperations() const { return _updateOperations.get(); }
197
198 /** Add an update operation.*/
199 void addUpdateOperation(osg::Operation* operation);
200
201 /** Remove an update operation.*/
202 void removeUpdateOperation(osg::Operation* operation);
203
204
205 /** Set the graphics operation to call on realization of the viewers graphics windows.*/
206 void setRealizeOperation(osg::Operation* op) { _realizeOperation = op; }
207
208 /** Get the graphics operation to call on realization of the viewers graphics windows.*/
209 osg::Operation* getRealizeOperation() { return _realizeOperation.get(); }
210
211 /** Set the graphics operation to call before the viewers graphics contexts close.*/
212 void setCleanUpOperation(osg::Operation* op) { _cleanUpOperation = op; }
213
214 /** Get the graphics operation to call before the viewers graphics contexts close.*/
215 osg::Operation* getCleanUpOperation() { return _cleanUpOperation.get(); }
216
217 /** Set the incremental compile operation.
218 * Used to manage the OpenGL object compilation and merging of subgraphs in a way that avoids overloading
219 * the rendering of frame with too many new objects in one frame. */
220 void setIncrementalCompileOperation(osgUtil::IncrementalCompileOperation* ico);
221
222 /** Get the incremental compile operation. */
223 osgUtil::IncrementalCompileOperation* getIncrementalCompileOperation() { return _incrementalCompileOperation.get(); }
224
225
226 enum FrameScheme
227 {
228 ON_DEMAND,
229 CONTINUOUS
230 };
231
232 void setRunFrameScheme(FrameScheme fs) { _runFrameScheme = fs; }
233 FrameScheme getRunFrameScheme() const { return _runFrameScheme; }
234
235 void setRunMaxFrameRate(double frameRate) { _runMaxFrameRate = frameRate; }
236 double getRunMaxFrameRate() const { return _runMaxFrameRate; }
237
238 /** Execute a main frame loop.
239 * Equivalent to while (!viewer.done()) viewer.frame();
240 * Also calls realize() if the viewer is not already realized,
241 * and installs trackball manipulator if one is not already assigned.
242 */
243 virtual int run();
244
245 /** check to see if the new frame is required, called by run(..) when FrameScheme is set to ON_DEMAND.*/
246 virtual bool checkNeedToDoFrame() = 0;
247
248 /** check to see if events have been received, return true if events are now available.*/
249 virtual bool checkEvents() = 0;
250
251 /** Render a complete new frame.
252 * Calls advance(), eventTraversal(), updateTraversal(), renderingTraversals(). */
253 virtual void frame(double simulationTime=USE_REFERENCE_TIME);
254
255 virtual void advance(double simulationTime=USE_REFERENCE_TIME) = 0;
256
257 virtual void eventTraversal() = 0;
258
259 virtual void updateTraversal() = 0;
260
261 virtual void renderingTraversals();
262
263 typedef std::vector<osg::Camera*> Cameras;
264 virtual void getCameras(Cameras& cameras, bool onlyActive=true) = 0;
265
266 typedef std::vector<osg::GraphicsContext*> Contexts;
267 virtual void getContexts(Contexts& contexts, bool onlyValid=true) = 0;
268
269 typedef std::vector<osgViewer::GraphicsWindow*> Windows;
270 virtual void getWindows(Windows& windows, bool onlyValid=true);
271
272 typedef std::vector<OpenThreads::Thread*> Threads;
273 virtual void getAllThreads(Threads& threads, bool onlyActive=true) = 0;
274
275 typedef std::vector<osg::OperationThread*> OperationThreads;
276 virtual void getOperationThreads(OperationThreads& threads, bool onlyActive=true) = 0;
277
278 typedef std::vector<osgViewer::Scene*> Scenes;
279 virtual void getScenes(Scenes& scenes, bool onlyValid=true) = 0;
280
281 typedef std::vector<osgViewer::View*> Views;
282 virtual void getViews(Views& views, bool onlyValid=true) = 0;
283
284 /** Check to see if any windows are still open. If not, set viewer done to true. */
285 void checkWindowStatus();
286
287 /** Check to see if windows are still open using the list of contexts given as a parameter.
288 * If no windows are open, stop rendering threads and set viewer done to true.
289 * This function is more effective than checkWindowStatus() as it does not query
290 * the context list and should be used whenever context list is already available in your code.*/
291 void checkWindowStatus(const Contexts& contexts);
292
293 virtual double elapsedTime() = 0;
294
295 virtual osg::FrameStamp* getViewerFrameStamp() = 0;
296
297 /** Get the keyboard and mouse usage of this viewer.*/
298 virtual void getUsage(osg::ApplicationUsage& usage) const = 0;
299
300 bool getRequestRedraw() const { return _requestRedraw; }
301
302 bool getRequestContinousUpdate() const { return _requestContinousUpdate; }
303
304protected:
305
306 void viewerBaseInit();
307
308 friend class osgViewer::View;
309
310 inline void makeCurrent(osg::GraphicsContext* gc)
311 {
312 if (_currentContext==gc) return;
313
314 releaseContext();
315
316 if (gc && gc->valid() && gc->makeCurrent()) _currentContext = gc;
317 }
318
319 inline void releaseContext()
320 {
321 if (_currentContext.valid() && _currentContext->valid())
322 {
323 _currentContext->releaseContext();
324 }
325 _currentContext = 0;
326 }
327
328 virtual void viewerInit() = 0;
329
330 bool _firstFrame;
331 bool _done;
332 int _keyEventSetsDone;
333 bool _quitEventSetsDone;
334 bool _releaseContextAtEndOfFrameHint;
335
336 bool _useConfigureAffinity;
337 OpenThreads::Affinity _affinity;
338 ThreadingModel _threadingModel;
339 bool _threadsRunning;
340
341 bool _requestRedraw;
342 bool _requestContinousUpdate;
343
344 FrameScheme _runFrameScheme;
345 double _runMaxFrameRate;
346
347
348 BarrierPosition _endBarrierPosition;
349 osg::BarrierOperation::PreBlockOp _endBarrierOperation;
350
351 osg::ref_ptr<osg::BarrierOperation> _startRenderingBarrier;
352 osg::ref_ptr<osg::BarrierOperation> _endRenderingDispatchBarrier;
353 osg::ref_ptr<osg::EndOfDynamicDrawBlock> _endDynamicDrawBlock;
354
355 osg::ref_ptr<osgGA::EventVisitor> _eventVisitor;
356
357 osg::ref_ptr<osg::OperationQueue> _updateOperations;
358 osg::ref_ptr<osgUtil::UpdateVisitor> _updateVisitor;
359
360 osg::ref_ptr<osg::Operation> _realizeOperation;
361 osg::ref_ptr<osg::Operation> _cleanUpOperation;
362 osg::ref_ptr<osgUtil::IncrementalCompileOperation> _incrementalCompileOperation;
363
364 osg::observer_ptr<osg::GraphicsContext> _currentContext;
365
366 private:
367
368 // Define private copy constructor
369 // otherwsie VS2015 will construct it's own which will call the private copy operator from osg::Object resulting in an compile error.
370 ViewerBase& operator = (const ViewerBase&) { return *this; }
371};
372
373}
374
375#endif