1 // Copyright (C) 2008 Tim Moore
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.
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.
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.
17 #ifndef CAMERAGROUP_HXX
18 #define CAMERAGROUP_HXX 1
25 #include <osg/ref_ptr>
26 #include <osg/Referenced>
28 #include <osg/TextureRectangle>
29 #include <osg/Texture2D>
31 #include <osgUtil/RenderBin>
33 // For osgUtil::LineSegmentIntersector::Intersections, which is a typedef.
34 #include <osgUtil/LineSegmentIntersector>
52 struct RenderBufferInfo {
53 RenderBufferInfo(osg::Texture2D* t = 0, float s = 1.0 ) : texture(t), scaleFactor(s) {}
54 osg::ref_ptr<osg::Texture2D> texture;
57 typedef std::map<std::string,RenderBufferInfo> RenderBufferMap;
58 typedef std::map<osg::Camera::BufferComponent,std::string> AttachmentMap;
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)
67 osg::ref_ptr<osg::Camera> camera;
68 AttachmentMap buffers;
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;
82 typedef std::map<std::string,RenderStageInfo> CameraMap;
84 /** A wrapper around osg::Camera that contains some extra information.
86 struct CameraInfo : public osg::Referenced
88 CameraInfo(unsigned 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 view( new osg::Uniform( "fg_ViewMatrix",osg::Matrixf() ) ),
98 du( new osg::Uniform( "fg_du",osg::Vec4() ) ),
99 dv( new osg::Uniform( "fg_dv",osg::Vec4() ) )
103 /** Update and resize cameras
105 void updateCameras();
106 void resized(double w, double h);
107 /** The name as given in the config file.
110 /** Properties of the camera. @see CameraGroup::Flags.
114 /** Viewport parameters.
120 /** Physical size parameters.
122 double physicalWidth;
123 double physicalHeight;
124 double bezelHeightTop;
125 double bezelHeightBottom;
126 double bezelWidthLeft;
127 double bezelWidthRight;
128 /** The parent camera for relative camera configurations.
130 unsigned relativeCameraParent;
132 /** the camera objects
135 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; }
136 void addCamera( const std::string& k, osg::Camera* c, bool fs ) { cameras[k].camera = c; cameras[k].fullscreen = fs; }
137 void addCamera( const std::string& k, osg::Camera* c, float s ) { cameras[k].camera = c; cameras[k].scaleFactor = s; }
138 osg::Camera* getCamera(const std::string& k) const;
139 int getMainSlaveIndex() const;
140 RenderStageInfo& getRenderStageInfo( const std::string& k ) { return cameras[k]; }
142 /** the buffer objects
144 RenderBufferMap buffers;
145 void addBuffer(const std::string& k, osg::Texture2D* tex, float scale = 1.0 ) { buffers[k] = RenderBufferInfo(tex,scale); }
146 osg::Texture2D* getBuffer(const std::string& k) const;
148 osg::ref_ptr<osg::TexGen> shadowTexGen[4];
150 osg::ref_ptr<osg::Uniform> bufferSize;
151 //osg::ref_ptr<osg::Uniform> bloomOffset[2];
152 osg::ref_ptr<osg::Uniform> projInverse;
153 osg::ref_ptr<osg::Uniform> viewInverse;
154 osg::ref_ptr<osg::Uniform> view;
155 osg::ref_ptr<osg::Uniform> du;
156 osg::ref_ptr<osg::Uniform> dv;
158 void setMatrices( osg::Camera* c );
160 osgUtil::RenderBin::RenderBinList savedTransparentBins;
161 /** The reference points in the parents projection space.
163 osg::Vec2d parentReference[2];
164 /** The reference points in the current projection space.
166 osg::Vec2d thisReference[2];
169 /** Update the OSG cameras from the camera info.
171 void updateCameras(const CameraInfo* info);
173 class CameraGroup : public osg::Referenced
176 /** properties of a camera.
180 VIEW_ABSOLUTE = 0x1, /**< The camera view is absolute, not
181 relative to the master camera. */
182 PROJECTION_ABSOLUTE = 0x2, /**< The projection is absolute. */
183 ORTHO = 0x4, /**< The projection is orthographic */
184 GUI = 0x8, /**< Camera draws the GUI. */
185 DO_INTERSECTION_TEST = 0x10,/**< scene intersection tests this
187 FIXED_NEAR_FAR = 0x20, /**< take the near far values in the
188 projection for real. */
189 ENABLE_MASTER_ZOOM = 0x40 /**< Can apply the zoom algorithm. */
191 /** Create a camera group associated with an osgViewer::Viewer.
192 * @param viewer the viewer
194 CameraGroup(osgViewer::Viewer* viewer);
195 /** Get the camera group's Viewer.
198 osgViewer::Viewer* getViewer() { return _viewer.get(); }
199 /** Create an osg::Camera from a property node and add it to the
201 * @param cameraNode the property node.
202 * @return a CameraInfo object for the camera.
204 CameraInfo* buildCamera(SGPropertyNode* cameraNode);
205 /** Create a camera from properties that will draw the GUI and add
206 * it to the camera group.
207 * @param cameraNode the property node. This can be 0, in which
208 * case a default GUI camera is created.
209 * @param window the GraphicsWindow to use for the GUI camera. If
210 * this is 0, the window is determined from the property node.
211 * @return a CameraInfo object for the GUI camera.
213 CameraInfo* buildGUICamera(SGPropertyNode* cameraNode,
214 GraphicsWindow* window = 0);
215 /** Update the view for the camera group.
216 * @param position the world position of the view
217 * @param orientation the world orientation of the view.
219 void update(const osg::Vec3d& position, const osg::Quat& orientation);
220 /** Set the parameters of the viewer's master camera. This won't
221 * affect cameras that have CameraFlags::PROJECTION_ABSOLUTE set.
222 * XXX Should znear and zfar be settable?
223 * @param vfov the vertical field of view angle
224 * @param aspectRatio the master camera's aspect ratio. This
225 * doesn't actually change the viewport, but should reflect the
228 void setCameraParameters(float vfov, float aspectRatio);
229 /** Set the default CameraGroup, which is the only one that
230 * matters at this time.
231 * @param group the group to set.
233 static void setDefault(CameraGroup* group) { _defaultGroup = group; }
234 /** Get the default CameraGroup.
235 * @return the default camera group.
237 static CameraGroup* getDefault() { return _defaultGroup.get(); }
238 typedef std::vector<osg::ref_ptr<CameraInfo> > CameraList;
239 typedef CameraList::iterator CameraIterator;
240 typedef CameraList::const_iterator ConstCameraIterator;
241 /** Get iterator for camera vector. The iterator's value is a ref_ptr.
243 CameraIterator camerasBegin() { return _cameras.begin(); }
244 /** Get iteator pointing to the end of the camera list.
246 CameraIterator camerasEnd() { return _cameras.end(); }
247 ConstCameraIterator camerasBegin() const { return _cameras.begin(); }
248 ConstCameraIterator camerasEnd() const { return _cameras.end(); }
249 void addCamera(CameraInfo* info) { _cameras.push_back(info); }
250 /** Build a complete CameraGroup from a property node.
251 * @param viewer the viewer associated with this camera group.
252 * @param the camera group property node.
254 static CameraGroup* buildCameraGroup(osgViewer::Viewer* viewer,
255 SGPropertyNode* node);
256 /** Set the cull mask on all non-GUI cameras
258 void setCameraCullMasks(osg::Node::NodeMask nm);
259 /** Update camera properties after a resize event.
263 void buildDistortionCamera(const SGPropertyNode* psNode,
264 osg::Camera* camera);
267 * get aspect ratio of master camera's viewport
269 double getMasterAspectRatio() const;
272 * find the GUI camera if one is defined
274 const CameraInfo* getGUICamera() const;
277 osg::ref_ptr<osgViewer::Viewer> _viewer;
278 static osg::ref_ptr<CameraGroup> _defaultGroup;
279 // Near, far for the master camera if used.
283 typedef std::map<std::string, osg::ref_ptr<osg::TextureRectangle> > TextureMap;
284 TextureMap _textureTargets;
291 class GUIEventAdapter;
296 /** Get the osg::Camera that draws the GUI, if any, from a camera
298 * @param cgroup the camera group
299 * @return the GUI camera or 0
301 osg::Camera* getGUICamera(CameraGroup* cgroup);
302 /** Choose a camera using an event and do intersection testing on its
303 * view of the scene. Only cameras with the DO_INTERSECTION_TEST flag
304 * set are considered.
305 * @param cgroup the CameraGroup
306 * @param ea the event containing a window and mouse coordinates
307 * @param intersections container for the result of intersection
309 * @return true if any intersections are found
311 bool computeIntersections(const CameraGroup* cgroup,
312 const osgGA::GUIEventAdapter* ea,
313 osgUtil::LineSegmentIntersector::Intersections&
315 /** Warp the pointer to coordinates in the GUI camera of a camera group.
316 * @param cgroup the camera group
317 * @param x x window coordinate of pointer
318 * @param y y window coordinate of pointer, in "y down" coordinates.
320 void warpGUIPointer(CameraGroup* cgroup, int x, int y);