]> git.mxchange.org Git - flightgear.git/blob - src/Main/CameraGroup.hxx
ITM radio calculations are only considered valid
[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           physicalWidth(0), physicalHeight(0), bezelHeightTop(0),
57           bezelHeightBottom(0), bezelWidthLeft(0), bezelWidthRight(0),
58           relativeCameraParent(~0u)
59     {
60     }
61     /** The name as given in the config file.
62      */
63     std::string name;
64     /** Properties of the camera. @see CameraGroup::Flags.
65      */
66     unsigned flags;
67     /** the camera object
68      */
69     osg::ref_ptr<osg::Camera> camera;
70     /** camera for rendering far field, if needed
71      */
72     osg::ref_ptr<osg::Camera> farCamera;
73     /** Index of this camera in the osgViewer::Viewer slave list.
74      */
75     int slaveIndex;
76     /** index of far camera in slave list
77      */
78     int farSlaveIndex;
79     /** Viewport parameters.
80      */
81     double x;
82     double y;
83     double width;
84     double height;
85     /** Physical size parameters.
86      */
87     double physicalWidth;
88     double physicalHeight;
89     double bezelHeightTop;
90     double bezelHeightBottom;
91     double bezelWidthLeft;
92     double bezelWidthRight;
93     /** The parent camera for relative camera configurations.
94      */
95     unsigned relativeCameraParent;
96     /** The reference points in the parents projection space.
97      */
98     osg::Vec2d parentReference[2];
99     /** The reference points in the current projection space.
100      */
101     osg::Vec2d thisReference[2];
102 };
103
104 /** Update the OSG cameras from the camera info.
105  */
106 void updateCameras(const CameraInfo* info);
107
108 class CameraGroup : public osg::Referenced
109 {
110 public:
111     /** properties of a camera.
112      */
113     enum Flags
114     {
115         VIEW_ABSOLUTE = 0x1, /**< The camera view is absolute, not
116                                 relative to the master camera. */
117         PROJECTION_ABSOLUTE = 0x2, /**< The projection is absolute. */
118         ORTHO = 0x4,               /**< The projection is orthographic */
119         GUI = 0x8,                 /**< Camera draws the GUI. */
120         DO_INTERSECTION_TEST = 0x10,/**< scene intersection tests this
121                                        camera. */
122         FIXED_NEAR_FAR = 0x20,     /**< take the near far values in the
123                                       projection for real. */
124         ENABLE_MASTER_ZOOM = 0x40  /**< Can apply the zoom algorithm. */
125     };
126     /** Create a camera group associated with an osgViewer::Viewer.
127      * @param viewer the viewer
128      */
129     CameraGroup(osgViewer::Viewer* viewer);
130     /** Get the camera group's Viewer.
131      * @return the viewer
132      */
133     osgViewer::Viewer* getViewer() { return _viewer.get(); }
134     /** Add a camera to the group. The camera is added to the viewer
135      * as a slave. See osgViewer::Viewer::addSlave.
136      * @param flags properties of the camera; see CameraGroup::Flags
137      * @param projection slave projection matrix
138      * @param view slave view matrix
139      * @param useMasterSceneData whether the camera displays the
140      * viewer's scene data.
141      * @return a CameraInfo object for the camera.
142      */
143     CameraInfo* addCamera(unsigned flags, osg::Camera* camera,
144                           const osg::Matrix& projection,
145                           const osg::Matrix& view,
146                           bool useMasterSceneData = true);
147     /** Create an osg::Camera from a property node and add it to the
148      * camera group.
149      * @param cameraNode the property node.
150      * @return a CameraInfo object for the camera.
151      */
152     CameraInfo* buildCamera(SGPropertyNode* cameraNode);
153     /** Create a camera from properties that will draw the GUI and add
154      * it to the camera group.
155      * @param cameraNode the property node. This can be 0, in which
156      * case a default GUI camera is created.
157      * @param window the GraphicsWindow to use for the GUI camera. If
158      * this is 0, the window is determined from the property node.
159      * @return a CameraInfo object for the GUI camera.
160      */
161     CameraInfo* buildGUICamera(SGPropertyNode* cameraNode,
162                                GraphicsWindow* window = 0);
163     /** Update the view for the camera group.
164      * @param position the world position of the view
165      * @param orientation the world orientation of the view.
166      */
167     void update(const osg::Vec3d& position, const osg::Quat& orientation);
168     /** Set the parameters of the viewer's master camera. This won't
169      * affect cameras that have CameraFlags::PROJECTION_ABSOLUTE set.
170      * XXX Should znear and zfar be settable?
171      * @param vfov the vertical field of view angle
172      * @param aspectRatio the master camera's aspect ratio. This
173      * doesn't actually change the viewport, but should reflect the
174      * current viewport.
175      */
176     void setCameraParameters(float vfov, float aspectRatio);
177     /** Set the default CameraGroup, which is the only one that
178      * matters at this time.
179      * @param group the group to set.
180      */
181     static void setDefault(CameraGroup* group) { _defaultGroup = group; }
182     /** Get the default CameraGroup.
183      * @return the default camera group.
184      */
185     static CameraGroup* getDefault() { return _defaultGroup.get(); }
186     typedef std::vector<osg::ref_ptr<CameraInfo> > CameraList;
187     typedef CameraList::iterator CameraIterator;
188     typedef CameraList::const_iterator ConstCameraIterator;
189     /** Get iterator for camera vector. The iterator's value is a ref_ptr.
190      */
191     CameraIterator camerasBegin() { return _cameras.begin(); }
192     /** Get iteator pointing to the end of the camera list.
193      */
194     CameraIterator camerasEnd() { return _cameras.end(); }
195     ConstCameraIterator camerasBegin() const { return _cameras.begin(); }
196     ConstCameraIterator camerasEnd() const { return _cameras.end(); }
197     /** Build a complete CameraGroup from a property node.
198      * @param viewer the viewer associated with this camera group.
199      * @param the camera group property node.
200      */
201     static CameraGroup* buildCameraGroup(osgViewer::Viewer* viewer,
202                                          SGPropertyNode* node);
203     /** Set the cull mask on all non-GUI cameras
204      */
205     void setCameraCullMasks(osg::Node::NodeMask nm);
206     /** Update camera properties after a resize event.
207      */
208     void resized();
209
210     void buildDistortionCamera(const SGPropertyNode* psNode,
211                                osg::Camera* camera);
212   
213     /**
214      * get aspect ratio of master camera's viewport
215      */
216     double getMasterAspectRatio() const;
217 protected:
218     CameraList _cameras;
219     osg::ref_ptr<osgViewer::Viewer> _viewer;
220     static osg::ref_ptr<CameraGroup> _defaultGroup;
221     // Near, far for the master camera if used.
222     float _zNear;
223     float _zFar;
224     float _nearField;
225     typedef std::map<std::string, osg::ref_ptr<osg::TextureRectangle> > TextureMap;
226     TextureMap _textureTargets;
227 };
228
229 }
230
231 namespace osgGA
232 {
233 class GUIEventAdapter;
234 }
235
236 namespace flightgear
237 {
238 /** Get the osg::Camera that draws the GUI, if any, from a camera
239  * group.
240  * @param cgroup the camera group
241  * @return the GUI camera or 0
242  */
243 osg::Camera* getGUICamera(CameraGroup* cgroup);
244 /** Choose a camera using an event and do intersection testing on its
245  * view of the scene. Only cameras with the DO_INTERSECTION_TEST flag
246  * set are considered.
247  * @param cgroup the CameraGroup
248  * @param ea the event containing a window and mouse coordinates
249  * @param intersections container for the result of intersection
250  * testing.
251  * @return true if any intersections are found
252  */
253 bool computeIntersections(const CameraGroup* cgroup,
254                           const osgGA::GUIEventAdapter* ea,
255                           osgUtil::LineSegmentIntersector::Intersections&
256                           intersections);
257 /** Warp the pointer to coordinates in the GUI camera of a camera group.
258  * @param cgroup the camera group
259  * @param x x window coordinate of pointer
260  * @param y y window coordinate of pointer, in "y down" coordinates.
261  */
262 void warpGUIPointer(CameraGroup* cgroup, int x, int y);
263 }
264 #endif