2007-09-30 19:37:00 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSGVIEWER_VIEWERBASE
|
|
|
|
#define OSGVIEWER_VIEWERBASE 1
|
|
|
|
|
|
|
|
#include <osg/Stats>
|
|
|
|
|
|
|
|
#include <osgUtil/UpdateVisitor>
|
|
|
|
|
|
|
|
#include <osgGA/MatrixManipulator>
|
|
|
|
#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:
|
|
|
|
|
|
|
|
ViewerBase();
|
|
|
|
ViewerBase(const ViewerBase& vb);
|
|
|
|
|
|
|
|
|
|
|
|
/** Set the Stats object used for collect various frame related timing and scene graph stats.*/
|
|
|
|
void setStats(osg::Stats* stats) { _stats = stats; }
|
|
|
|
|
|
|
|
/** Get the Viewers Stats object.*/
|
|
|
|
osg::Stats* getStats() { return _stats.get(); }
|
|
|
|
|
|
|
|
/** Get the Viewers Stats object.*/
|
|
|
|
const osg::Stats* getStats() const { return _stats.get(); }
|
|
|
|
|
|
|
|
|
|
|
|
/** read the viewer configuration from a configuration file.*/
|
|
|
|
virtual bool readConfiguration(const std::string& filename) = 0;
|
|
|
|
|
|
|
|
/** 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;
|
|
|
|
|
|
|
|
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);
|
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.*/
|
|
|
|
virtual void setUpThreading();
|
|
|
|
|
|
|
|
/** Return true if viewer threads are running. */
|
|
|
|
bool areThreadsRunning() const { return _threadsRunning; }
|
|
|
|
|
|
|
|
/** Stop any threads begin run by viewer.*/
|
|
|
|
virtual void stopThreading();
|
|
|
|
|
|
|
|
/** Start any threads required by the viewer.*/
|
|
|
|
virtual void startThreading();
|
|
|
|
|
|
|
|
enum BarrierPosition
|
|
|
|
{
|
|
|
|
BeforeSwapBuffers,
|
|
|
|
AfterSwapBuffers
|
|
|
|
};
|
2007-09-30 19:37:00 +08:00
|
|
|
|
2007-10-03 05:23:58 +08:00
|
|
|
/** Set the position of the end barrier.
|
|
|
|
* AfterSwapBuffers will may result is slightly higher framerates, by 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; }
|
|
|
|
|
|
|
|
|
2007-09-30 19:37:00 +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; }
|
|
|
|
|
|
|
|
/** 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; }
|
|
|
|
|
|
|
|
/** @return true if the viewer respond to the QUIT_APPLICATION-event */
|
|
|
|
bool getQuitEventSetsDone() const { return _quitEventSetsDone; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 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(); }
|
|
|
|
|
|
|
|
/** Get the const Update OperationQueue. */
|
|
|
|
const osg::OperationQueue* getUpdateOperations() const { return _updateOperations.get(); }
|
|
|
|
|
|
|
|
/** 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(); }
|
|
|
|
|
|
|
|
|
|
|
|
/** Check to see if windows are still open, if not set viewer done to true. */
|
|
|
|
void checkWindowStatus();
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
virtual int run() = 0;
|
|
|
|
|
|
|
|
/** 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;
|
|
|
|
|
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;
|
|
|
|
virtual void getWindows(Windows& windows, bool onlyValid=true) = 0;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2007-10-03 05:23:58 +08:00
|
|
|
inline void makeCurrent(osg::GraphicsContext* gc)
|
|
|
|
{
|
|
|
|
if (_currentContext==gc) return;
|
|
|
|
|
|
|
|
releaseContext();
|
|
|
|
|
|
|
|
if (gc && gc->valid() && gc->makeCurrent()) _currentContext = gc;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void releaseContext()
|
|
|
|
{
|
|
|
|
if (_currentContext.valid() && _currentContext->valid())
|
|
|
|
{
|
|
|
|
_currentContext->releaseContext();
|
|
|
|
}
|
|
|
|
_currentContext = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void viewerInit() = 0;
|
|
|
|
|
|
|
|
|
2007-09-30 19:37:00 +08:00
|
|
|
osg::ref_ptr<osg::Stats> _stats;
|
|
|
|
|
2007-10-03 05:23:58 +08:00
|
|
|
bool _firstFrame;
|
2007-09-30 19:37:00 +08:00
|
|
|
bool _done;
|
|
|
|
int _keyEventSetsDone;
|
|
|
|
bool _quitEventSetsDone;
|
|
|
|
|
|
|
|
ThreadingModel _threadingModel;
|
|
|
|
bool _threadsRunning;
|
2007-10-03 05:23:58 +08:00
|
|
|
|
|
|
|
BarrierPosition _endBarrierPosition;
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::BarrierOperation> _startRenderingBarrier;
|
|
|
|
osg::ref_ptr<osg::BarrierOperation> _endRenderingDispatchBarrier;
|
|
|
|
osg::ref_ptr<osg::EndOfDynamicDrawBlock> _endDynamicDrawBlock;
|
2007-09-30 19:37:00 +08:00
|
|
|
|
|
|
|
osg::ref_ptr<osgGA::EventVisitor> _eventVisitor;
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::OperationQueue> _updateOperations;
|
|
|
|
osg::ref_ptr<osgUtil::UpdateVisitor> _updateVisitor;
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Operation> _realizeOperation;
|
|
|
|
|
2007-10-03 05:23:58 +08:00
|
|
|
osg::observer_ptr<osg::GraphicsContext> _currentContext;
|
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
|