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