OpenWalnut  1.4.0
WGEPostprocessor.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WGEPOSTPROCESSOR_H
26 #define WGEPOSTPROCESSOR_H
27 
28 #include <vector>
29 #include <string>
30 
31 #include <boost/shared_ptr.hpp>
32 
33 #include <osg/ref_ptr>
34 #include <osg/Node>
35 #include <osg/Camera>
36 #include <osg/Texture>
37 
38 #include "../offscreen/WGEOffscreenRenderNode.h"
39 #include "../offscreen/WGEOffscreenRenderPass.h"
40 #include "../offscreen/WGEOffscreenFinalPass.h"
41 
42 #include "../shaders/WGEShaderPropertyDefineOptions.h"
43 #include "../WGECamera.h"
44 
45 #include "../../common/WProperties.h"
46 #include "../../common/WPrototyped.h"
47 
48 /**
49  * The base class for all custom post-processors. It allows building an own texture processing pipeline for special processings.
50  */
52 {
53 public:
54  /**
55  * This class encapsulates a G-Buffer. Basically, this is a collection of per-pixel geometry information.
56  */
58  {
59  public:
60  /**
61  * Constructs an instance from a given list of textures. The order in the list define color, normal, parameter, tangent, depth. There are
62  * no restrictions to the input list. If textures are missing, the corresponding textures in the GBuffer are missing.
63  *
64  * \param from source list
65  */
66  explicit PostprocessorInput( std::vector< osg::ref_ptr< osg::Texture2D > > from );
67 
68  /**
69  * Construct GBuffer with an explicit list of textures.
70  *
71  * \param color color texture
72  * \param normal normal texture
73  * \param parameter parameter texture
74  * \param tangent tangent texture
75  * \param depth depth texture
76  */
77  PostprocessorInput( osg::ref_ptr< osg::Texture2D > color,
78  osg::ref_ptr< osg::Texture2D > normal,
79  osg::ref_ptr< osg::Texture2D > parameter,
80  osg::ref_ptr< osg::Texture2D > tangent,
81  osg::ref_ptr< osg::Texture2D > depth );
82 
83  /**
84  * Constructor creates empty GBuffer. All textures are un-initialized.
85  */
87 
88  /**
89  * Attaches the needed textures to the specified render pass and returns the G-Buffer
90  *
91  * \param from the renderpass to attach this to
92  *
93  * \return the buffer.
94  */
95  static PostprocessorInput attach( osg::ref_ptr< WGEOffscreenRenderPass > from );
96 
97  /**
98  * Attaches these textures to the specified renderpass
99  *
100  * \param to attach to this
101  *
102  * \return the ID of the NEXT free texture unit you can use
103  */
104  size_t bind( osg::ref_ptr< WGEOffscreenRenderPass > to ) const;
105 
106  /**
107  * Color in RGBA
108  */
109  osg::ref_ptr< osg::Texture2D > m_colorTexture;
110 
111  /**
112  * Normal in RGB
113  */
114  osg::ref_ptr< osg::Texture2D > m_normalTexture;
115 
116  /**
117  * Some not yet defined parameter texture, LUMINANCE only
118  */
119  osg::ref_ptr< osg::Texture2D > m_parameterTexture;
120 
121  /**
122  * Tangent in RGB
123  */
124  osg::ref_ptr< osg::Texture2D > m_tangentTexture;
125 
126  /**
127  * Depth
128  */
129  osg::ref_ptr< osg::Texture2D > m_depthTexture;
130  };
131 
132  /**
133  * Convenience typedef for an osg::ref_ptr< WGEPostprocessor >.
134  */
135  typedef boost::shared_ptr< WGEPostprocessor > SPtr;
136 
137  /**
138  * Convenience typedef for an osg::ref_ptr< const WGEPostprocessor >.
139  */
140  typedef boost::shared_ptr< const WGEPostprocessor > ConstSPtr;
141 
142  /**
143  * Type used for returning lists of postprocessor prototypes.
144  */
145  typedef std::vector< WGEPostprocessor::SPtr > ProcessorList;
146 
147  /**
148  * Returns a list of all known postprocessor prototypes
149  *
150  * \return the list
151  */
153 
154  /**
155  * Needs to be called prior to any "getPostprocessors" call. Needed for initialization. This is done by WGraphicsEngine.
156  */
157  static void initPostprocessors();
158 
159  /**
160  * Allows adding a postprocessor. After this call, everyone using the WGEPostprocessor can utilize your addded postproc.
161  *
162  * \param processor the postprocessor to add
163  *
164  * \return the index of the newly added postprocessor.
165  */
166  static size_t addPostprocessor( SPtr processor );
167 
168  /**
169  * Create named prototype. You should call this in your prototype constructor.
170  *
171  * \param name name of processor
172  * \param description description.
173  */
174  WGEPostprocessor( std::string name, std::string description );
175 
176  /**
177  * Destructor.
178  */
179  virtual ~WGEPostprocessor();
180 
181  /**
182  * Create instance. Uses the protected constructor. Implement it if you derive from this class! This is called whenever your postprocessor is
183  * applied to the standard render-output. You can add your own constructors and creators for other cases.
184  *
185  * \param offscreen use this offscreen node to add your texture pass'
186  * \param gbuffer the input textures you should use
187  * \returns shared pointer to the created instance
188  */
189  virtual SPtr create( osg::ref_ptr< WGEOffscreenRenderNode > offscreen, const PostprocessorInput& gbuffer ) const = 0;
190 
191  /**
192  * Returns the set of properties controlling the post-processing node. You can use them to provide them to the user for example.
193  *
194  * \return the properties as a group.
195  */
196  virtual WPropGroup getProperties() const;
197 
198  /**
199  * Returns the result texture. Use this to continue processing.
200  *
201  * \param idx which output. Each postprocessor returns at least one texture in index 0, which also is the default value
202  *
203  * \return the result texture
204  */
205  virtual osg::ref_ptr< osg::Texture2D > getOutput( size_t idx = 0 ) const;
206 
207  /**
208  * This processor can produce multiple outputs. Grab them here. This vector always contains at least the first filtered texture in unit 0.
209  *
210  * \return the vector as copy.
211  */
212  const std::vector< osg::ref_ptr< osg::Texture2D > >& getOutputList() const;
213 
214  /**
215  * Returns the new depth texture. Allows you to modify the depth values. By default, this is NULL. Check this!
216  *
217  * \return the depth texture
218  */
219  virtual osg::ref_ptr< osg::Texture2D > getDepth() const;
220 
221  /**
222  * Gets the name of this postprocessor.
223  *
224  * \return the name.
225  */
226  virtual const std::string getName() const;
227 
228  /**
229  * Gets the description for this postprocessor
230  *
231  * \return the description
232  */
233  virtual const std::string getDescription() const;
234 
235  /**
236  * 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
237  * images offscreen. You can implement this function in your postprocessor to modify this. Please be aware that this does not modify the
238  * camera's projection matrix. This is especially important for correct aspect ratios. You can modify the camera dynamically by using
239  * callbacks.
240  *
241  * \return true if fixed.
242  */
243  virtual bool getFixedViewportSize() const;
244 
245 protected:
246  /**
247  * The textures contain the result. Add at least one result texture
248  */
249  std::vector< osg::ref_ptr< osg::Texture2D > > m_resultTextures;
250 
251  /**
252  * The texture contains the new depth
253  */
254  osg::ref_ptr< osg::Texture2D > m_depthTexture;
255 
256  /**
257  * All the properties of the post-processor.
258  */
259  WPropGroup m_properties;
260 
261  /**
262  * A flag denoting whether the effect should be combined with color or not.
263  */
264  WPropBool m_effectOnly;
265 
266  /**
267  * Scale the effect prior to blending it.
268  */
269  WPropDouble m_effectScale;
270 
271  /**
272  * For convenience, this is a shader preprocessor controlled by m_effectOnly property.
273  */
275 private:
276  /**
277  * Name string. Set by the constructor.
278  */
279  std::string m_name;
280 
281  /**
282  * Description string. Set by the constructor.
283  */
284  std::string m_description;
285 
286  /**
287  * List of all postprocessors. Handled as singleton.
288  */
290 };
291 
292 #endif // WGEPOSTPROCESSOR_H
293 
osg::ref_ptr< osg::Texture2D > m_depthTexture
Depth.
virtual const std::string getName() const
Gets the name of this postprocessor.
virtual bool getFixedViewportSize() const
When this returns true, the viewport size is fixed to the size of the target texture.
virtual osg::ref_ptr< osg::Texture2D > getOutput(size_t idx=0) const
Returns the result texture.
osg::ref_ptr< osg::Texture2D > m_parameterTexture
Some not yet defined parameter texture, LUMINANCE only.
This class encapsulates a G-Buffer.
std::string m_name
Name string.
WPropBool m_effectOnly
A flag denoting whether the effect should be combined with color or not.
std::vector< WGEPostprocessor::SPtr > ProcessorList
Type used for returning lists of postprocessor prototypes.
boost::shared_ptr< WGEPostprocessor > SPtr
Convenience typedef for an osg::ref_ptr< WGEPostprocessor >.
virtual const std::string getDescription() const
Gets the description for this postprocessor.
boost::shared_ptr< const WGEPostprocessor > ConstSPtr
Convenience typedef for an osg::ref_ptr< const WGEPostprocessor >.
WGEPostprocessor(std::string name, std::string description)
Create named prototype.
WPropDouble m_effectScale
Scale the effect prior to blending it.
WGEShaderPreprocessor::SPtr m_effectOnlyPreprocessor
For convenience, this is a shader preprocessor controlled by m_effectOnly property.
virtual SPtr create(osg::ref_ptr< WGEOffscreenRenderNode > offscreen, const PostprocessorInput &gbuffer) const =0
Create instance.
Interface class for the concept "Prototype".
Definition: WPrototyped.h:37
PostprocessorInput()
Constructor creates empty GBuffer.
The base class for all custom post-processors.
static void initPostprocessors()
Needs to be called prior to any "getPostprocessors" call.
virtual osg::ref_ptr< osg::Texture2D > getDepth() const
Returns the new depth texture.
size_t bind(osg::ref_ptr< WGEOffscreenRenderPass > to) const
Attaches these textures to the specified renderpass.
osg::ref_ptr< osg::Texture2D > m_normalTexture
Normal in RGB.
static size_t addPostprocessor(SPtr processor)
Allows adding a postprocessor.
std::vector< osg::ref_ptr< osg::Texture2D > > m_resultTextures
The textures contain the result.
osg::ref_ptr< osg::Texture2D > m_depthTexture
The texture contains the new depth.
std::string m_description
Description string.
boost::shared_ptr< WGEShaderPreprocessor > SPtr
Shared pointer for this class.
const std::vector< osg::ref_ptr< osg::Texture2D > > & getOutputList() const
This processor can produce multiple outputs.
static PostprocessorInput attach(osg::ref_ptr< WGEOffscreenRenderPass > from)
Attaches the needed textures to the specified render pass and returns the G-Buffer.
osg::ref_ptr< osg::Texture2D > m_colorTexture
Color in RGBA.
virtual ~WGEPostprocessor()
Destructor.
static ProcessorList m_postProcessors
List of all postprocessors.
osg::ref_ptr< osg::Texture2D > m_tangentTexture
Tangent in RGB.
virtual WPropGroup getProperties() const
Returns the set of properties controlling the post-processing node.
static ProcessorList getPostprocessors()
Returns a list of all known postprocessor prototypes.
WPropGroup m_properties
All the properties of the post-processor.