2012-03-22 01:36:20 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2003-01-22 00:45:36 +08:00
*
2012-03-22 01:36:20 +08:00
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
2003-01-22 00:45:36 +08:00
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
2012-03-22 01:36:20 +08:00
*
2003-01-22 00:45:36 +08:00
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2012-03-22 01:36:20 +08:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2003-01-22 00:45:36 +08:00
* OpenSceneGraph Public License for more details.
*/
2001-10-04 23:12:57 +08:00
2001-09-20 05:19:47 +08:00
#ifndef OSGUTIL_RENDERSTAGE
#define OSGUTIL_RENDERSTAGE 1
2001-12-03 06:20:46 +08:00
#include <osg/ColorMask>
2002-04-13 19:08:04 +08:00
#include <osg/Viewport>
2005-08-17 18:12:49 +08:00
#include <osg/Texture>
#include <osg/FrameBufferObject>
2006-11-27 22:52:07 +08:00
#include <osg/Camera>
2001-09-20 05:19:47 +08:00
#include <osgUtil/RenderBin>
2005-09-02 18:35:38 +08:00
#include <osgUtil/PositionalStateContainer>
2001-09-20 05:19:47 +08:00
namespace osgUtil {
/**
2004-09-27 02:39:34 +08:00
* RenderStage base class. Used for encapsulate a complete stage in
2001-09-20 05:19:47 +08:00
* rendering - setting up of viewport, the projection and model
* matrices and rendering the RenderBin's enclosed with this RenderStage.
2001-09-28 20:36:40 +08:00
* RenderStage also has a dependency list of other RenderStages, each
2001-09-20 05:19:47 +08:00
* of which must be called before the rendering of this stage. These
* 'pre' rendering stages are used for advanced rendering techniques
* like multistage pixel shading or impostors.
*/
class OSGUTIL_EXPORT RenderStage : public RenderBin
{
public:
2012-03-22 01:36:20 +08:00
2001-09-20 05:19:47 +08:00
2005-05-24 23:34:23 +08:00
RenderStage();
RenderStage(SortMode mode);
2012-03-22 01:36:20 +08:00
2002-09-17 23:47:23 +08:00
RenderStage(const RenderStage& rhs,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
2012-03-22 01:36:20 +08:00
2002-12-16 21:40:58 +08:00
virtual osg::Object* cloneType() const { return new RenderStage(); }
virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new RenderStage(*this,copyop); } // note only implements a clone of type.
2001-09-20 05:19:47 +08:00
virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const RenderStage*>(obj)!=0L; }
virtual const char* className() const { return "RenderStage"; }
virtual void reset();
2012-03-22 01:36:20 +08:00
2005-08-17 18:12:49 +08:00
/** Set the draw buffer used at the start of each frame draw. */
2009-08-21 17:34:48 +08:00
void setDrawBuffer(GLenum buffer, bool applyMask = true ) { _drawBuffer = buffer; setDrawBufferApplyMask( applyMask ); }
2005-08-17 18:12:49 +08:00
/** Get the draw buffer used at the start of each frame draw. */
GLenum getDrawBuffer() const { return _drawBuffer; }
2009-08-21 17:34:48 +08:00
/** Get the apply mask defining whether glDrawBuffer is called at each frame draw. */
bool getDrawBufferApplyMask() const { return _drawBufferApplyMask; }
/** Set the apply mask defining whether glDrawBuffer is called at each frame draw. */
void setDrawBufferApplyMask( bool applyMask ) { _drawBufferApplyMask = applyMask; }
2005-08-17 18:12:49 +08:00
/** Set the read buffer for any required copy operations to use. */
2009-08-21 17:34:48 +08:00
void setReadBuffer(GLenum buffer, bool applyMask = true) { _readBuffer = buffer; setReadBufferApplyMask( applyMask ); }
2005-08-17 18:12:49 +08:00
/** Get the read buffer for any required copy operations to use. */
GLenum getReadBuffer() const { return _readBuffer; }
2009-08-21 17:34:48 +08:00
/** Get the apply mask defining whether glReadBuffer is called at each frame draw. */
bool getReadBufferApplyMask() const { return _readBufferApplyMask; }
/** Set the apply mask defining whether glReadBuffer is called at each frame draw. */
void setReadBufferApplyMask( bool applyMask ) { _readBufferApplyMask = applyMask; }
2005-08-17 18:12:49 +08:00
2001-09-22 10:42:08 +08:00
/** Set the viewport.*/
void setViewport(osg::Viewport* viewport) { _viewport = viewport; }
/** Get the const viewport. */
const osg::Viewport* getViewport() const { return _viewport.get(); }
/** Get the viewport. */
osg::Viewport* getViewport() { return _viewport.get(); }
2012-03-22 01:36:20 +08:00
2010-04-19 22:49:42 +08:00
/** Set the initial view matrix.*/
void setInitialViewMatrix(const osg::RefMatrix* matrix) { _initialViewMatrix = matrix; }
2012-03-22 01:36:20 +08:00
2010-04-19 22:49:42 +08:00
/** Get the initial view matrix.*/
const osg::RefMatrix* getInitialViewMatrix() { return _initialViewMatrix.get(); }
2001-09-22 10:42:08 +08:00
2001-09-20 05:19:47 +08:00
/** Set the clear mask used in glClear(..).
* Defaults to GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT. */
2002-09-02 20:31:35 +08:00
void setClearMask(GLbitfield mask) { _clearMask = mask; }
2001-09-20 05:19:47 +08:00
/** Get the clear mask.*/
2002-09-02 20:31:35 +08:00
GLbitfield getClearMask() const { return _clearMask; }
2001-09-20 05:19:47 +08:00
2001-12-03 06:20:46 +08:00
void setColorMask(osg::ColorMask* cm) { _colorMask = cm; }
osg::ColorMask* getColorMask() { return _colorMask.get(); }
const osg::ColorMask* getColorMask() const { return _colorMask.get(); }
2012-03-22 01:36:20 +08:00
/** Set the clear color used in glClearColor(..).
2001-09-20 05:19:47 +08:00
* glClearColor is only called if mask & GL_COLOR_BUFFER_BIT is true*/
void setClearColor(const osg::Vec4& color) { _clearColor=color; }
2012-03-22 01:36:20 +08:00
2001-09-20 05:19:47 +08:00
/** Get the clear color.*/
const osg::Vec4& getClearColor() const { return _clearColor; }
2012-03-22 01:36:20 +08:00
/** Set the clear accum used in glClearAccum(..).
2004-09-27 02:39:34 +08:00
* glClearAcumm is only called if mask & GL_ACCUM_BUFFER_BIT is true. */
2001-09-20 05:19:47 +08:00
void setClearAccum(const osg::Vec4& color) { _clearAccum=color; }
2012-03-22 01:36:20 +08:00
2001-09-20 05:19:47 +08:00
/** Get the clear accum.*/
const osg::Vec4& getClearAccum() const { return _clearAccum; }
2012-03-22 01:36:20 +08:00
2001-09-20 05:19:47 +08:00
/** Set the clear depth used in glClearDepth(..). Defaults to 1.0
2004-09-27 02:39:34 +08:00
* glClearDepth is only called if mask & GL_DEPTH_BUFFER_BIT is true. */
2002-09-02 20:31:35 +08:00
void setClearDepth(double depth) { _clearDepth=depth; }
2012-03-22 01:36:20 +08:00
2001-09-20 05:19:47 +08:00
/** Get the clear depth.*/
2002-09-02 20:31:35 +08:00
double getClearDepth() const { return _clearDepth; }
2001-09-20 05:19:47 +08:00
2004-09-27 02:39:34 +08:00
/** Set the clear stencil value used in glClearStencil(). Defaults to 0;
2001-09-20 05:19:47 +08:00
* glClearStencil is only called if mask & GL_STENCIL_BUFFER_BIT is true*/
2002-09-02 20:31:35 +08:00
void setClearStencil(int stencil) { _clearStencil=stencil; }
2012-03-22 01:36:20 +08:00
2001-09-20 05:19:47 +08:00
/** Get the clear color.*/
2002-09-02 20:31:35 +08:00
int getClearStencil() const { return _clearStencil; }
2012-03-22 01:36:20 +08:00
2001-09-20 05:19:47 +08:00
2006-11-27 22:52:07 +08:00
void setCamera(osg::Camera* camera) { if (_camera!=camera) { _camera = camera; _cameraRequiresSetUp = true; } }
2013-06-21 21:59:18 +08:00
osg::Camera* getCamera() { return _camera.get(); }
const osg::Camera* getCamera() const { return _camera.get(); }
2005-10-06 00:21:51 +08:00
2005-11-02 19:55:02 +08:00
void setCameraRequiresSetUp(bool flag) { _cameraRequiresSetUp = flag; }
2005-10-06 00:21:51 +08:00
bool getCameraRequiresSetUp() const { return _cameraRequiresSetUp; }
2012-03-22 01:36:20 +08:00
2005-10-06 19:26:07 +08:00
/** Attempt the set the RenderStage from the Camera settings.*/
2006-09-19 04:54:48 +08:00
void runCameraSetUp(osg::RenderInfo& renderInfo);
2012-03-22 01:36:20 +08:00
2005-08-17 18:12:49 +08:00
void setTexture(osg::Texture* texture, unsigned int level = 0, unsigned int face=0) { _texture = texture; _level = level; _face = face; }
osg::Texture* getTexture() { return _texture.get(); }
2012-03-22 01:36:20 +08:00
2005-08-17 18:12:49 +08:00
void setImage(osg::Image* image) { _image = image; }
osg::Image* getImage() { return _image.get(); }
2012-03-22 01:36:20 +08:00
2005-08-17 18:12:49 +08:00
void setImageReadPixelFormat(GLenum format) { _imageReadPixelFormat = format; }
GLenum getImageReadPixelFormat() const { return _imageReadPixelFormat; }
void setImageReadPixelDataType(GLenum type) { _imageReadPixelDataType = type; }
GLenum getImageReadPixelDataType() const { return _imageReadPixelDataType; }
2008-06-18 22:09:11 +08:00
/** Set a framebuffer object to render into. It is permissible for the
* framebuffer object to be multisampled, in which case you should also
* set a resolve framebuffer object - see setMultisampleResolveFramebufferObject(). */
2005-08-17 18:12:49 +08:00
void setFrameBufferObject(osg::FrameBufferObject* fbo) { _fbo = fbo; }
osg::FrameBufferObject* getFrameBufferObject() { return _fbo.get(); }
const osg::FrameBufferObject* getFrameBufferObject() const { return _fbo.get(); }
2008-06-18 22:09:11 +08:00
/** Sets the destination framebuffer object for glBlitFramebufferEXT to
* resolve a multisampled framebuffer object after the RenderStage is
* drawn. The resolve framebuffer object must not be multisampled. The
* resolve framebuffer object is only necessary if the primary framebuffer
* object is multisampled, if not then leave it set to null. */
void setMultisampleResolveFramebufferObject(osg::FrameBufferObject* fbo);
osg::FrameBufferObject* getMultisampleResolveFramebufferObject() { return _resolveFbo.get(); }
const osg::FrameBufferObject* getMultisampleResolveFramebufferObject() const { return _resolveFbo.get(); }
/** Set whether the framebuffer object should be unbound after
* rendering. By default this is set to true. Set it to false if the
* unbinding is not required. */
void setDisableFboAfterRender(bool disable) {_disableFboAfterRender = disable;}
bool getDisableFboAfterRender() const {return _disableFboAfterRender;}
2005-08-17 18:12:49 +08:00
void setGraphicsContext(osg::GraphicsContext* context) { _graphicsContext = context; }
osg::GraphicsContext* getGraphicsContext() { return _graphicsContext.get(); }
const osg::GraphicsContext* getGraphicsContext() const { return _graphicsContext.get(); }
2005-09-02 18:35:38 +08:00
void setInheritedPositionalStateContainerMatrix(const osg::Matrix& matrix) { _inheritedPositionalStateContainerMatrix = matrix; }
const osg::Matrix& getInheritedPositionalStateContainerMatrix() const { return _inheritedPositionalStateContainerMatrix; }
2005-07-20 00:30:55 +08:00
2005-09-02 18:35:38 +08:00
void setInheritedPositionalStateContainer(PositionalStateContainer* rsl) { _inheritedPositionalStateContainer = rsl; }
PositionalStateContainer* getInheritedPositionalStateContainer() { return _inheritedPositionalStateContainer.get(); }
2005-07-20 00:30:55 +08:00
2005-09-02 18:35:38 +08:00
void setPositionalStateContainer(PositionalStateContainer* rsl) { _renderStageLighting = rsl; }
2001-09-20 05:19:47 +08:00
2005-09-02 18:35:38 +08:00
PositionalStateContainer* getPositionalStateContainer() const
2001-09-20 05:19:47 +08:00
{
2005-09-02 18:35:38 +08:00
if (!_renderStageLighting.valid()) _renderStageLighting = new PositionalStateContainer;
2001-09-20 05:19:47 +08:00
return _renderStageLighting.get();
}
2003-01-10 17:25:42 +08:00
virtual void addPositionedAttribute(osg::RefMatrix* matrix,const osg::StateAttribute* attr)
2001-09-20 05:19:47 +08:00
{
2005-09-02 18:35:38 +08:00
getPositionalStateContainer()->addPositionedAttribute(matrix,attr);
2001-09-20 05:19:47 +08:00
}
2012-03-22 01:36:20 +08:00
2004-06-17 22:02:15 +08:00
virtual void addPositionedTextureAttribute(unsigned int textureUnit, osg::RefMatrix* matrix,const osg::StateAttribute* attr)
{
2005-09-02 18:35:38 +08:00
getPositionalStateContainer()->addPositionedTextureAttribute(textureUnit, matrix,attr);
2004-06-17 22:02:15 +08:00
}
2006-09-19 04:54:48 +08:00
void copyTexture(osg::RenderInfo& renderInfo);
2005-08-20 16:59:03 +08:00
2005-11-08 19:46:52 +08:00
virtual void sort();
2012-03-22 01:36:20 +08:00
2006-09-19 04:54:48 +08:00
virtual void drawPreRenderStages(osg::RenderInfo& renderInfo,RenderLeaf*& previous);
2002-10-17 21:48:46 +08:00
2006-09-19 04:54:48 +08:00
virtual void draw(osg::RenderInfo& renderInfo,RenderLeaf*& previous);
2005-08-20 16:59:03 +08:00
2006-09-19 04:54:48 +08:00
virtual void drawInner(osg::RenderInfo& renderInfo,RenderLeaf*& previous, bool& doCopyTexture);
2012-03-22 01:36:20 +08:00
2006-09-19 04:54:48 +08:00
virtual void drawPostRenderStages(osg::RenderInfo& renderInfo,RenderLeaf*& previous);
2005-06-15 04:51:35 +08:00
2006-09-19 04:54:48 +08:00
virtual void drawImplementation(osg::RenderInfo& renderInfo,RenderLeaf*& previous);
2001-09-20 05:19:47 +08:00
2005-08-20 16:59:03 +08:00
2005-06-15 04:51:35 +08:00
void addToDependencyList(RenderStage* rs) { addPreRenderStage(rs); }
2006-09-04 21:35:26 +08:00
void addPreRenderStage(RenderStage* rs, int order = 0);
2012-03-22 01:36:20 +08:00
2006-09-04 21:35:26 +08:00
void addPostRenderStage(RenderStage* rs, int order = 0);
2001-09-20 05:19:47 +08:00
2004-09-27 02:39:34 +08:00
/** Extract stats for current draw list. */
2012-03-22 01:36:20 +08:00
bool getStats(Statistics& stats) const;
2007-01-30 06:44:29 +08:00
/** Compute the number of dynamic RenderLeaves.*/
virtual unsigned int computeNumberOfDynamicRenderLeaves() const;
2006-11-27 19:25:40 +08:00
2007-01-30 06:44:29 +08:00
struct Attachment
2006-11-27 19:25:40 +08:00
{
osg::ref_ptr<osg::Image> _image;
GLenum _imageReadPixelFormat;
GLenum _imageReadPixelDataType;
};
2009-06-06 03:05:37 +08:00
2006-11-27 22:52:07 +08:00
void attach(osg::Camera::BufferComponent buffer, osg::Image* image);
2009-06-06 03:05:37 +08:00
/** 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.*/
void collateReferencesToDependentCameras();
2009-06-08 20:44:23 +08:00
/** clear the references to any dependent cameras.*/
2009-06-06 03:05:37 +08:00
void clearReferencesToDependentCameras();
2011-04-20 19:45:01 +08:00
/** If State is non-zero, this function releases any associated OpenGL objects for
* the specified graphics context. Otherwise, releases OpenGL objexts
* for all graphics contexts. */
virtual void releaseGLObjects(osg::State* state= 0) const;
protected:
2009-06-06 03:05:37 +08:00
2004-08-02 20:19:50 +08:00
virtual ~RenderStage();
2006-09-04 21:35:26 +08:00
typedef std::pair< int , osg::ref_ptr<RenderStage> > RenderStageOrderPair;
typedef std::list< RenderStageOrderPair > RenderStageList;
2009-06-06 03:05:37 +08:00
typedef std::vector< osg::ref_ptr<osg::Camera> > Cameras;
2001-09-20 05:19:47 +08:00
bool _stageDrawnThisFrame;
2005-06-15 04:51:35 +08:00
RenderStageList _preRenderList;
RenderStageList _postRenderList;
2001-09-20 05:19:47 +08:00
2009-06-06 03:05:37 +08:00
Cameras _dependentCameras;
2001-09-20 05:19:47 +08:00
// viewport x,y,width,height.
2001-09-22 10:42:08 +08:00
osg::ref_ptr<osg::Viewport> _viewport;
2010-04-19 22:49:42 +08:00
osg::ref_ptr<const osg::RefMatrix> _initialViewMatrix;
2012-03-22 01:36:20 +08:00
2005-08-17 18:12:49 +08:00
GLenum _drawBuffer;
2009-08-21 17:34:48 +08:00
bool _drawBufferApplyMask;
2005-08-17 18:12:49 +08:00
GLenum _readBuffer;
2009-08-21 17:34:48 +08:00
bool _readBufferApplyMask;
2001-09-20 05:19:47 +08:00
GLbitfield _clearMask;
2001-12-03 06:20:46 +08:00
osg::ref_ptr<osg::ColorMask> _colorMask;
2001-09-20 05:19:47 +08:00
osg::Vec4 _clearColor;
osg::Vec4 _clearAccum;
double _clearDepth;
int _clearStencil;
2007-07-15 01:07:59 +08:00
bool _cameraRequiresSetUp;
2013-06-21 21:59:18 +08:00
osg::observer_ptr<osg::Camera> _camera;
2012-03-22 01:36:20 +08:00
2005-08-17 18:12:49 +08:00
osg::ref_ptr<osg::Texture> _texture;
unsigned int _level;
unsigned int _face;
osg::ref_ptr<osg::Image> _image;
GLenum _imageReadPixelFormat;
GLenum _imageReadPixelDataType;
2012-03-22 01:36:20 +08:00
2006-11-27 22:52:07 +08:00
std::map< osg::Camera::BufferComponent, Attachment> _bufferAttachmentMap;
2012-03-22 01:36:20 +08:00
2005-08-17 18:12:49 +08:00
osg::ref_ptr<osg::FrameBufferObject> _fbo;
2008-06-18 22:09:11 +08:00
osg::ref_ptr<osg::FrameBufferObject> _resolveFbo;
2005-08-17 18:12:49 +08:00
osg::ref_ptr<osg::GraphicsContext> _graphicsContext;
2008-06-18 22:09:11 +08:00
bool _disableFboAfterRender;
2005-08-17 18:12:49 +08:00
2005-09-02 18:35:38 +08:00
mutable osg::Matrix _inheritedPositionalStateContainerMatrix;
mutable osg::ref_ptr<PositionalStateContainer> _inheritedPositionalStateContainer;
mutable osg::ref_ptr<PositionalStateContainer> _renderStageLighting;
2009-06-06 03:05:37 +08:00
2001-09-20 05:19:47 +08:00
};
2002-02-03 20:33:41 +08:00
}
2001-09-20 05:19:47 +08:00
#endif