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 | 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 |
|
---|
96 | protected:
|
---|
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 |
|
---|
158 | ////////////
|
---|
159 |
|
---|
160 | /// deferred shading output image width
|
---|
161 | int mWidth;
|
---|
162 | /// deferred shading output image height
|
---|
163 | int mHeight;
|
---|
164 |
|
---|
165 | //////////////////
|
---|
166 |
|
---|
167 |
|
---|
168 | PerspectiveCamera *mCamera;
|
---|
169 |
|
---|
170 | bool mUseTemporalCoherence;
|
---|
171 |
|
---|
172 | int mSamplingMethod;
|
---|
173 |
|
---|
174 | int mShadingMethod;
|
---|
175 |
|
---|
176 | bool mRegenerateSamples;
|
---|
177 |
|
---|
178 | int mIllumFboIndex;
|
---|
179 | /// the fbo for indirect illumination (ssao + color bleeding)
|
---|
180 | FrameBufferObject *mIllumFbo;
|
---|
181 |
|
---|
182 | FrameBufferObject *mDownSampleFbo;
|
---|
183 |
|
---|
184 | FBOContainer mFBOs;
|
---|
185 |
|
---|
186 | Matrix4x4 mProjViewMatrix;
|
---|
187 | Matrix4x4 mOldProjViewMatrix;
|
---|
188 |
|
---|
189 | Vector3 mCornersView[4];
|
---|
190 | Vector3 mOldCornersView[4];
|
---|
191 |
|
---|
192 | Vector3 mEyePos;
|
---|
193 | Vector3 mOldEyePos;
|
---|
194 |
|
---|
195 | bool mSortSamples;
|
---|
196 |
|
---|
197 | float mKernelRadius;
|
---|
198 | float mSsaoFilterRadius;
|
---|
199 | float mSampleIntensity;
|
---|
200 |
|
---|
201 | int mSunVisiblePixels;
|
---|
202 |
|
---|
203 | int mSavedFrameNumber;
|
---|
204 | std::string mSavedFrameSuffix;
|
---|
205 | };
|
---|
206 |
|
---|
207 |
|
---|
208 | } // namespace
|
---|
209 |
|
---|
210 | #endif // _SSAOSHADER_H__ |
---|