2006-11-27 22:52:07 +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_Viewer
|
|
|
|
#define OSGVIEWER_Viewer 1
|
|
|
|
|
|
|
|
#include <osgViewer/GraphicsWindow>
|
|
|
|
#include <osgViewer/View>
|
2007-01-10 18:09:05 +08:00
|
|
|
#include <osgGA/EventVisitor>
|
2006-11-27 22:52:07 +08:00
|
|
|
|
2006-11-29 04:35:31 +08:00
|
|
|
namespace osgViewer {
|
2006-11-27 22:52:07 +08:00
|
|
|
|
|
|
|
/** Viewer holds a single view on to a single scene..*/
|
|
|
|
class OSGVIEWER_EXPORT Viewer : public osgViewer::View
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
Viewer();
|
|
|
|
virtual ~Viewer();
|
2006-12-20 00:00:51 +08:00
|
|
|
|
2007-01-05 18:59:23 +08:00
|
|
|
/** Get whether at least of one of this viewers windows are realized.*/
|
|
|
|
bool isRealized() const;
|
|
|
|
|
2006-12-20 00:00:51 +08:00
|
|
|
/** set up windows and associated threads.*/
|
|
|
|
void realize();
|
2006-11-27 22:52:07 +08:00
|
|
|
|
2006-12-22 00:56:20 +08:00
|
|
|
void setDone(bool done) { _done = done; }
|
|
|
|
|
2007-01-06 05:19:01 +08:00
|
|
|
bool done() const { return _done; }
|
2007-01-03 01:39:31 +08:00
|
|
|
|
2007-01-16 16:56:33 +08:00
|
|
|
void setStartTick(osg::Timer_t tick);
|
2007-01-15 22:46:16 +08:00
|
|
|
osg::Timer_t getStartTick() const { return _startTick; }
|
|
|
|
|
|
|
|
void setReferenceTime(double time=0.0);
|
|
|
|
|
|
|
|
osg::FrameStamp* getFrameStamp() { return _frameStamp.get(); }
|
|
|
|
const osg::FrameStamp* getFrameStamp() const { return _frameStamp.get(); }
|
|
|
|
|
|
|
|
|
|
|
|
virtual void setSceneData(osg::Node* node);
|
2007-01-06 05:19:01 +08:00
|
|
|
|
2007-01-03 01:39:31 +08:00
|
|
|
enum ThreadingModel
|
|
|
|
{
|
|
|
|
SingleThreaded,
|
2007-02-06 22:38:08 +08:00
|
|
|
CullDrawThreadPerContext,
|
|
|
|
DrawThreadPerContext,
|
|
|
|
CullThreadPerCameraDrawThreadPerContext,
|
|
|
|
AutomaticSelection
|
2007-01-03 01:39:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Set the threading model the rendering traversals will use.*/
|
|
|
|
void setThreadingModel(ThreadingModel threadingModel);
|
|
|
|
|
|
|
|
/** Get the threading model the rendering traversals will use.*/
|
|
|
|
ThreadingModel getThreadingModel() const { return _threadingModel; }
|
2006-12-22 00:56:20 +08:00
|
|
|
|
2007-01-30 06:44:29 +08:00
|
|
|
/** Set whether the main thread, calling frame(), should be used for the rendering traversals.*/
|
|
|
|
void setUseMainThreadForRenderingTraversals(bool flag);
|
|
|
|
|
|
|
|
/** Get whether the main thread, calling frame(), should be used for the rendering traversals.*/
|
|
|
|
bool getUseMainThreadForRenderingTraversals() const { return _useMainThreadForRenderingTraversal; }
|
|
|
|
|
2007-02-06 22:38:08 +08:00
|
|
|
/** Let the viewer suggest the best threading model for the viewers camera/window setup and the hardware available.*/
|
|
|
|
ThreadingModel suggestBestThreadingModel();
|
2007-01-30 06:44:29 +08:00
|
|
|
|
2007-01-13 05:05:39 +08:00
|
|
|
enum BarrierPosition
|
|
|
|
{
|
|
|
|
BeforeSwapBuffers,
|
|
|
|
AfterSwapBuffers
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Set the position of the end barrier.
|
|
|
|
* AfterSwapBuffers will may result is slightly higher framerates, by may
|
|
|
|
* lead to inconcistent swapping between different windows.
|
|
|
|
* 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; }
|
|
|
|
|
|
|
|
/** Set the key event that the viewer checks on each frame to see if the viewer's done flag should be set to
|
2007-01-05 05:28:16 +08:00
|
|
|
* signal end of viewers main loop.
|
|
|
|
* Default value is Escape (osgGA::GUIEVentAdapter::KEY_Escape).
|
|
|
|
* Setting to 0 switches off the feature.*/
|
2007-01-13 05:05:39 +08:00
|
|
|
void setKeyEventSetsDone(int key) { _keyEventSetsDone = key; }
|
2007-01-05 05:28:16 +08:00
|
|
|
|
2007-01-13 05:05:39 +08:00
|
|
|
/** get the key event that the viewer checks on each frame to see if the viewer's done flag.*/
|
|
|
|
int getKeyEventSetsDone() const { return _keyEventSetsDone; }
|
2007-01-09 18:06:20 +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; }
|
|
|
|
|
|
|
|
/** @return true if the viewer respond to the QUIT_APPLICATION-event */
|
|
|
|
bool getQuitEventSetsDone() const { return _quitEventSetsDone; }
|
2007-01-05 05:28:16 +08:00
|
|
|
|
2007-01-05 18:59:23 +08:00
|
|
|
/** Execute a main frame loop.
|
|
|
|
* Equivialant to while (!viewer.done()) viewer.frame();
|
|
|
|
* Also calls realize() if the viewer is not already realized,
|
|
|
|
* and installs trackball manipulator if one is not already assigned.
|
|
|
|
*/
|
2007-01-05 21:16:24 +08:00
|
|
|
virtual int run();
|
2007-01-05 18:59:23 +08:00
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
/** Render a complete new frame.
|
2007-01-06 00:48:04 +08:00
|
|
|
* Calls advance(), eventTraversal(), updateTraversal(), renderingTraversals(). */
|
2007-01-25 20:02:51 +08:00
|
|
|
virtual void frame(double simulationTime=USE_REFERENCE_TIME);
|
2006-11-27 22:52:07 +08:00
|
|
|
|
2007-01-25 20:02:51 +08:00
|
|
|
virtual void advance(double simulationTime=USE_REFERENCE_TIME);
|
2006-12-22 00:56:20 +08:00
|
|
|
|
2007-01-06 00:48:04 +08:00
|
|
|
virtual void eventTraversal();
|
2006-12-22 00:56:20 +08:00
|
|
|
|
2007-01-06 00:48:04 +08:00
|
|
|
virtual void updateTraversal();
|
2006-12-22 00:56:20 +08:00
|
|
|
|
2007-01-06 00:48:04 +08:00
|
|
|
virtual void renderingTraversals();
|
2006-12-23 01:46:21 +08:00
|
|
|
|
2007-01-02 02:20:10 +08:00
|
|
|
void setCameraWithFocus(osg::Camera* camera) { _cameraWithFocus = camera; }
|
|
|
|
osg::Camera* getCameraWithFocus() { return _cameraWithFocus.get(); }
|
|
|
|
const osg::Camera* getCameraWithFocus() const { return _cameraWithFocus.get(); }
|
|
|
|
|
2007-01-02 20:50:57 +08:00
|
|
|
typedef std::vector<osg::GraphicsContext*> Contexts;
|
2007-01-04 19:49:15 +08:00
|
|
|
void getContexts(Contexts& contexts, bool onlyValid=true);
|
2007-01-02 20:50:57 +08:00
|
|
|
|
|
|
|
typedef std::vector<osgViewer::GraphicsWindow*> Windows;
|
2007-01-04 19:49:15 +08:00
|
|
|
void getWindows(Windows& windows, bool onlyValid=true);
|
2007-01-30 19:40:23 +08:00
|
|
|
|
|
|
|
typedef std::vector<osg::Camera*> Cameras;
|
|
|
|
void getCameras(Cameras& cameras, bool onlyActive=true);
|
2007-01-02 20:50:57 +08:00
|
|
|
|
2007-02-06 19:03:13 +08:00
|
|
|
typedef std::vector<OpenThreads::Thread*> Threads;
|
|
|
|
void getAllThreads(Threads& threads, bool onlyActive=true);
|
|
|
|
|
|
|
|
typedef std::vector<osg::OperationsThread*> OperationsThreads;
|
|
|
|
void getOperationsThreads(OperationsThreads& threads, bool onlyActive=true);
|
|
|
|
|
2007-01-29 01:11:21 +08:00
|
|
|
/** Set the graphics operation to call on realization of the viewers graphics windows.*/
|
2007-02-03 06:30:36 +08:00
|
|
|
void setRealizeOperation(osg::Operation* op) { _realizeOperation = op; }
|
2007-01-29 01:11:21 +08:00
|
|
|
|
|
|
|
/** Get the graphics operation to call on realization of the viewers graphics windows.*/
|
2007-02-03 06:30:36 +08:00
|
|
|
osg::Operation* getRealizeOperation() { return _realizeOperation.get(); }
|
2007-01-29 01:11:21 +08:00
|
|
|
|
2007-02-08 20:14:26 +08:00
|
|
|
/** Set up the threading and processor affinity as per the viewers threading model.*/
|
|
|
|
void setUpThreading();
|
|
|
|
|
2007-01-29 01:11:21 +08:00
|
|
|
/** Stop any threads begin run by viewer.*/
|
2007-01-15 22:46:16 +08:00
|
|
|
void stopThreading();
|
2007-01-29 01:11:21 +08:00
|
|
|
|
2007-02-08 20:14:26 +08:00
|
|
|
/** Start any threads required by the viewer.*/
|
2007-01-15 22:46:16 +08:00
|
|
|
void startThreading();
|
2007-01-29 01:11:21 +08:00
|
|
|
|
2007-02-03 06:30:36 +08:00
|
|
|
/** Set up the Operations to render the various viewer cameras on the viewers graphics windows.*/
|
2007-01-15 22:46:16 +08:00
|
|
|
void setUpRenderingSupport();
|
|
|
|
|
2007-02-12 21:39:18 +08:00
|
|
|
/** Get the keyboard and mouse usage of this viewer.*/
|
|
|
|
virtual void getUsage(osg::ApplicationUsage& usage) const;
|
|
|
|
|
2007-01-04 19:49:15 +08:00
|
|
|
protected:
|
2006-12-23 01:46:21 +08:00
|
|
|
|
2007-01-04 19:49:15 +08:00
|
|
|
void checkWindowStatus();
|
|
|
|
|
2007-02-06 19:03:13 +08:00
|
|
|
inline void makeCurrent(osg::GraphicsContext* gc)
|
|
|
|
{
|
|
|
|
if (_currentContext==gc) return;
|
|
|
|
|
|
|
|
releaseContext();
|
|
|
|
|
|
|
|
if (gc && gc->makeCurrent()) _currentContext = gc;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void releaseContext()
|
|
|
|
{
|
|
|
|
if (_currentContext.valid())
|
|
|
|
{
|
|
|
|
_currentContext->releaseContext();
|
|
|
|
_currentContext = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-06 22:38:08 +08:00
|
|
|
|
2006-12-21 05:13:29 +08:00
|
|
|
bool _firstFrame;
|
2007-01-05 05:28:16 +08:00
|
|
|
|
2006-12-22 00:56:20 +08:00
|
|
|
bool _done;
|
2007-01-13 05:05:39 +08:00
|
|
|
int _keyEventSetsDone;
|
2007-01-09 18:06:20 +08:00
|
|
|
bool _quitEventSetsDone;
|
2007-01-03 01:39:31 +08:00
|
|
|
|
2007-02-01 19:07:53 +08:00
|
|
|
ThreadingModel _threadingModel;
|
|
|
|
bool _threadsRunning;
|
|
|
|
|
|
|
|
bool _useMainThreadForRenderingTraversal;
|
|
|
|
BarrierPosition _endBarrierPosition;
|
2007-01-13 05:05:39 +08:00
|
|
|
|
2006-12-23 01:46:21 +08:00
|
|
|
osg::ref_ptr<osg::BarrierOperation> _startRenderingBarrier;
|
|
|
|
osg::ref_ptr<osg::BarrierOperation> _endRenderingDispatchBarrier;
|
2007-01-30 06:44:29 +08:00
|
|
|
osg::ref_ptr<EndOfDynamicDrawBlock> _endDynamicDrawBlock;
|
2007-01-04 19:49:15 +08:00
|
|
|
|
2007-02-08 20:14:26 +08:00
|
|
|
unsigned int _numWindowsOpenAtLastSetUpThreading;
|
2007-01-02 02:20:10 +08:00
|
|
|
|
2007-02-01 06:24:20 +08:00
|
|
|
typedef std::list< osg::ref_ptr<osgUtil::SceneView> > SceneViews;
|
|
|
|
SceneViews _sceneViews;
|
2007-01-16 16:56:33 +08:00
|
|
|
|
2007-01-15 22:46:16 +08:00
|
|
|
osg::Timer_t _startTick;
|
|
|
|
osg::ref_ptr<osg::FrameStamp> _frameStamp;
|
|
|
|
|
2007-01-10 18:09:05 +08:00
|
|
|
osg::observer_ptr<osg::Camera> _cameraWithFocus;
|
|
|
|
|
|
|
|
osg::ref_ptr<osgGA::EventVisitor> _eventVisitor;
|
2007-01-29 01:11:21 +08:00
|
|
|
|
2007-02-03 06:30:36 +08:00
|
|
|
osg::ref_ptr<osg::Operation> _realizeOperation;
|
2007-02-06 19:03:13 +08:00
|
|
|
|
|
|
|
osg::observer_ptr<osg::GraphicsContext> _currentContext;
|
2006-11-27 22:52:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|