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