]> git.mxchange.org Git - flightgear.git/blob - src/Main/CameraGroup.hxx
Continuous replay: use correct replay duration
[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 <map>
21 #include <string>
22 #include <vector>
23
24 #include <osg/Matrix>
25 #include <osg/ref_ptr>
26 #include <osg/Referenced>
27 #include <osg/Node>
28 #include <osg/TextureRectangle>
29
30 // For osgUtil::LineSegmentIntersector::Intersections, which is a typedef.
31 #include <osgUtil/LineSegmentIntersector>
32 namespace osg
33 {
34 class Camera;
35 }
36
37 namespace osgViewer
38 {
39 class Viewer;
40 }
41
42 class SGPropertyNode;
43
44 namespace flightgear
45 {
46
47 class GraphicsWindow;
48
49 /** A wrapper around osg::Camera that contains some extra information.
50  */
51 struct CameraInfo : public osg::Referenced
52 {
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     {
57     }
58     /** Properties of the camera. @see CameraGroup::Flags.
59      */
60     unsigned flags;
61     /** the camera object
62      */
63     osg::ref_ptr<osg::Camera> camera;
64     /** camera for rendering far field, if needed
65      */
66     osg::ref_ptr<osg::Camera> farCamera;
67     /** Index of this camera in the osgViewer::Viewer slave list.
68      */
69     int slaveIndex;
70     /** index of far camera in slave list
71      */
72     int farSlaveIndex;
73     /** Viewport parameters.
74      */
75     double x;
76     double y;
77     double width;
78     double height;
79 };
80
81 /** Update the OSG cameras from the camera info.
82  */
83 void updateCameras(const CameraInfo* info);
84
85 class CameraGroup : public osg::Referenced
86 {
87 public:
88     /** properties of a camera.
89      */
90     enum Flags
91     {
92         VIEW_ABSOLUTE = 0x1, /**< The camera view is absolute, not
93                                 relative to the master camera. */
94         PROJECTION_ABSOLUTE = 0x2, /**< The projection is absolute. */
95         ORTHO = 0x4,               /**< The projection is orthographic */
96         GUI = 0x8,                 /**< Camera draws the GUI. */
97         DO_INTERSECTION_TEST = 0x10 /**< scene intersection tests this
98                                        camera. */
99     };
100     /** Create a camera group associated with an osgViewer::Viewer.
101      * @param viewer the viewer
102      */
103     CameraGroup(osgViewer::Viewer* viewer);
104     /** Get the camera group's Viewer.
105      * @return the viewer
106      */
107     osgViewer::Viewer* getViewer() { return _viewer.get(); }
108     /** Add a camera to the group. The camera is added to the viewer
109      * as a slave. See osgViewer::Viewer::addSlave.
110      * @param flags properties of the camera; see CameraGroup::Flags
111      * @param projection slave projection matrix
112      * @param view slave view matrix
113      * @param useMasterSceneData whether the camera displays the
114      * viewer's scene data.
115      * @return a CameraInfo object for the camera.
116      */
117     CameraInfo* addCamera(unsigned flags, osg::Camera* camera,
118                           const osg::Matrix& projection,
119                           const osg::Matrix& view,
120                           bool useMasterSceneData = true);
121     /** Create an osg::Camera from a property node and add it to the
122      * camera group.
123      * @param cameraNode the property node.
124      * @return a CameraInfo object for the camera.
125      */
126     CameraInfo* buildCamera(SGPropertyNode* cameraNode);
127     /** Create a camera from properties that will draw the GUI and add
128      * it to the camera group.
129      * @param cameraNode the property node. This can be 0, in which
130      * case a default GUI camera is created.
131      * @param window the GraphicsWindow to use for the GUI camera. If
132      * this is 0, the window is determined from the property node.
133      * @return a CameraInfo object for the GUI camera.
134      */
135     CameraInfo* buildGUICamera(SGPropertyNode* cameraNode,
136                                GraphicsWindow* window = 0);
137     /** Update the view for the camera group.
138      * @param position the world position of the view
139      * @param orientation the world orientation of the view.
140      */
141     void update(const osg::Vec3d& position, const osg::Quat& orientation);
142     /** Set the parameters of the viewer's master camera. This won't
143      * affect cameras that have CameraFlags::PROJECTION_ABSOLUTE set.
144      * XXX Should znear and zfar be settable?
145      * @param vfov the vertical field of view angle
146      * @param aspectRatio the master camera's aspect ratio. This
147      * doesn't actually change the viewport, but should reflect the
148      * current viewport.
149      */
150     void setCameraParameters(float vfov, float aspectRatio);
151     /** Set the default CameraGroup, which is the only one that
152      * matters at this time.
153      * @param group the group to set.
154      */
155     static void setDefault(CameraGroup* group) { _defaultGroup = group; }
156     /** Get the default CameraGroup.
157      * @return the default camera group.
158      */
159     static CameraGroup* getDefault() { return _defaultGroup.get(); }
160     typedef std::vector<osg::ref_ptr<CameraInfo> > CameraList;
161     typedef CameraList::iterator CameraIterator;
162     typedef CameraList::const_iterator ConstCameraIterator;
163     /** Get iterator for camera vector. The iterator's value is a ref_ptr.
164      */
165     CameraIterator camerasBegin() { return _cameras.begin(); }
166     /** Get iteator pointing to the end of the camera list.
167      */
168     CameraIterator camerasEnd() { return _cameras.end(); }
169     ConstCameraIterator camerasBegin() const { return _cameras.begin(); }
170     ConstCameraIterator camerasEnd() const { return _cameras.end(); }
171     /** Build a complete CameraGroup from a property node.
172      * @param viewer the viewer associated with this camera group.
173      * @param the camera group property node.
174      */
175     static CameraGroup* buildCameraGroup(osgViewer::Viewer* viewer,
176                                          SGPropertyNode* node);
177     /** Set the cull mask on all non-GUI cameras
178      */
179     void setCameraCullMasks(osg::Node::NodeMask nm);
180     /** Update camera properties after a resize event.
181      */
182     void resized();
183
184     void buildDistortionCamera(const SGPropertyNode* psNode,
185                                osg::Camera* camera);
186 protected:
187     CameraList _cameras;
188     osg::ref_ptr<osgViewer::Viewer> _viewer;
189     static osg::ref_ptr<CameraGroup> _defaultGroup;
190     // Near, far for the master camera if used.
191     float _zNear;
192     float _zFar;
193     float _nearField;
194     typedef std::map<std::string, osg::ref_ptr<osg::TextureRectangle> > TextureMap;
195     TextureMap _textureTargets;
196 };
197
198 }
199
200 namespace osgGA
201 {
202 class GUIEventAdapter;
203 }
204
205 namespace flightgear
206 {
207 /** Get the osg::Camera that draws the GUI, if any, from a camera
208  * group.
209  * @param cgroup the camera group
210  * @return the GUI camera or 0
211  */
212 osg::Camera* getGUICamera(CameraGroup* cgroup);
213 /** Choose a camera using an event and do intersection testing on its
214  * view of the scene. Only cameras with the DO_INTERSECTION_TEST flag
215  * set are considered.
216  * @param cgroup the CameraGroup
217  * @param ea the event containing a window and mouse coordinates
218  * @param intersections container for the result of intersection
219  * testing.
220  * @return true if any intersections are found
221  */
222 bool computeIntersections(const CameraGroup* cgroup,
223                           const osgGA::GUIEventAdapter* ea,
224                           osgUtil::LineSegmentIntersector::Intersections&
225                           intersections);
226 /** Warp the pointer to coordinates in the GUI camera of a camera group.
227  * @param cgroup the camera group
228  * @param x x window coordinate of pointer
229  * @param y y window coordinate of pointer, in "y down" coordinates.
230  */
231 void warpGUIPointer(CameraGroup* cgroup, int x, int y);
232 }
233 #endif