1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
14#ifndef OSGVIEWER_VIEWERBASE
15#define OSGVIEWER_VIEWERBASE 1
19#include <osgUtil/UpdateVisitor>
20#include <osgUtil/IncrementalCompileOperation>
22#include <osgGA/EventVisitor>
23#include <osgGA/EventQueue>
25#include <osgViewer/Scene>
26#include <osgViewer/GraphicsWindow>
30#define USE_REFERENCE_TIME DBL_MAX
34/** ViewerBase is the view base class that is inherited by both Viewer and CompositeViewer.*/
35class OSGVIEWER_EXPORT ViewerBase : public virtual osg::Object
40 ViewerBase(const ViewerBase& vb);
43 /** Set the Stats object used to collect various frame related timing and scene graph stats.*/
44 virtual void setViewerStats(osg::Stats* stats) = 0;
46 /** Get the Viewers Stats object.*/
47 virtual osg::Stats* getViewerStats() = 0;
49 /** Get the Viewers Stats object.*/
50 virtual const osg::Stats* getViewerStats() const = 0;
53 /** read the viewer configuration from a configuration file.*/
54 virtual bool readConfiguration(const std::string& filename) = 0;
56 /** Get whether at least of one of this viewers windows are realized.*/
57 virtual bool isRealized() const = 0;
59 /** set up windows and associated threads.*/
60 virtual void realize() = 0;
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; }
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; }
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();
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; }
80 CullDrawThreadPerContext,
81 ThreadPerContext = CullDrawThreadPerContext,
83 CullThreadPerCameraDrawThreadPerContext,
84 ThreadPerCamera = CullThreadPerCameraDrawThreadPerContext,
88 /** Set the threading model the rendering traversals will use.*/
89 virtual void setThreadingModel(ThreadingModel threadingModel);
91 /** Get the threading model the rendering traversals will use.*/
92 ThreadingModel getThreadingModel() const { return _threadingModel; }
94 /** Let the viewer suggest the best threading model for the viewers camera/window setup and the hardware available.*/
95 virtual ThreadingModel suggestBestThreadingModel();
97 /** Set up the threading and processor affinity as per the viewers threading model.*/
98 virtual void setUpThreading();
100 /** Return true if viewer threads are running. */
101 bool areThreadsRunning() const { return _threadsRunning; }
103 /** Stop any threads being run by viewer.*/
104 virtual void stopThreading();
106 /** Start any threads required by the viewer.*/
107 virtual void startThreading();
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);
122 /** Get the end barrier position.*/
123 BarrierPosition getEndBarrierPosition() const { return _endBarrierPosition; }
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);
131 /** Get the end barrier operation. */
132 osg::BarrierOperation::PreBlockOp getEndBarrierOperation() const { return _endBarrierOperation; }
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; }
138 /** Return true if viewer's work is done and should exit the frame loop.*/
139 bool done() const { return _done; }
141 /** Set the EventVisitor. */
142 void setEventVisitor(osgGA::EventVisitor* eventVisitor) { _eventVisitor = eventVisitor; }
144 /** Get the EventVisitor. */
145 osgGA::EventVisitor* getEventVisitor() { return _eventVisitor.get(); }
147 /** Get the const EventVisitor. */
148 const osgGA::EventVisitor* getEventVisitor() const { return _eventVisitor.get(); }
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; }
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; }
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; }
162 /** @return true if the viewer respond to the QUIT_APPLICATION-event */
163 bool getQuitEventSetsDone() const { return _quitEventSetsDone; }
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; }
175 /** Hint to tell the renderingTraversals() method whether to call releaseContext().*/
176 bool getReleaseContextAtEndOfFrameHint() const { return _releaseContextAtEndOfFrameHint; }
179 /** Set the UpdateVisitor. */
180 void setUpdateVisitor(osgUtil::UpdateVisitor* updateVisitor) { _updateVisitor = updateVisitor; }
182 /** Get the UpdateVisitor. */
183 osgUtil::UpdateVisitor* getUpdateVisitor() { return _updateVisitor.get(); }
185 /** Get the const UpdateVisitor. */
186 const osgUtil::UpdateVisitor* getUpdateVisitor() const { return _updateVisitor.get(); }
189 /** Set the Update OperationQueue. */
190 void setUpdateOperations(osg::OperationQueue* operations) { _updateOperations = operations; }
192 /** Get the Update OperationQueue. */
193 osg::OperationQueue* getUpdateOperations() { return _updateOperations.get(); }
195 /** Get the const Update OperationQueue. */
196 const osg::OperationQueue* getUpdateOperations() const { return _updateOperations.get(); }
198 /** Add an update operation.*/
199 void addUpdateOperation(osg::Operation* operation);
201 /** Remove an update operation.*/
202 void removeUpdateOperation(osg::Operation* operation);
205 /** Set the graphics operation to call on realization of the viewers graphics windows.*/
206 void setRealizeOperation(osg::Operation* op) { _realizeOperation = op; }
208 /** Get the graphics operation to call on realization of the viewers graphics windows.*/
209 osg::Operation* getRealizeOperation() { return _realizeOperation.get(); }
211 /** Set the graphics operation to call before the viewers graphics contexts close.*/
212 void setCleanUpOperation(osg::Operation* op) { _cleanUpOperation = op; }
214 /** Get the graphics operation to call before the viewers graphics contexts close.*/
215 osg::Operation* getCleanUpOperation() { return _cleanUpOperation.get(); }
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);
222 /** Get the incremental compile operation. */
223 osgUtil::IncrementalCompileOperation* getIncrementalCompileOperation() { return _incrementalCompileOperation.get(); }
232 void setRunFrameScheme(FrameScheme fs) { _runFrameScheme = fs; }
233 FrameScheme getRunFrameScheme() const { return _runFrameScheme; }
235 void setRunMaxFrameRate(double frameRate) { _runMaxFrameRate = frameRate; }
236 double getRunMaxFrameRate() const { return _runMaxFrameRate; }
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.
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;
248 /** check to see if events have been received, return true if events are now available.*/
249 virtual bool checkEvents() = 0;
251 /** Render a complete new frame.
252 * Calls advance(), eventTraversal(), updateTraversal(), renderingTraversals(). */
253 virtual void frame(double simulationTime=USE_REFERENCE_TIME);
255 virtual void advance(double simulationTime=USE_REFERENCE_TIME) = 0;
257 virtual void eventTraversal() = 0;
259 virtual void updateTraversal() = 0;
261 virtual void renderingTraversals();
263 typedef std::vector<osg::Camera*> Cameras;
264 virtual void getCameras(Cameras& cameras, bool onlyActive=true) = 0;
266 typedef std::vector<osg::GraphicsContext*> Contexts;
267 virtual void getContexts(Contexts& contexts, bool onlyValid=true) = 0;
269 typedef std::vector<osgViewer::GraphicsWindow*> Windows;
270 virtual void getWindows(Windows& windows, bool onlyValid=true);
272 typedef std::vector<OpenThreads::Thread*> Threads;
273 virtual void getAllThreads(Threads& threads, bool onlyActive=true) = 0;
275 typedef std::vector<osg::OperationThread*> OperationThreads;
276 virtual void getOperationThreads(OperationThreads& threads, bool onlyActive=true) = 0;
278 typedef std::vector<osgViewer::Scene*> Scenes;
279 virtual void getScenes(Scenes& scenes, bool onlyValid=true) = 0;
281 typedef std::vector<osgViewer::View*> Views;
282 virtual void getViews(Views& views, bool onlyValid=true) = 0;
284 /** Check to see if any windows are still open. If not, set viewer done to true. */
285 void checkWindowStatus();
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);
293 virtual double elapsedTime() = 0;
295 virtual osg::FrameStamp* getViewerFrameStamp() = 0;
297 /** Get the keyboard and mouse usage of this viewer.*/
298 virtual void getUsage(osg::ApplicationUsage& usage) const = 0;
300 bool getRequestRedraw() const { return _requestRedraw; }
302 bool getRequestContinousUpdate() const { return _requestContinousUpdate; }
306 void viewerBaseInit();
308 friend class osgViewer::View;
310 inline void makeCurrent(osg::GraphicsContext* gc)
312 if (_currentContext==gc) return;
316 if (gc && gc->valid() && gc->makeCurrent()) _currentContext = gc;
319 inline void releaseContext()
321 if (_currentContext.valid() && _currentContext->valid())
323 _currentContext->releaseContext();
328 virtual void viewerInit() = 0;
332 int _keyEventSetsDone;
333 bool _quitEventSetsDone;
334 bool _releaseContextAtEndOfFrameHint;
336 bool _useConfigureAffinity;
337 OpenThreads::Affinity _affinity;
338 ThreadingModel _threadingModel;
339 bool _threadsRunning;
342 bool _requestContinousUpdate;
344 FrameScheme _runFrameScheme;
345 double _runMaxFrameRate;
348 BarrierPosition _endBarrierPosition;
349 osg::BarrierOperation::PreBlockOp _endBarrierOperation;
351 osg::ref_ptr<osg::BarrierOperation> _startRenderingBarrier;
352 osg::ref_ptr<osg::BarrierOperation> _endRenderingDispatchBarrier;
353 osg::ref_ptr<osg::EndOfDynamicDrawBlock> _endDynamicDrawBlock;
355 osg::ref_ptr<osgGA::EventVisitor> _eventVisitor;
357 osg::ref_ptr<osg::OperationQueue> _updateOperations;
358 osg::ref_ptr<osgUtil::UpdateVisitor> _updateVisitor;
360 osg::ref_ptr<osg::Operation> _realizeOperation;
361 osg::ref_ptr<osg::Operation> _cleanUpOperation;
362 osg::ref_ptr<osgUtil::IncrementalCompileOperation> _incrementalCompileOperation;
364 osg::observer_ptr<osg::GraphicsContext> _currentContext;
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; }