]> git.mxchange.org Git - flightgear.git/blob - src/Main/CameraGroup.hxx
e76f3a5fa57493864ff4249cd43a07f5e55c27dd
[flightgear.git] / src / Main / CameraGroup.hxx
1 // Copyright (C) 2008  Tim Moore
2 //
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.
7 //
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.
12 //
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.
16
17 #ifndef CAMERAGROUP_HXX
18 #define CAMERAGROUP_HXX 1
19
20 #include <string>
21 #include <vector>
22
23 #include <osg/Matrix>
24 #include <osg/ref_ptr>
25 #include <osg/Referenced>
26
27 // For osgUtil::LineSegmentIntersector::Intersections, which is a typedef.
28 #include <osgUtil/LineSegmentIntersector>
29 namespace osg
30 {
31 class Camera;
32 }
33
34 namespace osgViewer
35 {
36 class Viewer;
37 }
38
39 class SGPropertyNode;
40
41 namespace flightgear
42 {
43
44 class GraphicsWindow;
45
46 /** A wrapper around osg::Camera that contains some extra information.
47  */
48 struct CameraInfo : public osg::Referenced
49 {
50     CameraInfo(unsigned flags_, osg::Camera* camera_)
51         : flags(flags_), camera(camera_), slaveIndex(-1)
52     {
53     }
54     /** Properties of the camera. @see CameraGroup::Flags.
55      */
56     unsigned flags;
57     /** the camera object
58      */
59     osg::ref_ptr<osg::Camera> camera;
60     /** Index of this camera in the osgViewer::Viewer slave list.
61      */
62     int slaveIndex;
63 };
64
65 class CameraGroup : public osg::Referenced
66 {
67 public:
68     /** properties of a camera.
69      */
70     enum Flags
71     {
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
78                                        camera. */
79     };
80     /** Create a camera group associated with an osgViewer::Viewer.
81      * @param viewer the viewer
82      */
83     CameraGroup(osgViewer::Viewer* viewer);
84     /** Get the camera group's Viewer.
85      * @return the viewer
86      */
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.
96      */
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
102      * camera group.
103      * @param cameraNode the property node.
104      * @return a CameraInfo object for the camera.
105      */
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.
114      */
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.
120      */
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
128      * current viewport.
129      */
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.
134      */
135     static void setDefault(CameraGroup* group) { _defaultGroup = group; }
136     /** Get the default CameraGroup.
137      * @return the default camera group.
138      */
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.
144      */
145     CameraIterator camerasBegin() { return _cameras.begin(); }
146     /** Get iteator pointing to the end of the camera list.
147      */
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.
154      */
155     static CameraGroup* buildCameraGroup(osgViewer::Viewer* viewer,
156                                          const SGPropertyNode* node);
157 protected:
158     CameraList _cameras;
159     osg::ref_ptr<osgViewer::Viewer> _viewer;
160     static osg::ref_ptr<CameraGroup> _defaultGroup;
161 };
162
163 }
164
165 namespace osgGA
166 {
167 class GUIEventAdapter;
168 }
169
170 namespace flightgear
171 {
172 /** Get the osg::Camera that draws the GUI, if any, from a camera
173  * group.
174  * @param cgroup the camera group
175  * @return the GUI camera or 0
176  */
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
184  * testing.
185  * @return true if any intersections are found
186  */
187 bool computeIntersections(const CameraGroup* cgroup,
188                           const osgGA::GUIEventAdapter* ea,
189                           osgUtil::LineSegmentIntersector::Intersections&
190                           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.
195  */
196 void warpGUIPointer(CameraGroup* cgroup, int x, int y);
197 }
198 #endif