2011-04-19 19:46:05 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2007-09-30 19:37:00 +08:00
*
2011-04-19 19:46:05 +08:00
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
2007-09-30 19:37:00 +08:00
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
2011-04-19 19:46:05 +08:00
*
2007-09-30 19:37:00 +08:00
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2011-04-19 19:46:05 +08:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2007-09-30 19:37:00 +08:00
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGVIEWER_VIEWERBASE
#define OSGVIEWER_VIEWERBASE 1
#include <osg/Stats>
#include <osgUtil/UpdateVisitor>
2009-03-12 23:21:04 +08:00
#include <osgUtil/IncrementalCompileOperation>
2007-09-30 19:37:00 +08:00
#include <osgGA/EventVisitor>
#include <osgGA/EventQueue>
#include <osgViewer/Scene>
#include <osgViewer/GraphicsWindow>
namespace osgViewer {
#define USE_REFERENCE_TIME DBL_MAX
class View;
2007-12-11 01:30:18 +08:00
/** ViewerBase is the view base class that is inherited by both Viewer and CompositeViewer.*/
2007-09-30 19:37:00 +08:00
class OSGVIEWER_EXPORT ViewerBase : public virtual osg::Object
{
public:
2011-04-19 19:46:05 +08:00
2007-09-30 19:37:00 +08:00
ViewerBase();
ViewerBase(const ViewerBase& vb);
2011-04-19 19:46:05 +08:00
2015-04-08 02:01:12 +08:00
/** Set the Stats object used to collect various frame related timing and scene graph stats.*/
2009-01-27 21:23:20 +08:00
virtual void setViewerStats(osg::Stats* stats) = 0;
2007-09-30 19:37:00 +08:00
/** Get the Viewers Stats object.*/
2009-01-27 21:23:20 +08:00
virtual osg::Stats* getViewerStats() = 0;
2007-09-30 19:37:00 +08:00
/** Get the Viewers Stats object.*/
2009-01-27 21:23:20 +08:00
virtual const osg::Stats* getViewerStats() const = 0;
2007-09-30 19:37:00 +08:00
2011-04-19 19:46:05 +08:00
2007-09-30 19:37:00 +08:00
/** read the viewer configuration from a configuration file.*/
virtual bool readConfiguration(const std::string& filename) = 0;
2011-04-19 19:46:05 +08:00
2007-09-30 19:37:00 +08:00
/** Get whether at least of one of this viewers windows are realized.*/
virtual bool isRealized() const = 0;
/** set up windows and associated threads.*/
virtual void realize() = 0;
2016-09-29 01:44:58 +08:00
2016-09-29 03:30:12 +08:00
/** Set whether the setUpThreading() method should call configureAffinity() to set up up the processor affinity of viewer threads.*/
void setUseConfigureAffinity(bool flag) { _useConfigureAffinity = flag; }
/** get whether the setUpThreading() method should call configureAffinity() to set up up the processor affinity of viewer threads.*/
bool getUseConfigureAffinity() const { return _useConfigureAffinity; }
2016-09-29 01:44:58 +08:00
/** analyse the viewer configuration and select an appropriate Affinity for main thread, and any graphics, camera threads and database pagers that are required.*/
virtual void configureAffinity();
/** Set the processor affinity of main thread.*/
2016-10-11 00:02:38 +08:00
virtual void setProcessorAffinity(const OpenThreads::Affinity& affinity) { _affinity = affinity; }
OpenThreads::Affinity& getProcessorAffinity() { return _affinity; }
const OpenThreads::Affinity& getProcessorAffinity() const { return _affinity; }
2016-09-29 01:44:58 +08:00
2007-09-30 19:37:00 +08:00
enum ThreadingModel
{
SingleThreaded,
CullDrawThreadPerContext,
ThreadPerContext = CullDrawThreadPerContext,
DrawThreadPerContext,
CullThreadPerCameraDrawThreadPerContext,
ThreadPerCamera = CullThreadPerCameraDrawThreadPerContext,
AutomaticSelection
};
/** Set the threading model the rendering traversals will use.*/
2007-10-03 05:23:58 +08:00
virtual void setThreadingModel(ThreadingModel threadingModel);
2011-04-19 19:46:05 +08:00
2007-09-30 19:37:00 +08:00
/** Get the threading model the rendering traversals will use.*/
ThreadingModel getThreadingModel() const { return _threadingModel; }
2007-10-03 05:23:58 +08:00
/** Let the viewer suggest the best threading model for the viewers camera/window setup and the hardware available.*/
virtual ThreadingModel suggestBestThreadingModel();
/** Set up the threading and processor affinity as per the viewers threading model.*/
2011-04-19 19:46:05 +08:00
virtual void setUpThreading();
2007-10-03 05:23:58 +08:00
/** Return true if viewer threads are running. */
bool areThreadsRunning() const { return _threadsRunning; }
2015-04-08 02:01:12 +08:00
/** Stop any threads being run by viewer.*/
2007-10-03 05:23:58 +08:00
virtual void stopThreading();
/** Start any threads required by the viewer.*/
2011-04-19 19:46:05 +08:00
virtual void startThreading();
2007-10-03 05:23:58 +08:00
enum BarrierPosition
{
BeforeSwapBuffers,
AfterSwapBuffers
};
2011-04-19 19:46:05 +08:00
2007-10-03 05:23:58 +08:00
/** Set the position of the end barrier.
2008-07-17 21:51:14 +08:00
* AfterSwapBuffers may result in slightly higher framerates, but may
2007-12-11 01:30:18 +08:00
* lead to inconsistent swapping between different windows.
2007-10-03 05:23:58 +08:00
* BeforeSwapBuffers may lead to slightly lower framerate, but improve consistency in timing of swap buffers,
* especially important if you are likely to consistently break frame.*/
void setEndBarrierPosition(BarrierPosition bp);
/** Get the end barrier position.*/
BarrierPosition getEndBarrierPosition() const { return _endBarrierPosition; }
2012-03-22 01:36:20 +08:00
2012-01-25 01:21:14 +08:00
/** Set the end barrier operation. \c op may be one of GL_FLUSH, GL_FINISH,
* or NO_OPERATION. NO_OPERATION is the default. Per BarrierOperation::operator()(),
* a glFlush() command, glFinish() command, or no additional OpenGL command will be
* issued before entering the end barrier. */
2012-01-25 01:30:44 +08:00
void setEndBarrierOperation(osg::BarrierOperation::PreBlockOp op);
2012-03-22 01:36:20 +08:00
2012-01-25 01:21:14 +08:00
/** Get the end barrier operation. */
osg::BarrierOperation::PreBlockOp getEndBarrierOperation() const { return _endBarrierOperation; }
2007-10-03 05:23:58 +08:00
2011-04-19 19:46:05 +08:00
2007-12-11 01:30:18 +08:00
/** Set the done flag to signal the viewer's work is done and should exit the frame loop.*/
2007-09-30 19:37:00 +08:00
void setDone(bool done) { _done = done; }
2007-12-11 01:30:18 +08:00
/** Return true if viewer's work is done and should exit the frame loop.*/
2007-09-30 19:37:00 +08:00
bool done() const { return _done; }
/** Set the EventVisitor. */
void setEventVisitor(osgGA::EventVisitor* eventVisitor) { _eventVisitor = eventVisitor; }
/** Get the EventVisitor. */
osgGA::EventVisitor* getEventVisitor() { return _eventVisitor.get(); }
/** Get the const EventVisitor. */
const osgGA::EventVisitor* getEventVisitor() const { return _eventVisitor.get(); }
/** Set the key event that the viewer checks on each frame to see if the viewer's done flag should be set to
* signal end of viewers main loop.
* Default value is Escape (osgGA::GUIEVentAdapter::KEY_Escape).
* Setting to 0 switches off the feature.*/
void setKeyEventSetsDone(int key) { _keyEventSetsDone = key; }
/** get the key event that the viewer checks on each frame to see if the viewer's done flag.*/
int getKeyEventSetsDone() const { return _keyEventSetsDone; }
2011-04-19 19:46:05 +08:00
2007-09-30 19:37:00 +08:00
/** if the flag is true, the viewer set its done flag when a QUIT_APPLICATION is received, false disables this feature */
void setQuitEventSetsDone(bool flag) { _quitEventSetsDone = flag; }
2011-04-19 19:46:05 +08:00
2007-09-30 19:37:00 +08:00
/** @return true if the viewer respond to the QUIT_APPLICATION-event */
bool getQuitEventSetsDone() const { return _quitEventSetsDone; }
2015-04-08 02:01:12 +08:00
/** Hint to tell the renderingTraversals() method whether to call releaseContext() on the last
2011-04-19 19:46:05 +08:00
* context that was made current by the thread calling renderingTraverals(). Note, when
2008-06-05 00:46:14 +08:00
* running multi-threaded viewer no threads will be made current or release current.
* Setting this hint to false can enable the frame loop to be lazy about calling makeCurrent
* and releaseContext on each new frame, helping performance. However, if you frame loop
* is managing multiple graphics context all from the main frame thread then this hint must
* be left on, otherwise the wrong context could be left active, introducing errors in rendering.*/
void setReleaseContextAtEndOfFrameHint(bool hint) { _releaseContextAtEndOfFrameHint = hint; }
2011-04-19 19:46:05 +08:00
2015-04-08 02:01:12 +08:00
/** Hint to tell the renderingTraversals() method whether to call releaseContext().*/
2008-06-05 00:46:14 +08:00
bool getReleaseContextAtEndOfFrameHint() const { return _releaseContextAtEndOfFrameHint; }
2007-09-30 19:37:00 +08:00
/** Set the UpdateVisitor. */
void setUpdateVisitor(osgUtil::UpdateVisitor* updateVisitor) { _updateVisitor = updateVisitor; }
/** Get the UpdateVisitor. */
osgUtil::UpdateVisitor* getUpdateVisitor() { return _updateVisitor.get(); }
/** Get the const UpdateVisitor. */
const osgUtil::UpdateVisitor* getUpdateVisitor() const { return _updateVisitor.get(); }
/** Set the Update OperationQueue. */
void setUpdateOperations(osg::OperationQueue* operations) { _updateOperations = operations; }
/** Get the Update OperationQueue. */
osg::OperationQueue* getUpdateOperations() { return _updateOperations.get(); }
2011-04-19 19:46:05 +08:00
2007-09-30 19:37:00 +08:00
/** Get the const Update OperationQueue. */
const osg::OperationQueue* getUpdateOperations() const { return _updateOperations.get(); }
2011-04-19 19:46:05 +08:00
2007-09-30 19:37:00 +08:00
/** Add an update operation.*/
void addUpdateOperation(osg::Operation* operation);
/** Remove an update operation.*/
void removeUpdateOperation(osg::Operation* operation);
2007-10-03 05:23:58 +08:00
/** Set the graphics operation to call on realization of the viewers graphics windows.*/
void setRealizeOperation(osg::Operation* op) { _realizeOperation = op; }
/** Get the graphics operation to call on realization of the viewers graphics windows.*/
osg::Operation* getRealizeOperation() { return _realizeOperation.get(); }
2011-04-19 19:46:05 +08:00
2016-07-01 04:09:22 +08:00
/** Set the graphics operation to call before the viewers graphics contexts close.*/
void setCleanUpOperation(osg::Operation* op) { _cleanUpOperation = op; }
/** Get the graphics operation to call before the viewers graphics contexts close.*/
osg::Operation* getCleanUpOperation() { return _cleanUpOperation.get(); }
2011-04-19 19:46:05 +08:00
2009-03-08 20:00:36 +08:00
/** Set the incremental compile operation.
* Used to manage the OpenGL object compilation and merging of subgraphs in a way that avoids overloading
* the rendering of frame with too many new objects in one frame. */
void setIncrementalCompileOperation(osgUtil::IncrementalCompileOperation* ico);
/** Get the incremental compile operation. */
osgUtil::IncrementalCompileOperation* getIncrementalCompileOperation() { return _incrementalCompileOperation.get(); }
2007-10-03 05:23:58 +08:00
2009-04-25 00:20:50 +08:00
enum FrameScheme
{
ON_DEMAND,
CONTINUOUS
};
2007-10-03 05:23:58 +08:00
2009-04-25 00:20:50 +08:00
void setRunFrameScheme(FrameScheme fs) { _runFrameScheme = fs; }
FrameScheme getRunFrameScheme() const { return _runFrameScheme; }
void setRunMaxFrameRate(double frameRate) { _runMaxFrameRate = frameRate; }
double getRunMaxFrameRate() const { return _runMaxFrameRate; }
2007-10-03 05:23:58 +08:00
2007-09-30 19:37:00 +08:00
/** Execute a main frame loop.
2007-12-11 01:30:18 +08:00
* Equivalent to while (!viewer.done()) viewer.frame();
2007-09-30 19:37:00 +08:00
* Also calls realize() if the viewer is not already realized,
* and installs trackball manipulator if one is not already assigned.
*/
2009-04-25 00:20:50 +08:00
virtual int run();
/** check to see if the new frame is required, called by run(..) when FrameScheme is set to ON_DEMAND.*/
virtual bool checkNeedToDoFrame() = 0;
2016-03-11 00:35:08 +08:00
2013-05-24 17:35:58 +08:00
/** check to see if events have been received, return true if events are now available.*/
virtual bool checkEvents() = 0;
2007-09-30 19:37:00 +08:00
/** Render a complete new frame.
* Calls advance(), eventTraversal(), updateTraversal(), renderingTraversals(). */
2007-10-03 05:23:58 +08:00
virtual void frame(double simulationTime=USE_REFERENCE_TIME);
2007-09-30 19:37:00 +08:00
virtual void advance(double simulationTime=USE_REFERENCE_TIME) = 0;
virtual void eventTraversal() = 0;
virtual void updateTraversal() = 0;
2011-04-19 19:46:05 +08:00
2007-10-03 05:23:58 +08:00
virtual void renderingTraversals();
2007-09-30 19:37:00 +08:00
typedef std::vector<osg::Camera*> Cameras;
virtual void getCameras(Cameras& cameras, bool onlyActive=true) = 0;
typedef std::vector<osg::GraphicsContext*> Contexts;
virtual void getContexts(Contexts& contexts, bool onlyValid=true) = 0;
typedef std::vector<osgViewer::GraphicsWindow*> Windows;
2008-09-18 23:18:59 +08:00
virtual void getWindows(Windows& windows, bool onlyValid=true);
2007-09-30 19:37:00 +08:00
typedef std::vector<OpenThreads::Thread*> Threads;
virtual void getAllThreads(Threads& threads, bool onlyActive=true) = 0;
typedef std::vector<osg::OperationThread*> OperationThreads;
virtual void getOperationThreads(OperationThreads& threads, bool onlyActive=true) = 0;
typedef std::vector<osgViewer::Scene*> Scenes;
virtual void getScenes(Scenes& scenes, bool onlyValid=true) = 0;
typedef std::vector<osgViewer::View*> Views;
virtual void getViews(Views& views, bool onlyValid=true) = 0;
2011-04-19 20:01:38 +08:00
/** Check to see if any windows are still open. If not, set viewer done to true. */
void checkWindowStatus();
/** Check to see if windows are still open using the list of contexts given as a parameter.
* If no windows are open, stop rendering threads and set viewer done to true.
* This function is more effective than checkWindowStatus() as it does not query
* the context list and should be used whenever context list is already available in your code.*/
void checkWindowStatus(const Contexts& contexts);
2007-10-03 05:23:58 +08:00
virtual double elapsedTime() = 0;
2007-09-30 19:37:00 +08:00
2007-10-03 05:23:58 +08:00
virtual osg::FrameStamp* getViewerFrameStamp() = 0;
2007-09-30 19:37:00 +08:00
/** Get the keyboard and mouse usage of this viewer.*/
virtual void getUsage(osg::ApplicationUsage& usage) const = 0;
2011-04-19 19:46:05 +08:00
2016-03-11 00:35:08 +08:00
bool getRequestRedraw() const { return _requestRedraw; }
2016-09-29 01:44:58 +08:00
2016-03-11 00:35:08 +08:00
bool getRequestContinousUpdate() const { return _requestContinousUpdate; }
protected:
2009-04-25 00:20:50 +08:00
void viewerBaseInit();
friend class osgViewer::View;
2007-10-03 05:23:58 +08:00
inline void makeCurrent(osg::GraphicsContext* gc)
{
if (_currentContext==gc) return;
2009-04-25 00:20:50 +08:00
2007-10-03 05:23:58 +08:00
releaseContext();
2009-04-25 00:20:50 +08:00
2007-10-03 05:23:58 +08:00
if (gc && gc->valid() && gc->makeCurrent()) _currentContext = gc;
}
2009-04-25 00:20:50 +08:00
2007-10-03 05:23:58 +08:00
inline void releaseContext()
{
2008-11-20 04:34:53 +08:00
if (_currentContext.valid() && _currentContext->valid())
2007-10-03 05:23:58 +08:00
{
_currentContext->releaseContext();
}
_currentContext = 0;
}
virtual void viewerInit() = 0;
2007-09-30 19:37:00 +08:00
2009-03-08 20:00:36 +08:00
bool _firstFrame;
bool _done;
int _keyEventSetsDone;
bool _quitEventSetsDone;
bool _releaseContextAtEndOfFrameHint;
2009-04-25 00:20:50 +08:00
2016-09-29 03:30:12 +08:00
bool _useConfigureAffinity;
2016-09-29 01:44:58 +08:00
OpenThreads::Affinity _affinity;
2009-03-08 20:00:36 +08:00
ThreadingModel _threadingModel;
bool _threadsRunning;
2007-10-03 05:23:58 +08:00
2009-04-25 00:20:50 +08:00
bool _requestRedraw;
bool _requestContinousUpdate;
FrameScheme _runFrameScheme;
double _runMaxFrameRate;
2009-03-08 20:00:36 +08:00
BarrierPosition _endBarrierPosition;
2012-01-25 01:21:14 +08:00
osg::BarrierOperation::PreBlockOp _endBarrierOperation;
2007-10-03 05:23:58 +08:00
2009-03-08 20:00:36 +08:00
osg::ref_ptr<osg::BarrierOperation> _startRenderingBarrier;
osg::ref_ptr<osg::BarrierOperation> _endRenderingDispatchBarrier;
osg::ref_ptr<osg::EndOfDynamicDrawBlock> _endDynamicDrawBlock;
2009-04-25 00:20:50 +08:00
2009-03-08 20:00:36 +08:00
osg::ref_ptr<osgGA::EventVisitor> _eventVisitor;
2009-04-25 00:20:50 +08:00
2009-03-08 20:00:36 +08:00
osg::ref_ptr<osg::OperationQueue> _updateOperations;
osg::ref_ptr<osgUtil::UpdateVisitor> _updateVisitor;
2009-04-25 00:20:50 +08:00
2009-03-08 20:00:36 +08:00
osg::ref_ptr<osg::Operation> _realizeOperation;
2016-07-01 04:09:22 +08:00
osg::ref_ptr<osg::Operation> _cleanUpOperation;
2009-03-08 20:00:36 +08:00
osg::ref_ptr<osgUtil::IncrementalCompileOperation> _incrementalCompileOperation;
2007-09-30 19:37:00 +08:00
2009-03-08 20:00:36 +08:00
osg::observer_ptr<osg::GraphicsContext> _currentContext;
2016-03-11 00:35:08 +08:00
2015-07-23 22:37:17 +08:00
private:
2016-03-11 00:35:08 +08:00
2015-07-23 22:37:17 +08:00
// Define private copy constructor
// otherwsie VS2015 will construct it's own which will call the private copy operator from osg::Object resulting in an compile error.
ViewerBase& operator = (const ViewerBase&) { return *this; }
2007-09-30 19:37:00 +08:00
};
2007-10-03 05:23:58 +08:00
2007-09-30 19:37:00 +08:00
}
#endif