]> git.mxchange.org Git - flightgear.git/blob - src/Main/CameraGroup.hxx
CameraGroup: adapt order of member vars to their init sequence.
[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 #include <osg/Texture2D>
30 #include <osgUtil/RenderBin>
31
32 // For osgUtil::LineSegmentIntersector::Intersections, which is a typedef.
33 #include <osgUtil/LineSegmentIntersector>
34 namespace osg
35 {
36 class Camera;
37 }
38
39 namespace osgViewer
40 {
41 class Viewer;
42 }
43
44 class SGPropertyNode;
45
46 namespace flightgear
47 {
48
49 class GraphicsWindow;
50
51 struct RenderBufferInfo {
52         enum Kind {
53                 DEPTH_BUFFER,
54                 NORMAL_BUFFER,
55                 DIFFUSE_BUFFER,
56                 SPEC_EMIS_BUFFER,
57                 LIGHTING_BUFFER
58         };
59
60         RenderBufferInfo(osg::Texture2D* t = 0, float s = 1.0 ) : texture(t), scaleFactor(s) {}
61         osg::ref_ptr<osg::Texture2D> texture;
62         float scaleFactor;
63 };
64 typedef std::map<RenderBufferInfo::Kind,RenderBufferInfo> RenderBufferMap;
65 typedef std::map<osg::Camera::BufferComponent,size_t> AttachmentMap;
66
67 struct RenderStageInfo {
68         RenderStageInfo(osg::Camera* camera_ = 0, int si = -1, bool fs = false)
69                 : camera(camera_), slaveIndex(si), scaleFactor(1.0f), fullscreen(fs)
70                 , resizable(true)
71         {
72         }
73
74         osg::ref_ptr<osg::Camera> camera;
75         AttachmentMap buffers;
76         int slaveIndex;
77         float scaleFactor;
78         bool fullscreen;
79         bool resizable;
80 };
81
82 enum CameraKind {
83         MAIN_CAMERA,
84         FAR_CAMERA,
85         GEOMETRY_CAMERA,
86         SHADOW_CAMERA,
87         BLOOM_CAMERA_1,
88         BLOOM_CAMERA_2,
89         AO_CAMERA_1,
90         AO_CAMERA_2,
91         AO_CAMERA_3,
92         LIGHTING_CAMERA,
93         DISPLAY_CAMERA
94 };
95 typedef std::map<CameraKind,RenderStageInfo> CameraMap;
96
97 /** A wrapper around osg::Camera that contains some extra information.
98  */
99 struct CameraInfo : public osg::Referenced
100 {
101     CameraInfo(unsigned flags_)
102         : flags(flags_),
103           x(0.0), y(0.0), width(0.0), height(0.0),
104           physicalWidth(0), physicalHeight(0), bezelHeightTop(0),
105           bezelHeightBottom(0), bezelWidthLeft(0), bezelWidthRight(0),
106           relativeCameraParent(~0u),
107           bufferSize( new osg::Uniform("fg_BufferSize", osg::Vec2f() ) ),
108           projInverse( new osg::Uniform( "fg_ProjectionMatrixInverse", osg::Matrixf() ) ),
109           viewInverse( new osg::Uniform( "fg_ViewMatrixInverse",osg::Matrixf() ) ),
110           view( new osg::Uniform( "fg_ViewMatrix",osg::Matrixf() ) ),
111           du( new osg::Uniform( "fg_du",osg::Vec4() ) ),
112           dv( new osg::Uniform( "fg_dv",osg::Vec4() ) )
113     {
114     }
115
116         /** Update and resize cameras
117          */
118         void updateCameras();
119         void resized(double w, double h);
120     /** The name as given in the config file.
121      */
122     std::string name;
123     /** Properties of the camera. @see CameraGroup::Flags.
124      */
125     unsigned flags;
126
127     /** Viewport parameters.
128      */
129     double x;
130     double y;
131     double width;
132     double height;
133     /** Physical size parameters.
134      */
135     double physicalWidth;
136     double physicalHeight;
137     double bezelHeightTop;
138     double bezelHeightBottom;
139     double bezelWidthLeft;
140     double bezelWidthRight;
141     /** The parent camera for relative camera configurations.
142      */
143     unsigned relativeCameraParent;
144
145     /** the camera objects
146      */
147         CameraMap cameras;
148         void addCamera( CameraKind k, osg::Camera* c, int si = -1, bool fs = false ) { cameras[k].camera = c; cameras[k].slaveIndex = si; cameras[k].fullscreen = fs; }
149         void addCamera( CameraKind k, osg::Camera* c, bool fs ) { cameras[k].camera = c; cameras[k].fullscreen = fs; }
150         osg::Camera* getCamera(CameraKind k) const;
151         int getMainSlaveIndex() const;
152         RenderStageInfo& getRenderStageInfo( CameraKind k ) { return cameras[k]; }
153
154         /** the buffer objects
155          */
156         RenderBufferMap buffers;
157         void addBuffer(RenderBufferInfo::Kind k, osg::Texture2D* tex, float scale = 1.0 ) { buffers[k] = RenderBufferInfo(tex,scale); }
158         osg::Texture2D* getBuffer(RenderBufferInfo::Kind k) { return buffers[k].texture.get(); }
159
160     osg::ref_ptr<osg::Uniform> bufferSize;
161     //osg::ref_ptr<osg::Uniform> bloomOffset[2];
162     osg::ref_ptr<osg::Uniform> projInverse;
163     osg::ref_ptr<osg::Uniform> viewInverse;
164     osg::ref_ptr<osg::Uniform> view;
165     osg::ref_ptr<osg::Uniform> du;
166     osg::ref_ptr<osg::Uniform> dv;
167
168         void setMatrices( osg::Camera* c );
169
170         osgUtil::RenderBin::RenderBinList savedTransparentBins;
171     /** The reference points in the parents projection space.
172      */
173     osg::Vec2d parentReference[2];
174     /** The reference points in the current projection space.
175      */
176     osg::Vec2d thisReference[2];
177 };
178
179 /** Update the OSG cameras from the camera info.
180  */
181 void updateCameras(const CameraInfo* info);
182
183 class CameraGroup : public osg::Referenced
184 {
185 public:
186     /** properties of a camera.
187      */
188     enum Flags
189     {
190         VIEW_ABSOLUTE = 0x1, /**< The camera view is absolute, not
191                                 relative to the master camera. */
192         PROJECTION_ABSOLUTE = 0x2, /**< The projection is absolute. */
193         ORTHO = 0x4,               /**< The projection is orthographic */
194         GUI = 0x8,                 /**< Camera draws the GUI. */
195         DO_INTERSECTION_TEST = 0x10,/**< scene intersection tests this
196                                        camera. */
197         FIXED_NEAR_FAR = 0x20,     /**< take the near far values in the
198                                       projection for real. */
199         ENABLE_MASTER_ZOOM = 0x40  /**< Can apply the zoom algorithm. */
200     };
201     /** Create a camera group associated with an osgViewer::Viewer.
202      * @param viewer the viewer
203      */
204     CameraGroup(osgViewer::Viewer* viewer);
205     /** Get the camera group's Viewer.
206      * @return the viewer
207      */
208     osgViewer::Viewer* getViewer() { return _viewer.get(); }
209     /** Create an osg::Camera from a property node and add it to the
210      * camera group.
211      * @param cameraNode the property node.
212      * @return a CameraInfo object for the camera.
213      */
214     CameraInfo* buildCamera(SGPropertyNode* cameraNode);
215     /** Create a camera from properties that will draw the GUI and add
216      * it to the camera group.
217      * @param cameraNode the property node. This can be 0, in which
218      * case a default GUI camera is created.
219      * @param window the GraphicsWindow to use for the GUI camera. If
220      * this is 0, the window is determined from the property node.
221      * @return a CameraInfo object for the GUI camera.
222      */
223     CameraInfo* buildGUICamera(SGPropertyNode* cameraNode,
224                                GraphicsWindow* window = 0);
225     /** Update the view for the camera group.
226      * @param position the world position of the view
227      * @param orientation the world orientation of the view.
228      */
229     void update(const osg::Vec3d& position, const osg::Quat& orientation);
230     /** Set the parameters of the viewer's master camera. This won't
231      * affect cameras that have CameraFlags::PROJECTION_ABSOLUTE set.
232      * XXX Should znear and zfar be settable?
233      * @param vfov the vertical field of view angle
234      * @param aspectRatio the master camera's aspect ratio. This
235      * doesn't actually change the viewport, but should reflect the
236      * current viewport.
237      */
238     void setCameraParameters(float vfov, float aspectRatio);
239     /** Set the default CameraGroup, which is the only one that
240      * matters at this time.
241      * @param group the group to set.
242      */
243     static void setDefault(CameraGroup* group) { _defaultGroup = group; }
244     /** Get the default CameraGroup.
245      * @return the default camera group.
246      */
247     static CameraGroup* getDefault() { return _defaultGroup.get(); }
248     typedef std::vector<osg::ref_ptr<CameraInfo> > CameraList;
249     typedef CameraList::iterator CameraIterator;
250     typedef CameraList::const_iterator ConstCameraIterator;
251     /** Get iterator for camera vector. The iterator's value is a ref_ptr.
252      */
253     CameraIterator camerasBegin() { return _cameras.begin(); }
254     /** Get iteator pointing to the end of the camera list.
255      */
256     CameraIterator camerasEnd() { return _cameras.end(); }
257     ConstCameraIterator camerasBegin() const { return _cameras.begin(); }
258     ConstCameraIterator camerasEnd() const { return _cameras.end(); }
259     void addCamera(CameraInfo* info) { _cameras.push_back(info); }
260     /** Build a complete CameraGroup from a property node.
261      * @param viewer the viewer associated with this camera group.
262      * @param the camera group property node.
263      */
264     static CameraGroup* buildCameraGroup(osgViewer::Viewer* viewer,
265                                          SGPropertyNode* node);
266     /** Set the cull mask on all non-GUI cameras
267      */
268     void setCameraCullMasks(osg::Node::NodeMask nm);
269     /** Update camera properties after a resize event.
270      */
271     void resized();
272
273     void buildDistortionCamera(const SGPropertyNode* psNode,
274                                osg::Camera* camera);
275   
276     /**
277      * get aspect ratio of master camera's viewport
278      */
279     double getMasterAspectRatio() const;
280   
281     /**
282      * find the GUI camera if one is defined 
283      */
284     const CameraInfo* getGUICamera() const;
285 protected:
286     CameraList _cameras;
287     osg::ref_ptr<osgViewer::Viewer> _viewer;
288     static osg::ref_ptr<CameraGroup> _defaultGroup;
289     // Near, far for the master camera if used.
290     float _zNear;
291     float _zFar;
292     float _nearField;
293     typedef std::map<std::string, osg::ref_ptr<osg::TextureRectangle> > TextureMap;
294     TextureMap _textureTargets;
295 };
296
297 }
298
299 namespace osgGA
300 {
301 class GUIEventAdapter;
302 }
303
304 namespace flightgear
305 {
306 /** Get the osg::Camera that draws the GUI, if any, from a camera
307  * group.
308  * @param cgroup the camera group
309  * @return the GUI camera or 0
310  */
311 osg::Camera* getGUICamera(CameraGroup* cgroup);
312 /** Choose a camera using an event and do intersection testing on its
313  * view of the scene. Only cameras with the DO_INTERSECTION_TEST flag
314  * set are considered.
315  * @param cgroup the CameraGroup
316  * @param ea the event containing a window and mouse coordinates
317  * @param intersections container for the result of intersection
318  * testing.
319  * @return true if any intersections are found
320  */
321 bool computeIntersections(const CameraGroup* cgroup,
322                           const osgGA::GUIEventAdapter* ea,
323                           osgUtil::LineSegmentIntersector::Intersections&
324                           intersections);
325 /** Warp the pointer to coordinates in the GUI camera of a camera group.
326  * @param cgroup the camera group
327  * @param x x window coordinate of pointer
328  * @param y y window coordinate of pointer, in "y down" coordinates.
329  */
330 void warpGUIPointer(CameraGroup* cgroup, int x, int y);
331 }
332 #endif