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