| 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 OSGUTIL_RENDERSTAGE |
|---|
| 15 | #define OSGUTIL_RENDERSTAGE 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/ColorMask> |
|---|
| 18 | #include <osg/Viewport> |
|---|
| 19 | #include <osg/Texture> |
|---|
| 20 | #include <osg/FrameBufferObject> |
|---|
| 21 | #include <osg/Camera> |
|---|
| 22 | |
|---|
| 23 | #include <osgUtil/RenderBin> |
|---|
| 24 | #include <osgUtil/PositionalStateContainer> |
|---|
| 25 | |
|---|
| 26 | namespace osgUtil { |
|---|
| 27 | |
|---|
| 28 | /** |
|---|
| 29 | * RenderStage base class. Used for encapsulate a complete stage in |
|---|
| 30 | * rendering - setting up of viewport, the projection and model |
|---|
| 31 | * matrices and rendering the RenderBin's enclosed with this RenderStage. |
|---|
| 32 | * RenderStage also has a dependency list of other RenderStages, each |
|---|
| 33 | * of which must be called before the rendering of this stage. These |
|---|
| 34 | * 'pre' rendering stages are used for advanced rendering techniques |
|---|
| 35 | * like multistage pixel shading or impostors. |
|---|
| 36 | */ |
|---|
| 37 | class OSGUTIL_EXPORT RenderStage : public RenderBin |
|---|
| 38 | { |
|---|
| 39 | public: |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | RenderStage(); |
|---|
| 43 | RenderStage(SortMode mode); |
|---|
| 44 | |
|---|
| 45 | RenderStage(const RenderStage& rhs,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); |
|---|
| 46 | |
|---|
| 47 | virtual osg::Object* cloneType() const { return new RenderStage(); } |
|---|
| 48 | virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new RenderStage(*this,copyop); } // note only implements a clone of type. |
|---|
| 49 | virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const RenderStage*>(obj)!=0L; } |
|---|
| 50 | virtual const char* className() const { return "RenderStage"; } |
|---|
| 51 | |
|---|
| 52 | virtual void reset(); |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | /** Set the draw buffer used at the start of each frame draw. */ |
|---|
| 56 | void setDrawBuffer(GLenum buffer, bool applyMask = true ) { _drawBuffer = buffer; setDrawBufferApplyMask( applyMask ); } |
|---|
| 57 | |
|---|
| 58 | /** Get the draw buffer used at the start of each frame draw. */ |
|---|
| 59 | GLenum getDrawBuffer() const { return _drawBuffer; } |
|---|
| 60 | |
|---|
| 61 | /** Get the apply mask defining whether glDrawBuffer is called at each frame draw. */ |
|---|
| 62 | bool getDrawBufferApplyMask() const { return _drawBufferApplyMask; } |
|---|
| 63 | |
|---|
| 64 | /** Set the apply mask defining whether glDrawBuffer is called at each frame draw. */ |
|---|
| 65 | void setDrawBufferApplyMask( bool applyMask ) { _drawBufferApplyMask = applyMask; } |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | /** Set the read buffer for any required copy operations to use. */ |
|---|
| 70 | void setReadBuffer(GLenum buffer, bool applyMask = true) { _readBuffer = buffer; setReadBufferApplyMask( applyMask ); } |
|---|
| 71 | |
|---|
| 72 | /** Get the read buffer for any required copy operations to use. */ |
|---|
| 73 | GLenum getReadBuffer() const { return _readBuffer; } |
|---|
| 74 | |
|---|
| 75 | /** Get the apply mask defining whether glReadBuffer is called at each frame draw. */ |
|---|
| 76 | bool getReadBufferApplyMask() const { return _readBufferApplyMask; } |
|---|
| 77 | |
|---|
| 78 | /** Set the apply mask defining whether glReadBuffer is called at each frame draw. */ |
|---|
| 79 | void setReadBufferApplyMask( bool applyMask ) { _readBufferApplyMask = applyMask; } |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | /** Set the viewport.*/ |
|---|
| 83 | void setViewport(osg::Viewport* viewport) { _viewport = viewport; } |
|---|
| 84 | |
|---|
| 85 | /** Get the const viewport. */ |
|---|
| 86 | const osg::Viewport* getViewport() const { return _viewport.get(); } |
|---|
| 87 | |
|---|
| 88 | /** Get the viewport. */ |
|---|
| 89 | osg::Viewport* getViewport() { return _viewport.get(); } |
|---|
| 90 | |
|---|
| 91 | /** Set the initial view matrix.*/ |
|---|
| 92 | void setInitialViewMatrix(const osg::RefMatrix* matrix) { _initialViewMatrix = matrix; } |
|---|
| 93 | |
|---|
| 94 | /** Get the initial view matrix.*/ |
|---|
| 95 | const osg::RefMatrix* getInitialViewMatrix() { return _initialViewMatrix.get(); } |
|---|
| 96 | |
|---|
| 97 | /** Set the clear mask used in glClear(..). |
|---|
| 98 | * Defaults to GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT. */ |
|---|
| 99 | void setClearMask(GLbitfield mask) { _clearMask = mask; } |
|---|
| 100 | |
|---|
| 101 | /** Get the clear mask.*/ |
|---|
| 102 | GLbitfield getClearMask() const { return _clearMask; } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | void setColorMask(osg::ColorMask* cm) { _colorMask = cm; } |
|---|
| 106 | osg::ColorMask* getColorMask() { return _colorMask.get(); } |
|---|
| 107 | const osg::ColorMask* getColorMask() const { return _colorMask.get(); } |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | /** Set the clear color used in glClearColor(..). |
|---|
| 111 | * glClearColor is only called if mask & GL_COLOR_BUFFER_BIT is true*/ |
|---|
| 112 | void setClearColor(const osg::Vec4& color) { _clearColor=color; } |
|---|
| 113 | |
|---|
| 114 | /** Get the clear color.*/ |
|---|
| 115 | const osg::Vec4& getClearColor() const { return _clearColor; } |
|---|
| 116 | |
|---|
| 117 | /** Set the clear accum used in glClearAccum(..). |
|---|
| 118 | * glClearAcumm is only called if mask & GL_ACCUM_BUFFER_BIT is true. */ |
|---|
| 119 | void setClearAccum(const osg::Vec4& color) { _clearAccum=color; } |
|---|
| 120 | |
|---|
| 121 | /** Get the clear accum.*/ |
|---|
| 122 | const osg::Vec4& getClearAccum() const { return _clearAccum; } |
|---|
| 123 | |
|---|
| 124 | /** Set the clear depth used in glClearDepth(..). Defaults to 1.0 |
|---|
| 125 | * glClearDepth is only called if mask & GL_DEPTH_BUFFER_BIT is true. */ |
|---|
| 126 | void setClearDepth(double depth) { _clearDepth=depth; } |
|---|
| 127 | |
|---|
| 128 | /** Get the clear depth.*/ |
|---|
| 129 | double getClearDepth() const { return _clearDepth; } |
|---|
| 130 | |
|---|
| 131 | /** Set the clear stencil value used in glClearStencil(). Defaults to 0; |
|---|
| 132 | * glClearStencil is only called if mask & GL_STENCIL_BUFFER_BIT is true*/ |
|---|
| 133 | void setClearStencil(int stencil) { _clearStencil=stencil; } |
|---|
| 134 | |
|---|
| 135 | /** Get the clear color.*/ |
|---|
| 136 | int getClearStencil() const { return _clearStencil; } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | void setCamera(osg::Camera* camera) { if (_camera!=camera) { _camera = camera; _cameraRequiresSetUp = true; } } |
|---|
| 140 | osg::Camera* getCamera() { return _camera; } |
|---|
| 141 | const osg::Camera* getCamera() const { return _camera; } |
|---|
| 142 | |
|---|
| 143 | void setCameraRequiresSetUp(bool flag) { _cameraRequiresSetUp = flag; } |
|---|
| 144 | bool getCameraRequiresSetUp() const { return _cameraRequiresSetUp; } |
|---|
| 145 | |
|---|
| 146 | /** Attempt the set the RenderStage from the Camera settings.*/ |
|---|
| 147 | void runCameraSetUp(osg::RenderInfo& renderInfo); |
|---|
| 148 | |
|---|
| 149 | void setTexture(osg::Texture* texture, unsigned int level = 0, unsigned int face=0) { _texture = texture; _level = level; _face = face; } |
|---|
| 150 | osg::Texture* getTexture() { return _texture.get(); } |
|---|
| 151 | |
|---|
| 152 | void setImage(osg::Image* image) { _image = image; } |
|---|
| 153 | osg::Image* getImage() { return _image.get(); } |
|---|
| 154 | |
|---|
| 155 | void setImageReadPixelFormat(GLenum format) { _imageReadPixelFormat = format; } |
|---|
| 156 | GLenum getImageReadPixelFormat() const { return _imageReadPixelFormat; } |
|---|
| 157 | |
|---|
| 158 | void setImageReadPixelDataType(GLenum type) { _imageReadPixelDataType = type; } |
|---|
| 159 | GLenum getImageReadPixelDataType() const { return _imageReadPixelDataType; } |
|---|
| 160 | |
|---|
| 161 | /** Set a framebuffer object to render into. It is permissible for the |
|---|
| 162 | * framebuffer object to be multisampled, in which case you should also |
|---|
| 163 | * set a resolve framebuffer object - see setMultisampleResolveFramebufferObject(). */ |
|---|
| 164 | void setFrameBufferObject(osg::FrameBufferObject* fbo) { _fbo = fbo; } |
|---|
| 165 | osg::FrameBufferObject* getFrameBufferObject() { return _fbo.get(); } |
|---|
| 166 | const osg::FrameBufferObject* getFrameBufferObject() const { return _fbo.get(); } |
|---|
| 167 | |
|---|
| 168 | /** Sets the destination framebuffer object for glBlitFramebufferEXT to |
|---|
| 169 | * resolve a multisampled framebuffer object after the RenderStage is |
|---|
| 170 | * drawn. The resolve framebuffer object must not be multisampled. The |
|---|
| 171 | * resolve framebuffer object is only necessary if the primary framebuffer |
|---|
| 172 | * object is multisampled, if not then leave it set to null. */ |
|---|
| 173 | void setMultisampleResolveFramebufferObject(osg::FrameBufferObject* fbo); |
|---|
| 174 | osg::FrameBufferObject* getMultisampleResolveFramebufferObject() { return _resolveFbo.get(); } |
|---|
| 175 | const osg::FrameBufferObject* getMultisampleResolveFramebufferObject() const { return _resolveFbo.get(); } |
|---|
| 176 | |
|---|
| 177 | /** Set whether the framebuffer object should be unbound after |
|---|
| 178 | * rendering. By default this is set to true. Set it to false if the |
|---|
| 179 | * unbinding is not required. */ |
|---|
| 180 | void setDisableFboAfterRender(bool disable) {_disableFboAfterRender = disable;} |
|---|
| 181 | bool getDisableFboAfterRender() const {return _disableFboAfterRender;} |
|---|
| 182 | |
|---|
| 183 | void setGraphicsContext(osg::GraphicsContext* context) { _graphicsContext = context; } |
|---|
| 184 | osg::GraphicsContext* getGraphicsContext() { return _graphicsContext.get(); } |
|---|
| 185 | const osg::GraphicsContext* getGraphicsContext() const { return _graphicsContext.get(); } |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | void setInheritedPositionalStateContainerMatrix(const osg::Matrix& matrix) { _inheritedPositionalStateContainerMatrix = matrix; } |
|---|
| 191 | const osg::Matrix& getInheritedPositionalStateContainerMatrix() const { return _inheritedPositionalStateContainerMatrix; } |
|---|
| 192 | |
|---|
| 193 | void setInheritedPositionalStateContainer(PositionalStateContainer* rsl) { _inheritedPositionalStateContainer = rsl; } |
|---|
| 194 | PositionalStateContainer* getInheritedPositionalStateContainer() { return _inheritedPositionalStateContainer.get(); } |
|---|
| 195 | |
|---|
| 196 | void setPositionalStateContainer(PositionalStateContainer* rsl) { _renderStageLighting = rsl; } |
|---|
| 197 | |
|---|
| 198 | PositionalStateContainer* getPositionalStateContainer() const |
|---|
| 199 | { |
|---|
| 200 | if (!_renderStageLighting.valid()) _renderStageLighting = new PositionalStateContainer; |
|---|
| 201 | return _renderStageLighting.get(); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | virtual void addPositionedAttribute(osg::RefMatrix* matrix,const osg::StateAttribute* attr) |
|---|
| 205 | { |
|---|
| 206 | getPositionalStateContainer()->addPositionedAttribute(matrix,attr); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | virtual void addPositionedTextureAttribute(unsigned int textureUnit, osg::RefMatrix* matrix,const osg::StateAttribute* attr) |
|---|
| 210 | { |
|---|
| 211 | getPositionalStateContainer()->addPositionedTextureAttribute(textureUnit, matrix,attr); |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | void copyTexture(osg::RenderInfo& renderInfo); |
|---|
| 215 | |
|---|
| 216 | virtual void sort(); |
|---|
| 217 | |
|---|
| 218 | virtual void drawPreRenderStages(osg::RenderInfo& renderInfo,RenderLeaf*& previous); |
|---|
| 219 | |
|---|
| 220 | virtual void draw(osg::RenderInfo& renderInfo,RenderLeaf*& previous); |
|---|
| 221 | |
|---|
| 222 | virtual void drawInner(osg::RenderInfo& renderInfo,RenderLeaf*& previous, bool& doCopyTexture); |
|---|
| 223 | |
|---|
| 224 | virtual void drawPostRenderStages(osg::RenderInfo& renderInfo,RenderLeaf*& previous); |
|---|
| 225 | |
|---|
| 226 | virtual void drawImplementation(osg::RenderInfo& renderInfo,RenderLeaf*& previous); |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | void addToDependencyList(RenderStage* rs) { addPreRenderStage(rs); } |
|---|
| 230 | |
|---|
| 231 | void addPreRenderStage(RenderStage* rs, int order = 0); |
|---|
| 232 | |
|---|
| 233 | void addPostRenderStage(RenderStage* rs, int order = 0); |
|---|
| 234 | |
|---|
| 235 | /** Extract stats for current draw list. */ |
|---|
| 236 | bool getStats(Statistics& stats) const; |
|---|
| 237 | |
|---|
| 238 | /** Compute the number of dynamic RenderLeaves.*/ |
|---|
| 239 | virtual unsigned int computeNumberOfDynamicRenderLeaves() const; |
|---|
| 240 | |
|---|
| 241 | struct Attachment |
|---|
| 242 | { |
|---|
| 243 | osg::ref_ptr<osg::Image> _image; |
|---|
| 244 | GLenum _imageReadPixelFormat; |
|---|
| 245 | GLenum _imageReadPixelDataType; |
|---|
| 246 | }; |
|---|
| 247 | |
|---|
| 248 | void attach(osg::Camera::BufferComponent buffer, osg::Image* image); |
|---|
| 249 | |
|---|
| 250 | /** search through any pre and post RenderStage that reference a Camera, and take a reference to each of these cameras to prevent them being deleted while they are still be used by the drawing thread.*/ |
|---|
| 251 | void collateReferencesToDependentCameras(); |
|---|
| 252 | |
|---|
| 253 | /** clear the references to any dependent cameras.*/ |
|---|
| 254 | void clearReferencesToDependentCameras(); |
|---|
| 255 | |
|---|
| 256 | /** If State is non-zero, this function releases any associated OpenGL objects for |
|---|
| 257 | * the specified graphics context. Otherwise, releases OpenGL objexts |
|---|
| 258 | * for all graphics contexts. */ |
|---|
| 259 | virtual void releaseGLObjects(osg::State* state= 0) const; |
|---|
| 260 | |
|---|
| 261 | protected: |
|---|
| 262 | |
|---|
| 263 | virtual ~RenderStage(); |
|---|
| 264 | |
|---|
| 265 | typedef std::pair< int , osg::ref_ptr<RenderStage> > RenderStageOrderPair; |
|---|
| 266 | typedef std::list< RenderStageOrderPair > RenderStageList; |
|---|
| 267 | typedef std::vector< osg::ref_ptr<osg::Camera> > Cameras; |
|---|
| 268 | |
|---|
| 269 | bool _stageDrawnThisFrame; |
|---|
| 270 | RenderStageList _preRenderList; |
|---|
| 271 | RenderStageList _postRenderList; |
|---|
| 272 | |
|---|
| 273 | Cameras _dependentCameras; |
|---|
| 274 | |
|---|
| 275 | // viewport x,y,width,height. |
|---|
| 276 | osg::ref_ptr<osg::Viewport> _viewport; |
|---|
| 277 | osg::ref_ptr<const osg::RefMatrix> _initialViewMatrix; |
|---|
| 278 | |
|---|
| 279 | GLenum _drawBuffer; |
|---|
| 280 | bool _drawBufferApplyMask; |
|---|
| 281 | GLenum _readBuffer; |
|---|
| 282 | bool _readBufferApplyMask; |
|---|
| 283 | GLbitfield _clearMask; |
|---|
| 284 | osg::ref_ptr<osg::ColorMask> _colorMask; |
|---|
| 285 | osg::Vec4 _clearColor; |
|---|
| 286 | osg::Vec4 _clearAccum; |
|---|
| 287 | double _clearDepth; |
|---|
| 288 | int _clearStencil; |
|---|
| 289 | |
|---|
| 290 | bool _cameraRequiresSetUp; |
|---|
| 291 | osg::Camera* _camera; |
|---|
| 292 | |
|---|
| 293 | osg::ref_ptr<osg::Texture> _texture; |
|---|
| 294 | unsigned int _level; |
|---|
| 295 | unsigned int _face; |
|---|
| 296 | |
|---|
| 297 | osg::ref_ptr<osg::Image> _image; |
|---|
| 298 | GLenum _imageReadPixelFormat; |
|---|
| 299 | GLenum _imageReadPixelDataType; |
|---|
| 300 | |
|---|
| 301 | std::map< osg::Camera::BufferComponent, Attachment> _bufferAttachmentMap; |
|---|
| 302 | |
|---|
| 303 | osg::ref_ptr<osg::FrameBufferObject> _fbo; |
|---|
| 304 | osg::ref_ptr<osg::FrameBufferObject> _resolveFbo; |
|---|
| 305 | osg::ref_ptr<osg::GraphicsContext> _graphicsContext; |
|---|
| 306 | bool _disableFboAfterRender; |
|---|
| 307 | |
|---|
| 308 | mutable osg::Matrix _inheritedPositionalStateContainerMatrix; |
|---|
| 309 | mutable osg::ref_ptr<PositionalStateContainer> _inheritedPositionalStateContainer; |
|---|
| 310 | mutable osg::ref_ptr<PositionalStateContainer> _renderStageLighting; |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | }; |
|---|
| 314 | |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | #endif |
|---|
| 318 | |
|---|