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