openscenegraph
osg/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 OSG_VIEW
15#define OSG_VIEW 1
16
17#include <osg/Camera>
18#include <osg/Light>
19#include <osg/Stats>
20
21#include <OpenThreads/Mutex>
22
23namespace osg {
24
25/** View - maintains a master camera view and a list of slave cameras that are relative to this master camera.
26 * Note, if no slave cameras are attached to the view then the master camera does both the control and implementation of the rendering of the scene,
27 * but if slave cameras are present then the master controls the view onto the scene, while the slaves implement the rendering of the scene.
28*/
29class OSG_EXPORT View : public virtual osg::Object
30{
31 public :
32
33 View();
34
35 View(const osg::View& view, const osg::CopyOp& copyop=CopyOp::SHALLOW_COPY);
36
37 META_Object(osg,View);
38
39 /** Take all the settings, Camera and Slaves from the passed in view, leaving it empty. */
40 virtual void take(View& rhs);
41
42
43 /** Set the Stats object used to collect various frame related timing and scene graph stats.*/
44 void setStats(osg::Stats* stats) { _stats = stats; }
45
46 /** Get the Viewers Stats object.*/
47 osg::Stats* getStats() { return _stats.get(); }
48
49 /** Get the Viewers Stats object.*/
50 const osg::Stats* getStats() const { return _stats.get(); }
51
52
53 /** Options for controlling the global lighting used for the view.*/
54 enum LightingMode
55 {
56 NO_LIGHT,
57 HEADLIGHT,
58 SKY_LIGHT
59 };
60
61 /** Set the global lighting to use for this view.
62 * Defaults to headlight. */
63 void setLightingMode(LightingMode lightingMode);
64
65 /** Get the global lighting used for this view.*/
66 LightingMode getLightingMode() const { return _lightingMode; }
67
68 /** Get the global light.*/
69 void setLight(osg::Light* light) { _light = light; }
70
71 /** Get the global lighting if assigned.*/
72 osg::Light* getLight() { return _light.get(); }
73
74 /** Get the const global lighting if assigned.*/
75 const osg::Light* getLight() const { return _light.get(); }
76
77 /** Set the master camera of the view. */
78 void setCamera(osg::Camera* camera);
79
80 /** Get the master camera of the view. */
81 osg::Camera* getCamera() { return _camera.get(); }
82
83 /** Get the const master camera of the view. */
84 const osg::Camera* getCamera() const { return _camera.get(); }
85
86 /** Set the frame stamp of the view. */
87 void setFrameStamp(osg::FrameStamp* fs) { _frameStamp = fs; }
88
89 /** Get the frame stamp of the view. */
90 osg::FrameStamp* getFrameStamp() { return _frameStamp.get(); }
91
92 /** Get the frame stamp of the view. */
93 const osg::FrameStamp* getFrameStamp() const { return _frameStamp.get(); }
94
95
96 /** Slave allows one to up a camera that follows the master with a local offset to the project and view matrices.*/
97 struct OSG_EXPORT Slave
98 {
99 Slave(bool useMastersSceneData=true):
100 _useMastersSceneData(useMastersSceneData) {}
101
102 Slave(osg::Camera* camera, const osg::Matrixd& projectionOffset, const osg::Matrixd& viewOffset, bool useMastersSceneData=true):
103 _camera(camera),
104 _projectionOffset(projectionOffset),
105 _viewOffset(viewOffset),
106 _useMastersSceneData(useMastersSceneData) {}
107
108 Slave(const Slave& rhs) :
109 _camera(rhs._camera),
110 _projectionOffset(rhs._projectionOffset),
111 _viewOffset(rhs._viewOffset),
112 _useMastersSceneData(rhs._useMastersSceneData),
113 _updateSlaveCallback(rhs._updateSlaveCallback) {}
114
115 virtual ~Slave() {}
116
117 Slave& operator = (const Slave& rhs)
118 {
119 _camera = rhs._camera;
120 _projectionOffset = rhs._projectionOffset;
121 _viewOffset = rhs._viewOffset;
122 _useMastersSceneData = rhs._useMastersSceneData;
123 _updateSlaveCallback = rhs._updateSlaveCallback;
124 return *this;
125 }
126
127 struct UpdateSlaveCallback : public virtual Referenced
128 {
129 virtual void updateSlave(osg::View& view, osg::View::Slave& slave) = 0;
130 };
131
132 void updateSlave(View& view)
133 {
134 if (_updateSlaveCallback.valid()) _updateSlaveCallback->updateSlave(view, *this);
135 else updateSlaveImplementation(view);
136 }
137
138 virtual void updateSlaveImplementation(View& view);
139
140 osg::ref_ptr<osg::Camera> _camera;
141 osg::Matrixd _projectionOffset;
142 osg::Matrixd _viewOffset;
143 bool _useMastersSceneData;
144 osg::ref_ptr<UpdateSlaveCallback> _updateSlaveCallback;
145 };
146
147 bool addSlave(osg::Camera* camera, bool useMastersSceneData=true) { return addSlave(camera, osg::Matrix::identity(), osg::Matrix::identity(), useMastersSceneData); }
148
149 bool addSlave(osg::Camera* camera, const osg::Matrix& projectionOffset, const osg::Matrix& viewOffset, bool useMastersSceneData=true);
150
151 bool removeSlave(unsigned int pos);
152
153 unsigned int getNumSlaves() const { return static_cast<unsigned int>(_slaves.size()); }
154
155 Slave& getSlave(unsigned int pos) { return _slaves[pos]; }
156 const Slave& getSlave(unsigned int pos) const { return _slaves[pos]; }
157
158 unsigned int findSlaveIndexForCamera(osg::Camera* camera) const;
159
160 Slave * findSlaveForCamera(osg::Camera* camera);
161
162 void updateSlaves();
163
164 virtual void resizeGLObjectBuffers(unsigned int maxSize);
165 virtual void releaseGLObjects(osg::State* = 0) const;
166
167 protected :
168
169 virtual ~View();
170
171 virtual osg::GraphicsOperation* createRenderer(osg::Camera*) { return 0; }
172
173 osg::ref_ptr<osg::Stats> _stats;
174
175 LightingMode _lightingMode;
176 osg::ref_ptr<osg::Light> _light;
177
178 osg::ref_ptr<osg::Camera> _camera;
179
180 typedef std::vector<Slave> Slaves;
181 Slaves _slaves;
182
183 osg::ref_ptr<osg::FrameStamp> _frameStamp;
184};
185
186}
187
188#endif