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