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