source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h @ 3242

Revision 3242, 6.1 KB checked in by mattausch, 15 years ago (diff)
Line 
1#ifndef _DEFERREDRENDERER_H__
2#define _DEFERREDRENDERER_H__
3
4#include "common.h"
5#include "Matrix4x4.h"
6#include "Vector3.h"
7
8namespace CHCDemoEngine
9{
10
11class FrameBufferObject;
12class Vector3;
13class PerspectiveCamera;
14class Matrix4x4;
15class ShadowMap;
16class DirectionalLight;
17
18
19
20
21/** This class implements a deferred shading algorithm that takes
22        a frame buffer object as input and outputs an image in the given size
23*/
24class DeferredRenderer
25{
26public:
27        /** Constructor for a deferred shader taking the requested output image size,
28                the current camera, and if the ssao should be full or half resolution
29        */
30        DeferredRenderer(int w, int h, PerspectiveCamera *cam, bool ssaoUsefulResolution);
31        /** Destructor
32        */
33        ~DeferredRenderer();
34        /** The main render function
35            Currently our fbo consists of one combined color + depth buffer, a normal buffer,
36                and a buffer holding the difference of the pixel positions from the last frame.
37
38                If a shadowMap is specified that is not NULL, the shadow mapped shading algorithm is applied.
39        */
40        void Render(FrameBufferObject *fbo,
41                                DirectionalLight *light,
42                                ShadowMap *shadowMap = NULL
43                                );
44
45       
46        enum SAMPLING_METHOD {SAMPLING_POISSON, SAMPLING_QUADRATIC, SAMPLING_DEFAULT};
47        /** Use ssao or ssao + color bleeding
48        */
49        enum SHADING_METHOD {DEFAULT, SSAO, GI};
50        /** Set the samplig method for the indirect illumination
51        */
52        void SetSamplingMethod(SAMPLING_METHOD s);
53        /** Set the shading method (SSAO, SSAO + color bleeding
54        */
55        void SetShadingMethod(SHADING_METHOD s);
56        /** Sort the samples in order to provide faster texture accesses.
57        */
58        //void SetSortSamples(bool sortSamples);
59        /** Sets ssao sample intensity.
60        */
61        void SetSampleIntensity(float sampleIntensity);
62        /** Sets ssao kernel radius.
63        */
64        void SetKernelRadius(float kernelRadius);
65        /** Sets ssao filter radius.
66        */
67        void SetSsaoFilterRadius(float radius);
68        /** Passes the number of pixels that are visible from the sun.
69        */
70        void SetSunVisiblePixels(int visiblePixels);
71        /** If true temporal coherence is used for ssao
72        */
73        void SetUseTemporalCoherence(bool temporal);
74        /** if set to something other than -1 the current frame is stored on disc
75                using the specified frame number
76        */
77        void SetSaveFrame(const std::string &suffix, int frameNumber);
78        /** Sets the maximal visible distance.
79        */
80        void SetMaxDistance(float maxDist);
81        /**     Enables / disables toneMapping
82        */
83        void SetUseToneMapping(bool toneMapping);
84       
85        /** Enable antiAliasing if some basic edge antialiasing should be performed
86        */
87        void SetUseAntiAliasing(bool antiAliasing);
88        /** Enables / disables depth of field.
89        */
90        void SetUseDepthOfField(bool dof);
91        /**
92                The temporal coherence factor is the maximal number of ssao samples that are accumulated
93                without losing any prior sample information.
94                Set this number too low and flickering can be seen, too
95                high and the adaption to some changes in the ssao might be too slow. Usually from about
96                1000 samples a flickering cannot be seen.
97        */
98        void SetTemporalCoherenceFactorForSsao(float factor);
99
100
101        // hack: store the color buffer idx for the currently used flip flop-MRT here
102        // TODO matt: make this less hacky
103        static int colorBufferIdx;
104
105
106protected:
107
108        void ComputeSsao(FrameBufferObject *fbo, float tempCohFactor);
109
110        void ComputeGlobIllum(FrameBufferObject *fbo, float tempCohFactor);
111
112        void FirstPass(FrameBufferObject *fbo, DirectionalLight *light);
113
114        void FirstPassShadow(FrameBufferObject *fbo, DirectionalLight *light, ShadowMap *shadowMap);
115
116        void ComputeToneParameters(FrameBufferObject *fbo,
117                                                           DirectionalLight *light,
118                                                           float &imageKey,
119                                                           float &whiteLum,
120                                                           float &middleGrey);
121
122        void ToneMap(FrameBufferObject *fbo, float imageKey, float whiteLum, float middleGrey);
123
124
125        void CombineSsao(FrameBufferObject *fbo);
126
127        void CombineIllum(FrameBufferObject *fbo);
128        /** Does some basic antialiasing (searches for edges using a edge detector,
129                smoothes these edges.
130                This function is usually the last function in the pipeline,
131                so one can specify if the frame should be put out directly or stored to
132                another texture.
133        */
134        void AntiAliasing(FrameBufferObject *fbo, DirectionalLight *light, bool displayFrame = true);
135        /** Downsample buffer of fbo to buffer of downSampleFbo. The downSampleFbo must have half the
136                resolution of fbo.
137        */
138        void DownSample(FrameBufferObject *fbo,
139                                        int bufferIdx,
140                                        FrameBufferObject *downSampleFbo,
141                                        int downSampleBufferIdx,
142                                        ShaderProgram *program);
143
144        void DrawQuad(ShaderProgram *p);
145
146        void Output(FrameBufferObject *fbo);
147
148        /** Initialises the deferred shader and loads the required shaders:
149                This function has to be called only once.
150        */
151        void InitCg();
152        /** Is called once per render call and sets the most important parameters.
153        */
154        void InitFrame();
155
156        void FlipFbos(FrameBufferObject *fbo);
157
158        void PrepareSsao(FrameBufferObject *fbo);
159
160        void SortSamples();
161
162        void LenseFlare(FrameBufferObject *fbo, DirectionalLight *light);
163 
164        void PrepareSsaoFilter();
165
166        void SaveFrame(FrameBufferObject *fbo);
167
168        void DepthOfField(FrameBufferObject *fbo);
169
170
171
172        ////////////
173
174        /// deferred shading output image width
175        int mWidth;
176        /// deferred shading output image height
177        int mHeight;
178
179        //////////////////
180
181
182        PerspectiveCamera *mCamera;
183
184        bool mUseTemporalCoherence;
185
186        int mSamplingMethod;
187
188        int mShadingMethod;
189
190        bool mRegenerateSamples;
191
192        int mIllumFboIndex;
193        /// the fbo for indirect illumination (ssao + color bleeding)
194        FrameBufferObject *mIllumFbo;
195
196        FrameBufferObject *mDownSampleFbo;
197
198        FBOContainer mFBOs;
199
200        Matrix4x4 mProjViewMatrix;
201        Matrix4x4 mOldProjViewMatrix;
202
203        Vector3 mCornersView[4];
204        Vector3 mOldCornersView[4];
205
206        Vector3 mEyePos;
207        Vector3 mOldEyePos;
208
209        bool mSortSamples;
210
211        float mKernelRadius;
212        float mSsaoFilterRadius;
213        float mSampleIntensity;
214
215        int mSunVisiblePixels;
216
217        int mSavedFrameNumber;
218        std::string mSavedFrameSuffix;
219
220        float mMaxDistance;
221
222        float mTempCohFactor;
223        bool mUseToneMapping;
224        bool mUseAntiAliasing;
225        bool mUseDepthOfField;   
226};
227
228
229} // namespace
230
231#endif // _SSAOSHADER_H__
Note: See TracBrowser for help on using the repository browser.