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
24 #include <osg/ref_ptr>
25 #include <osg/Referenced>
28 // For osgUtil::LineSegmentIntersector::Intersections, which is a typedef.
29 #include <osgUtil/LineSegmentIntersector>
47 /** A wrapper around osg::Camera that contains some extra information.
49 struct CameraInfo : public osg::Referenced
51 CameraInfo(unsigned flags_, osg::Camera* camera_ = 0)
52 : flags(flags_), camera(camera_), slaveIndex(-1), farSlaveIndex(-1),
53 x(0.0), y(0.0), width(0.0), height(0.0)
56 /** Properties of the camera. @see CameraGroup::Flags.
61 osg::ref_ptr<osg::Camera> camera;
62 /** camera for rendering far field, if needed
64 osg::ref_ptr<osg::Camera> farCamera;
65 /** Index of this camera in the osgViewer::Viewer slave list.
68 /** index of far camera in slave list
71 /** Viewport parameters.
79 class CameraGroup : public osg::Referenced
82 /** properties of a camera.
86 VIEW_ABSOLUTE = 0x1, /**< The camera view is absolute, not
87 relative to the master camera. */
88 PROJECTION_ABSOLUTE = 0x2, /**< The projection is absolute. */
89 ORTHO = 0x4, /**< The projection is orthographic */
90 GUI = 0x8, /**< Camera draws the GUI. */
91 DO_INTERSECTION_TEST = 0x10 /**< scene intersection tests this
94 /** Create a camera group associated with an osgViewer::Viewer.
95 * @param viewer the viewer
97 CameraGroup(osgViewer::Viewer* viewer);
98 /** Get the camera group's Viewer.
101 osgViewer::Viewer* getViewer() { return _viewer.get(); }
102 /** Add a camera to the group. The camera is added to the viewer
103 * as a slave. See osgViewer::Viewer::addSlave.
104 * @param flags properties of the camera; see CameraGroup::Flags
105 * @param projection slave projection matrix
106 * @param view slave view matrix
107 * @param useMasterSceneData whether the camera displays the
108 * viewer's scene data.
109 * @return a CameraInfo object for the camera.
111 CameraInfo* addCamera(unsigned flags, osg::Camera* camera,
112 const osg::Matrix& projection,
113 const osg::Matrix& view,
114 bool useMasterSceneData = true);
115 /** Create an osg::Camera from a property node and add it to the
117 * @param cameraNode the property node.
118 * @return a CameraInfo object for the camera.
120 CameraInfo* buildCamera(SGPropertyNode* cameraNode);
121 /** Create a camera from properties that will draw the GUI and add
122 * it to the camera group.
123 * @param cameraNode the property node. This can be 0, in which
124 * case a default GUI camera is created.
125 * @param window the GraphicsWindow to use for the GUI camera. If
126 * this is 0, the window is determined from the property node.
127 * @return a CameraInfo object for the GUI camera.
129 CameraInfo* buildGUICamera(SGPropertyNode* cameraNode,
130 GraphicsWindow* window = 0);
131 /** Update the view for the camera group.
132 * @param position the world position of the view
133 * @param orientation the world orientation of the view.
135 void update(const osg::Vec3d& position, const osg::Quat& orientation);
136 /** Set the parameters of the viewer's master camera. This won't
137 * affect cameras that have CameraFlags::PROJECTION_ABSOLUTE set.
138 * XXX Should znear and zfar be settable?
139 * @param vfov the vertical field of view angle
140 * @param aspectRatio the master camera's aspect ratio. This
141 * doesn't actually change the viewport, but should reflect the
144 void setCameraParameters(float vfov, float aspectRatio);
145 /** Set the default CameraGroup, which is the only one that
146 * matters at this time.
147 * @param group the group to set.
149 static void setDefault(CameraGroup* group) { _defaultGroup = group; }
150 /** Get the default CameraGroup.
151 * @return the default camera group.
153 static CameraGroup* getDefault() { return _defaultGroup.get(); }
154 typedef std::vector<osg::ref_ptr<CameraInfo> > CameraList;
155 typedef CameraList::iterator CameraIterator;
156 typedef CameraList::const_iterator ConstCameraIterator;
157 /** Get iterator for camera vector. The iterator's value is a ref_ptr.
159 CameraIterator camerasBegin() { return _cameras.begin(); }
160 /** Get iteator pointing to the end of the camera list.
162 CameraIterator camerasEnd() { return _cameras.end(); }
163 ConstCameraIterator camerasBegin() const { return _cameras.begin(); }
164 ConstCameraIterator camerasEnd() const { return _cameras.end(); }
165 /** Build a complete CameraGroup from a property node.
166 * @param viewer the viewer associated with this camera group.
167 * @param the camera group property node.
169 static CameraGroup* buildCameraGroup(osgViewer::Viewer* viewer,
170 SGPropertyNode* node);
171 /** Set the cull mask on all non-GUI cameras
173 void setCameraCullMasks(osg::Node::NodeMask nm);
177 osg::ref_ptr<osgViewer::Viewer> _viewer;
178 static osg::ref_ptr<CameraGroup> _defaultGroup;
185 class GUIEventAdapter;
190 /** Get the osg::Camera that draws the GUI, if any, from a camera
192 * @param cgroup the camera group
193 * @return the GUI camera or 0
195 osg::Camera* getGUICamera(CameraGroup* cgroup);
196 /** Choose a camera using an event and do intersection testing on its
197 * view of the scene. Only cameras with the DO_INTERSECTION_TEST flag
198 * set are considered.
199 * @param cgroup the CameraGroup
200 * @param ea the event containing a window and mouse coordinates
201 * @param intersections container for the result of intersection
203 * @return true if any intersections are found
205 bool computeIntersections(const CameraGroup* cgroup,
206 const osgGA::GUIEventAdapter* ea,
207 osgUtil::LineSegmentIntersector::Intersections&
209 /** Warp the pointer to coordinates in the GUI camera of a camera group.
210 * @param cgroup the camera group
211 * @param x x window coordinate of pointer
212 * @param y y window coordinate of pointer, in "y down" coordinates.
214 void warpGUIPointer(CameraGroup* cgroup, int x, int y);