OpenWalnut  1.4.0
WGEPostprocessor.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WGEPOSTPROCESSOR_H
00026 #define WGEPOSTPROCESSOR_H
00027 
00028 #include <vector>
00029 #include <string>
00030 
00031 #include <boost/shared_ptr.hpp>
00032 
00033 #include <osg/ref_ptr>
00034 #include <osg/Node>
00035 #include <osg/Camera>
00036 #include <osg/Texture>
00037 
00038 #include "../offscreen/WGEOffscreenRenderNode.h"
00039 #include "../offscreen/WGEOffscreenRenderPass.h"
00040 #include "../offscreen/WGEOffscreenFinalPass.h"
00041 
00042 #include "../shaders/WGEShaderPropertyDefineOptions.h"
00043 #include "../WGECamera.h"
00044 
00045 #include "../../common/WProperties.h"
00046 #include "../../common/WPrototyped.h"
00047 
00048 /**
00049  * The base class for all custom post-processors. It allows building an own texture processing pipeline for special processings.
00050  */
00051 class WGEPostprocessor: public WPrototyped
00052 {
00053 public:
00054     /**
00055      * This class encapsulates a G-Buffer. Basically, this is a collection of per-pixel geometry information.
00056      */
00057     class PostprocessorInput
00058     {
00059     public:
00060         /**
00061          * Constructs an instance from a given list of textures. The order in the list define color, normal, parameter, tangent, depth. There are
00062          * no restrictions to the input list. If textures are missing, the corresponding textures in the GBuffer are missing.
00063          *
00064          * \param from source list
00065          */
00066         explicit PostprocessorInput( std::vector< osg::ref_ptr< osg::Texture2D > > from );
00067 
00068         /**
00069          * Construct GBuffer with an explicit list of textures.
00070          *
00071          * \param color color texture
00072          * \param normal normal texture
00073          * \param parameter parameter texture
00074          * \param tangent tangent texture
00075          * \param depth depth texture
00076          */
00077         PostprocessorInput( osg::ref_ptr< osg::Texture2D > color,
00078                             osg::ref_ptr< osg::Texture2D > normal,
00079                             osg::ref_ptr< osg::Texture2D > parameter,
00080                             osg::ref_ptr< osg::Texture2D > tangent,
00081                             osg::ref_ptr< osg::Texture2D > depth );
00082 
00083         /**
00084          * Constructor creates empty GBuffer. All textures are un-initialized.
00085          */
00086         PostprocessorInput();
00087 
00088         /**
00089          * Attaches the needed textures to the specified render pass and returns the G-Buffer
00090          *
00091          * \param from the renderpass to attach this to
00092          *
00093          * \return the buffer.
00094          */
00095         static PostprocessorInput attach( osg::ref_ptr< WGEOffscreenRenderPass > from );
00096 
00097         /**
00098          * Attaches these textures to the specified renderpass
00099          *
00100          * \param to attach to this
00101          *
00102          * \return the ID of the NEXT free texture unit you can use
00103          */
00104         size_t bind( osg::ref_ptr< WGEOffscreenRenderPass > to ) const;
00105 
00106         /**
00107          * Color in RGBA
00108          */
00109         osg::ref_ptr< osg::Texture2D > m_colorTexture;
00110 
00111         /**
00112          * Normal in RGB
00113          */
00114         osg::ref_ptr< osg::Texture2D > m_normalTexture;
00115 
00116         /**
00117          * Some not yet defined parameter texture, LUMINANCE only
00118          */
00119         osg::ref_ptr< osg::Texture2D > m_parameterTexture;
00120 
00121         /**
00122          * Tangent in RGB
00123          */
00124         osg::ref_ptr< osg::Texture2D > m_tangentTexture;
00125 
00126         /**
00127          * Depth
00128          */
00129         osg::ref_ptr< osg::Texture2D > m_depthTexture;
00130     };
00131 
00132     /**
00133      * Convenience typedef for an osg::ref_ptr< WGEPostprocessor >.
00134      */
00135     typedef boost::shared_ptr< WGEPostprocessor > SPtr;
00136 
00137     /**
00138      * Convenience typedef for an osg::ref_ptr< const WGEPostprocessor >.
00139      */
00140     typedef boost::shared_ptr< const WGEPostprocessor > ConstSPtr;
00141 
00142     /**
00143      * Type used for returning lists of postprocessor prototypes.
00144      */
00145     typedef std::vector< WGEPostprocessor::SPtr > ProcessorList;
00146 
00147     /**
00148      * Returns a list of all known postprocessor prototypes
00149      *
00150      * \return the list
00151      */
00152     static ProcessorList getPostprocessors();
00153 
00154     /**
00155      * Needs to be called prior to any "getPostprocessors" call. Needed for initialization. This is done by WGraphicsEngine.
00156      */
00157     static void initPostprocessors();
00158 
00159     /**
00160      * Allows adding a postprocessor. After this call, everyone using the WGEPostprocessor can utilize your addded postproc.
00161      *
00162      * \param processor the postprocessor to add
00163      *
00164      * \return the index of the newly added postprocessor.
00165      */
00166     static size_t addPostprocessor( SPtr processor );
00167 
00168     /**
00169      * Create named prototype. You should call this in your prototype constructor.
00170      *
00171      * \param name name of processor
00172      * \param description description.
00173      */
00174     WGEPostprocessor( std::string name, std::string description );
00175 
00176     /**
00177      * Destructor.
00178      */
00179     virtual ~WGEPostprocessor();
00180 
00181     /**
00182      * Create instance. Uses the protected constructor. Implement it if you derive from this class! This is called whenever your postprocessor is
00183      * applied to the standard render-output. You can add your own constructors and creators for other cases.
00184      *
00185      * \param offscreen use this offscreen node to add your texture pass'
00186      * \param gbuffer the input textures you should use
00187      * \returns shared pointer to the created instance
00188      */
00189     virtual SPtr create( osg::ref_ptr< WGEOffscreenRenderNode > offscreen, const PostprocessorInput& gbuffer ) const = 0;
00190 
00191     /**
00192      * Returns the set of properties controlling the post-processing node. You can use them to provide them to the user for example.
00193      *
00194      * \return the properties as a group.
00195      */
00196     virtual WPropGroup getProperties() const;
00197 
00198     /**
00199      * Returns the result texture. Use this to continue processing.
00200      *
00201      * \param idx which output. Each postprocessor returns at least one texture in index 0, which also is the default value
00202      *
00203      * \return the result texture
00204      */
00205     virtual osg::ref_ptr< osg::Texture2D > getOutput( size_t idx = 0 ) const;
00206 
00207     /**
00208      * This processor can produce multiple outputs. Grab them here. This vector always contains at least the first filtered texture in unit 0.
00209      *
00210      * \return the vector as copy.
00211      */
00212     const std::vector< osg::ref_ptr< osg::Texture2D > >& getOutputList() const;
00213 
00214     /**
00215      * Returns the new depth texture. Allows you to modify the depth values. By default, this is NULL. Check this!
00216      *
00217      * \return the depth texture
00218      */
00219     virtual osg::ref_ptr< osg::Texture2D > getDepth() const;
00220 
00221     /**
00222      * Gets the name of this postprocessor.
00223      *
00224      * \return the name.
00225      */
00226     virtual const std::string getName() const;
00227 
00228     /**
00229      * Gets the description for this postprocessor
00230      *
00231      * \return the description
00232      */
00233     virtual const std::string getDescription() const;
00234 
00235     /**
00236      * When this returns true, the viewport size is fixed to the size of the target texture. This is very useful if you want to process high resolution
00237      * images offscreen. You can implement this function in your postprocessor to modify this. Please be aware that this does not modify the
00238      * camera's projection matrix. This is especially important for correct aspect ratios. You can modify the camera dynamically by using
00239      * callbacks.
00240      *
00241      * \return true if fixed.
00242      */
00243     virtual bool getFixedViewportSize() const;
00244 
00245 protected:
00246     /**
00247      * The textures contain the result. Add at least one result texture
00248      */
00249     std::vector< osg::ref_ptr< osg::Texture2D > > m_resultTextures;
00250 
00251     /**
00252      * The texture contains the new depth
00253      */
00254     osg::ref_ptr< osg::Texture2D > m_depthTexture;
00255 
00256     /**
00257      * All the properties of the post-processor.
00258      */
00259     WPropGroup m_properties;
00260 
00261     /**
00262      * A flag denoting whether the effect should be combined with color or not.
00263      */
00264     WPropBool m_effectOnly;
00265 
00266     /**
00267      * Scale the effect prior to blending it.
00268      */
00269     WPropDouble m_effectScale;
00270 
00271     /**
00272      * For convenience, this is a shader preprocessor controlled by m_effectOnly property.
00273      */
00274     WGEShaderPreprocessor::SPtr m_effectOnlyPreprocessor;
00275 private:
00276     /**
00277      * Name string. Set by the constructor.
00278      */
00279     std::string  m_name;
00280 
00281     /**
00282      * Description string. Set by the constructor.
00283      */
00284     std::string m_description;
00285 
00286     /**
00287      * List of all postprocessors. Handled as singleton.
00288      */
00289     static ProcessorList m_postProcessors;
00290 };
00291 
00292 #endif  // WGEPOSTPROCESSOR_H
00293