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

Revision 3235, 5.8 KB checked in by mattausch, 16 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                Set useToneMapping to true if tone mapping should be applied
39                Set useAntiAliasing true if some basic edge antialiasing should be performed
40                If a shadowMap is specified that is not NULL, the shadow mapped shading algorithm is applied.
41
42                The temporal coherence factor is the maximal number of ssao samples that are accumulated
43                without losing any prior sample information.
44                Set this number too low and flickering can be seen, too
45                high and the adaption to some changes in the ssao might be too slow. Usually from about
46                1000 samples a flickering cannot be seen.
47        */
48        void Render(FrameBufferObject *fbo,
49                                float tempCohFactor,
50                                DirectionalLight *light,
51                                bool useToneMapping,
52                                bool useAntiAliasing,
53                                ShadowMap *shadowMap = NULL
54                                );
55
56       
57        enum SAMPLING_METHOD {SAMPLING_POISSON, SAMPLING_QUADRATIC, SAMPLING_DEFAULT};
58        /** Use ssao or ssao + color bleeding
59        */
60        enum SHADING_METHOD {DEFAULT, SSAO, GI};
61        /** Set the samplig method for the indirect illumination
62        */
63        void SetSamplingMethod(SAMPLING_METHOD s);
64        /** Set the shading method (SSAO, SSAO + color bleeding
65        */
66        void SetShadingMethod(SHADING_METHOD s);
67        /** Sort the samples so texture access is faster
68        */
69        void SetSortSamples(bool sortSamples);
70        /** Sets ssao sample intensity.
71        */
72        void SetSampleIntensity(float sampleIntensity);
73        /** Sets ssao kernel radius.
74        */
75        void SetKernelRadius(float kernelRadius);
76        /** Sets ssao filter radius.
77        */
78        void SetSsaoFilterRadius(float radius);
79        /** Sets the number of visible pixels of the sun
80        */
81        void SetSunVisiblePixels(int visiblePixels);
82        /** If true tem poral coherence is used for ssao
83        */
84        void SetUseTemporalCoherence(bool temporal);
85        /** if set to something other than -1 the current frame is stored on disc
86                using the specified frame number
87        */
88        void SetSaveFrame(const std::string &suffix, int frameNumber);
89
90        void SetMaxDistance(float maxDist);
91
92
93        // hack: store the color buffer idx for the currently used flip flop-MRT here
94        // TODO matt: make this less hacky
95        static int colorBufferIdx;
96
97
98protected:
99
100        void ComputeSsao(FrameBufferObject *fbo, float tempCohFactor);
101
102        void ComputeGlobIllum(FrameBufferObject *fbo, float tempCohFactor);
103
104        void FirstPass(FrameBufferObject *fbo, DirectionalLight *light);
105
106        void FirstPassShadow(FrameBufferObject *fbo, DirectionalLight *light, ShadowMap *shadowMap);
107
108        void ComputeToneParameters(FrameBufferObject *fbo,
109                                                           DirectionalLight *light,
110                                                           float &imageKey,
111                                                           float &whiteLum,
112                                                           float &middleGrey);
113
114        void ToneMap(FrameBufferObject *fbo, float imageKey, float whiteLum, float middleGrey);
115
116
117        void CombineSsao(FrameBufferObject *fbo);
118        void CombineIllum(FrameBufferObject *fbo);
119        /** Does some basic antialiasing (searches for edges using a edge detector,
120                smoothes these edges.
121                This function is usually the last function in the pipeline,
122                so one can specify if the frame should be put out directly or stored to
123                another texture.
124        */
125        void AntiAliasing(FrameBufferObject *fbo, DirectionalLight *light, bool displayFrame = true);
126        /** Downsample buffer of fbo to buffer of downSampleFbo. The downSampleFbo must have half the
127                resolution of fbo.
128        */
129        void DownSample(FrameBufferObject *fbo,
130                                        int bufferIdx,
131                                        FrameBufferObject *downSampleFbo,
132                                        int downSampleBufferIdx,
133                                        ShaderProgram *program);
134
135        void DrawQuad(ShaderProgram *p);
136
137        void Output(FrameBufferObject *fbo);
138
139        /** Initialises the deferred shader and loads the required shaders:
140                This function has to be called only once.
141        */
142        void InitCg();
143        /** Is called once per render call and sets the most important parameters.
144        */
145        void InitFrame();
146
147        void FlipFbos(FrameBufferObject *fbo);
148
149        void PrepareSsao(FrameBufferObject *fbo);
150
151        void SortSamples();
152
153        void LenseFlare(FrameBufferObject *fbo, DirectionalLight *light);
154 
155        void PrepareSsaoFilter();
156
157        void SaveFrame(FrameBufferObject *fbo);
158
159        void DepthOfField(FrameBufferObject *fbo);
160
161
162
163        ////////////
164
165        /// deferred shading output image width
166        int mWidth;
167        /// deferred shading output image height
168        int mHeight;
169
170        //////////////////
171
172
173        PerspectiveCamera *mCamera;
174
175        bool mUseTemporalCoherence;
176
177        int mSamplingMethod;
178
179        int mShadingMethod;
180
181        bool mRegenerateSamples;
182
183        int mIllumFboIndex;
184        /// the fbo for indirect illumination (ssao + color bleeding)
185        FrameBufferObject *mIllumFbo;
186
187        FrameBufferObject *mDownSampleFbo;
188
189        FBOContainer mFBOs;
190
191        Matrix4x4 mProjViewMatrix;
192        Matrix4x4 mOldProjViewMatrix;
193
194        Vector3 mCornersView[4];
195        Vector3 mOldCornersView[4];
196
197        Vector3 mEyePos;
198        Vector3 mOldEyePos;
199
200        bool mSortSamples;
201
202        float mKernelRadius;
203        float mSsaoFilterRadius;
204        float mSampleIntensity;
205
206        int mSunVisiblePixels;
207
208        int mSavedFrameNumber;
209        std::string mSavedFrameSuffix;
210
211        float mMaxDistance;
212};
213
214
215} // namespace
216
217#endif // _SSAOSHADER_H__
Note: See TracBrowser for help on using the repository browser.