openscenegraph
osgViewer/View
Go to the documentation of this file.
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#include <osgGA/Device>
28
29#include <osgViewer/Scene>
30#include <osgViewer/ViewerBase>
31#include <osgViewer/Keystone>
32
33namespace osgViewer {
34
35
36/** Base class for View configurations for setting up Camera and Windowing.*/
37class OSGVIEWER_EXPORT ViewConfig : public osg::Object
38{
39 public:
40
41 ViewConfig() {}
42
43 ViewConfig(const ViewConfig& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) : osg::Object(rhs,copyop) {}
44
45 META_Object(osgViewer,ViewConfig);
46
47 /** configure method that is overridden by Config subclasses.*/
48 virtual void configure(osgViewer::View& /*view*/) const {}
49
50 /** convenience method for getting the relevant display settings to use.*/
51 virtual osg::DisplaySettings* getActiveDisplaySetting(osgViewer::View& view) const;
52};
53
54
55struct OSGVIEWER_EXPORT DepthPartitionSettings : public osg::Referenced
56{
57 enum DepthMode
58 {
59 FIXED_RANGE,
60 BOUNDING_VOLUME
61 };
62
63 DepthPartitionSettings(DepthMode mode=BOUNDING_VOLUME);
64
65 virtual bool getDepthRange(osg::View& view, unsigned int partition, double& zNear, double& zFar);
66
67 DepthMode _mode;
68 double _zNear;
69 double _zMid;
70 double _zFar;
71};
72
73
74/** View holds a single view on a scene, this view may be composed of one or more slave cameras.*/
75class OSGVIEWER_EXPORT View : public osg::View, public osgGA::GUIActionAdapter
76{
77 public:
78
79 View();
80
81 View(const osgViewer::View& view, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
82
83 META_Object(osgViewer,View);
84
85 /** Provide a mechanism for getting the osg::View associated from the GUIActionAdapter.
86 * One would use this to case view to osgViewer::View(er) if supported by the subclass.*/
87 virtual osg::View* asView() { return this; }
88
89 /** Provide a mechanism for getting the viewer object from this osgViewer::View.
90 * In the case of a osgViewer::Viewer the ViewerBase will effectively point to this object as Viewer subclasses from View.
91 * In the case of a osgViewer::CompsoiteViewer the ViewerBase will point to the CompositeViewer that owns this View. */
92 ViewerBase* getViewerBase() { return _viewerBase.get(); }
93
94 /** Take all the settings, Camera and Slaves from the passed in view, leaving it empty. */
95 virtual void take(osg::View& rhs);
96
97 virtual void setStartTick(osg::Timer_t tick);
98 osg::Timer_t getStartTick() const { return _startTick; }
99
100 Scene* getScene() { return _scene.get(); }
101 const Scene* getScene() const { return _scene.get(); }
102
103 /** Set the scene graph that the View will use.*/
104 virtual void setSceneData(osg::Node* node);
105
106 template<class T> void setSceneData(const osg::ref_ptr<T>& node) { setSceneData(node.get()); }
107
108 /** Get the View's scene graph.*/
109 osg::Node* getSceneData() { return _scene.valid() ? _scene->getSceneData() : 0; }
110
111 /** Get the const View's scene graph.*/
112 const osg::Node* getSceneData() const { return _scene.valid() ? _scene->getSceneData() : 0; }
113
114
115 /** Set the View's database pager.*/
116 void setDatabasePager(osgDB::DatabasePager* dp);
117
118 template<class T> void setDatabasePager(const osg::ref_ptr<T>& dp) { setDatabasePager(dp.get()); }
119
120 /** Get the View's database pager.*/
121 osgDB::DatabasePager* getDatabasePager();
122
123 /** Get the const View's database pager.*/
124 const osgDB::DatabasePager* getDatabasePager() const;
125
126
127 /** Set the View's image pager.*/
128 void setImagePager(osgDB::ImagePager* ip);
129
130 template<class T> void setImagePager(const osg::ref_ptr<T>& ip) { setImagePager(ip.get()); }
131
132 /** Get the View's image pager.*/
133 osgDB::ImagePager* getImagePager();
134
135 /** Get the const View's image pager.*/
136 const osgDB::ImagePager* getImagePager() const;
137
138
139 /** Add a Device.
140 * The Device is polled on each new frame via it's Device::checkEvents() method and any events generated then collected via Device::getEventQueue()*/
141 void addDevice(osgGA::Device* eventSource);
142
143 template<class T> void addDevice(const osg::ref_ptr<T>& eventSource) { addDevice(eventSource.get()); }
144
145 /** Remove a Device. */
146 void removeDevice(osgGA::Device* eventSource);
147
148 template<class T> void removeDevice(const osg::ref_ptr<T>& eventSource) { removeDevice(eventSource.get()); }
149
150 typedef std::vector< osg::ref_ptr<osgGA::Device> > Devices;
151
152 Devices& getDevices() { return _eventSources; }
153 const Devices& getDevices() const { return _eventSources; }
154
155
156 /* Set the EventQueue that the View uses to integrate external non window related events.*/
157 void setEventQueue(osgGA::EventQueue* eventQueue) { _eventQueue = eventQueue; }
158
159 template<class T> void setEventQueue(const osg::ref_ptr<T>& eventQueue) { setEventQueue(eventQueue.get()); }
160
161 /* Get the View's EventQueue.*/
162 osgGA::EventQueue* getEventQueue() { return _eventQueue.get(); }
163
164 /* Get the const View's EventQueue.*/
165 const osgGA::EventQueue* getEventQueue() const { return _eventQueue.get(); }
166
167 /** Set the CameraManipulator that moves the View's master Camera position in response to events.
168 * The parameter resetPosition determines whether manipulator is set to its home position.*/
169 void setCameraManipulator(osgGA::CameraManipulator* manipulator, bool resetPosition = true);
170
171 template<class T> void setCameraManipulator(const osg::ref_ptr<T>& manipulator, bool resetPosition = true) { setCameraManipulator(manipulator.get(), resetPosition); }
172
173 /** Get the View's CameraManipulator.*/
174 osgGA::CameraManipulator* getCameraManipulator() { return _cameraManipulator.get(); }
175
176 /** Get the const View's CameraManipulator.*/
177 const osgGA::CameraManipulator* getCameraManipulator() const { return _cameraManipulator.get(); }
178
179 /** Set the view to the CameraManipulator's home position, if none is attached home() it does nothing.
180 * Note, to set the home position use getCamaraManipulator()->setHomePosition(...). */
181 void home();
182
183
184 typedef std::list< osg::ref_ptr<osgGA::EventHandler> > EventHandlers;
185
186 /** Add an EventHandler that adds handling of events to the View.*/
187 void addEventHandler(osgGA::EventHandler* eventHandler);
188
189 template<class T> void addEventHandler(const osg::ref_ptr<T>& eventHandler) { addEventHandler(eventHandler.get()); }
190
191 /** Remove an EventHandler from View.*/
192 void removeEventHandler(osgGA::EventHandler* eventHandler);
193
194 template<class T> void removeEventHandler(const osg::ref_ptr<T>& eventHandler) { removeEventHandler(eventHandler.get()); }
195
196 /** Get the View's list of EventHandlers.*/
197 EventHandlers& getEventHandlers() { return _eventHandlers; }
198
199 /** Get the const View's list of EventHandlers.*/
200 const EventHandlers& getEventHandlers() const { return _eventHandlers; }
201
202
203 /** Set the NodePath to any active CoordinateSystemNode present in the Scene.
204 * The CoordinateSystemNode path is used to help applications and CamaraManipulators handle geocentric coordinates systems,
205 * so that the local up direction is known at any position on the whole earth. */
206 void setCoordinateSystemNodePath(const osg::NodePath& nodePath);
207
208 /** Get the NodePath to any active CoordinateSystemNode present in the Scene.*/
209 osg::NodePath getCoordinateSystemNodePath() const;
210
211 /** Compute the NodePath to any active CoordinateSystemNode present in the Scene.*/
212 void computeActiveCoordinateSystemNodePath();
213
214
215 /** Set the DisplaySettings object associated with this view.*/
216 void setDisplaySettings(osg::DisplaySettings* ds) { _displaySettings = ds; }
217
218 template<class T> void setDisplaySettings(const osg::ref_ptr<T>& ds) { setDisplaySettings(ds.get()); }
219
220 /** Set the DisplaySettings object associated with this view.*/
221 osg::DisplaySettings* getDisplaySettings() { return _displaySettings.get(); }
222
223 /** Set the DisplaySettings object associated with this view.*/
224 const osg::DisplaySettings* getDisplaySettings() const { return _displaySettings.get(); }
225
226 /** Set the FusionDistanceMode and Value. Note, only used when working in stereo.*/
227 void setFusionDistance(osgUtil::SceneView::FusionDistanceMode mode,float value=1.0f)
228 {
229 _fusionDistanceMode = mode;
230 _fusionDistanceValue = value;
231 }
232
233 /** Get the FusionDistanceMode.*/
234 osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const { return _fusionDistanceMode; }
235
236 /** Get the FusionDistanceValue. Note, only used for USE_FUSION_DISTANCE_VALUE & PROPORTIONAL_TO_SCREEN_DISTANCE modes.*/
237 float getFusionDistanceValue() const { return _fusionDistanceValue; }
238
239
240 /** Apply a viewer configuration to set up Cameras and Windowing. */
241 void apply(ViewConfig* config);
242
243 template<class T> void apply(const osg::ref_ptr<T>& config) { apply(config.get()); }
244
245 ViewConfig* getLastAppliedViewConfig() { return _lastAppliedViewConfig.get(); }
246 const ViewConfig* getLastAppliedViewConfig() const { return _lastAppliedViewConfig.get(); }
247
248
249 /** deprecated, use view.apply(new osgViewer::AcrossAllScreens()). */
250 void setUpViewAcrossAllScreens();
251
252 /** deprecated, use view.apply(new osgViewer::SingleWindow(x,y,width,screenNum)). */
253 void setUpViewInWindow(int x, int y, int width, int height, unsigned int screenNum=0);
254
255 /** deprecated, use view.apply(new osgViewer::SingleScreen(screenNum)). */
256 void setUpViewOnSingleScreen(unsigned int screenNum=0);
257
258 /** deprecated, use view.apply(new osgViewer::SphericalDisplay(radius, collar, screenNum, intensityMap, projectorMatrix)). */
259 void setUpViewFor3DSphericalDisplay(double radius=1.0, double collar=0.45, unsigned int screenNum=0, osg::Image* intensityMap=0, const osg::Matrixd& projectorMatrix = osg::Matrixd());
260
261 /** deprecated, use view.apply(new osgViewer::PanoramicSphericalDisplay(radius, collar, screenNum, intensityMap, projectorMatrix)). */
262 void setUpViewForPanoramicSphericalDisplay(double radius=1.0, double collar=0.45, unsigned int screenNum=0, osg::Image* intensityMap=0, const osg::Matrixd& projectorMatrix = osg::Matrixd());
263
264 /** deprecated. use view.apply(new osgViewer::WoWVxDisplay(type (20 to 42), screenNum). */
265 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);
266
267
268
269 /** Convenience method for setting up depth partitioning on the specified camera.*/
270 bool setUpDepthPartitionForCamera(osg::Camera* cameraToPartition, DepthPartitionSettings* dps=0);
271
272 /** Convenience method for setting up multiple slave cameras with depth partitioning on each of the view's active cameras.*/
273 bool setUpDepthPartition(DepthPartitionSettings* dsp=0);
274
275
276 /** Return true if this view contains a specified camera.*/
277 bool containsCamera(const osg::Camera* camera) const;
278
279 template<class T> bool containsCamera(const osg::ref_ptr<T>& camera) const { return containsCamera(camera.get()); }
280
281 /** deprecated. */
282 const osg::Camera* getCameraContainingPosition(float x, float y, float& local_x, float& local_y) const;
283
284 /** deprecated. */
285 bool computeIntersections(float x,float y, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask = 0xffffffff);
286
287 /** deprecated. */
288 bool computeIntersections(float x,float y, const osg::NodePath& nodePath, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask = 0xffffffff);
289
290
291 /** Compute intersections of a ray, starting the current mouse position, through the specified camera. */
292 bool computeIntersections(const osgGA::GUIEventAdapter& ea, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask = 0xffffffff);
293
294 /** 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. */
295 bool computeIntersections(const osgGA::GUIEventAdapter& ea, const osg::NodePath& nodePath, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask = 0xffffffff);
296
297
298 /** Compute intersections of a ray through the specified camera. */
299 bool computeIntersections(const osg::Camera* camera, osgUtil::Intersector::CoordinateFrame cf, float x,float y, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask = 0xffffffff);
300
301 /** Compute intersections of a ray through the specified camera and a specified nodePath's subgraph. */
302 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);
303
304 virtual void requestRedraw();
305 virtual void requestContinuousUpdate(bool needed=true);
306 virtual void requestWarpPointer(float x,float y);
307
308 /** Return true if there are pending updates to the scene graph that require an update. */
309 virtual bool requiresUpdateSceneGraph() const;
310
311 /** Return true if there are graphics operations that require a draw of the grpahics context. */
312 virtual bool requiresRedraw() const;
313
314public:
315
316 osg::Texture* createDistortionTexture(int width, int height);
317 osg::Camera* assignRenderToTextureCamera(osg::GraphicsContext* gc, int width, int height, osg::Texture* texture);
318 osg::Camera* assignKeystoneDistortionCamera(osg::DisplaySettings* ds, osg::GraphicsContext* gc, int x, int y, int width, int height, GLenum buffer, osg::Texture* texture, Keystone* keystone);
319 osg::Camera* assignStereoCamera(osg::DisplaySettings* ds, osg::GraphicsContext* gc, int x, int y, int width, int height, GLenum buffer, double eyeScale);
320 void assignStereoOrKeystoneToCamera(osg::Camera* camera, osg::DisplaySettings* ds);
321
322 struct StereoSlaveCallback : public osg::View::Slave::UpdateSlaveCallback
323 {
324 StereoSlaveCallback(osg::DisplaySettings* ds, double eyeScale):_ds(ds), _eyeScale(eyeScale) {}
325
326 virtual void updateSlave(osg::View& view, osg::View::Slave& slave);
327
328 osg::ref_ptr<osg::DisplaySettings> _ds;
329 double _eyeScale;
330 };
331
332
333 public:
334
335 void assignSceneDataToCameras();
336 void init();
337
338 protected:
339
340 friend class CompositeViewer;
341
342 virtual ~View();
343
344 virtual osg::GraphicsOperation* createRenderer(osg::Camera* camera);
345
346 osg::observer_ptr<ViewerBase> _viewerBase;
347
348
349 osg::Timer_t _startTick;
350
351 Devices _eventSources;
352
353 osg::ref_ptr<osgViewer::Scene> _scene;
354 osg::ref_ptr<osgGA::EventQueue> _eventQueue;
355 osg::ref_ptr<osgGA::CameraManipulator> _cameraManipulator;
356 EventHandlers _eventHandlers;
357
358 osg::ObserverNodePath _coordinateSystemNodePath;
359
360 osg::ref_ptr<osg::DisplaySettings> _displaySettings;
361 osgUtil::SceneView::FusionDistanceMode _fusionDistanceMode;
362 float _fusionDistanceValue;
363
364 osg::ref_ptr<ViewConfig> _lastAppliedViewConfig;
365
366};
367
368}
369
370#endif