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