1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
21#include <OpenThreads/Mutex>
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.
29class OSG_EXPORT View : public virtual osg::Object
35 View(const osg::View& view, const osg::CopyOp& copyop=CopyOp::SHALLOW_COPY);
37 META_Object(osg,View);
39 /** Take all the settings, Camera and Slaves from the passed in view, leaving it empty. */
40 virtual void take(View& rhs);
43 /** Set the Stats object used to collect various frame related timing and scene graph stats.*/
44 void setStats(osg::Stats* stats) { _stats = stats; }
46 /** Get the Viewers Stats object.*/
47 osg::Stats* getStats() { return _stats.get(); }
49 /** Get the Viewers Stats object.*/
50 const osg::Stats* getStats() const { return _stats.get(); }
53 /** Options for controlling the global lighting used for the view.*/
61 /** Set the global lighting to use for this view.
62 * Defaults to headlight. */
63 void setLightingMode(LightingMode lightingMode);
65 /** Get the global lighting used for this view.*/
66 LightingMode getLightingMode() const { return _lightingMode; }
68 /** Get the global light.*/
69 void setLight(osg::Light* light) { _light = light; }
71 /** Get the global lighting if assigned.*/
72 osg::Light* getLight() { return _light.get(); }
74 /** Get the const global lighting if assigned.*/
75 const osg::Light* getLight() const { return _light.get(); }
77 /** Set the master camera of the view. */
78 void setCamera(osg::Camera* camera);
80 /** Get the master camera of the view. */
81 osg::Camera* getCamera() { return _camera.get(); }
83 /** Get the const master camera of the view. */
84 const osg::Camera* getCamera() const { return _camera.get(); }
86 /** Set the frame stamp of the view. */
87 void setFrameStamp(osg::FrameStamp* fs) { _frameStamp = fs; }
89 /** Get the frame stamp of the view. */
90 osg::FrameStamp* getFrameStamp() { return _frameStamp.get(); }
92 /** Get the frame stamp of the view. */
93 const osg::FrameStamp* getFrameStamp() const { return _frameStamp.get(); }
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
99 Slave(bool useMastersSceneData=true):
100 _useMastersSceneData(useMastersSceneData) {}
102 Slave(osg::Camera* camera, const osg::Matrixd& projectionOffset, const osg::Matrixd& viewOffset, bool useMastersSceneData=true):
104 _projectionOffset(projectionOffset),
105 _viewOffset(viewOffset),
106 _useMastersSceneData(useMastersSceneData) {}
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) {}
117 Slave& operator = (const Slave& rhs)
119 _camera = rhs._camera;
120 _projectionOffset = rhs._projectionOffset;
121 _viewOffset = rhs._viewOffset;
122 _useMastersSceneData = rhs._useMastersSceneData;
123 _updateSlaveCallback = rhs._updateSlaveCallback;
127 struct UpdateSlaveCallback : public virtual Referenced
129 virtual void updateSlave(osg::View& view, osg::View::Slave& slave) = 0;
132 void updateSlave(View& view)
134 if (_updateSlaveCallback.valid()) _updateSlaveCallback->updateSlave(view, *this);
135 else updateSlaveImplementation(view);
138 virtual void updateSlaveImplementation(View& view);
140 osg::ref_ptr<osg::Camera> _camera;
141 osg::Matrixd _projectionOffset;
142 osg::Matrixd _viewOffset;
143 bool _useMastersSceneData;
144 osg::ref_ptr<UpdateSlaveCallback> _updateSlaveCallback;
147 bool addSlave(osg::Camera* camera, bool useMastersSceneData=true) { return addSlave(camera, osg::Matrix::identity(), osg::Matrix::identity(), useMastersSceneData); }
149 bool addSlave(osg::Camera* camera, const osg::Matrix& projectionOffset, const osg::Matrix& viewOffset, bool useMastersSceneData=true);
151 bool removeSlave(unsigned int pos);
153 unsigned int getNumSlaves() const { return static_cast<unsigned int>(_slaves.size()); }
155 Slave& getSlave(unsigned int pos) { return _slaves[pos]; }
156 const Slave& getSlave(unsigned int pos) const { return _slaves[pos]; }
158 unsigned int findSlaveIndexForCamera(osg::Camera* camera) const;
160 Slave * findSlaveForCamera(osg::Camera* camera);
164 virtual void resizeGLObjectBuffers(unsigned int maxSize);
165 virtual void releaseGLObjects(osg::State* = 0) const;
171 virtual osg::GraphicsOperation* createRenderer(osg::Camera*) { return 0; }
173 osg::ref_ptr<osg::Stats> _stats;
175 LightingMode _lightingMode;
176 osg::ref_ptr<osg::Light> _light;
178 osg::ref_ptr<osg::Camera> _camera;
180 typedef std::vector<Slave> Slaves;
183 osg::ref_ptr<osg::FrameStamp> _frameStamp;