1 // Copyright (C) 2008 Tim Moore
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License as
5 // published by the Free Software Foundation; either version 2 of the
6 // License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful, but
9 // WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 #ifndef CAMERAGROUP_HXX
18 #define CAMERAGROUP_HXX 1
25 #include <osg/ref_ptr>
26 #include <osg/Referenced>
28 #include <osg/TextureRectangle>
29 #include <osg/Texture2D>
30 #include <osgUtil/RenderBin>
32 // For osgUtil::LineSegmentIntersector::Intersections, which is a typedef.
33 #include <osgUtil/LineSegmentIntersector>
51 struct RenderBufferInfo {
60 RenderBufferInfo(osg::Texture2D* t = 0, float s = 1.0 ) : texture(t), scaleFactor(s) {}
61 osg::ref_ptr<osg::Texture2D> texture;
64 typedef std::map<RenderBufferInfo::Kind,RenderBufferInfo> RenderBufferMap;
65 typedef std::map<osg::Camera::BufferComponent,size_t> AttachmentMap;
67 struct RenderStageInfo {
68 RenderStageInfo(osg::Camera* camera_ = 0, int si = -1, bool fs = false)
69 : camera(camera_), slaveIndex(si), scaleFactor(1.0f), fullscreen(fs)
74 osg::ref_ptr<osg::Camera> camera;
75 AttachmentMap buffers;
95 typedef std::map<CameraKind,RenderStageInfo> CameraMap;
97 /** A wrapper around osg::Camera that contains some extra information.
99 struct CameraInfo : public osg::Referenced
101 CameraInfo(unsigned flags_)
103 x(0.0), y(0.0), width(0.0), height(0.0),
104 physicalWidth(0), physicalHeight(0), bezelHeightTop(0),
105 bezelHeightBottom(0), bezelWidthLeft(0), bezelWidthRight(0),
106 relativeCameraParent(~0u),
107 bufferSize( new osg::Uniform("fg_BufferSize", osg::Vec2f() ) ),
108 projInverse( new osg::Uniform( "fg_ProjectionMatrixInverse", osg::Matrixf() ) ),
109 viewInverse( new osg::Uniform( "fg_ViewMatrixInverse",osg::Matrixf() ) ),
110 view( new osg::Uniform( "fg_ViewMatrix",osg::Matrixf() ) ),
111 du( new osg::Uniform( "fg_du",osg::Vec4() ) ),
112 dv( new osg::Uniform( "fg_dv",osg::Vec4() ) )
116 /** Update and resize cameras
118 void updateCameras();
119 void resized(double w, double h);
120 /** The name as given in the config file.
123 /** Properties of the camera. @see CameraGroup::Flags.
126 /** the camera objects
129 void addCamera( CameraKind k, osg::Camera* c, int si = -1, bool fs = false ) { cameras[k].camera = c; cameras[k].slaveIndex = si; cameras[k].fullscreen = fs; }
130 void addCamera( CameraKind k, osg::Camera* c, bool fs ) { cameras[k].camera = c; cameras[k].fullscreen = fs; }
131 osg::Camera* getCamera(CameraKind k) const;
132 int getMainSlaveIndex() const;
133 RenderStageInfo& getRenderStageInfo( CameraKind k ) { return cameras[k]; }
135 /** the buffer objects
137 RenderBufferMap buffers;
138 void addBuffer(RenderBufferInfo::Kind k, osg::Texture2D* tex, float scale = 1.0 ) { buffers[k] = RenderBufferInfo(tex,scale); }
139 osg::Texture2D* getBuffer(RenderBufferInfo::Kind k) { return buffers[k].texture.get(); }
141 osg::ref_ptr<osg::Uniform> bufferSize;
142 //osg::ref_ptr<osg::Uniform> bloomOffset[2];
143 osg::ref_ptr<osg::Uniform> projInverse;
144 osg::ref_ptr<osg::Uniform> viewInverse;
145 osg::ref_ptr<osg::Uniform> du;
146 osg::ref_ptr<osg::Uniform> dv;
147 osg::ref_ptr<osg::Uniform> view;
149 void setMatrices( osg::Camera* c );
151 osgUtil::RenderBin::RenderBinList savedTransparentBins;
153 /** Viewport parameters.
159 /** Physical size parameters.
161 double physicalWidth;
162 double physicalHeight;
163 double bezelHeightTop;
164 double bezelHeightBottom;
165 double bezelWidthLeft;
166 double bezelWidthRight;
167 /** The parent camera for relative camera configurations.
169 unsigned relativeCameraParent;
170 /** The reference points in the parents projection space.
172 osg::Vec2d parentReference[2];
173 /** The reference points in the current projection space.
175 osg::Vec2d thisReference[2];
178 /** Update the OSG cameras from the camera info.
180 void updateCameras(const CameraInfo* info);
182 class CameraGroup : public osg::Referenced
185 /** properties of a camera.
189 VIEW_ABSOLUTE = 0x1, /**< The camera view is absolute, not
190 relative to the master camera. */
191 PROJECTION_ABSOLUTE = 0x2, /**< The projection is absolute. */
192 ORTHO = 0x4, /**< The projection is orthographic */
193 GUI = 0x8, /**< Camera draws the GUI. */
194 DO_INTERSECTION_TEST = 0x10,/**< scene intersection tests this
196 FIXED_NEAR_FAR = 0x20, /**< take the near far values in the
197 projection for real. */
198 ENABLE_MASTER_ZOOM = 0x40 /**< Can apply the zoom algorithm. */
200 /** Create a camera group associated with an osgViewer::Viewer.
201 * @param viewer the viewer
203 CameraGroup(osgViewer::Viewer* viewer);
204 /** Get the camera group's Viewer.
207 osgViewer::Viewer* getViewer() { return _viewer.get(); }
208 /** Create an osg::Camera from a property node and add it to the
210 * @param cameraNode the property node.
211 * @return a CameraInfo object for the camera.
213 CameraInfo* buildCamera(SGPropertyNode* cameraNode);
214 /** Create a camera from properties that will draw the GUI and add
215 * it to the camera group.
216 * @param cameraNode the property node. This can be 0, in which
217 * case a default GUI camera is created.
218 * @param window the GraphicsWindow to use for the GUI camera. If
219 * this is 0, the window is determined from the property node.
220 * @return a CameraInfo object for the GUI camera.
222 CameraInfo* buildGUICamera(SGPropertyNode* cameraNode,
223 GraphicsWindow* window = 0);
224 /** Update the view for the camera group.
225 * @param position the world position of the view
226 * @param orientation the world orientation of the view.
228 void update(const osg::Vec3d& position, const osg::Quat& orientation);
229 /** Set the parameters of the viewer's master camera. This won't
230 * affect cameras that have CameraFlags::PROJECTION_ABSOLUTE set.
231 * XXX Should znear and zfar be settable?
232 * @param vfov the vertical field of view angle
233 * @param aspectRatio the master camera's aspect ratio. This
234 * doesn't actually change the viewport, but should reflect the
237 void setCameraParameters(float vfov, float aspectRatio);
238 /** Set the default CameraGroup, which is the only one that
239 * matters at this time.
240 * @param group the group to set.
242 static void setDefault(CameraGroup* group) { _defaultGroup = group; }
243 /** Get the default CameraGroup.
244 * @return the default camera group.
246 static CameraGroup* getDefault() { return _defaultGroup.get(); }
247 typedef std::vector<osg::ref_ptr<CameraInfo> > CameraList;
248 typedef CameraList::iterator CameraIterator;
249 typedef CameraList::const_iterator ConstCameraIterator;
250 /** Get iterator for camera vector. The iterator's value is a ref_ptr.
252 CameraIterator camerasBegin() { return _cameras.begin(); }
253 /** Get iteator pointing to the end of the camera list.
255 CameraIterator camerasEnd() { return _cameras.end(); }
256 ConstCameraIterator camerasBegin() const { return _cameras.begin(); }
257 ConstCameraIterator camerasEnd() const { return _cameras.end(); }
258 void addCamera(CameraInfo* info) { _cameras.push_back(info); }
259 /** Build a complete CameraGroup from a property node.
260 * @param viewer the viewer associated with this camera group.
261 * @param the camera group property node.
263 static CameraGroup* buildCameraGroup(osgViewer::Viewer* viewer,
264 SGPropertyNode* node);
265 /** Set the cull mask on all non-GUI cameras
267 void setCameraCullMasks(osg::Node::NodeMask nm);
268 /** Update camera properties after a resize event.
272 void buildDistortionCamera(const SGPropertyNode* psNode,
273 osg::Camera* camera);
276 * get aspect ratio of master camera's viewport
278 double getMasterAspectRatio() const;
281 * find the GUI camera if one is defined
283 const CameraInfo* getGUICamera() const;
286 osg::ref_ptr<osgViewer::Viewer> _viewer;
287 static osg::ref_ptr<CameraGroup> _defaultGroup;
288 // Near, far for the master camera if used.
292 typedef std::map<std::string, osg::ref_ptr<osg::TextureRectangle> > TextureMap;
293 TextureMap _textureTargets;
300 class GUIEventAdapter;
305 /** Get the osg::Camera that draws the GUI, if any, from a camera
307 * @param cgroup the camera group
308 * @return the GUI camera or 0
310 osg::Camera* getGUICamera(CameraGroup* cgroup);
311 /** Choose a camera using an event and do intersection testing on its
312 * view of the scene. Only cameras with the DO_INTERSECTION_TEST flag
313 * set are considered.
314 * @param cgroup the CameraGroup
315 * @param ea the event containing a window and mouse coordinates
316 * @param intersections container for the result of intersection
318 * @return true if any intersections are found
320 bool computeIntersections(const CameraGroup* cgroup,
321 const osgGA::GUIEventAdapter* ea,
322 osgUtil::LineSegmentIntersector::Intersections&
324 /** Warp the pointer to coordinates in the GUI camera of a camera group.
325 * @param cgroup the camera group
326 * @param x x window coordinate of pointer
327 * @param y y window coordinate of pointer, in "y down" coordinates.
329 void warpGUIPointer(CameraGroup* cgroup, int x, int y);