View 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. #ifndef OSGVIEWER_VIEW
  14. #define OSGVIEWER_VIEW 1
  15. #include <osg/View>
  16. #include <osgUtil/PolytopeIntersector>
  17. #include <osgUtil/LineSegmentIntersector>
  18. #include <osgUtil/UpdateVisitor>
  19. #include <osgUtil/SceneView>
  20. #include <osgGA/CameraManipulator>
  21. #include <osgGA/EventVisitor>
  22. #include <osgGA/EventQueue>
  23. #include <osgGA/Device>
  24. #include <osgViewer/Scene>
  25. #include <osgViewer/ViewerBase>
  26. #include <osgViewer/Keystone>
  27. namespace osgViewer {
  28. /** Base class for View configurations for setting up Camera and Windowing.*/
  29. class OSGVIEWER_EXPORT ViewConfig : public osg::Object
  30. {
  31. public:
  32. ViewConfig() {}
  33. ViewConfig(const ViewConfig& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) : osg::Object(rhs,copyop) {}
  34. META_Object(osgViewer,ViewConfig);
  35. /** configure method that is overridden by Config subclasses.*/
  36. virtual void configure(osgViewer::View& /*view*/) const {}
  37. /** convenience method for getting the relevant display settings to use.*/
  38. virtual osg::DisplaySettings* getActiveDisplaySetting(osgViewer::View& view) const;
  39. };
  40. struct OSGVIEWER_EXPORT DepthPartitionSettings : public osg::Referenced
  41. {
  42. enum DepthMode
  43. {
  44. FIXED_RANGE,
  45. BOUNDING_VOLUME
  46. };
  47. DepthPartitionSettings(DepthMode mode=BOUNDING_VOLUME);
  48. virtual bool getDepthRange(osg::View& view, unsigned int partition, double& zNear, double& zFar);
  49. DepthMode _mode;
  50. double _zNear;
  51. double _zMid;
  52. double _zFar;
  53. };
  54. /** View holds a single view on a scene, this view may be composed of one or more slave cameras.*/
  55. class OSGVIEWER_EXPORT View : public osg::View, public osgGA::GUIActionAdapter
  56. {
  57. public:
  58. View();
  59. View(const osgViewer::View& view, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  60. META_Object(osgViewer,View);
  61. /** Provide a mechanism for getting the osg::View associated from the GUIActionAdapter.
  62. * One would use this to case view to osgViewer::View(er) if supported by the subclass.*/
  63. virtual osg::View* asView() { return this; }
  64. /** Provide a mechanism for getting the viewer object from this osgViewer::View.
  65. * In the case of a osgViewer::Viewer the ViewerBase will effectively point to this object as Viewer subclasses from View.
  66. * In the case of a osgViewer::CompsoiteViewer the ViewerBase will point to the CompositeViewer that owns this View. */
  67. ViewerBase* getViewerBase() { return _viewerBase.get(); }
  68. /** Take all the settings, Camera and Slaves from the passed in view, leaving it empty. */
  69. virtual void take(osg::View& rhs);
  70. virtual void setStartTick(osg::Timer_t tick);
  71. osg::Timer_t getStartTick() const { return _startTick; }
  72. Scene* getScene() { return _scene.get(); }
  73. const Scene* getScene() const { return _scene.get(); }
  74. /** Set the scene graph that the View will use.*/
  75. virtual void setSceneData(osg::Node* node);
  76. template<class T> void setSceneData(const osg::ref_ptr<T>& node) { setSceneData(node.get()); }
  77. /** Get the View's scene graph.*/
  78. osg::Node* getSceneData() { return _scene.valid() ? _scene->getSceneData() : 0; }
  79. /** Get the const View's scene graph.*/
  80. const osg::Node* getSceneData() const { return _scene.valid() ? _scene->getSceneData() : 0; }
  81. /** Set the View's database pager.*/
  82. void setDatabasePager(osgDB::DatabasePager* dp);
  83. template<class T> void setDatabasePager(const osg::ref_ptr<T>& dp) { setDatabasePager(dp.get()); }
  84. /** Get the View's database pager.*/
  85. osgDB::DatabasePager* getDatabasePager();
  86. /** Get the const View's database pager.*/
  87. const osgDB::DatabasePager* getDatabasePager() const;
  88. /** Set the View's image pager.*/
  89. void setImagePager(osgDB::ImagePager* ip);
  90. template<class T> void setImagePager(const osg::ref_ptr<T>& ip) { setImagePager(ip.get()); }
  91. /** Get the View's image pager.*/
  92. osgDB::ImagePager* getImagePager();
  93. /** Get the const View's image pager.*/
  94. const osgDB::ImagePager* getImagePager() const;
  95. /** Add a Device.
  96. * The Device is polled on each new frame via it's Device::checkEvents() method and any events generated then collected via Device::getEventQueue()*/
  97. void addDevice(osgGA::Device* eventSource);
  98. template<class T> void addDevice(const osg::ref_ptr<T>& eventSource) { addDevice(eventSource.get()); }
  99. /** Remove a Device. */
  100. void removeDevice(osgGA::Device* eventSource);
  101. template<class T> void removeDevice(const osg::ref_ptr<T>& eventSource) { removeDevice(eventSource.get()); }
  102. typedef std::vector< osg::ref_ptr<osgGA::Device> > Devices;
  103. Devices& getDevices() { return _eventSources; }
  104. const Devices& getDevices() const { return _eventSources; }
  105. /* Set the EventQueue that the View uses to integrate external non window related events.*/
  106. void setEventQueue(osgGA::EventQueue* eventQueue) { _eventQueue = eventQueue; }
  107. template<class T> void setEventQueue(const osg::ref_ptr<T>& eventQueue) { setEventQueue(eventQueue.get()); }
  108. /* Get the View's EventQueue.*/
  109. osgGA::EventQueue* getEventQueue() { return _eventQueue.get(); }
  110. /* Get the const View's EventQueue.*/
  111. const osgGA::EventQueue* getEventQueue() const { return _eventQueue.get(); }
  112. /** Set the CameraManipulator that moves the View's master Camera position in response to events.
  113. * The parameter resetPosition determines whether manipulator is set to its home position.*/
  114. void setCameraManipulator(osgGA::CameraManipulator* manipulator, bool resetPosition = true);
  115. template<class T> void setCameraManipulator(const osg::ref_ptr<T>& manipulator, bool resetPosition = true) { setCameraManipulator(manipulator.get(), resetPosition); }
  116. /** Get the View's CameraManipulator.*/
  117. osgGA::CameraManipulator* getCameraManipulator() { return _cameraManipulator.get(); }
  118. /** Get the const View's CameraManipulator.*/
  119. const osgGA::CameraManipulator* getCameraManipulator() const { return _cameraManipulator.get(); }
  120. /** Set the view to the CameraManipulator's home position, if none is attached home() it does nothing.
  121. * Note, to set the home position use getCamaraManipulator()->setHomePosition(...). */
  122. void home();
  123. typedef std::list< osg::ref_ptr<osgGA::EventHandler> > EventHandlers;
  124. /** Add an EventHandler that adds handling of events to the View.*/
  125. void addEventHandler(osgGA::EventHandler* eventHandler);
  126. template<class T> void addEventHandler(const osg::ref_ptr<T>& eventHandler) { addEventHandler(eventHandler.get()); }
  127. /** Remove an EventHandler from View.*/
  128. void removeEventHandler(osgGA::EventHandler* eventHandler);
  129. template<class T> void removeEventHandler(const osg::ref_ptr<T>& eventHandler) { removeEventHandler(eventHandler.get()); }
  130. /** Get the View's list of EventHandlers.*/
  131. EventHandlers& getEventHandlers() { return _eventHandlers; }
  132. /** Get the const View's list of EventHandlers.*/
  133. const EventHandlers& getEventHandlers() const { return _eventHandlers; }
  134. /** Set the NodePath to any active CoordinateSystemNode present in the Scene.
  135. * The CoordinateSystemNode path is used to help applications and CamaraManipulators handle geocentric coordinates systems,
  136. * so that the local up direction is known at any position on the whole earth. */
  137. void setCoordinateSystemNodePath(const osg::NodePath& nodePath);
  138. /** Get the NodePath to any active CoordinateSystemNode present in the Scene.*/
  139. osg::NodePath getCoordinateSystemNodePath() const;
  140. /** Compute the NodePath to any active CoordinateSystemNode present in the Scene.*/
  141. void computeActiveCoordinateSystemNodePath();
  142. /** Set the DisplaySettings object associated with this view.*/
  143. void setDisplaySettings(osg::DisplaySettings* ds) { _displaySettings = ds; }
  144. template<class T> void setDisplaySettings(const osg::ref_ptr<T>& ds) { setDisplaySettings(ds.get()); }
  145. /** Set the DisplaySettings object associated with this view.*/
  146. osg::DisplaySettings* getDisplaySettings() { return _displaySettings.get(); }
  147. /** Set the DisplaySettings object associated with this view.*/
  148. const osg::DisplaySettings* getDisplaySettings() const { return _displaySettings.get(); }
  149. /** Set the FusionDistanceMode and Value. Note, only used when working in stereo.*/
  150. void setFusionDistance(osgUtil::SceneView::FusionDistanceMode mode,float value=1.0f)
  151. {
  152. _fusionDistanceMode = mode;
  153. _fusionDistanceValue = value;
  154. }
  155. /** Get the FusionDistanceMode.*/
  156. osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const { return _fusionDistanceMode; }
  157. /** Get the FusionDistanceValue. Note, only used for USE_FUSION_DISTANCE_VALUE & PROPORTIONAL_TO_SCREEN_DISTANCE modes.*/
  158. float getFusionDistanceValue() const { return _fusionDistanceValue; }
  159. /** Apply a viewer configuration to set up Cameras and Windowing. */
  160. void apply(ViewConfig* config);
  161. template<class T> void apply(const osg::ref_ptr<T>& config) { apply(config.get()); }
  162. ViewConfig* getLastAppliedViewConfig() { return _lastAppliedViewConfig.get(); }
  163. const ViewConfig* getLastAppliedViewConfig() const { return _lastAppliedViewConfig.get(); }
  164. /** deprecated, use view.apply(new osgViewer::AcrossAllScreens()). */
  165. void setUpViewAcrossAllScreens();
  166. /** deprecated, use view.apply(new osgViewer::SingleWindow(x,y,width,screenNum)). */
  167. void setUpViewInWindow(int x, int y, int width, int height, unsigned int screenNum=0);
  168. /** deprecated, use view.apply(new osgViewer::SingleScreen(screenNum)). */
  169. void setUpViewOnSingleScreen(unsigned int screenNum=0);
  170. /** deprecated, use view.apply(new osgViewer::SphericalDisplay(radius, collar, screenNum, intensityMap, projectorMatrix)). */
  171. void setUpViewFor3DSphericalDisplay(double radius=1.0, double collar=0.45, unsigned int screenNum=0, osg::Image* intensityMap=0, const osg::Matrixd& projectorMatrix = osg::Matrixd());
  172. /** deprecated, use view.apply(new osgViewer::PanoramicSphericalDisplay(radius, collar, screenNum, intensityMap, projectorMatrix)). */
  173. void setUpViewForPanoramicSphericalDisplay(double radius=1.0, double collar=0.45, unsigned int screenNum=0, osg::Image* intensityMap=0, const osg::Matrixd& projectorMatrix = osg::Matrixd());
  174. /** deprecated. use view.apply(new osgViewer::WoWVxDisplay(type (20 to 42), screenNum). */
  175. 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);
  176. /** Convenience method for setting up depth partitioning on the specified camera.*/
  177. bool setUpDepthPartitionForCamera(osg::Camera* cameraToPartition, DepthPartitionSettings* dps=0);
  178. /** Convenience method for setting up multiple slave cameras with depth partitioning on each of the view's active cameras.*/
  179. bool setUpDepthPartition(DepthPartitionSettings* dsp=0);
  180. /** Return true if this view contains a specified camera.*/
  181. bool containsCamera(const osg::Camera* camera) const;
  182. template<class T> bool containsCamera(const osg::ref_ptr<T>& camera) const { return containsCamera(camera.get()); }
  183. /** deprecated. */
  184. const osg::Camera* getCameraContainingPosition(float x, float y, float& local_x, float& local_y) const;
  185. /** deprecated. */
  186. bool computeIntersections(float x,float y, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask = 0xffffffff);
  187. /** deprecated. */
  188. bool computeIntersections(float x,float y, const osg::NodePath& nodePath, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask = 0xffffffff);
  189. /** Compute intersections of a ray, starting the current mouse position, through the specified camera. */
  190. bool computeIntersections(const osgGA::GUIEventAdapter& ea, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask = 0xffffffff);
  191. /** Compute intersections of a ray, starting the current mouse position, through the specified master camera's window/eye coordinates and a specified nodePath's subgraph. */
  192. bool computeIntersections(const osgGA::GUIEventAdapter& ea, const osg::NodePath& nodePath, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask = 0xffffffff);
  193. /** Compute intersections of a ray through the specified camera. */
  194. bool computeIntersections(const osg::Camera* camera, osgUtil::Intersector::CoordinateFrame cf, float x,float y, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask = 0xffffffff);
  195. /** Compute intersections of a ray through the specified camera and a specified nodePath's subgraph. */
  196. bool computeIntersections(const osg::Camera* camera, osgUtil::Intersector::CoordinateFrame cf, float x,float y, const osg::NodePath& nodePath, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask = 0xffffffff);
  197. virtual void requestRedraw();
  198. virtual void requestContinuousUpdate(bool needed=true);
  199. virtual void requestWarpPointer(float x,float y);
  200. /** Return true if there are pending updates to the scene graph that require an update. */
  201. virtual bool requiresUpdateSceneGraph() const;
  202. /** Return true if there are graphics operations that require a draw of the grpahics context. */
  203. virtual bool requiresRedraw() const;
  204. public:
  205. osg::Texture* createDistortionTexture(int width, int height);
  206. osg::Camera* assignRenderToTextureCamera(osg::GraphicsContext* gc, int width, int height, osg::Texture* texture);
  207. osg::Camera* assignKeystoneDistortionCamera(osg::DisplaySettings* ds, osg::GraphicsContext* gc, int x, int y, int width, int height, GLenum buffer, osg::Texture* texture, Keystone* keystone);
  208. osg::Camera* assignStereoCamera(osg::DisplaySettings* ds, osg::GraphicsContext* gc, int x, int y, int width, int height, GLenum buffer, double eyeScale);
  209. void assignStereoOrKeystoneToCamera(osg::Camera* camera, osg::DisplaySettings* ds);
  210. struct StereoSlaveCallback : public osg::View::Slave::UpdateSlaveCallback
  211. {
  212. StereoSlaveCallback(osg::DisplaySettings* ds, double eyeScale):_ds(ds), _eyeScale(eyeScale) {}
  213. virtual void updateSlave(osg::View& view, osg::View::Slave& slave);
  214. osg::ref_ptr<osg::DisplaySettings> _ds;
  215. double _eyeScale;
  216. };
  217. public:
  218. void assignSceneDataToCameras();
  219. void init();
  220. protected:
  221. friend class CompositeViewer;
  222. virtual ~View();
  223. virtual osg::GraphicsOperation* createRenderer(osg::Camera* camera);
  224. osg::observer_ptr<ViewerBase> _viewerBase;
  225. osg::Timer_t _startTick;
  226. Devices _eventSources;
  227. osg::ref_ptr<osgViewer::Scene> _scene;
  228. osg::ref_ptr<osgGA::EventQueue> _eventQueue;
  229. osg::ref_ptr<osgGA::CameraManipulator> _cameraManipulator;
  230. EventHandlers _eventHandlers;
  231. osg::ObserverNodePath _coordinateSystemNodePath;
  232. osg::ref_ptr<osg::DisplaySettings> _displaySettings;
  233. osgUtil::SceneView::FusionDistanceMode _fusionDistanceMode;
  234. float _fusionDistanceValue;
  235. osg::ref_ptr<ViewConfig> _lastAppliedViewConfig;
  236. };
  237. }
  238. #endif