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>
27 // For osgUtil::LineSegmentIntersector::Intersections, which is a typedef.
28 #include <osgUtil/LineSegmentIntersector>
46 /** A wrapper around osg::Camera that contains some extra information.
48 struct CameraInfo : public osg::Referenced
50 CameraInfo(unsigned flags_, osg::Camera* camera_)
51 : flags(flags_), camera(camera_), slaveIndex(-1)
54 /** Properties of the camera. @see CameraGroup::Flags.
59 osg::ref_ptr<osg::Camera> camera;
60 /** Index of this camera in the osgViewer::Viewer slave list.
65 class CameraGroup : public osg::Referenced
68 /** properties of a camera.
72 VIEW_ABSOLUTE = 0x1, /**< The camera view is absolute, not
73 relative to the master camera. */
74 PROJECTION_ABSOLUTE = 0x2, /**< The projection is absolute. */
75 ORTHO = 0x4, /**< The projection is orthographic */
76 GUI = 0x8, /**< Camera draws the GUI. */
77 DO_INTERSECTION_TEST = 0x10 /**< scene intersection tests this
80 /** Create a camera group associated with an osgViewer::Viewer.
81 * @param viewer the viewer
83 CameraGroup(osgViewer::Viewer* viewer);
84 /** Get the camera group's Viewer.
87 osgViewer::Viewer* getViewer() { return _viewer.get(); }
88 /** Add a camera to the group. The camera is added to the viewer
89 * as a slave. See osgViewer::Viewer::addSlave.
90 * @param flags properties of the camera; see CameraGroup::Flags
91 * @param projection slave projection matrix
92 * @param view slave view matrix
93 * @param useMasterSceneData whether the camera displays the
94 * viewer's scene data.
95 * @return a CameraInfo object for the camera.
97 CameraInfo* addCamera(unsigned flags, osg::Camera* camera,
98 const osg::Matrix& projection,
99 const osg::Matrix& view,
100 bool useMasterSceneData = true);
101 /** Create an osg::Camera from a property node and add it to the
103 * @param cameraNode the property node.
104 * @return a CameraInfo object for the camera.
106 CameraInfo* buildCamera(const SGPropertyNode* cameraNode);
107 /** Create a camera from properties that will draw the GUI and add
108 * it to the camera group.
109 * @param cameraNode the property node. This can be 0, in which
110 * case a default GUI camera is created.
111 * @param window the GraphicsWindow to use for the GUI camera. If
112 * this is 0, the window is determined from the property node.
113 * @return a CameraInfo object for the GUI camera.
115 CameraInfo* buildGUICamera(const SGPropertyNode* cameraNode,
116 GraphicsWindow* window = 0);
117 /** Update the view for the camera group.
118 * @param position the world position of the view
119 * @param orientation the world orientation of the view.
121 void update(const osg::Vec3d& position, const osg::Quat& orientation);
122 /** Set the parameters of the viewer's master camera. This won't
123 * affect cameras that have CameraFlags::PROJECTION_ABSOLUTE set.
124 * XXX Should znear and zfar be settable?
125 * @param vfov the vertical field of view angle
126 * @param aspectRatio the master camera's aspect ratio. This
127 * doesn't actually change the viewport, but should reflect the
130 void setCameraParameters(float vfov, float aspectRatio);
131 /** Set the default CameraGroup, which is the only one that
132 * matters at this time.
133 * @param group the group to set.
135 static void setDefault(CameraGroup* group) { _defaultGroup = group; }
136 /** Get the default CameraGroup.
137 * @return the default camera group.
139 static CameraGroup* getDefault() { return _defaultGroup.get(); }
140 typedef std::vector<osg::ref_ptr<CameraInfo> > CameraList;
141 typedef CameraList::iterator CameraIterator;
142 typedef CameraList::const_iterator ConstCameraIterator;
143 /** Get iterator for camera vector. The iterator's value is a ref_ptr.
145 CameraIterator camerasBegin() { return _cameras.begin(); }
146 /** Get iteator pointing to the end of the camera list.
148 CameraIterator camerasEnd() { return _cameras.end(); }
149 ConstCameraIterator camerasBegin() const { return _cameras.begin(); }
150 ConstCameraIterator camerasEnd() const { return _cameras.end(); }
151 /** Build a complete CameraGroup from a property node.
152 * @param viewer the viewer associated with this camera group.
153 * @param the camera group property node.
155 static CameraGroup* buildCameraGroup(osgViewer::Viewer* viewer,
156 const SGPropertyNode* node);
159 osg::ref_ptr<osgViewer::Viewer> _viewer;
160 static osg::ref_ptr<CameraGroup> _defaultGroup;
167 class GUIEventAdapter;
172 /** Get the osg::Camera that draws the GUI, if any, from a camera
174 * @param cgroup the camera group
175 * @return the GUI camera or 0
177 osg::Camera* getGUICamera(CameraGroup* cgroup);
178 /** Choose a camera using an event and do intersection testing on its
179 * view of the scene. Only cameras with the DO_INTERSECTION_TEST flag
180 * set are considered.
181 * @param cgroup the CameraGroup
182 * @param ea the event containing a window and mouse coordinates
183 * @param intersections container for the result of intersection
185 * @return true if any intersections are found
187 bool computeIntersections(const CameraGroup* cgroup,
188 const osgGA::GUIEventAdapter* ea,
189 osgUtil::LineSegmentIntersector::Intersections&
191 /** Warp the pointer to coordinates in the GUI camera of a camera group.
192 * @param cgroup the camera group
193 * @param x x window coordinate of pointer
194 * @param y y window coordinate of pointer, in "y down" coordinates.
196 void warpGUIPointer(CameraGroup* cgroup, int x, int y);