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