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 |
|
---|
86 |
|
---|
87 | // hack: store the color buffer idx for the currently used flip flop-MRT here
|
---|
88 | // TODO matt: make this less hacky
|
---|
89 | static int colorBufferIdx;
|
---|
90 |
|
---|
91 |
|
---|
92 | protected:
|
---|
93 |
|
---|
94 | void ComputeSsao(FrameBufferObject *fbo, float tempCohFactor);
|
---|
95 |
|
---|
96 | void ComputeGlobIllum(FrameBufferObject *fbo, float tempCohFactor);
|
---|
97 |
|
---|
98 | void FirstPass(FrameBufferObject *fbo, DirectionalLight *light);
|
---|
99 |
|
---|
100 | void FirstPassShadow(FrameBufferObject *fbo, DirectionalLight *light, ShadowMap *shadowMap);
|
---|
101 |
|
---|
102 | void ComputeToneParameters(FrameBufferObject *fbo,
|
---|
103 | DirectionalLight *light,
|
---|
104 | float &imageKey,
|
---|
105 | float &whiteLum,
|
---|
106 | float &middleGrey);
|
---|
107 |
|
---|
108 | void ToneMap(FrameBufferObject *fbo, float imageKey, float whiteLum, float middleGrey);
|
---|
109 |
|
---|
110 |
|
---|
111 | void CombineSsao(FrameBufferObject *fbo);
|
---|
112 | void CombineIllum(FrameBufferObject *fbo);
|
---|
113 |
|
---|
114 | void AntiAliasing(FrameBufferObject *fbo, DirectionalLight *light);
|
---|
115 | /** Downsample buffer of fbo to buffer of downSampleFbo. The downSampleFbo must have half the
|
---|
116 | resolution of fbo.
|
---|
117 | */
|
---|
118 | void DownSample(FrameBufferObject *fbo,
|
---|
119 | int bufferIdx,
|
---|
120 | FrameBufferObject *downSampleFbo,
|
---|
121 | int downSampleBufferIdx,
|
---|
122 | ShaderProgram *program);
|
---|
123 |
|
---|
124 | void DrawQuad(ShaderProgram *p);
|
---|
125 |
|
---|
126 | void Output(FrameBufferObject *fbo);
|
---|
127 |
|
---|
128 | /** Initialises the deferred shader and loads the required shaders:
|
---|
129 | This function has to be called only once.
|
---|
130 | */
|
---|
131 | void InitCg();
|
---|
132 | /** Is called once per render call and sets the most important parameters.
|
---|
133 | */
|
---|
134 | void InitFrame();
|
---|
135 |
|
---|
136 | void FlipFbos(FrameBufferObject *fbo);
|
---|
137 |
|
---|
138 | void PrepareSsao(FrameBufferObject *fbo);
|
---|
139 |
|
---|
140 | void SortSamples();
|
---|
141 |
|
---|
142 | void LenseFlare(FrameBufferObject *fbo, DirectionalLight *light);
|
---|
143 |
|
---|
144 | void PrepareSsaoFilter();
|
---|
145 |
|
---|
146 |
|
---|
147 | ////////////
|
---|
148 |
|
---|
149 | /// deferred shading output image width
|
---|
150 | int mWidth;
|
---|
151 | /// deferred shading output image height
|
---|
152 | int mHeight;
|
---|
153 |
|
---|
154 | //////////////////
|
---|
155 |
|
---|
156 |
|
---|
157 | PerspectiveCamera *mCamera;
|
---|
158 |
|
---|
159 | bool mUseTemporalCoherence;
|
---|
160 |
|
---|
161 | int mSamplingMethod;
|
---|
162 |
|
---|
163 | int mShadingMethod;
|
---|
164 |
|
---|
165 | bool mRegenerateSamples;
|
---|
166 |
|
---|
167 | int mIllumFboIndex;
|
---|
168 | /// the fbo for indirect illumination (ssao + color bleeding)
|
---|
169 | FrameBufferObject *mIllumFbo;
|
---|
170 |
|
---|
171 | FrameBufferObject *mDownSampleFbo;
|
---|
172 |
|
---|
173 | FBOContainer mFBOs;
|
---|
174 |
|
---|
175 | Matrix4x4 mProjViewMatrix;
|
---|
176 | Matrix4x4 mOldProjViewMatrix;
|
---|
177 |
|
---|
178 | Vector3 mCornersView[4];
|
---|
179 | Vector3 mOldCornersView[4];
|
---|
180 |
|
---|
181 | Vector3 mEyePos;
|
---|
182 | Vector3 mOldEyePos;
|
---|
183 |
|
---|
184 | bool mSortSamples;
|
---|
185 |
|
---|
186 | float mKernelRadius;
|
---|
187 | float mSsaoFilterRadius;
|
---|
188 | float mSampleIntensity;
|
---|
189 |
|
---|
190 | int mSunVisiblePixels;
|
---|
191 | };
|
---|
192 |
|
---|
193 |
|
---|
194 | } // namespace
|
---|
195 |
|
---|
196 | #endif // _SsaoShader_H__ |
---|