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>
30 // For osgUtil::LineSegmentIntersector::Intersections, which is a typedef.
31 #include <osgUtil/LineSegmentIntersector>
49 /** A wrapper around osg::Camera that contains some extra information.
51 struct CameraInfo : public osg::Referenced
53 CameraInfo(unsigned flags_, osg::Camera* camera_ = 0)
54 : flags(flags_), camera(camera_), slaveIndex(-1), farSlaveIndex(-1),
55 x(0.0), y(0.0), width(0.0), height(0.0),
56 physicalWidth(0), physicalHeight(0), bezelHeightTop(0),
57 bezelHeightBottom(0), bezelWidthLeft(0), bezelWidthRight(0),
58 relativeCameraParent(~0u)
61 /** Update and resize cameras
64 /** The name as given in the config file.
67 /** Properties of the camera. @see CameraGroup::Flags.
72 osg::ref_ptr<osg::Camera> camera;
73 /** camera for rendering far field, if needed
75 osg::ref_ptr<osg::Camera> farCamera;
76 /** Index of this camera in the osgViewer::Viewer slave list.
79 /** index of far camera in slave list
82 /** Viewport parameters.
88 /** Physical size parameters.
91 double physicalHeight;
92 double bezelHeightTop;
93 double bezelHeightBottom;
94 double bezelWidthLeft;
95 double bezelWidthRight;
96 /** The parent camera for relative camera configurations.
98 unsigned relativeCameraParent;
99 /** The reference points in the parents projection space.
101 osg::Vec2d parentReference[2];
102 /** The reference points in the current projection space.
104 osg::Vec2d thisReference[2];
107 /** Update the OSG cameras from the camera info.
109 void updateCameras(const CameraInfo* info);
111 class CameraGroup : public osg::Referenced
114 /** properties of a camera.
118 VIEW_ABSOLUTE = 0x1, /**< The camera view is absolute, not
119 relative to the master camera. */
120 PROJECTION_ABSOLUTE = 0x2, /**< The projection is absolute. */
121 ORTHO = 0x4, /**< The projection is orthographic */
122 GUI = 0x8, /**< Camera draws the GUI. */
123 DO_INTERSECTION_TEST = 0x10,/**< scene intersection tests this
125 FIXED_NEAR_FAR = 0x20, /**< take the near far values in the
126 projection for real. */
127 ENABLE_MASTER_ZOOM = 0x40 /**< Can apply the zoom algorithm. */
129 /** Create a camera group associated with an osgViewer::Viewer.
130 * @param viewer the viewer
132 CameraGroup(osgViewer::Viewer* viewer);
133 /** Get the camera group's Viewer.
136 osgViewer::Viewer* getViewer() { return _viewer.get(); }
137 /** Create an osg::Camera from a property node and add it to the
139 * @param cameraNode the property node.
140 * @return a CameraInfo object for the camera.
142 CameraInfo* buildCamera(SGPropertyNode* cameraNode);
143 /** Create a camera from properties that will draw the GUI and add
144 * it to the camera group.
145 * @param cameraNode the property node. This can be 0, in which
146 * case a default GUI camera is created.
147 * @param window the GraphicsWindow to use for the GUI camera. If
148 * this is 0, the window is determined from the property node.
149 * @return a CameraInfo object for the GUI camera.
151 CameraInfo* buildGUICamera(SGPropertyNode* cameraNode,
152 GraphicsWindow* window = 0);
153 /** Update the view for the camera group.
154 * @param position the world position of the view
155 * @param orientation the world orientation of the view.
157 void update(const osg::Vec3d& position, const osg::Quat& orientation);
158 /** Set the parameters of the viewer's master camera. This won't
159 * affect cameras that have CameraFlags::PROJECTION_ABSOLUTE set.
160 * XXX Should znear and zfar be settable?
161 * @param vfov the vertical field of view angle
162 * @param aspectRatio the master camera's aspect ratio. This
163 * doesn't actually change the viewport, but should reflect the
166 void setCameraParameters(float vfov, float aspectRatio);
167 /** Set the default CameraGroup, which is the only one that
168 * matters at this time.
169 * @param group the group to set.
171 static void setDefault(CameraGroup* group) { _defaultGroup = group; }
172 /** Get the default CameraGroup.
173 * @return the default camera group.
175 static CameraGroup* getDefault() { return _defaultGroup.get(); }
176 typedef std::vector<osg::ref_ptr<CameraInfo> > CameraList;
177 typedef CameraList::iterator CameraIterator;
178 typedef CameraList::const_iterator ConstCameraIterator;
179 /** Get iterator for camera vector. The iterator's value is a ref_ptr.
181 CameraIterator camerasBegin() { return _cameras.begin(); }
182 /** Get iteator pointing to the end of the camera list.
184 CameraIterator camerasEnd() { return _cameras.end(); }
185 ConstCameraIterator camerasBegin() const { return _cameras.begin(); }
186 ConstCameraIterator camerasEnd() const { return _cameras.end(); }
187 void addCamera(CameraInfo* info) { _cameras.push_back(info); }
188 /** Build a complete CameraGroup from a property node.
189 * @param viewer the viewer associated with this camera group.
190 * @param the camera group property node.
192 static CameraGroup* buildCameraGroup(osgViewer::Viewer* viewer,
193 SGPropertyNode* node);
194 /** Set the cull mask on all non-GUI cameras
196 void setCameraCullMasks(osg::Node::NodeMask nm);
197 /** Update camera properties after a resize event.
201 void buildDistortionCamera(const SGPropertyNode* psNode,
202 osg::Camera* camera);
205 * get aspect ratio of master camera's viewport
207 double getMasterAspectRatio() const;
210 * find the GUI camera if one is defined
212 const CameraInfo* getGUICamera() const;
215 osg::ref_ptr<osgViewer::Viewer> _viewer;
216 static osg::ref_ptr<CameraGroup> _defaultGroup;
217 // Near, far for the master camera if used.
221 typedef std::map<std::string, osg::ref_ptr<osg::TextureRectangle> > TextureMap;
222 TextureMap _textureTargets;
229 class GUIEventAdapter;
234 /** Get the osg::Camera that draws the GUI, if any, from a camera
236 * @param cgroup the camera group
237 * @return the GUI camera or 0
239 osg::Camera* getGUICamera(CameraGroup* cgroup);
240 /** Choose a camera using an event and do intersection testing on its
241 * view of the scene. Only cameras with the DO_INTERSECTION_TEST flag
242 * set are considered.
243 * @param cgroup the CameraGroup
244 * @param ea the event containing a window and mouse coordinates
245 * @param intersections container for the result of intersection
247 * @return true if any intersections are found
249 bool computeIntersections(const CameraGroup* cgroup,
250 const osgGA::GUIEventAdapter* ea,
251 osgUtil::LineSegmentIntersector::Intersections&
253 /** Warp the pointer to coordinates in the GUI camera of a camera group.
254 * @param cgroup the camera group
255 * @param x x window coordinate of pointer
256 * @param y y window coordinate of pointer, in "y down" coordinates.
258 void warpGUIPointer(CameraGroup* cgroup, int x, int y);