1 | #ifndef _DEFERREDRENDERER_H__
|
---|
2 | #define _DEFERREDRENDERER_H__
|
---|
3 |
|
---|
4 | #include "common.h"
|
---|
5 | #include "Matrix4x4.h"
|
---|
6 | #include "Vector3.h"
|
---|
7 |
|
---|
8 | namespace CHCDemoEngine
|
---|
9 | {
|
---|
10 |
|
---|
11 | class FrameBufferObject;
|
---|
12 | class Vector3;
|
---|
13 | class PerspectiveCamera;
|
---|
14 | class Matrix4x4;
|
---|
15 | class ShadowMap;
|
---|
16 | class 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 | */
|
---|
24 | class DeferredRenderer
|
---|
25 | {
|
---|
26 | public:
|
---|
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 | /** Passes the number of pixels that are visible from the sun.
|
---|
66 | */
|
---|
67 | void SetSunVisiblePixels(int visiblePixels);
|
---|
68 | /** If true temporal coherence is used for ssao
|
---|
69 | */
|
---|
70 | void SetUseTemporalCoherence(bool temporal);
|
---|
71 | /** if set to something other than -1 the current frame is stored on disc
|
---|
72 | using the specified frame number
|
---|
73 | */
|
---|
74 | void SetSaveFrame(const std::string &suffix, int frameNumber);
|
---|
75 | /** Sets the maximal visible distance.
|
---|
76 | */
|
---|
77 | void SetMaxDistance(float maxDist);
|
---|
78 | /** Enables / disables toneMapping
|
---|
79 | */
|
---|
80 | void SetUseToneMapping(bool toneMapping);
|
---|
81 |
|
---|
82 | /** Enable antiAliasing if some basic edge antialiasing should be performed
|
---|
83 | */
|
---|
84 | void SetUseAntiAliasing(bool antiAliasing);
|
---|
85 | /** Enables / disables depth of field.
|
---|
86 | */
|
---|
87 | void SetUseDepthOfField(bool dof);
|
---|
88 | /**
|
---|
89 | The temporal coherence factor is the maximal number of ssao samples that are accumulated
|
---|
90 | without losing any prior sample information.
|
---|
91 | Set this number too low and flickering can be seen, too
|
---|
92 | high and the adaption to some changes in the ssao might be too slow. Usually from about
|
---|
93 | 1000 samples a flickering cannot be seen.
|
---|
94 | */
|
---|
95 | void SetTemporalCoherenceFactorForSsao(float factor);
|
---|
96 |
|
---|
97 | /** Sets the maxinal convergence factor for ssao filter
|
---|
98 | */
|
---|
99 | void SetMaxConvergence(float c);
|
---|
100 |
|
---|
101 | void SetSpatialWeight(float w);
|
---|
102 |
|
---|
103 | // hack: store the color buffer idx for the currently used flip flop-MRT here
|
---|
104 | // TODO matt: make this less hacky
|
---|
105 | static int colorBufferIdx;
|
---|
106 |
|
---|
107 |
|
---|
108 | protected:
|
---|
109 |
|
---|
110 | void ComputeSsao(FrameBufferObject *fbo, float tempCohFactor);
|
---|
111 |
|
---|
112 | void ComputeGlobIllum(FrameBufferObject *fbo, float tempCohFactor);
|
---|
113 |
|
---|
114 | void FirstPass(FrameBufferObject *fbo, DirectionalLight *light);
|
---|
115 |
|
---|
116 | void FirstPassShadow(FrameBufferObject *fbo, DirectionalLight *light, ShadowMap *shadowMap);
|
---|
117 |
|
---|
118 | void ComputeToneParameters(FrameBufferObject *fbo,
|
---|
119 | DirectionalLight *light,
|
---|
120 | float &imageKey,
|
---|
121 | float &whiteLum,
|
---|
122 | float &middleGrey);
|
---|
123 |
|
---|
124 | void ToneMap(FrameBufferObject *fbo, float imageKey, float whiteLum, float middleGrey);
|
---|
125 |
|
---|
126 |
|
---|
127 | void CombineSsao(FrameBufferObject *fbo);
|
---|
128 |
|
---|
129 | void CombineIllum(FrameBufferObject *fbo);
|
---|
130 | /** Does some basic antialiasing (searches for edges using a edge detector,
|
---|
131 | smoothes these edges.
|
---|
132 | This function is usually the last function in the pipeline,
|
---|
133 | so one can specify if the frame should be put out directly or stored to
|
---|
134 | another texture.
|
---|
135 | */
|
---|
136 | void AntiAliasing(FrameBufferObject *fbo, DirectionalLight *light, bool displayFrame = true);
|
---|
137 | /** Downsample buffer of fbo to buffer of downSampleFbo. The downSampleFbo must have half the
|
---|
138 | resolution of fbo.
|
---|
139 | */
|
---|
140 | void DownSample(FrameBufferObject *fbo,
|
---|
141 | int bufferIdx,
|
---|
142 | FrameBufferObject *downSampleFbo,
|
---|
143 | int downSampleBufferIdx,
|
---|
144 | ShaderProgram *program);
|
---|
145 |
|
---|
146 | void DrawQuad(ShaderProgram *p);
|
---|
147 |
|
---|
148 | void Output(FrameBufferObject *fbo);
|
---|
149 |
|
---|
150 | /** Initialises the deferred shader and loads the required shaders:
|
---|
151 | This function has to be called only once.
|
---|
152 | */
|
---|
153 | void InitCg();
|
---|
154 | /** Is called once per render call and sets the most important parameters.
|
---|
155 | */
|
---|
156 | void InitFrame();
|
---|
157 |
|
---|
158 | void FlipFbos(FrameBufferObject *fbo);
|
---|
159 |
|
---|
160 | void PrepareSsao(FrameBufferObject *fbo);
|
---|
161 |
|
---|
162 | void SortSamples();
|
---|
163 |
|
---|
164 | void LenseFlare(FrameBufferObject *fbo, DirectionalLight *light);
|
---|
165 |
|
---|
166 | void SaveFrame(FrameBufferObject *fbo);
|
---|
167 |
|
---|
168 | void DepthOfField(FrameBufferObject *fbo);
|
---|
169 |
|
---|
170 | void FilterSsao(FrameBufferObject *fbo);
|
---|
171 |
|
---|
172 |
|
---|
173 | ////////////
|
---|
174 |
|
---|
175 | /// deferred shading output image width
|
---|
176 | int mWidth;
|
---|
177 | /// deferred shading output image height
|
---|
178 | int mHeight;
|
---|
179 |
|
---|
180 | //////////////////
|
---|
181 |
|
---|
182 |
|
---|
183 | PerspectiveCamera *mCamera;
|
---|
184 |
|
---|
185 | bool mUseTemporalCoherence;
|
---|
186 |
|
---|
187 | int mSamplingMethod;
|
---|
188 |
|
---|
189 | int mShadingMethod;
|
---|
190 |
|
---|
191 | bool mRegenerateSamples;
|
---|
192 |
|
---|
193 | int mIllumFboIndex;
|
---|
194 | /// the fbo for indirect illumination (ssao + color bleeding)
|
---|
195 | FrameBufferObject *mIllumFbo;
|
---|
196 |
|
---|
197 | FrameBufferObject *mDownSampleFbo;
|
---|
198 | FrameBufferObject *mTempFbo;
|
---|
199 |
|
---|
200 | FBOContainer mFBOs;
|
---|
201 |
|
---|
202 | Matrix4x4 mProjViewMatrix;
|
---|
203 | Matrix4x4 mOldProjViewMatrix;
|
---|
204 |
|
---|
205 | Vector3 mCornersView[4];
|
---|
206 | Vector3 mOldCornersView[4];
|
---|
207 |
|
---|
208 | Vector3 mEyePos;
|
---|
209 | Vector3 mOldEyePos;
|
---|
210 |
|
---|
211 | bool mSortSamples;
|
---|
212 |
|
---|
213 | float mKernelRadius;
|
---|
214 | float mSampleIntensity;
|
---|
215 |
|
---|
216 | int mSunVisiblePixels;
|
---|
217 |
|
---|
218 | int mSavedFrameNumber;
|
---|
219 | std::string mSavedFrameSuffix;
|
---|
220 |
|
---|
221 | float mMaxDistance;
|
---|
222 |
|
---|
223 | float mTempCohFactor;
|
---|
224 | bool mUseToneMapping;
|
---|
225 | bool mUseAntiAliasing;
|
---|
226 | bool mUseDepthOfField;
|
---|
227 |
|
---|
228 | bool mSsaoUseFullResolution;
|
---|
229 |
|
---|
230 | float mMaxConvergence;
|
---|
231 | float mSpatialWeight;
|
---|
232 | };
|
---|
233 |
|
---|
234 |
|
---|
235 | } // namespace
|
---|
236 |
|
---|
237 | #endif // _SSAOSHADER_H__ |
---|