| 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 | #include <memory> |
| 24 | |
| 25 | #include <osg/Matrix> |
| 26 | #include <osg/ref_ptr> |
| 27 | #include <osg/Referenced> |
| 28 | #include <osg/Node> |
| 29 | #include <osg/TextureRectangle> |
| 30 | #include <osg/Texture2D> |
| 31 | #include <osg/TexGen> |
| 32 | #include <osgUtil/RenderBin> |
| 33 | #include <osgViewer/View> |
| 34 | |
| 35 | #include <simgear/scene/viewer/Compositor.hxx> |
| 36 | |
| 37 | namespace osg |
| 38 | { |
| 39 | class Camera; |
| 40 | } |
| 41 | |
| 42 | namespace osgViewer |
| 43 | { |
| 44 | class Viewer; |
| 45 | } |
| 46 | |
| 47 | class SGPropertyNode; |
| 48 | |
| 49 | namespace flightgear |
| 50 | { |
| 51 | |
| 52 | class CameraGroupListener; |
| 53 | class GraphicsWindow; |
| 54 | class CameraGroup; |
| 55 | |
| 56 | /** A wrapper around osg::Camera that contains some extra information. |
| 57 | */ |
| 58 | struct CameraInfo : public osg::Referenced |
| 59 | { |
| 60 | /** properties of a camera. |
| 61 | */ |
| 62 | enum Flags |
| 63 | { |
| 64 | VIEW_ABSOLUTE = 0x1, /**< The camera view is absolute, not |
| 65 | relative to the master camera. */ |
| 66 | PROJECTION_ABSOLUTE = 0x2, /**< The projection is absolute. */ |
| 67 | ORTHO = 0x4, /**< The projection is orthographic */ |
| 68 | GUI = 0x8, /**< Camera draws the GUI. */ |
| 69 | DO_INTERSECTION_TEST = 0x10,/**< scene intersection tests this |
| 70 | camera. */ |
| 71 | FIXED_NEAR_FAR = 0x20, /**< take the near far values in the |
| 72 | projection for real. */ |
| 73 | ENABLE_MASTER_ZOOM = 0x40, /**< Can apply the zoom algorithm. */ |
| 74 | VR_MIRROR = 0x80, /**< Switch to a mirror of VR. */ |
| 75 | SPLASH = 0x100 /**< For splash screen. */ |
| 76 | }; |
| 77 | |
| 78 | CameraInfo(unsigned flags_) : |
| 79 | flags(flags_), |
| 80 | physicalWidth(0), physicalHeight(0), bezelHeightTop(0), |
| 81 | bezelHeightBottom(0), bezelWidthLeft(0), bezelWidthRight(0), |
| 82 | relativeCameraParent(0), reloadCompositorCallback(nullptr) { } |
| 83 | /** The name as given in the config file. |
| 84 | */ |
| 85 | std::string name; |
| 86 | /** Properties of the camera. @see CameraGroup::Flags. |
| 87 | */ |
| 88 | unsigned flags; |
| 89 | /** Physical size parameters. |
| 90 | */ |
| 91 | double physicalWidth; |
| 92 | double physicalHeight; |
| 93 | double bezelHeightTop; |
| 94 | double bezelHeightBottom; |
| 95 | double bezelWidthLeft; |
| 96 | double bezelWidthRight; |
| 97 | /** Non-owning reference to the parent camera for relative camera |
| 98 | * configurations. |
| 99 | */ |
| 100 | const CameraInfo *relativeCameraParent; |
| 101 | /** The reference points in the parents projection space. |
| 102 | */ |
| 103 | osg::Vec2d parentReference[2]; |
| 104 | /** The reference points in the current projection space. |
| 105 | */ |
| 106 | osg::Vec2d thisReference[2]; |
| 107 | /** View offset from the viewer master camera. |
| 108 | */ |
| 109 | osg::Matrix viewOffset; |
| 110 | /** Projection offset from the viewer master camera. |
| 111 | */ |
| 112 | osg::Matrix projOffset; |
| 113 | /** Current view and projection matrices for this camera. |
| 114 | * They are only used by other child cameras through relativeCameraParent |
| 115 | * so they can avoid recalculating them. |
| 116 | */ |
| 117 | osg::Matrix viewMatrix, projMatrix; |
| 118 | /** The Compositor used to manage the pipeline of this camera. |
| 119 | */ |
| 120 | std::unique_ptr<simgear::compositor::Compositor> compositor; |
| 121 | /** Compositor path. Used to recreate the pipeline when reloading. |
| 122 | * If the path is empty, it means that this camera isn't using a custom |
| 123 | * Compositor path and should use the default one. |
| 124 | */ |
| 125 | std::string compositor_path; |
| 126 | |
| 127 | struct ReloadCompositorCallback : public virtual osg::Referenced |
| 128 | { |
| 129 | virtual void preReloadCompositor(CameraGroup *, CameraInfo *) = 0; |
| 130 | virtual void postReloadCompositor(CameraGroup *, CameraInfo *) = 0; |
| 131 | }; |
| 132 | osg::ref_ptr<ReloadCompositorCallback> reloadCompositorCallback; |
| 133 | |
| 134 | }; |
| 135 | |
| 136 | class CameraGroup : public osg::Referenced |
| 137 | { |
| 138 | public: |
| 139 | /** Create a camera group associated with an osgViewer::Viewer. |
| 140 | * @param viewer the viewer |
| 141 | */ |
| 142 | CameraGroup(osgViewer::View* viewer); |
| 143 | virtual ~CameraGroup(); |
| 144 | |
| 145 | /** Set the default CameraGroup, which is the only one that |
| 146 | * matters at this time. |
| 147 | * @param group the group to set. |
| 148 | */ |
| 149 | static void buildDefaultGroup(osgViewer::View* view); |
| 150 | static void setDefault(CameraGroup* group) { _defaultGroup = group; } |
| 151 | /** Get the default CameraGroup. |
| 152 | * @return the default camera group. |
| 153 | */ |
| 154 | static CameraGroup* getDefault() { return _defaultGroup.get(); } |
| 155 | /** Get the camera group's Viewer. |
| 156 | * @return the viewer |
| 157 | */ |
| 158 | osgViewer::View* getView() { return _viewer.get(); } |
| 159 | /** Create an osg::Camera from a property node and add it to the |
| 160 | * camera group. |
| 161 | * @param cameraNode the property node. |
| 162 | * @return a CameraInfo object for the camera. |
| 163 | */ |
| 164 | CameraInfo* buildCamera(SGPropertyNode* cameraNode); |
| 165 | /** Remove a camera from the camera group. |
| 166 | * @param info the camera info to remove. |
| 167 | */ |
| 168 | void removeCamera(CameraInfo *info); |
| 169 | /** Create a camera from properties that will draw the splash screen and add |
| 170 | * it to the camera group. |
| 171 | * @param cameraNode the property node. This can be 0, in which |
| 172 | * case a default GUI camera is created. |
| 173 | * @param window the GraphicsWindow to use for the splash camera. If |
| 174 | * this is 0, the window is determined from the property node. |
| 175 | * @return a CameraInfo object for the GUI camera. |
| 176 | */ |
| 177 | void buildSplashCamera(SGPropertyNode* cameraNode, |
| 178 | GraphicsWindow* window = 0); |
| 179 | /** Create a camera from properties that will draw the GUI and add |
| 180 | * it to the camera group. |
| 181 | * @param cameraNode the property node. This can be 0, in which |
| 182 | * case a default GUI camera is created. |
| 183 | * @param window the GraphicsWindow to use for the GUI camera. If |
| 184 | * this is 0, the window is determined from the property node. |
| 185 | * @return a CameraInfo object for the GUI camera. |
| 186 | */ |
| 187 | void buildGUICamera(SGPropertyNode* cameraNode, |
| 188 | GraphicsWindow* window = 0); |
| 189 | /** Update the view for the camera group. |
| 190 | * @param position the world position of the view |
| 191 | * @param orientation the world orientation of the view. |
| 192 | */ |
| 193 | void update(const osg::Vec3d& position, |
| 194 | const osg::Quat& orientation); |
| 195 | /** Set the parameters of the viewer's master camera. This won't |
| 196 | * affect cameras that have CameraFlags::PROJECTION_ABSOLUTE set. |
| 197 | * XXX Should znear and zfar be settable? |
| 198 | * @param vfov the vertical field of view angle |
| 199 | * @param aspectRatio the master camera's aspect ratio. This |
| 200 | * doesn't actually change the viewport, but should reflect the |
| 201 | * current viewport. |
| 202 | */ |
| 203 | void setCameraParameters(float vfov, float aspectRatio); |
| 204 | /** Set the cull mask on all non-GUI cameras |
| 205 | */ |
| 206 | void setCameraCullMasks(osg::Node::NodeMask nm); |
| 207 | /** Update camera properties after a resize event. |
| 208 | */ |
| 209 | void resized(); |
| 210 | |
| 211 | void buildDistortionCamera(const SGPropertyNode* psNode, |
| 212 | osg::Camera* camera); |
| 213 | /** |
| 214 | * get aspect ratio of master camera's viewport |
| 215 | */ |
| 216 | double getMasterAspectRatio() const; |
| 217 | |
| 218 | CameraInfo *getGUICamera() const; |
| 219 | |
| 220 | typedef std::vector<osg::ref_ptr<CameraInfo>> CameraList; |
| 221 | const CameraList& getCameras(); |
| 222 | |
| 223 | protected: |
| 224 | friend CameraGroupListener; |
| 225 | friend bool computeIntersections(const CameraGroup* cgroup, |
| 226 | const osg::Vec2d& windowPos, |
| 227 | osgUtil::LineSegmentIntersector::Intersections& |
| 228 | intersections); |
| 229 | friend void reloadCompositors(CameraGroup *cgroup); |
| 230 | |
| 231 | CameraList _cameras; |
| 232 | osg::ref_ptr<osgViewer::View> _viewer; |
| 233 | static osg::ref_ptr<CameraGroup> _defaultGroup; |
| 234 | std::unique_ptr<CameraGroupListener> _listener; |
| 235 | |
| 236 | // Near, far for the master camera if used. |
| 237 | float _zNear; |
| 238 | float _zFar; |
| 239 | float _nearField; |
| 240 | |
| 241 | /** Create a compositor for a VR mirror. |
| 242 | * @param gc the graphics context to use. |
| 243 | * @param viewport the viewport to render to. |
| 244 | * @return a new compositor or nullptr if no VR mirror is needed. |
| 245 | */ |
| 246 | simgear::compositor::Compositor *buildVRMirrorCompositor(osg::GraphicsContext *gc, |
| 247 | osg::Viewport *viewport); |
| 248 | /** Build a complete CameraGroup from a property node. |
| 249 | * @param viewer the viewer associated with this camera group. |
| 250 | * @param wbuilder the window builder to be used for this camera group. |
| 251 | * @param the camera group property node. |
| 252 | */ |
| 253 | static CameraGroup* buildCameraGroup(osgViewer::View* viewer, |
| 254 | SGPropertyNode* node); |
| 255 | }; |
| 256 | |
| 257 | /** Get the osg::Camera that draws the GUI, if any, from a camera |
| 258 | * group. |
| 259 | * @param cgroup the camera group |
| 260 | * @return the GUI camera or 0 |
| 261 | */ |
| 262 | osg::Camera* getGUICamera(CameraGroup* cgroup); |
| 263 | /** Choose a camera using an event and do intersection testing on its |
| 264 | * view of the scene. Only cameras with the DO_INTERSECTION_TEST flag |
| 265 | * set are considered. |
| 266 | * @param cgroup the CameraGroup |
| 267 | * @param ea the event containing a window and mouse coordinates |
| 268 | * @param intersections container for the result of intersection |
| 269 | * testing. |
| 270 | * @return true if any intersections are found |
| 271 | */ |
| 272 | bool computeIntersections(const CameraGroup* cgroup, |
| 273 | const osg::Vec2d& windowPos, |
| 274 | osgUtil::LineSegmentIntersector::Intersections& |
| 275 | intersections); |
| 276 | /** Warp the pointer to coordinates in the GUI camera of a camera group. |
| 277 | * @param cgroup the camera group |
| 278 | * @param x x window coordinate of pointer |
| 279 | * @param y y window coordinate of pointer, in "y down" coordinates. |
| 280 | */ |
| 281 | void warpGUIPointer(CameraGroup* cgroup, int x, int y); |
| 282 | |
| 283 | /** Force a reload of all Compositor instances in the CameraGroup, |
| 284 | * except the one used by the GUI camera. |
| 285 | */ |
| 286 | void reloadCompositors(CameraGroup *cgroup); |
| 287 | |
| 288 | } |
| 289 | |
| 290 | #endif |