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

Revision 3232, 5.7 KB checked in by mattausch, 16 years ago (diff)

started dof implementation (not working yet!)

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
91        // hack: store the color buffer idx for the currently used flip flop-MRT here
92        // TODO matt: make this less hacky
93        static int colorBufferIdx;
94
95
96protected:
97
98        void ComputeSsao(FrameBufferObject *fbo, float tempCohFactor);
99
100        void ComputeGlobIllum(FrameBufferObject *fbo, float tempCohFactor);
101
102        void FirstPass(FrameBufferObject *fbo, DirectionalLight *light);
103
104        void FirstPassShadow(FrameBufferObject *fbo, DirectionalLight *light, ShadowMap *shadowMap);
105
106        void ComputeToneParameters(FrameBufferObject *fbo,
107                                                           DirectionalLight *light,
108                                                           float &imageKey,
109                                                           float &whiteLum,
110                                                           float &middleGrey);
111
112        void ToneMap(FrameBufferObject *fbo, float imageKey, float whiteLum, float middleGrey);
113
114
115        void CombineSsao(FrameBufferObject *fbo);
116        void CombineIllum(FrameBufferObject *fbo);
117        /** Does some basic antialiasing (searches for edges using a edge detector,
118                smoothes these edges.
119                This function is usually the last function in the pipeline,
120                so one can specify if the frame should be put out directly or stored to
121                another texture.
122        */
123        void AntiAliasing(FrameBufferObject *fbo, DirectionalLight *light, bool displayFrame = true);
124        /** Downsample buffer of fbo to buffer of downSampleFbo. The downSampleFbo must have half the
125                resolution of fbo.
126        */
127        void DownSample(FrameBufferObject *fbo,
128                                        int bufferIdx,
129                                        FrameBufferObject *downSampleFbo,
130                                        int downSampleBufferIdx,
131                                        ShaderProgram *program);
132
133        void DrawQuad(ShaderProgram *p);
134
135        void Output(FrameBufferObject *fbo);
136
137        /** Initialises the deferred shader and loads the required shaders:
138                This function has to be called only once.
139        */
140        void InitCg();
141        /** Is called once per render call and sets the most important parameters.
142        */
143        void InitFrame();
144
145        void FlipFbos(FrameBufferObject *fbo);
146
147        void PrepareSsao(FrameBufferObject *fbo);
148
149        void SortSamples();
150
151        void LenseFlare(FrameBufferObject *fbo, DirectionalLight *light);
152 
153        void PrepareSsaoFilter();
154
155        void SaveFrame(FrameBufferObject *fbo);
156
157        void DepthOfField(FrameBufferObject *fbo);
158
159
160        ////////////
161
162        /// deferred shading output image width
163        int mWidth;
164        /// deferred shading output image height
165        int mHeight;
166
167        //////////////////
168
169
170        PerspectiveCamera *mCamera;
171
172        bool mUseTemporalCoherence;
173
174        int mSamplingMethod;
175
176        int mShadingMethod;
177
178        bool mRegenerateSamples;
179
180        int mIllumFboIndex;
181        /// the fbo for indirect illumination (ssao + color bleeding)
182        FrameBufferObject *mIllumFbo;
183
184        FrameBufferObject *mDownSampleFbo;
185
186        FBOContainer mFBOs;
187
188        Matrix4x4 mProjViewMatrix;
189        Matrix4x4 mOldProjViewMatrix;
190
191        Vector3 mCornersView[4];
192        Vector3 mOldCornersView[4];
193
194        Vector3 mEyePos;
195        Vector3 mOldEyePos;
196
197        bool mSortSamples;
198
199        float mKernelRadius;
200        float mSsaoFilterRadius;
201        float mSampleIntensity;
202
203        int mSunVisiblePixels;
204
205        int mSavedFrameNumber;
206        std::string mSavedFrameSuffix;
207};
208
209
210} // namespace
211
212#endif // _SSAOSHADER_H__
Note: See TracBrowser for help on using the repository browser.