| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSG_CAMERA |
|---|
| 15 | #define OSG_CAMERA 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/Transform> |
|---|
| 18 | #include <osg/Viewport> |
|---|
| 19 | #include <osg/ColorMask> |
|---|
| 20 | #include <osg/CullSettings> |
|---|
| 21 | #include <osg/Texture> |
|---|
| 22 | #include <osg/Image> |
|---|
| 23 | #include <osg/GraphicsContext> |
|---|
| 24 | #include <osg/Stats> |
|---|
| 25 | |
|---|
| 26 | #include <OpenThreads/Mutex> |
|---|
| 27 | |
|---|
| 28 | namespace osg { |
|---|
| 29 | |
|---|
| 30 | // forward declare View to allow Camera to point back to the View that its within |
|---|
| 31 | class View; |
|---|
| 32 | class RenderInfo; |
|---|
| 33 | |
|---|
| 34 | /** Camera - is a subclass of Transform which represents encapsulates the settings of a Camera. |
|---|
| 35 | */ |
|---|
| 36 | class OSG_EXPORT Camera : public Transform, public CullSettings |
|---|
| 37 | { |
|---|
| 38 | public : |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | Camera(); |
|---|
| 42 | |
|---|
| 43 | /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ |
|---|
| 44 | Camera(const Camera&,const CopyOp& copyop=CopyOp::SHALLOW_COPY); |
|---|
| 45 | |
|---|
| 46 | META_Node(osg, Camera); |
|---|
| 47 | |
|---|
| 48 | virtual Camera* asCamera() { return this; } |
|---|
| 49 | virtual const Camera* asCamera() const { return this; } |
|---|
| 50 | |
|---|
| 51 | /** Set the View that this Camera is part of. */ |
|---|
| 52 | void setView(View* view) { _view = view; } |
|---|
| 53 | |
|---|
| 54 | /** Get the View that this Camera is part of. */ |
|---|
| 55 | View* getView() { return _view; } |
|---|
| 56 | |
|---|
| 57 | /** Get the const View that this Camera is part of. */ |
|---|
| 58 | const View* getView() const { return _view; } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | /** Set the Stats object used for collect various frame related |
|---|
| 62 | * timing and scene graph stats. */ |
|---|
| 63 | void setStats(osg::Stats* stats) { _stats = stats; } |
|---|
| 64 | |
|---|
| 65 | /** Get the Stats object.*/ |
|---|
| 66 | osg::Stats* getStats() { return _stats.get(); } |
|---|
| 67 | |
|---|
| 68 | /** Get the const Stats object.*/ |
|---|
| 69 | const osg::Stats* getStats() const { return _stats.get(); } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | /** Set whether this camera allows events to be generated by the |
|---|
| 73 | * associated graphics window to be associated with this camera. */ |
|---|
| 74 | void setAllowEventFocus(bool focus) { _allowEventFocus = focus; } |
|---|
| 75 | |
|---|
| 76 | /** Get whether this camera allows events to be generated by the |
|---|
| 77 | * associated graphics window to be associated with this camera. */ |
|---|
| 78 | bool getAllowEventFocus() const { return _allowEventFocus; } |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | /** Set the DisplaySettings object associated with this view.*/ |
|---|
| 82 | void setDisplaySettings(osg::DisplaySettings* ds) { _displaySettings = ds; } |
|---|
| 83 | |
|---|
| 84 | /** Get the DisplaySettings object associated with this view.*/ |
|---|
| 85 | osg::DisplaySettings* getDisplaySettings() { return _displaySettings.get(); } |
|---|
| 86 | |
|---|
| 87 | /** Get the const DisplaySettings object associated with this view.*/ |
|---|
| 88 | const osg::DisplaySettings* getDisplaySettings() const { return _displaySettings.get(); } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | /** Set the clear mask used in glClear(). |
|---|
| 92 | * Defaults to GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT. */ |
|---|
| 93 | inline void setClearMask(GLbitfield mask) { _clearMask = mask; applyMaskAction(CLEAR_MASK); } |
|---|
| 94 | |
|---|
| 95 | /** Get the clear mask.*/ |
|---|
| 96 | inline GLbitfield getClearMask() const { return _clearMask; } |
|---|
| 97 | |
|---|
| 98 | /** Set the clear color used in glClearColor(). |
|---|
| 99 | * glClearColor is only called if mask & GL_COLOR_BUFFER_BIT is true*/ |
|---|
| 100 | void setClearColor(const osg::Vec4& color) { _clearColor=color; applyMaskAction(CLEAR_COLOR); } |
|---|
| 101 | |
|---|
| 102 | /** Get the clear color.*/ |
|---|
| 103 | const osg::Vec4& getClearColor() const { return _clearColor; } |
|---|
| 104 | |
|---|
| 105 | /** Set the clear accum used in glClearAccum(). |
|---|
| 106 | * glClearAcumm is only called if mask & GL_ACCUM_BUFFER_BIT is true. */ |
|---|
| 107 | void setClearAccum(const osg::Vec4& color) { _clearAccum=color; } |
|---|
| 108 | |
|---|
| 109 | /** Get the clear accum value.*/ |
|---|
| 110 | const osg::Vec4& getClearAccum() const { return _clearAccum; } |
|---|
| 111 | |
|---|
| 112 | /** Set the clear depth used in glClearDepth(). Defaults to 1.0 |
|---|
| 113 | * glClearDepth is only called if mask & GL_DEPTH_BUFFER_BIT is true. */ |
|---|
| 114 | void setClearDepth(double depth) { _clearDepth=depth; } |
|---|
| 115 | |
|---|
| 116 | /** Get the clear depth value.*/ |
|---|
| 117 | double getClearDepth() const { return _clearDepth; } |
|---|
| 118 | |
|---|
| 119 | /** Set the clear stencil value used in glClearStencil(). Defaults to 0; |
|---|
| 120 | * glClearStencil is only called if mask & GL_STENCIL_BUFFER_BIT is true*/ |
|---|
| 121 | void setClearStencil(int stencil) { _clearStencil=stencil; } |
|---|
| 122 | |
|---|
| 123 | /** Get the clear stencil value.*/ |
|---|
| 124 | int getClearStencil() const { return _clearStencil; } |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | /** Set the color mask of the camera to use specified osg::ColorMask. */ |
|---|
| 128 | void setColorMask(osg::ColorMask* colorMask); |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | /** Set the color mask of the camera to specified values. */ |
|---|
| 132 | void setColorMask(bool red, bool green, bool blue, bool alpha); |
|---|
| 133 | |
|---|
| 134 | /** Get the const ColorMask. */ |
|---|
| 135 | const ColorMask* getColorMask() const { return _colorMask.get(); } |
|---|
| 136 | |
|---|
| 137 | /** Get the ColorMask. */ |
|---|
| 138 | ColorMask* getColorMask() { return _colorMask.get(); } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | /** Set the viewport of the camera to use specified osg::Viewport. */ |
|---|
| 142 | void setViewport(osg::Viewport* viewport); |
|---|
| 143 | |
|---|
| 144 | /** Set the viewport of the camera to specified dimensions. */ |
|---|
| 145 | void setViewport(int x,int y,int width,int height); |
|---|
| 146 | |
|---|
| 147 | /** Get the const viewport. */ |
|---|
| 148 | const Viewport* getViewport() const { return _viewport.get(); } |
|---|
| 149 | |
|---|
| 150 | /** Get the viewport. */ |
|---|
| 151 | Viewport* getViewport() { return _viewport.get(); } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | enum TransformOrder |
|---|
| 155 | { |
|---|
| 156 | PRE_MULTIPLY, |
|---|
| 157 | POST_MULTIPLY |
|---|
| 158 | }; |
|---|
| 159 | |
|---|
| 160 | /** Set the transformation order for world-to-local and local-to-world transformation.*/ |
|---|
| 161 | void setTransformOrder(TransformOrder order) { _transformOrder = order; } |
|---|
| 162 | |
|---|
| 163 | /** Get the transformation order.*/ |
|---|
| 164 | TransformOrder getTransformOrder() const { return _transformOrder; } |
|---|
| 165 | |
|---|
| 166 | enum ProjectionResizePolicy |
|---|
| 167 | { |
|---|
| 168 | FIXED, /**< Keep the projection matrix fixed, despite window resizes.*/ |
|---|
| 169 | HORIZONTAL, /**< Adjust the HORIZONTAL field of view on window resizes.*/ |
|---|
| 170 | VERTICAL /**< Adjust the VERTICAL field of view on window resizes.*/ |
|---|
| 171 | |
|---|
| 172 | }; |
|---|
| 173 | |
|---|
| 174 | /** Set the policy used to determine if and how the projection matrix should be adjusted on window resizes. */ |
|---|
| 175 | inline void setProjectionResizePolicy(ProjectionResizePolicy policy) { _projectionResizePolicy = policy; } |
|---|
| 176 | |
|---|
| 177 | /** Get the policy used to determine if and how the projection matrix should be adjusted on window resizes. */ |
|---|
| 178 | inline ProjectionResizePolicy getProjectionResizePolicy() const { return _projectionResizePolicy; } |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | /** Set the projection matrix. Can be thought of as setting the lens of a camera. */ |
|---|
| 182 | inline void setProjectionMatrix(const osg::Matrixf& matrix) { _projectionMatrix.set(matrix); } |
|---|
| 183 | |
|---|
| 184 | /** Set the projection matrix. Can be thought of as setting the lens of a camera. */ |
|---|
| 185 | inline void setProjectionMatrix(const osg::Matrixd& matrix) { _projectionMatrix.set(matrix); } |
|---|
| 186 | |
|---|
| 187 | /** Set to an orthographic projection. See OpenGL glOrtho for documentation further details.*/ |
|---|
| 188 | void setProjectionMatrixAsOrtho(double left, double right, |
|---|
| 189 | double bottom, double top, |
|---|
| 190 | double zNear, double zFar); |
|---|
| 191 | |
|---|
| 192 | /** Set to a 2D orthographic projection. See OpenGL glOrtho2D documentation for further details.*/ |
|---|
| 193 | void setProjectionMatrixAsOrtho2D(double left, double right, |
|---|
| 194 | double bottom, double top); |
|---|
| 195 | |
|---|
| 196 | /** Set to a perspective projection. See OpenGL glFrustum documentation for further details.*/ |
|---|
| 197 | void setProjectionMatrixAsFrustum(double left, double right, |
|---|
| 198 | double bottom, double top, |
|---|
| 199 | double zNear, double zFar); |
|---|
| 200 | |
|---|
| 201 | /** Create a symmetrical perspective projection, See OpenGL gluPerspective documentation for further details. |
|---|
| 202 | * Aspect ratio is defined as width/height.*/ |
|---|
| 203 | void setProjectionMatrixAsPerspective(double fovy,double aspectRatio, |
|---|
| 204 | double zNear, double zFar); |
|---|
| 205 | |
|---|
| 206 | /** Get the projection matrix.*/ |
|---|
| 207 | osg::Matrixd& getProjectionMatrix() { return _projectionMatrix; } |
|---|
| 208 | |
|---|
| 209 | /** Get the const projection matrix.*/ |
|---|
| 210 | const osg::Matrixd& getProjectionMatrix() const { return _projectionMatrix; } |
|---|
| 211 | |
|---|
| 212 | /** Get the orthographic settings of the orthographic projection matrix. |
|---|
| 213 | * Returns false if matrix is not an orthographic matrix, where parameter values are undefined.*/ |
|---|
| 214 | bool getProjectionMatrixAsOrtho(double& left, double& right, |
|---|
| 215 | double& bottom, double& top, |
|---|
| 216 | double& zNear, double& zFar) const; |
|---|
| 217 | |
|---|
| 218 | /** Get the frustum setting of a perspective projection matrix. |
|---|
| 219 | * Returns false if matrix is not a perspective matrix, where parameter values are undefined.*/ |
|---|
| 220 | bool getProjectionMatrixAsFrustum(double& left, double& right, |
|---|
| 221 | double& bottom, double& top, |
|---|
| 222 | double& zNear, double& zFar) const; |
|---|
| 223 | |
|---|
| 224 | /** Get the frustum setting of a symmetric perspective projection matrix. |
|---|
| 225 | * Returns false if matrix is not a perspective matrix, where parameter values are undefined. |
|---|
| 226 | * Note, if matrix is not a symmetric perspective matrix then the shear will be lost. |
|---|
| 227 | * Asymmetric matrices occur when stereo, power walls, caves and reality center display are used. |
|---|
| 228 | * In these configurations one should use the 'getProjectionMatrixAsFrustum' method instead.*/ |
|---|
| 229 | bool getProjectionMatrixAsPerspective(double& fovy,double& aspectRatio, |
|---|
| 230 | double& zNear, double& zFar) const; |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | /** Set the view matrix. Can be thought of as setting the position of the world relative to the camera in camera coordinates. */ |
|---|
| 235 | inline void setViewMatrix(const osg::Matrixf& matrix) { _viewMatrix.set(matrix); dirtyBound();} |
|---|
| 236 | |
|---|
| 237 | /** Set the view matrix. Can be thought of as setting the position of the world relative to the camera in camera coordinates. */ |
|---|
| 238 | inline void setViewMatrix(const osg::Matrixd& matrix) { _viewMatrix.set(matrix); dirtyBound();} |
|---|
| 239 | |
|---|
| 240 | /** Get the view matrix. */ |
|---|
| 241 | osg::Matrixd& getViewMatrix() { return _viewMatrix; } |
|---|
| 242 | |
|---|
| 243 | /** Get the const view matrix. */ |
|---|
| 244 | const osg::Matrixd& getViewMatrix() const { return _viewMatrix; } |
|---|
| 245 | |
|---|
| 246 | /** Set to the position and orientation of view matrix, using the same convention as gluLookAt. */ |
|---|
| 247 | void setViewMatrixAsLookAt(const osg::Vec3d& eye,const osg::Vec3d& center,const osg::Vec3d& up); |
|---|
| 248 | |
|---|
| 249 | /** Get to the position and orientation of a modelview matrix, using the same convention as gluLookAt. */ |
|---|
| 250 | void getViewMatrixAsLookAt(osg::Vec3d& eye,osg::Vec3d& center,osg::Vec3d& up,double lookDistance=1.0) const; |
|---|
| 251 | |
|---|
| 252 | /** Get to the position and orientation of a modelview matrix, using the same convention as gluLookAt. */ |
|---|
| 253 | void getViewMatrixAsLookAt(osg::Vec3f& eye,osg::Vec3f& center,osg::Vec3f& up,float lookDistance=1.0f) const; |
|---|
| 254 | |
|---|
| 255 | /** Get the inverse view matrix.*/ |
|---|
| 256 | Matrixd getInverseViewMatrix() const; |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | enum RenderOrder |
|---|
| 260 | { |
|---|
| 261 | PRE_RENDER, |
|---|
| 262 | NESTED_RENDER, |
|---|
| 263 | POST_RENDER |
|---|
| 264 | }; |
|---|
| 265 | |
|---|
| 266 | /** Set the rendering order of this camera's subgraph relative to any camera that this subgraph is nested within. |
|---|
| 267 | * For rendering to a texture, one typically uses PRE_RENDER. |
|---|
| 268 | * For Head Up Displays, one would typically use POST_RENDER.*/ |
|---|
| 269 | void setRenderOrder(RenderOrder order, int orderNum = 0) { _renderOrder = order; _renderOrderNum = orderNum; } |
|---|
| 270 | |
|---|
| 271 | /** Get the rendering order of this camera's subgraph relative to any camera that this subgraph is nested within.*/ |
|---|
| 272 | RenderOrder getRenderOrder() const { return _renderOrder; } |
|---|
| 273 | |
|---|
| 274 | /** Get the rendering order number of this camera relative to any sibling cameras in this subgraph.*/ |
|---|
| 275 | int getRenderOrderNum() const { return _renderOrderNum; } |
|---|
| 276 | |
|---|
| 277 | /** Return true if this Camera is set up as a render to texture camera, i.e. it has textures assigned to it.*/ |
|---|
| 278 | bool isRenderToTextureCamera() const; |
|---|
| 279 | |
|---|
| 280 | enum RenderTargetImplementation |
|---|
| 281 | { |
|---|
| 282 | FRAME_BUFFER_OBJECT, |
|---|
| 283 | PIXEL_BUFFER_RTT, |
|---|
| 284 | PIXEL_BUFFER, |
|---|
| 285 | FRAME_BUFFER, |
|---|
| 286 | SEPERATE_WINDOW |
|---|
| 287 | }; |
|---|
| 288 | |
|---|
| 289 | /** Set the render target.*/ |
|---|
| 290 | void setRenderTargetImplementation(RenderTargetImplementation impl); |
|---|
| 291 | |
|---|
| 292 | /** Set the render target and fall-back that's used if the former isn't available.*/ |
|---|
| 293 | void setRenderTargetImplementation(RenderTargetImplementation impl, RenderTargetImplementation fallback); |
|---|
| 294 | |
|---|
| 295 | /** Get the render target.*/ |
|---|
| 296 | RenderTargetImplementation getRenderTargetImplementation() const { return _renderTargetImplementation; } |
|---|
| 297 | |
|---|
| 298 | /** Get the render target fallback.*/ |
|---|
| 299 | RenderTargetImplementation getRenderTargetFallback() const { return _renderTargetFallback; } |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | /** Set the draw buffer used at the start of each frame draw. |
|---|
| 303 | * Note, a buffer value of GL_NONE is used to sepecify that the rendering back-end should choose the most appropriate buffer.*/ |
|---|
| 304 | void setDrawBuffer(GLenum buffer) { _drawBuffer = buffer; applyMaskAction( DRAW_BUFFER ); } |
|---|
| 305 | |
|---|
| 306 | /** Get the draw buffer used at the start of each frame draw. */ |
|---|
| 307 | GLenum getDrawBuffer() const { return _drawBuffer; } |
|---|
| 308 | |
|---|
| 309 | /** Set the read buffer for any required copy operations to use. |
|---|
| 310 | * Note, a buffer value of GL_NONE is used to sepecify that the rendering back-end should choose the most appropriate buffer.*/ |
|---|
| 311 | void setReadBuffer(GLenum buffer) { _readBuffer = buffer; applyMaskAction( READ_BUFFER ); } |
|---|
| 312 | |
|---|
| 313 | /** Get the read buffer for any required copy operations to use. */ |
|---|
| 314 | GLenum getReadBuffer() const { return _readBuffer; } |
|---|
| 315 | |
|---|
| 316 | enum BufferComponent |
|---|
| 317 | { |
|---|
| 318 | DEPTH_BUFFER, |
|---|
| 319 | STENCIL_BUFFER, |
|---|
| 320 | PACKED_DEPTH_STENCIL_BUFFER, |
|---|
| 321 | COLOR_BUFFER, |
|---|
| 322 | COLOR_BUFFER0, |
|---|
| 323 | COLOR_BUFFER1 = COLOR_BUFFER0+1, |
|---|
| 324 | COLOR_BUFFER2 = COLOR_BUFFER0+2, |
|---|
| 325 | COLOR_BUFFER3 = COLOR_BUFFER0+3, |
|---|
| 326 | COLOR_BUFFER4 = COLOR_BUFFER0+4, |
|---|
| 327 | COLOR_BUFFER5 = COLOR_BUFFER0+5, |
|---|
| 328 | COLOR_BUFFER6 = COLOR_BUFFER0+6, |
|---|
| 329 | COLOR_BUFFER7 = COLOR_BUFFER0+7, |
|---|
| 330 | COLOR_BUFFER8 = COLOR_BUFFER0+8, |
|---|
| 331 | COLOR_BUFFER9 = COLOR_BUFFER0+9, |
|---|
| 332 | COLOR_BUFFER10 = COLOR_BUFFER0+10, |
|---|
| 333 | COLOR_BUFFER11 = COLOR_BUFFER0+11, |
|---|
| 334 | COLOR_BUFFER12 = COLOR_BUFFER0+12, |
|---|
| 335 | COLOR_BUFFER13 = COLOR_BUFFER0+13, |
|---|
| 336 | COLOR_BUFFER14 = COLOR_BUFFER0+14, |
|---|
| 337 | COLOR_BUFFER15 = COLOR_BUFFER0+15 |
|---|
| 338 | }; |
|---|
| 339 | |
|---|
| 340 | static const unsigned int FACE_CONTROLLED_BY_GEOMETRY_SHADER; |
|---|
| 341 | |
|---|
| 342 | /** Attach a buffer with specified OpenGL internal format.*/ |
|---|
| 343 | void attach(BufferComponent buffer, GLenum internalFormat); |
|---|
| 344 | |
|---|
| 345 | /** Attach a Texture to specified buffer component. |
|---|
| 346 | * The level parameter controls the mip map level of the texture that is attached. |
|---|
| 347 | * The face parameter controls the face of texture cube map or z level of 3d texture. |
|---|
| 348 | * The mipMapGeneration flag controls whether mipmap generation should be done for texture.*/ |
|---|
| 349 | void attach(BufferComponent buffer, osg::Texture* texture, unsigned int level = 0, unsigned int face=0, bool mipMapGeneration=false, |
|---|
| 350 | unsigned int multisampleSamples = 0, |
|---|
| 351 | unsigned int multisampleColorSamples = 0); |
|---|
| 352 | |
|---|
| 353 | /** Attach a Image to specified buffer component.*/ |
|---|
| 354 | void attach(BufferComponent buffer, osg::Image* image, |
|---|
| 355 | unsigned int multisampleSamples = 0, |
|---|
| 356 | unsigned int multisampleColorSamples = 0); |
|---|
| 357 | |
|---|
| 358 | /** Detach specified buffer component.*/ |
|---|
| 359 | void detach(BufferComponent buffer); |
|---|
| 360 | |
|---|
| 361 | struct Attachment |
|---|
| 362 | { |
|---|
| 363 | Attachment(): |
|---|
| 364 | _internalFormat(GL_NONE), |
|---|
| 365 | _level(0), |
|---|
| 366 | _face(0), |
|---|
| 367 | _mipMapGeneration(false), |
|---|
| 368 | _multisampleSamples(0), |
|---|
| 369 | _multisampleColorSamples(0) {} |
|---|
| 370 | |
|---|
| 371 | int width() const |
|---|
| 372 | { |
|---|
| 373 | if (_texture.valid()) return _texture->getTextureWidth(); |
|---|
| 374 | if (_image.valid()) return _image->s(); |
|---|
| 375 | return 0; |
|---|
| 376 | }; |
|---|
| 377 | |
|---|
| 378 | int height() const |
|---|
| 379 | { |
|---|
| 380 | if (_texture.valid()) return _texture->getTextureHeight(); |
|---|
| 381 | if (_image.valid()) return _image->t(); |
|---|
| 382 | return 0; |
|---|
| 383 | }; |
|---|
| 384 | |
|---|
| 385 | int depth() const |
|---|
| 386 | { |
|---|
| 387 | if (_texture.valid()) return _texture->getTextureDepth(); |
|---|
| 388 | if (_image.valid()) return _image->r(); |
|---|
| 389 | return 0; |
|---|
| 390 | }; |
|---|
| 391 | |
|---|
| 392 | GLenum _internalFormat; |
|---|
| 393 | ref_ptr<Image> _image; |
|---|
| 394 | ref_ptr<Texture> _texture; |
|---|
| 395 | unsigned int _level; |
|---|
| 396 | unsigned int _face; |
|---|
| 397 | bool _mipMapGeneration; |
|---|
| 398 | unsigned int _multisampleSamples; |
|---|
| 399 | unsigned int _multisampleColorSamples; |
|---|
| 400 | }; |
|---|
| 401 | |
|---|
| 402 | typedef std::map< BufferComponent, Attachment> BufferAttachmentMap; |
|---|
| 403 | |
|---|
| 404 | /** Get the BufferAttachmentMap, used to configure frame buffer objects, pbuffers and texture reads.*/ |
|---|
| 405 | BufferAttachmentMap& getBufferAttachmentMap() { return _bufferAttachmentMap; } |
|---|
| 406 | |
|---|
| 407 | /** Get the const BufferAttachmentMap, used to configure frame buffer objects, pbuffers and texture reads.*/ |
|---|
| 408 | const BufferAttachmentMap& getBufferAttachmentMap() const { return _bufferAttachmentMap; } |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | /** Explicit control over implicit allocation of buffers when using FBO. |
|---|
| 412 | Implicit buffers are automatically substituted when user have not attached such buffer. |
|---|
| 413 | |
|---|
| 414 | Camera may set up two FBOs: primary Render FBO and secondary Resolve FBO for multisample usage. |
|---|
| 415 | So in practive we have two masks defined for the Camera: |
|---|
| 416 | implicitBufferAttachmentRenderMask |
|---|
| 417 | implicitBufferAttachmentResolveMask |
|---|
| 418 | They can be set together by setImplicitBufferAttachmentMask method, or separately |
|---|
| 419 | by setImplicitBufferAttachmentRenderMask and setImplicitBufferAttachmentResolveMask. |
|---|
| 420 | |
|---|
| 421 | Camera defaults are USE_DISPLAY_SETTINGS_MASK which means that by default |
|---|
| 422 | Camera chooses to substitue buffer attachments as defined by DisplaySettings. |
|---|
| 423 | |
|---|
| 424 | Usually DisplaySettings implicit buffer attachment selection defaults to: DEPTH and COLOR |
|---|
| 425 | for both primary (Render) FBO and seconday Multisample (Resolve) FBO |
|---|
| 426 | ie: IMPLICT_DEPTH_BUFFER_ATTACHMENT | IMPLICIT_COLOR_BUFFER_ATTACHMENT |
|---|
| 427 | |
|---|
| 428 | If these masks are not changed and user did not attach depth buffer and/or color buffer |
|---|
| 429 | to Camera, then OSG implicitly substitues these buffers. |
|---|
| 430 | By default it does not implicitly allocate a stencil buffer. |
|---|
| 431 | Use implicti buffer attachment masks to override default behavior: |
|---|
| 432 | to turn off DEPTH or COLOR buffer substitution or to enforce STENCIL buffer substitution. |
|---|
| 433 | |
|---|
| 434 | Note that both values are ignored if not using FBO. |
|---|
| 435 | Note that the second mask value is ignored if not using MSFBO. |
|---|
| 436 | */ |
|---|
| 437 | enum ImplicitBufferAttachment |
|---|
| 438 | { |
|---|
| 439 | IMPLICIT_DEPTH_BUFFER_ATTACHMENT = DisplaySettings::IMPLICIT_DEPTH_BUFFER_ATTACHMENT, |
|---|
| 440 | IMPLICIT_STENCIL_BUFFER_ATTACHMENT = DisplaySettings::IMPLICIT_STENCIL_BUFFER_ATTACHMENT, |
|---|
| 441 | IMPLICIT_COLOR_BUFFER_ATTACHMENT = DisplaySettings::IMPLICIT_COLOR_BUFFER_ATTACHMENT, |
|---|
| 442 | USE_DISPLAY_SETTINGS_MASK = (~0) |
|---|
| 443 | }; |
|---|
| 444 | |
|---|
| 445 | typedef int ImplicitBufferAttachmentMask; |
|---|
| 446 | |
|---|
| 447 | void setImplicitBufferAttachmentMask(ImplicitBufferAttachmentMask renderMask = DisplaySettings::DEFAULT_IMPLICIT_BUFFER_ATTACHMENT, ImplicitBufferAttachmentMask resolveMask = DisplaySettings::DEFAULT_IMPLICIT_BUFFER_ATTACHMENT) |
|---|
| 448 | { |
|---|
| 449 | _implicitBufferAttachmentRenderMask = renderMask; |
|---|
| 450 | _implicitBufferAttachmentResolveMask = resolveMask; |
|---|
| 451 | } |
|---|
| 452 | |
|---|
| 453 | void setImplicitBufferAttachmentRenderMask(ImplicitBufferAttachmentMask implicitBufferAttachmentRenderMask) |
|---|
| 454 | { |
|---|
| 455 | _implicitBufferAttachmentRenderMask = implicitBufferAttachmentRenderMask; |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| 458 | void setImplicitBufferAttachmentResolveMask(ImplicitBufferAttachmentMask implicitBufferAttachmentResolveMask) |
|---|
| 459 | { |
|---|
| 460 | _implicitBufferAttachmentResolveMask = implicitBufferAttachmentResolveMask; |
|---|
| 461 | } |
|---|
| 462 | |
|---|
| 463 | /** |
|---|
| 464 | Get mask selecting implict buffer attachments for Camera primary FBO |
|---|
| 465 | if effectiveMask parameter is set, method follows USE_DISPLAY_SETTINGS_MASK dependence and returns effective mask |
|---|
| 466 | if effectiveMask parameter is reset, method returns nominal mask set by the Camera |
|---|
| 467 | */ |
|---|
| 468 | ImplicitBufferAttachmentMask getImplicitBufferAttachmentRenderMask(bool effectiveMask = false) const |
|---|
| 469 | { |
|---|
| 470 | if( effectiveMask && _implicitBufferAttachmentRenderMask == USE_DISPLAY_SETTINGS_MASK ) |
|---|
| 471 | { |
|---|
| 472 | const DisplaySettings * ds = _displaySettings.valid() ? _displaySettings.get() : DisplaySettings::instance().get(); |
|---|
| 473 | return ds->getImplicitBufferAttachmentRenderMask(); |
|---|
| 474 | } |
|---|
| 475 | else |
|---|
| 476 | { |
|---|
| 477 | return _implicitBufferAttachmentRenderMask; |
|---|
| 478 | } |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | /** |
|---|
| 482 | Get mask selecting implict buffer attachments for Camera secondary MULTISAMPLE FBO |
|---|
| 483 | if effectiveMask parameter is set, method follows USE_DISPLAY_SETTINGS_MASK dependence and returns effective mask |
|---|
| 484 | if effectiveMask parameter is reset, method returns nominal mask set by the Camera |
|---|
| 485 | */ |
|---|
| 486 | ImplicitBufferAttachmentMask getImplicitBufferAttachmentResolveMask(bool effectiveMask = false) const |
|---|
| 487 | { |
|---|
| 488 | if( effectiveMask && _implicitBufferAttachmentResolveMask == USE_DISPLAY_SETTINGS_MASK ) |
|---|
| 489 | { |
|---|
| 490 | const DisplaySettings * ds = _displaySettings.valid() ? _displaySettings.get() : DisplaySettings::instance().get(); |
|---|
| 491 | return ds->getImplicitBufferAttachmentResolveMask(); |
|---|
| 492 | } |
|---|
| 493 | else |
|---|
| 494 | { |
|---|
| 495 | return _implicitBufferAttachmentResolveMask; |
|---|
| 496 | } |
|---|
| 497 | } |
|---|
| 498 | |
|---|
| 499 | /** Create a operation thread for this camera.*/ |
|---|
| 500 | void createCameraThread(); |
|---|
| 501 | |
|---|
| 502 | /** Assign a operation thread to the camera.*/ |
|---|
| 503 | void setCameraThread(OperationThread* gt); |
|---|
| 504 | |
|---|
| 505 | /** Get the operation thread assigned to this camera.*/ |
|---|
| 506 | OperationThread* getCameraThread() { return _cameraThread.get(); } |
|---|
| 507 | |
|---|
| 508 | /** Get the const operation thread assigned to this camera.*/ |
|---|
| 509 | const OperationThread* getCameraThread() const { return _cameraThread.get(); } |
|---|
| 510 | |
|---|
| 511 | |
|---|
| 512 | |
|---|
| 513 | /** Set the GraphicsContext that provides the mechansim for managing the OpenGL graphics context associated with this camera.*/ |
|---|
| 514 | void setGraphicsContext(GraphicsContext* context); |
|---|
| 515 | |
|---|
| 516 | /** Get the GraphicsContext.*/ |
|---|
| 517 | GraphicsContext* getGraphicsContext() { return _graphicsContext.get(); } |
|---|
| 518 | |
|---|
| 519 | /** Get the const GraphicsContext.*/ |
|---|
| 520 | const GraphicsContext* getGraphicsContext() const { return _graphicsContext.get(); } |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | /** Set the Rendering object that is used to implement rendering of the subgraph.*/ |
|---|
| 524 | void setRenderer(osg::GraphicsOperation* rc) { _renderer = rc; } |
|---|
| 525 | |
|---|
| 526 | /** Get the Rendering object that is used to implement rendering of the subgraph.*/ |
|---|
| 527 | osg::GraphicsOperation* getRenderer() { return _renderer.get(); } |
|---|
| 528 | |
|---|
| 529 | /** Get the const Rendering object that is used to implement rendering of the subgraph.*/ |
|---|
| 530 | const osg::GraphicsOperation* getRenderer() const { return _renderer.get(); } |
|---|
| 531 | |
|---|
| 532 | |
|---|
| 533 | /** Set the Rendering cache that is used for cached objects associated with rendering of subgraphs.*/ |
|---|
| 534 | void setRenderingCache(osg::Object* rc) { _renderingCache = rc; } |
|---|
| 535 | |
|---|
| 536 | /** Get the Rendering cache that is used for cached objects associated with rendering of subgraphs.*/ |
|---|
| 537 | osg::Object* getRenderingCache() { return _renderingCache.get(); } |
|---|
| 538 | |
|---|
| 539 | /** Get the const Rendering cache that is used for cached objects associated with rendering of subgraphs.*/ |
|---|
| 540 | const osg::Object* getRenderingCache() const { return _renderingCache.get(); } |
|---|
| 541 | |
|---|
| 542 | |
|---|
| 543 | /** Draw callback for custom operations.*/ |
|---|
| 544 | struct OSG_EXPORT DrawCallback : virtual public Object |
|---|
| 545 | { |
|---|
| 546 | DrawCallback() {} |
|---|
| 547 | |
|---|
| 548 | DrawCallback(const DrawCallback&,const CopyOp&) {} |
|---|
| 549 | |
|---|
| 550 | META_Object(osg, DrawCallback); |
|---|
| 551 | |
|---|
| 552 | /** Functor method called by rendering thread. Users will typically override this method to carry tasks such as screen capture.*/ |
|---|
| 553 | virtual void operator () (osg::RenderInfo& renderInfo) const; |
|---|
| 554 | |
|---|
| 555 | /** Functor method, provided for backwards compatibility, called by operator() (osg::RenderInfo& renderInfo) method.*/ |
|---|
| 556 | virtual void operator () (const osg::Camera& /*camera*/) const {} |
|---|
| 557 | }; |
|---|
| 558 | |
|---|
| 559 | /** Set the initial draw callback for custom operations to be done before the drawing of the camera's subgraph and pre render stages.*/ |
|---|
| 560 | void setInitialDrawCallback(DrawCallback* cb) { _initialDrawCallback = cb; } |
|---|
| 561 | |
|---|
| 562 | /** Get the initial draw callback.*/ |
|---|
| 563 | DrawCallback* getInitialDrawCallback() { return _initialDrawCallback.get(); } |
|---|
| 564 | |
|---|
| 565 | /** Get the const initial draw callback.*/ |
|---|
| 566 | const DrawCallback* getInitialDrawCallback() const { return _initialDrawCallback.get(); } |
|---|
| 567 | |
|---|
| 568 | |
|---|
| 569 | /** Set the pre draw callback for custom operations to be done before the drawing of the camera's subgraph but after any pre render stages have been completed.*/ |
|---|
| 570 | void setPreDrawCallback(DrawCallback* cb) { _preDrawCallback = cb; } |
|---|
| 571 | |
|---|
| 572 | /** Get the pre draw callback.*/ |
|---|
| 573 | DrawCallback* getPreDrawCallback() { return _preDrawCallback.get(); } |
|---|
| 574 | |
|---|
| 575 | /** Get the const pre draw callback.*/ |
|---|
| 576 | const DrawCallback* getPreDrawCallback() const { return _preDrawCallback.get(); } |
|---|
| 577 | |
|---|
| 578 | |
|---|
| 579 | /** Set the post draw callback for custom operations to be done after the drawing of the camera's subgraph but before the any post render stages have been completed.*/ |
|---|
| 580 | void setPostDrawCallback(DrawCallback* cb) { _postDrawCallback = cb; } |
|---|
| 581 | |
|---|
| 582 | /** Get the post draw callback.*/ |
|---|
| 583 | DrawCallback* getPostDrawCallback() { return _postDrawCallback.get(); } |
|---|
| 584 | |
|---|
| 585 | /** Get the const post draw callback.*/ |
|---|
| 586 | const DrawCallback* getPostDrawCallback() const { return _postDrawCallback.get(); } |
|---|
| 587 | |
|---|
| 588 | |
|---|
| 589 | /** Set the final draw callback for custom operations to be done after the drawing of the camera's subgraph and all of the post render stages has been completed.*/ |
|---|
| 590 | void setFinalDrawCallback(DrawCallback* cb) { _finalDrawCallback = cb; } |
|---|
| 591 | |
|---|
| 592 | /** Get the final draw callback.*/ |
|---|
| 593 | DrawCallback* getFinalDrawCallback() { return _finalDrawCallback.get(); } |
|---|
| 594 | |
|---|
| 595 | /** Get the const final draw callback.*/ |
|---|
| 596 | const DrawCallback* getFinalDrawCallback() const { return _finalDrawCallback.get(); } |
|---|
| 597 | |
|---|
| 598 | |
|---|
| 599 | OpenThreads::Mutex* getDataChangeMutex() const { return &_dataChangeMutex; } |
|---|
| 600 | |
|---|
| 601 | /** Resize any per context GLObject buffers to specified size. */ |
|---|
| 602 | virtual void resizeGLObjectBuffers(unsigned int maxSize); |
|---|
| 603 | |
|---|
| 604 | /** If State is non-zero, this function releases any associated OpenGL objects for |
|---|
| 605 | * the specified graphics context. Otherwise, releases OpenGL objexts |
|---|
| 606 | * for all graphics contexts. */ |
|---|
| 607 | virtual void releaseGLObjects(osg::State* = 0) const; |
|---|
| 608 | |
|---|
| 609 | public: |
|---|
| 610 | |
|---|
| 611 | /** Transform method that must be defined to provide generic interface for scene graph traversals.*/ |
|---|
| 612 | virtual bool computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor*) const; |
|---|
| 613 | |
|---|
| 614 | /** Transform method that must be defined to provide generic interface for scene graph traversals.*/ |
|---|
| 615 | virtual bool computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor*) const; |
|---|
| 616 | |
|---|
| 617 | /** Inherit the local cull settings variable from specified CullSettings object, according to the inheritance mask.*/ |
|---|
| 618 | virtual void inheritCullSettings(const CullSettings& settings, unsigned int inheritanceMask); |
|---|
| 619 | |
|---|
| 620 | protected : |
|---|
| 621 | |
|---|
| 622 | virtual ~Camera(); |
|---|
| 623 | |
|---|
| 624 | mutable OpenThreads::Mutex _dataChangeMutex; |
|---|
| 625 | |
|---|
| 626 | |
|---|
| 627 | View* _view; |
|---|
| 628 | |
|---|
| 629 | osg::ref_ptr<osg::Stats> _stats; |
|---|
| 630 | |
|---|
| 631 | bool _allowEventFocus; |
|---|
| 632 | |
|---|
| 633 | osg::ref_ptr<osg::DisplaySettings> _displaySettings; |
|---|
| 634 | |
|---|
| 635 | GLbitfield _clearMask; |
|---|
| 636 | osg::Vec4 _clearColor; |
|---|
| 637 | osg::Vec4 _clearAccum; |
|---|
| 638 | double _clearDepth; |
|---|
| 639 | int _clearStencil; |
|---|
| 640 | |
|---|
| 641 | ref_ptr<ColorMask> _colorMask; |
|---|
| 642 | ref_ptr<Viewport> _viewport; |
|---|
| 643 | |
|---|
| 644 | TransformOrder _transformOrder; |
|---|
| 645 | ProjectionResizePolicy _projectionResizePolicy; |
|---|
| 646 | |
|---|
| 647 | Matrixd _projectionMatrix; |
|---|
| 648 | Matrixd _viewMatrix; |
|---|
| 649 | |
|---|
| 650 | RenderOrder _renderOrder; |
|---|
| 651 | int _renderOrderNum; |
|---|
| 652 | |
|---|
| 653 | GLenum _drawBuffer; |
|---|
| 654 | GLenum _readBuffer; |
|---|
| 655 | |
|---|
| 656 | RenderTargetImplementation _renderTargetImplementation; |
|---|
| 657 | RenderTargetImplementation _renderTargetFallback; |
|---|
| 658 | BufferAttachmentMap _bufferAttachmentMap; |
|---|
| 659 | ImplicitBufferAttachmentMask _implicitBufferAttachmentRenderMask; |
|---|
| 660 | ImplicitBufferAttachmentMask _implicitBufferAttachmentResolveMask; |
|---|
| 661 | |
|---|
| 662 | ref_ptr<OperationThread> _cameraThread; |
|---|
| 663 | |
|---|
| 664 | ref_ptr<GraphicsContext> _graphicsContext; |
|---|
| 665 | |
|---|
| 666 | ref_ptr<GraphicsOperation> _renderer; |
|---|
| 667 | ref_ptr<Object> _renderingCache; |
|---|
| 668 | |
|---|
| 669 | ref_ptr<DrawCallback> _initialDrawCallback; |
|---|
| 670 | ref_ptr<DrawCallback> _preDrawCallback; |
|---|
| 671 | ref_ptr<DrawCallback> _postDrawCallback; |
|---|
| 672 | ref_ptr<DrawCallback> _finalDrawCallback; |
|---|
| 673 | }; |
|---|
| 674 | |
|---|
| 675 | } |
|---|
| 676 | |
|---|
| 677 | #endif |
|---|