| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 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. |
|---|
| 7 | * |
|---|
| 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. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSGVIEWER_VIEW |
|---|
| 15 | #define OSGVIEWER_VIEW 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/View> |
|---|
| 18 | |
|---|
| 19 | #include <osgUtil/PolytopeIntersector> |
|---|
| 20 | #include <osgUtil/LineSegmentIntersector> |
|---|
| 21 | #include <osgUtil/UpdateVisitor> |
|---|
| 22 | #include <osgUtil/SceneView> |
|---|
| 23 | |
|---|
| 24 | #include <osgGA/CameraManipulator> |
|---|
| 25 | #include <osgGA/EventVisitor> |
|---|
| 26 | #include <osgGA/EventQueue> |
|---|
| 27 | |
|---|
| 28 | #include <osgViewer/Scene> |
|---|
| 29 | #include <osgViewer/ViewerBase> |
|---|
| 30 | |
|---|
| 31 | namespace osgViewer { |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | struct OSGVIEWER_EXPORT DepthPartitionSettings : public osg::Referenced |
|---|
| 35 | { |
|---|
| 36 | enum DepthMode |
|---|
| 37 | { |
|---|
| 38 | FIXED_RANGE, |
|---|
| 39 | BOUNDING_VOLUME |
|---|
| 40 | }; |
|---|
| 41 | |
|---|
| 42 | DepthPartitionSettings(DepthMode mode=BOUNDING_VOLUME); |
|---|
| 43 | |
|---|
| 44 | virtual bool getDepthRange(osg::View& view, unsigned int partition, double& zNear, double& zFar); |
|---|
| 45 | |
|---|
| 46 | DepthMode _mode; |
|---|
| 47 | double _zNear; |
|---|
| 48 | double _zMid; |
|---|
| 49 | double _zFar; |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | /** View holds a single view on a scene, this view may be composed of one or more slave cameras.*/ |
|---|
| 54 | class OSGVIEWER_EXPORT View : public osg::View, public osgGA::GUIActionAdapter |
|---|
| 55 | { |
|---|
| 56 | public: |
|---|
| 57 | |
|---|
| 58 | View(); |
|---|
| 59 | |
|---|
| 60 | View(const osgViewer::View& view, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); |
|---|
| 61 | |
|---|
| 62 | META_Object(osgViewer,View); |
|---|
| 63 | |
|---|
| 64 | /** Provide a mechanism for getting the osg::View associated from the GUIActionAdapter. |
|---|
| 65 | * One would use this to case view to osgViewer::View(er) if supported by the subclass.*/ |
|---|
| 66 | virtual osg::View* asView() { return this; } |
|---|
| 67 | |
|---|
| 68 | /** Provide a mechanism for getting the viewer object from this osgViewer::View. |
|---|
| 69 | * In the case of a osgViewer::Viewer the ViewerBase will effectively point to this object as Viewer subclasses from View. |
|---|
| 70 | * In the case of a osgViewer::CompsoiteViewer the ViewerBase will point to the CompositeViewer that owns this View. */ |
|---|
| 71 | ViewerBase* getViewerBase() { return _viewerBase.get(); } |
|---|
| 72 | |
|---|
| 73 | /** Take all the settings, Camera and Slaves from the passed in view, leaving it empty. */ |
|---|
| 74 | virtual void take(osg::View& rhs); |
|---|
| 75 | |
|---|
| 76 | virtual void setStartTick(osg::Timer_t tick); |
|---|
| 77 | osg::Timer_t getStartTick() const { return _startTick; } |
|---|
| 78 | |
|---|
| 79 | Scene* getScene() { return _scene.get(); } |
|---|
| 80 | const Scene* getScene() const { return _scene.get(); } |
|---|
| 81 | |
|---|
| 82 | /** Set the scene graph that the View will use.*/ |
|---|
| 83 | virtual void setSceneData(osg::Node* node); |
|---|
| 84 | |
|---|
| 85 | /** Get the View's scene graph.*/ |
|---|
| 86 | osg::Node* getSceneData() { return _scene.valid() ? _scene->getSceneData() : 0; } |
|---|
| 87 | |
|---|
| 88 | /** Get the const View's scene graph.*/ |
|---|
| 89 | const osg::Node* getSceneData() const { return _scene.valid() ? _scene->getSceneData() : 0; } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | /** Set the View's database pager.*/ |
|---|
| 93 | void setDatabasePager(osgDB::DatabasePager* dp); |
|---|
| 94 | |
|---|
| 95 | /** Get the View's database pager.*/ |
|---|
| 96 | osgDB::DatabasePager* getDatabasePager(); |
|---|
| 97 | |
|---|
| 98 | /** Get the const View's database pager.*/ |
|---|
| 99 | const osgDB::DatabasePager* getDatabasePager() const; |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | /** Set the View's image pager.*/ |
|---|
| 103 | void setImagePager(osgDB::ImagePager* ip); |
|---|
| 104 | |
|---|
| 105 | /** Get the View's image pager.*/ |
|---|
| 106 | osgDB::ImagePager* getImagePager(); |
|---|
| 107 | |
|---|
| 108 | /** Get the const View's image pager.*/ |
|---|
| 109 | const osgDB::ImagePager* getImagePager() const; |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | /* Set the EventQueue that the View uses to integrate external non window related events.*/ |
|---|
| 113 | void setEventQueue(osgGA::EventQueue* eventQueue) { _eventQueue = eventQueue; } |
|---|
| 114 | |
|---|
| 115 | /* Get the View's EventQueue.*/ |
|---|
| 116 | osgGA::EventQueue* getEventQueue() { return _eventQueue.get(); } |
|---|
| 117 | |
|---|
| 118 | /* Get the const View's EventQueue.*/ |
|---|
| 119 | const osgGA::EventQueue* getEventQueue() const { return _eventQueue.get(); } |
|---|
| 120 | |
|---|
| 121 | /** Set the CameraManipulator that moves the View's master Camera position in response to events. |
|---|
| 122 | * The parameter resetPosition determines whether manipulator is set to its home position.*/ |
|---|
| 123 | void setCameraManipulator(osgGA::CameraManipulator* manipulator, bool resetPosition = true); |
|---|
| 124 | |
|---|
| 125 | /** Get the View's CameraManipulator.*/ |
|---|
| 126 | osgGA::CameraManipulator* getCameraManipulator() { return _cameraManipulator.get(); } |
|---|
| 127 | |
|---|
| 128 | /** Get the const View's CameraManipulator.*/ |
|---|
| 129 | const osgGA::CameraManipulator* getCameraManipulator() const { return _cameraManipulator.get(); } |
|---|
| 130 | |
|---|
| 131 | /** Set the view to the CameraManipulator's home position, if none is attached home() it does nothing. |
|---|
| 132 | * Note, to set the home position use getCamaraManipulator()->setHomePosition(...). */ |
|---|
| 133 | void home(); |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | typedef std::list< osg::ref_ptr<osgGA::GUIEventHandler> > EventHandlers; |
|---|
| 137 | |
|---|
| 138 | /** Add an EventHandler that adds handling of events to the View.*/ |
|---|
| 139 | void addEventHandler(osgGA::GUIEventHandler* eventHandler); |
|---|
| 140 | |
|---|
| 141 | /** Remove an EventHandler from View.*/ |
|---|
| 142 | void removeEventHandler(osgGA::GUIEventHandler* eventHandler); |
|---|
| 143 | |
|---|
| 144 | /** Get the View's list of EventHandlers.*/ |
|---|
| 145 | EventHandlers& getEventHandlers() { return _eventHandlers; } |
|---|
| 146 | |
|---|
| 147 | /** Get the const View's list of EventHandlers.*/ |
|---|
| 148 | const EventHandlers& getEventHandlers() const { return _eventHandlers; } |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | /** Set the NodePath to any active CoordinateSystemNode present in the Scene. |
|---|
| 152 | * The CoordinateSystemNode path is used to help applications and CamaraManipulators handle geocentric coordinates systems, |
|---|
| 153 | * so that the local up direction is known at any position on the whole earth. */ |
|---|
| 154 | void setCoordinateSystemNodePath(const osg::NodePath& nodePath); |
|---|
| 155 | |
|---|
| 156 | /** Get the NodePath to any active CoordinateSystemNode present in the Scene.*/ |
|---|
| 157 | osg::NodePath getCoordinateSystemNodePath() const; |
|---|
| 158 | |
|---|
| 159 | /** Compute the NodePath to any active CoordinateSystemNode present in the Scene.*/ |
|---|
| 160 | void computeActiveCoordinateSystemNodePath(); |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | /** Set the DisplaySettings object associated with this view.*/ |
|---|
| 164 | void setDisplaySettings(osg::DisplaySettings* ds) { _displaySettings = ds; } |
|---|
| 165 | |
|---|
| 166 | /** Set the DisplaySettings object associated with this view.*/ |
|---|
| 167 | osg::DisplaySettings* getDisplaySettings() { return _displaySettings.get(); } |
|---|
| 168 | |
|---|
| 169 | /** Set the DisplaySettings object associated with this view.*/ |
|---|
| 170 | const osg::DisplaySettings* getDisplaySettings() const { return _displaySettings.get(); } |
|---|
| 171 | |
|---|
| 172 | /** Set the FusionDistanceMode and Value. Note, only used when working in stereo.*/ |
|---|
| 173 | void setFusionDistance(osgUtil::SceneView::FusionDistanceMode mode,float value=1.0f) |
|---|
| 174 | { |
|---|
| 175 | _fusionDistanceMode = mode; |
|---|
| 176 | _fusionDistanceValue = value; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | /** Get the FusionDistanceMode.*/ |
|---|
| 180 | osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const { return _fusionDistanceMode; } |
|---|
| 181 | |
|---|
| 182 | /** Get the FusionDistanceValue. Note, only used for USE_FUSION_DISTANCE_VALUE & PROPORTIONAL_TO_SCREEN_DISTANCE modes.*/ |
|---|
| 183 | float getFusionDistanceValue() const { return _fusionDistanceValue; } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | /** Convenience method for creating slave Cameras and associated GraphicsWindows across all screens.*/ |
|---|
| 187 | void setUpViewAcrossAllScreens(); |
|---|
| 188 | |
|---|
| 189 | /** Convenience method for a single camera on a single window.*/ |
|---|
| 190 | void setUpViewInWindow(int x, int y, int width, int height, unsigned int screenNum=0); |
|---|
| 191 | |
|---|
| 192 | /** Convenience method for a single camera associated with a single full screen GraphicsWindow.*/ |
|---|
| 193 | void setUpViewOnSingleScreen(unsigned int screenNum=0); |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | /** Convenience method for spherical display using 6 slave cameras rendering the 6 sides of a cube map, and 7th camera doing distortion correction to present on a spherical display.*/ |
|---|
| 197 | void setUpViewFor3DSphericalDisplay(double radius=1.0, double collar=0.45, unsigned int screenNum=0, osg::Image* intensityMap=0, const osg::Matrixd& projectorMatrix = osg::Matrixd()); |
|---|
| 198 | |
|---|
| 199 | /** Convenience method for spherical display by rendering main scene to a panoramic 2:1 texture and then doing distortion correction to present onto a spherical display.*/ |
|---|
| 200 | void setUpViewForPanoramicSphericalDisplay(double radius=1.0, double collar=0.45, unsigned int screenNum=0, osg::Image* intensityMap=0, const osg::Matrixd& projectorMatrix = osg::Matrixd()); |
|---|
| 201 | |
|---|
| 202 | /** Convenience method for autostereoscopic Philips WoWvx display.*/ |
|---|
| 203 | void setUpViewForWoWVxDisplay(unsigned int screenNum, unsigned char wow_content, unsigned char wow_factor, unsigned char wow_offset, float wow_disparity_Zd, float wow_disparity_vz, float wow_disparity_M, float wow_disparity_C); |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | /** Convenience method for setting up depth partitioning on the specified camera.*/ |
|---|
| 207 | bool setUpDepthPartitionForCamera(osg::Camera* cameraToPartition, DepthPartitionSettings* dps=0); |
|---|
| 208 | |
|---|
| 209 | /** Convenience method for setting up multiple slave cameras with depth partitioning on each of the view's active cameras.*/ |
|---|
| 210 | bool setUpDepthPartition(DepthPartitionSettings* dsp=0); |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | /** Return true if this view contains a specified camera.*/ |
|---|
| 214 | bool containsCamera(const osg::Camera* camera) const; |
|---|
| 215 | |
|---|
| 216 | /** Get the camera which contains the pointer position x,y specified in the master camera's window/eye coordinates. |
|---|
| 217 | * Also passes back the local window coordinatess for the graphics context associated with the camera. */ |
|---|
| 218 | const osg::Camera* getCameraContainingPosition(float x, float y, float& local_x, float& local_y) const; |
|---|
| 219 | |
|---|
| 220 | /** Compute intersections between a ray through the specified master camera's window/eye coordinates and a specified node. |
|---|
| 221 | * Note, when a master camera has slaves and no viewport itself, its coordinate frame will be in clip space i.e. -1,-1 to 1,1, |
|---|
| 222 | * while if it has a viewport the coordintates will be relative to its viewport dimensions. |
|---|
| 223 | * Mouse events handled by the view will automatically be attached to the master camera window/clip coordinates so that they can be passed |
|---|
| 224 | * directly to the computeIntersections method. */ |
|---|
| 225 | bool computeIntersections(float x,float y, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask = 0xffffffff); |
|---|
| 226 | |
|---|
| 227 | /** Compute intersections between a ray through the specified master camera's window/eye coordinates and a specified nodePath's subgraph. */ |
|---|
| 228 | bool computeIntersections(float x,float y, const osg::NodePath& nodePath, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask = 0xffffffff); |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | virtual void requestRedraw(); |
|---|
| 232 | virtual void requestContinuousUpdate(bool needed=true); |
|---|
| 233 | virtual void requestWarpPointer(float x,float y); |
|---|
| 234 | |
|---|
| 235 | public: |
|---|
| 236 | |
|---|
| 237 | void assignSceneDataToCameras(); |
|---|
| 238 | void init(); |
|---|
| 239 | |
|---|
| 240 | protected: |
|---|
| 241 | |
|---|
| 242 | friend class CompositeViewer; |
|---|
| 243 | |
|---|
| 244 | virtual ~View(); |
|---|
| 245 | |
|---|
| 246 | virtual osg::GraphicsOperation* createRenderer(osg::Camera* camera); |
|---|
| 247 | |
|---|
| 248 | osg::observer_ptr<ViewerBase> _viewerBase; |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | osg::Timer_t _startTick; |
|---|
| 252 | |
|---|
| 253 | osg::ref_ptr<osgViewer::Scene> _scene; |
|---|
| 254 | osg::ref_ptr<osgGA::EventQueue> _eventQueue; |
|---|
| 255 | osg::ref_ptr<osgGA::CameraManipulator> _cameraManipulator; |
|---|
| 256 | EventHandlers _eventHandlers; |
|---|
| 257 | |
|---|
| 258 | osg::ObserverNodePath _coordinateSystemNodePath; |
|---|
| 259 | |
|---|
| 260 | osg::ref_ptr<osg::DisplaySettings> _displaySettings; |
|---|
| 261 | osgUtil::SceneView::FusionDistanceMode _fusionDistanceMode; |
|---|
| 262 | float _fusionDistanceValue; |
|---|
| 263 | }; |
|---|
| 264 | |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | #endif |
|---|