2005-04-15 05:41:28 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield
2003-01-21 21:14:29 +08:00
*
2005-01-22 23:41:03 +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
2003-01-21 21:14:29 +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.
2005-01-22 23:41:03 +08:00
*
2003-01-21 21:14:29 +08:00
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2005-01-22 23:41:03 +08:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2003-01-21 21:14:29 +08:00
* OpenSceneGraph Public License for more details.
*/
2003-07-08 22:44:00 +08:00
#ifndef OSGPRODUCER_VIEWER
#define OSGPRODUCER_VIEWER 1
2003-01-21 21:14:29 +08:00
#include <osg/NodeVisitor>
2003-02-19 00:36:42 +08:00
#include <osg/ArgumentParser>
2003-02-21 22:05:39 +08:00
#include <osg/ApplicationUsage>
2003-04-06 06:24:48 +08:00
#include <osg/AnimationPath>
2004-09-09 21:18:45 +08:00
#include <osg/RefNodePath>
2003-01-21 21:14:29 +08:00
Added convinence methods to osgProducer::Viewer:
/** compute, from normalized mouse coords, for sepecified Camera, the pixel coords relative to that Camera's RenderSurface.*/
bool computePixelCoords(float x,float y,unsigned int cameraNum,float& pixel_x,float& pixel_y);
/** compute, from normalized mouse coords, for sepecified Camera, the near and far points in worlds coords.*/
bool computeNearFar(float x,float y,unsigned int cameraNum,osg::Vec3& near, osg::Vec3& far);
/** compute, from normalized mouse coords, for sepecified Camera, intersections with the scene.*/
bool computeIntersections(float x,float y,unsigned int cameraNum,osgUtil::IntersectVisitor::HitList& hits);
/** compute, from normalized mouse coords, for all Cameras, intersections with the scene.*/
bool computeIntersections(float x,float y,osgUtil::IntersectVisitor::HitList& hits);
2003-04-16 22:17:49 +08:00
#include <osgUtil/IntersectVisitor>
2003-01-21 21:14:29 +08:00
#include <osgGA/GUIActionAdapter>
#include <osgGA/GUIEventHandler>
2005-02-25 22:02:48 +08:00
#include <osgGA/EventVisitor>
2003-05-19 23:15:17 +08:00
#include <osgGA/KeySwitchMatrixManipulator>
2003-01-21 21:14:29 +08:00
2003-02-25 20:28:16 +08:00
#include <osgProducer/OsgCameraGroup>
2003-01-21 21:14:29 +08:00
#include <osgProducer/KeyboardMouseCallback>
#include <list>
namespace osgProducer {
2005-01-22 23:41:03 +08:00
/** A Producer-based viewer. Just like OpenGL, the core of OSG is independent of
* windowing system. The integration between OSG and some windowing system is
* delegated to other, non-core parts of OSG (users are also allowed to
* integrate OSG with any exotic windowing system they happen to use).
* \c Viewer implements the integration between OSG and Producer, AKA Open
* Producer (http://www.andesengineering.com/Producer), thus offering an
* out-of-the-box, scalable and multi-platform abstraction of the windowing
* system.
*/
2003-02-25 20:28:16 +08:00
class OSGPRODUCER_EXPORT Viewer : public OsgCameraGroup, public osgGA::GUIActionAdapter
2003-01-21 21:14:29 +08:00
{
public :
Viewer();
Viewer(Producer::CameraConfig *cfg);
Viewer(const std::string& configFile);
2003-02-19 00:36:42 +08:00
Viewer(osg::ArgumentParser& arguments);
2003-08-24 04:48:36 +08:00
virtual ~Viewer();
2003-01-21 21:14:29 +08:00
2005-01-22 23:41:03 +08:00
/** Specifies options to control some aspects of the
* \c Viewer behavior (using the \c setUpViewer() member
* function).
*/
2003-01-21 21:14:29 +08:00
enum ViewerOptions
{
2005-01-22 23:41:03 +08:00
/** Do not activate any standard event handlers.
* If no event handlers are selected then the users would typically
* register their own custom event handlers to add user interaction via
* getEventHandlerList().push_back(myEventHandler) or addCameraManipulator(myCameraManipualtor).*/
NO_EVENT_HANDLERS = 0,
/** Add an \c osgGA::TrackballManipulator to manipulate the camera
* interactively.
*/
TRACKBALL_MANIPULATOR = 1,
/** Add an \c osgGA::DriveManipulator to manipulate the camera
* interactively.
*/
DRIVE_MANIPULATOR = 2,
/** Add an \c osgGA::FlightManipulator to manipulate the camera
* interactively.
*/
FLIGHT_MANIPULATOR = 4,
/** Add an \c osgGA::TerrainManipulator to manipulate the camera
* interactively.
*/
2004-05-06 19:01:16 +08:00
TERRAIN_MANIPULATOR = 8,
2005-01-22 23:41:03 +08:00
2005-03-11 14:09:38 +08:00
/** Add an \c osgGA::UFOManipulator to manipulate the camera
* interactively.
*/
UFO_MANIPULATOR = 0x10,
2005-01-22 23:41:03 +08:00
/** Add an \c osgGA::StateSetManipulator to interactively toggle
* some bits of the renderer state (texturing, lightning...)
*/
2004-07-30 23:44:59 +08:00
STATE_MANIPULATOR = 32,
2005-01-22 23:41:03 +08:00
/** Add a light source some point near the camera. */
2004-07-30 23:44:59 +08:00
HEAD_LIGHT_SOURCE = 64,
2005-01-22 23:41:03 +08:00
/** Add a light source above the scene; does nothing if
* \c HEAD_LIGHT_SOURCE is also used.
*/
2004-07-30 23:44:59 +08:00
SKY_LIGHT_SOURCE = 128,
2005-01-22 23:41:03 +08:00
/** Add peformance statistics reporting, currently implemented via VIEWER_MANPULATOR. */
2004-07-30 23:44:59 +08:00
STATS_MANIPULATOR = 256,
2005-01-22 23:41:03 +08:00
/** Add an \c osgProducer::ViewerEventHandler that enables lots of
* tricks like performance statistics and writing the scene to a
* file.
*/
2004-07-30 23:44:59 +08:00
VIEWER_MANIPULATOR = 512,
2005-01-22 23:41:03 +08:00
/** Finish the viewer execution when the ESC key is pressed. */
2004-07-30 23:44:59 +08:00
ESCAPE_SETS_DONE = 1024,
2005-01-22 23:41:03 +08:00
/** Enable a set of standard settings (\c TRACKBALL_MANIPULATOR,
* \c DRIVE_MANIPULATOR, \c FLIGHT_MANIPULATOR,
* \c TERRAIN_MANIPULATOR, \c STATE_MANIPULATOR,
* \c HEAD_LIGHT_SOURCE, \c STATS_MANIPULATOR,
* \c VIEWER_MANIPULATOR, \c ESCAPE_SETS_DONE).
*/
2003-01-21 21:14:29 +08:00
STANDARD_SETTINGS = TRACKBALL_MANIPULATOR|
DRIVE_MANIPULATOR |
FLIGHT_MANIPULATOR |
2004-05-06 19:01:16 +08:00
TERRAIN_MANIPULATOR |
2005-03-11 14:09:38 +08:00
UFO_MANIPULATOR |
2003-01-21 21:14:29 +08:00
STATE_MANIPULATOR |
2003-01-31 07:02:32 +08:00
HEAD_LIGHT_SOURCE |
2003-03-19 22:27:05 +08:00
STATS_MANIPULATOR |
2003-03-25 23:13:20 +08:00
VIEWER_MANIPULATOR |
2003-03-19 22:27:05 +08:00
ESCAPE_SETS_DONE
2003-01-21 21:14:29 +08:00
};
2005-01-22 23:41:03 +08:00
/** Set up the viewer, allowing to control some aspects of its behavior.
* @param options One or more of the options defined by the
* \c ViewerOptions enumeration, combined using the bitwise OR
* operator (``<tt>|</tt>'').
*/
2003-01-24 17:37:11 +08:00
void setUpViewer(unsigned int options=STANDARD_SETTINGS);
2003-09-06 04:48:42 +08:00
2004-10-05 17:53:36 +08:00
/** Set the viewer so it sets done to true once the refrence time equals or exceeds specified elapsed time.
* Automatically does a setDoneAtElapsedTimeEnabled(true). */
void setDoneAtElapsedTime(double elapsedTime) { _setDoneAtElapsedTimeEnabled = true; _setDoneAtElapsedTime = elapsedTime; }
/** Get the elapsed time that will cause done to be set to be true.*/
double getDoneAtElapsedTime() const { return _setDoneAtElapsedTime; }
/** Set whether to use a elapsed time to limit the run of the viewer.*/
void setDoneAtElapsedTimeEnabled(bool enabled) { _setDoneAtElapsedTimeEnabled = enabled; }
/** Get whether to use a elapsed time to limit the run of the viewer.*/
bool getDoneAtElapsedTimeEnabled() const { return _setDoneAtElapsedTimeEnabled; }
/** Set the viewer so it sets done to true once the frame number equals or exceeds specified frame number.
* Automatically does a setDoneAtFrameNumberEnabled(true). */
void setDoneAtFrameNumber(unsigned int frameNumber) { _setDoneAtFrameNumberEnabled = true; _setDoneAtFrameNumber = frameNumber; }
/** Get the frame number that will cause done to be set to be true.*/
unsigned int getDoneAtFrameNumber() const { return _setDoneAtFrameNumber; }
/** Set whether to use a frame number to limit the run of the viewer.*/
void setDoneAtFrameNumberEnabled(bool enabled) { _setDoneAtFrameNumberEnabled = enabled; }
/** Get whether to use a frame number to limit the run of the viewer.*/
bool getDoneAtFrameNumberEnabled() const { return _setDoneAtFrameNumberEnabled; }
/** Set the done flag signalling that the viewer exit.*/
2005-01-22 23:41:03 +08:00
void setDone(bool done) { _done = done; }
2004-10-05 17:53:36 +08:00
/** Get the done flag which signals that the viewer exit.*/
2005-01-22 23:41:03 +08:00
bool getDone() const { return _done; }
2003-09-06 04:48:42 +08:00
2005-01-22 23:41:03 +08:00
/** Return true if the application is done and should exit.*/
2003-04-10 20:55:48 +08:00
virtual bool done() const;
2003-04-08 23:47:45 +08:00
2004-10-05 17:53:36 +08:00
/** Set the viewer to take an image snapshot on the last frame() when done is enabled.*/
void setWriteImageWhenDone(bool enabled) { _writeImageWhenDone = enabled; }
/** Set the viewer to take an image snapshot on the last frame() when done is enabled.*/
bool getWriteImageWhenDone() const { return _writeImageWhenDone; }
/** Set the filename to write to when the viewer takes an image snapshot on the last frame() when done is enabled.*/
void setWriteImageFileName(const std::string& filename);
/** Set the filename to write to when the viewer takes an image snapshot on the last frame() when done is enabled.*/
const std::string& getWriteImageFileName() const;
2003-04-10 18:02:24 +08:00
/** Override the Producer::CameraGroup::setViewByMatrix to catch all changes to view.*/
virtual void setViewByMatrix( const Producer::Matrix & pm);
2003-01-21 21:14:29 +08:00
2003-04-08 23:18:45 +08:00
/** Set the threading model and then call realize().*/
virtual bool realize(ThreadingModel thread_model);
virtual bool realize();
2003-04-06 06:24:48 +08:00
2003-06-24 23:40:09 +08:00
/** Updated the scene. Handle any queued up events, do an update traversal and set the CameraGroup's setViewByMatrix if any camera manipulators are active.*/
2003-01-21 21:14:29 +08:00
virtual void update();
2005-01-22 23:41:03 +08:00
/** Set the update visitor which does the update traversal of the scene graph. Automatically called by the update() method.*/
2003-12-13 04:33:36 +08:00
void setUpdateVisitor(osg::NodeVisitor* nv) { _updateVisitor = nv; }
2005-01-22 23:41:03 +08:00
/** Get the update visitor.*/
2003-12-13 04:33:36 +08:00
osg::NodeVisitor* getUpdateVisitor() { return _updateVisitor.get(); }
2005-01-22 23:41:03 +08:00
/** Get the const update visitor.*/
2003-12-13 04:33:36 +08:00
const osg::NodeVisitor* getUpdateVisitor() const { return _updateVisitor.get(); }
2003-01-21 21:14:29 +08:00
2005-02-25 22:02:48 +08:00
/** Set the update visitor which does the event traversal of the scene graph. Automatically called by the update() method.*/
void setEventVisitor(osgGA::EventVisitor* nv) { _eventVisitor = nv; }
/** Get the update visitor.*/
osgGA::EventVisitor* getEventVisitor() { return _eventVisitor.get(); }
/** Get the const update visitor.*/
const osgGA::EventVisitor* getEventVisitor() const { return _eventVisitor.get(); }
2004-07-09 06:28:15 +08:00
void computeActiveCoordindateSystemNodePath();
2004-09-09 21:18:45 +08:00
void setCoordindateSystemNodePath(const osg::RefNodePath& nodePath) { _coordinateSystemNodePath = nodePath; }
2005-01-22 23:41:03 +08:00
2004-05-03 20:04:25 +08:00
void setCoordindateSystemNodePath(const osg::NodePath& nodePath);
2005-01-22 23:41:03 +08:00
2004-09-09 21:18:45 +08:00
const osg::RefNodePath& getCoordindateSystemNodePath() const { return _coordinateSystemNodePath; }
2004-04-30 06:19:57 +08:00
2003-04-06 06:24:48 +08:00
/** Dispatch the cull and draw for each of the Camera's for this frame.*/
virtual void frame();
2003-01-21 21:14:29 +08:00
2004-06-02 22:13:11 +08:00
virtual void requestRedraw();
virtual void requestContinuousUpdate(bool);
2003-04-05 03:10:37 +08:00
virtual void requestWarpPointer(float x,float y);
2003-01-21 21:14:29 +08:00
2003-04-16 04:54:10 +08:00
2005-01-22 23:41:03 +08:00
/** Compute, from normalized mouse coords, for sepecified Camera, the pixel coords relative to that Camera's RenderSurface.*/
Added convinence methods to osgProducer::Viewer:
/** compute, from normalized mouse coords, for sepecified Camera, the pixel coords relative to that Camera's RenderSurface.*/
bool computePixelCoords(float x,float y,unsigned int cameraNum,float& pixel_x,float& pixel_y);
/** compute, from normalized mouse coords, for sepecified Camera, the near and far points in worlds coords.*/
bool computeNearFar(float x,float y,unsigned int cameraNum,osg::Vec3& near, osg::Vec3& far);
/** compute, from normalized mouse coords, for sepecified Camera, intersections with the scene.*/
bool computeIntersections(float x,float y,unsigned int cameraNum,osgUtil::IntersectVisitor::HitList& hits);
/** compute, from normalized mouse coords, for all Cameras, intersections with the scene.*/
bool computeIntersections(float x,float y,osgUtil::IntersectVisitor::HitList& hits);
2003-04-16 22:17:49 +08:00
bool computePixelCoords(float x,float y,unsigned int cameraNum,float& pixel_x,float& pixel_y);
2005-01-22 23:41:03 +08:00
/** Compute, from normalized mouse coords, for sepecified Camera, the near and far points in worlds coords.*/
2003-04-16 22:22:36 +08:00
bool computeNearFarPoints(float x,float y,unsigned int cameraNum,osg::Vec3& near, osg::Vec3& far);
2005-01-22 23:41:03 +08:00
/** Compute, from normalized mouse coords, for all Cameras, intersections with the specified subgraph.*/
2003-12-04 17:43:34 +08:00
bool computeIntersections(float x,float y,unsigned int cameraNum,osg::Node *node,osgUtil::IntersectVisitor::HitList& hits,osg::Node::NodeMask traversalMask = 0xffffffff);
2003-11-28 21:41:10 +08:00
2005-01-22 23:41:03 +08:00
/** Compute, from normalized mouse coords, for sepecified Camera, intersections with the scene.*/
2003-12-04 17:43:34 +08:00
bool computeIntersections(float x,float y,unsigned int cameraNum,osgUtil::IntersectVisitor::HitList& hits,osg::Node::NodeMask traversalMask = 0xffffffff);
Added convinence methods to osgProducer::Viewer:
/** compute, from normalized mouse coords, for sepecified Camera, the pixel coords relative to that Camera's RenderSurface.*/
bool computePixelCoords(float x,float y,unsigned int cameraNum,float& pixel_x,float& pixel_y);
/** compute, from normalized mouse coords, for sepecified Camera, the near and far points in worlds coords.*/
bool computeNearFar(float x,float y,unsigned int cameraNum,osg::Vec3& near, osg::Vec3& far);
/** compute, from normalized mouse coords, for sepecified Camera, intersections with the scene.*/
bool computeIntersections(float x,float y,unsigned int cameraNum,osgUtil::IntersectVisitor::HitList& hits);
/** compute, from normalized mouse coords, for all Cameras, intersections with the scene.*/
bool computeIntersections(float x,float y,osgUtil::IntersectVisitor::HitList& hits);
2003-04-16 22:17:49 +08:00
2005-01-22 23:41:03 +08:00
/** Compute, from normalized mouse coords, for all Cameras, intersections with specified subgraph.*/
2003-12-04 17:43:34 +08:00
bool computeIntersections(float x,float y,osg::Node *node,osgUtil::IntersectVisitor::HitList& hits,osg::Node::NodeMask traversalMask = 0xffffffff);
2003-11-28 21:41:10 +08:00
2005-01-22 23:41:03 +08:00
/** Compute, from normalized mouse coords, for all Cameras, intersections with the scene.*/
2003-12-04 17:43:34 +08:00
bool computeIntersections(float x,float y,osgUtil::IntersectVisitor::HitList& hits,osg::Node::NodeMask traversalMask = 0xffffffff);
2003-04-16 04:54:10 +08:00
2003-08-24 04:48:36 +08:00
void setKeyboardMouse(Producer::KeyboardMouse* kbm);
Producer::KeyboardMouse* getKeyboardMouse() { return _kbm.get(); }
const Producer::KeyboardMouse* getKeyboardMouse() const { return _kbm.get(); }
Added convinence methods to osgProducer::Viewer:
/** compute, from normalized mouse coords, for sepecified Camera, the pixel coords relative to that Camera's RenderSurface.*/
bool computePixelCoords(float x,float y,unsigned int cameraNum,float& pixel_x,float& pixel_y);
/** compute, from normalized mouse coords, for sepecified Camera, the near and far points in worlds coords.*/
bool computeNearFar(float x,float y,unsigned int cameraNum,osg::Vec3& near, osg::Vec3& far);
/** compute, from normalized mouse coords, for sepecified Camera, intersections with the scene.*/
bool computeIntersections(float x,float y,unsigned int cameraNum,osgUtil::IntersectVisitor::HitList& hits);
/** compute, from normalized mouse coords, for all Cameras, intersections with the scene.*/
bool computeIntersections(float x,float y,osgUtil::IntersectVisitor::HitList& hits);
2003-04-16 22:17:49 +08:00
2003-08-25 22:03:49 +08:00
void setKeyboardMouseCallback(osgProducer::KeyboardMouseCallback* kbmcb);
osgProducer::KeyboardMouseCallback* getKeyboardMouseCallback() { return _kbmcb.get(); }
const osgProducer::KeyboardMouseCallback* getKeyboardMouseCallback() const { return _kbmcb.get(); }
2003-04-16 04:54:10 +08:00
2003-01-22 23:30:17 +08:00
typedef std::list< osg::ref_ptr<osgGA::GUIEventHandler> > EventHandlerList;
EventHandlerList& getEventHandlerList() { return _eventHandlerList; }
2003-04-06 06:24:48 +08:00
const EventHandlerList& getEventHandlerList() const { return _eventHandlerList; }
2005-01-22 23:41:03 +08:00
2003-05-19 23:15:17 +08:00
osgGA::KeySwitchMatrixManipulator* getKeySwitchMatrixManipulator() { return _keyswitchManipulator.get(); }
const osgGA::KeySwitchMatrixManipulator* getKeySwitchMatrixManipulator() const { return _keyswitchManipulator.get(); }
2003-01-22 23:30:17 +08:00
2003-05-19 23:15:17 +08:00
unsigned int addCameraManipulator(osgGA::MatrixManipulator* cm);
2003-01-22 23:30:17 +08:00
void selectCameraManipulator(unsigned int no);
2005-03-12 01:48:01 +08:00
void getCameraManipulatorNameList( std::list<std::string> &nameList );
bool selectCameraManipulatorByName( const std::string &name );
2005-03-12 13:31:26 +08:00
osgGA::MatrixManipulator *getCameraManipulatorByName( const std::string &name );
2003-01-22 23:30:17 +08:00
2003-04-06 06:24:48 +08:00
void setRecordingAnimationPath(bool on) { _recordingAnimationPath = on; }
bool getRecordingAnimationPath() const { return _recordingAnimationPath; }
void setAnimationPath(osg::AnimationPath* path) { _animationPath = path; }
osg::AnimationPath* getAnimationPath() { return _animationPath.get(); }
const osg::AnimationPath* getAnimationPath() const { return _animationPath.get(); }
2003-11-05 00:38:10 +08:00
const double* getPosition() const { return _position; }
double getSpeed() const { return _speed; }
osg::Quat getOrientation() const { return _orientation; }
2003-02-19 18:43:02 +08:00
/** Get the keyboard and mouse usage of this viewer.*/
virtual void getUsage(osg::ApplicationUsage& usage) const;
2004-05-03 20:04:25 +08:00
2005-01-22 23:41:03 +08:00
/** Update internal structures w.r.t updated scene data.*/
2004-05-03 20:04:25 +08:00
virtual void updatedSceneData();
2003-01-22 23:30:17 +08:00
2003-01-21 21:14:29 +08:00
protected :
2004-10-05 17:53:36 +08:00
bool _setDoneAtElapsedTimeEnabled;
double _setDoneAtElapsedTime;
bool _setDoneAtFrameNumberEnabled;
unsigned int _setDoneAtFrameNumber;
2003-01-21 21:14:29 +08:00
bool _done;
2004-10-05 17:53:36 +08:00
bool _writeImageWhenDone;
std::string _writeImageFileName;
2003-08-24 04:48:36 +08:00
osg::ref_ptr<Producer::KeyboardMouse> _kbm;
osg::ref_ptr<osgProducer::KeyboardMouseCallback> _kbmcb;
2003-01-21 21:14:29 +08:00
EventHandlerList _eventHandlerList;
2003-05-19 23:15:17 +08:00
osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> _keyswitchManipulator;
2003-01-21 21:14:29 +08:00
2003-11-05 00:38:10 +08:00
osg::ref_ptr<osg::NodeVisitor> _updateVisitor;
2005-02-25 22:02:48 +08:00
osg::ref_ptr<osgGA::EventVisitor> _eventVisitor;
2003-04-06 06:24:48 +08:00
2004-05-03 20:04:25 +08:00
2004-09-09 21:18:45 +08:00
osg::RefNodePath _coordinateSystemNodePath;
2004-04-30 06:19:57 +08:00
2003-04-06 06:24:48 +08:00
bool _recordingAnimationPath;
2004-03-26 19:04:37 +08:00
double _recordingStartTime;
2003-04-06 06:24:48 +08:00
osg::ref_ptr<osg::AnimationPath> _animationPath;
2003-11-05 00:38:10 +08:00
// record the current position and orientation of the view.
double _position[3];
osg::Quat _orientation;
double _speed;
2003-01-21 21:14:29 +08:00
};
}
#endif