1 | #include "DeferredRenderer.h"
|
---|
2 | #include "FrameBufferObject.h"
|
---|
3 | #include "RenderState.h"
|
---|
4 | #include "SampleGenerator.h"
|
---|
5 | #include "Vector3.h"
|
---|
6 | #include "Camera.h"
|
---|
7 | #include "shaderenv.h"
|
---|
8 | #include "Halton.h"
|
---|
9 | #include "ShadowMapping.h"
|
---|
10 | #include "Light.h"
|
---|
11 | #include "ShaderManager.h"
|
---|
12 |
|
---|
13 | #include <IL/il.h>
|
---|
14 | #include <assert.h>
|
---|
15 |
|
---|
16 |
|
---|
17 | #ifdef _CRT_SET
|
---|
18 | #define _CRTDBG_MAP_ALLOC
|
---|
19 | #include <stdlib.h>
|
---|
20 | #include <crtdbg.h>
|
---|
21 |
|
---|
22 | // redefine new operator
|
---|
23 | #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
|
---|
24 | #define new DEBUG_NEW
|
---|
25 | #endif
|
---|
26 |
|
---|
27 |
|
---|
28 | using namespace std;
|
---|
29 |
|
---|
30 |
|
---|
31 | static void startil()
|
---|
32 | {
|
---|
33 | ilInit();
|
---|
34 | assert(ilGetError() == IL_NO_ERROR);
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | static void stopil()
|
---|
39 | {
|
---|
40 | ilShutDown();
|
---|
41 | assert(ilGetError() == IL_NO_ERROR);
|
---|
42 | }
|
---|
43 |
|
---|
44 | namespace CHCDemoEngine
|
---|
45 | {
|
---|
46 |
|
---|
47 | static ShaderProgram *sCgSsaoProgram = NULL;
|
---|
48 | static ShaderProgram *sCgGiProgram = NULL;
|
---|
49 |
|
---|
50 | static ShaderProgram *sCgDeferredProgram = NULL;
|
---|
51 | static ShaderProgram *sCgAntiAliasingProgram = NULL;
|
---|
52 | static ShaderProgram *sCgDeferredShadowProgram = NULL;
|
---|
53 |
|
---|
54 | static ShaderProgram *sCgCombineSsaoProgram = NULL;
|
---|
55 | static ShaderProgram *sCgCombineIllumProgram = NULL;
|
---|
56 | static ShaderProgram *sCgLogLumProgram = NULL;
|
---|
57 | static ShaderProgram *sCgToneProgram = NULL;
|
---|
58 | static ShaderProgram *sCgDownSampleProgram = NULL;
|
---|
59 | static ShaderProgram *sCgScaleDepthProgram = NULL;
|
---|
60 | static ShaderProgram *sCgPrepareSsaoProgram = NULL;
|
---|
61 |
|
---|
62 |
|
---|
63 | static GLuint noiseTex2D = 0;
|
---|
64 | static GLuint noiseTex1D = 0;
|
---|
65 |
|
---|
66 |
|
---|
67 | // ssao random spherical samples
|
---|
68 | static Sample2 samples2[NUM_SAMPLES];
|
---|
69 | // number of pcf tabs
|
---|
70 | static Sample2 pcfSamples[NUM_PCF_TABS];
|
---|
71 |
|
---|
72 |
|
---|
73 | static float ssaoFilterOffsets[NUM_SSAO_FILTERSAMPLES * 2];
|
---|
74 | static float ssaoFilterWeights[NUM_SSAO_FILTERSAMPLES];
|
---|
75 |
|
---|
76 |
|
---|
77 | int DeferredRenderer::colorBufferIdx = 0;
|
---|
78 |
|
---|
79 |
|
---|
80 | /** Helper method that computes the view vectors in the corners of the current view frustum.
|
---|
81 | */
|
---|
82 | static void ComputeViewVectors(PerspectiveCamera *cam, Vector3 &bl, Vector3 &br, Vector3 &tl, Vector3 &tr)
|
---|
83 | {
|
---|
84 | Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr;
|
---|
85 | cam->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
|
---|
86 |
|
---|
87 | bl = Normalize(nbl - fbl);
|
---|
88 | br = Normalize(nbr - fbr);
|
---|
89 | tl = Normalize(ntl - ftl);
|
---|
90 | tr = Normalize(ntr - ftr);
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | static float GaussianDistribution(float x, float y, float rho)
|
---|
95 | {
|
---|
96 | float g = 1.0f / sqrtf(2.0f * M_PI * rho * rho);
|
---|
97 | g *= expf( -(x * x + y * y) / (2.0f * rho * rho));
|
---|
98 |
|
---|
99 | return g;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | static void PrintGLerror(char *msg)
|
---|
104 | {
|
---|
105 | GLenum errCode;
|
---|
106 | const GLubyte *errStr;
|
---|
107 |
|
---|
108 | if ((errCode = glGetError()) != GL_NO_ERROR)
|
---|
109 | {
|
---|
110 | errStr = gluErrorString(errCode);
|
---|
111 | fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | static void ComputeSampleOffsets(float *sampleOffsets,
|
---|
117 | int imageW, int imageH,
|
---|
118 | float width,
|
---|
119 | int samples)
|
---|
120 | {
|
---|
121 | const float xoffs = width / (float)imageW;
|
---|
122 | const float yoffs = width / (float)imageH;
|
---|
123 |
|
---|
124 | const int numSamples = (int)sqrt((float)samples);
|
---|
125 | const int startSamples = -numSamples / 2;
|
---|
126 | const int endSamples = numSamples + startSamples - 1;
|
---|
127 | //cout << startSamples << " " << endSamples << endl;
|
---|
128 |
|
---|
129 | int idx = 0;
|
---|
130 |
|
---|
131 | for (int x = startSamples; x <= endSamples; ++ x)
|
---|
132 | {
|
---|
133 | for (int y = startSamples; y <= endSamples; ++ y)
|
---|
134 | {
|
---|
135 | sampleOffsets[idx + 0] = (float)x * xoffs;
|
---|
136 | sampleOffsets[idx + 1] = (float)y * yoffs;
|
---|
137 | idx += 2;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | void DeferredRenderer::FlipFbos(FrameBufferObject *fbo)
|
---|
144 | {
|
---|
145 | fbo->Bind();
|
---|
146 | colorBufferIdx = 3 - colorBufferIdx;
|
---|
147 | glDrawBuffers(1, mrt + colorBufferIdx);
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | void DeferredRenderer::DrawQuad(ShaderProgram *p)
|
---|
152 | {
|
---|
153 | if (p) p->Bind();
|
---|
154 |
|
---|
155 | // interpolate the view vector
|
---|
156 | Vector3 bl = mCornersView[0];
|
---|
157 | Vector3 br = mCornersView[1];
|
---|
158 | Vector3 tl = mCornersView[2];
|
---|
159 | Vector3 tr = mCornersView[3];
|
---|
160 |
|
---|
161 | // note: slightly larger texture could hide ambient occlusion error on border but costs resolution
|
---|
162 | glBegin(GL_QUADS);
|
---|
163 |
|
---|
164 | glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex2f( .0f, .0f);
|
---|
165 | glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex2f(1.0f, .0f);
|
---|
166 | glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex2f(1.0f, 1.0f);
|
---|
167 | glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex2f( .0f, 1.0f);
|
---|
168 |
|
---|
169 | glEnd();
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | /** Generate poisson disc distributed sample points on the unit disc
|
---|
174 | */
|
---|
175 | static void GenerateSamples(int sampling)
|
---|
176 | {
|
---|
177 | switch (sampling)
|
---|
178 | {
|
---|
179 | case DeferredRenderer::SAMPLING_POISSON:
|
---|
180 | {
|
---|
181 | PoissonDiscSampleGenerator2 poisson(NUM_SAMPLES, 1.0f);
|
---|
182 | poisson.Generate((float *)samples2);
|
---|
183 | }
|
---|
184 | break;
|
---|
185 | case DeferredRenderer::SAMPLING_QUADRATIC:
|
---|
186 | {
|
---|
187 | QuadraticDiscSampleGenerator2 g(NUM_SAMPLES, 1.0f);
|
---|
188 | g.Generate((float *)samples2);
|
---|
189 | }
|
---|
190 | break;
|
---|
191 | default: // SAMPLING_DEFAULT
|
---|
192 | {
|
---|
193 | RandomSampleGenerator2 g(NUM_SAMPLES, 1.0f);
|
---|
194 | g.Generate((float *)samples2);
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | static void CreateNoiseTex2D(int w, int h)
|
---|
201 | {
|
---|
202 | //GLubyte *randomNormals = new GLubyte[mWidth * mHeight * 3];
|
---|
203 | float *randomNormals = new float[w * h * 3];
|
---|
204 |
|
---|
205 | static HaltonSequence halton;
|
---|
206 | float r[2];
|
---|
207 |
|
---|
208 | for (int i = 0; i < w * h * 3; i += 3)
|
---|
209 | {
|
---|
210 | // create random samples on a circle
|
---|
211 | r[0] = RandomValue(0, 1);
|
---|
212 | //halton.GetNext(1, r);
|
---|
213 |
|
---|
214 | const float theta = 2.0f * acos(sqrt(1.0f - r[0]));
|
---|
215 |
|
---|
216 | randomNormals[i + 0] = cos(theta);
|
---|
217 | randomNormals[i + 1] = sin(theta);
|
---|
218 | randomNormals[i + 2] = 0;
|
---|
219 | }
|
---|
220 |
|
---|
221 | glEnable(GL_TEXTURE_2D);
|
---|
222 | glGenTextures(1, &noiseTex2D);
|
---|
223 | glBindTexture(GL_TEXTURE_2D, noiseTex2D);
|
---|
224 |
|
---|
225 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
226 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
227 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
---|
228 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
---|
229 |
|
---|
230 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, w, h, 0, GL_RGB, GL_FLOAT, randomNormals);
|
---|
231 |
|
---|
232 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
233 | glDisable(GL_TEXTURE_2D);
|
---|
234 |
|
---|
235 | delete [] randomNormals;
|
---|
236 |
|
---|
237 | cout << "created noise texture" << endl;
|
---|
238 |
|
---|
239 | PrintGLerror("noisetexture");
|
---|
240 | }
|
---|
241 |
|
---|
242 |
|
---|
243 | static void CreateNoiseTex1D(int w)
|
---|
244 | {
|
---|
245 | float *randomValues = new float[w * 3];
|
---|
246 |
|
---|
247 | static HaltonSequence halton;
|
---|
248 |
|
---|
249 | randomValues[0] = randomValues[1] = randomValues[2] = 0;
|
---|
250 |
|
---|
251 | for (int i = 3; i < w * 3; i += 3)
|
---|
252 | {
|
---|
253 | // create random samples on a circle
|
---|
254 | randomValues[i + 0] = 20.0f * RandomValue(0, 1) / 512.0f;
|
---|
255 | randomValues[i + 1] = 20.0f * RandomValue(0, 1) / 384.0f;
|
---|
256 | randomValues[i + 2] = 0;
|
---|
257 | }
|
---|
258 |
|
---|
259 | glEnable(GL_TEXTURE_2D);
|
---|
260 | glGenTextures(1, &noiseTex1D);
|
---|
261 | glBindTexture(GL_TEXTURE_2D, noiseTex1D);
|
---|
262 |
|
---|
263 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
264 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
265 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
---|
266 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
---|
267 |
|
---|
268 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, w, 1, 0, GL_RGB, GL_FLOAT, randomValues);
|
---|
269 |
|
---|
270 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
271 | glDisable(GL_TEXTURE_2D);
|
---|
272 |
|
---|
273 | delete [] randomValues;
|
---|
274 |
|
---|
275 | cout << "created noise texture 1D" << endl;
|
---|
276 |
|
---|
277 | PrintGLerror("noisetexture 1D");
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|
281 |
|
---|
282 | DeferredRenderer::DeferredRenderer(int w, int h, PerspectiveCamera *cam):
|
---|
283 | mWidth(w), mHeight(h),
|
---|
284 | mCamera(cam),
|
---|
285 | mUseTemporalCoherence(true),
|
---|
286 | mRegenerateSamples(true),
|
---|
287 | mSamplingMethod(SAMPLING_POISSON),
|
---|
288 | mShadingMethod(DEFAULT),
|
---|
289 | mIllumFboIndex(0)
|
---|
290 | {
|
---|
291 | ///////////
|
---|
292 | //-- the flip-flop fbos
|
---|
293 |
|
---|
294 | const int dsw = w / 2; const int dsh = h / 2;
|
---|
295 | //const int dsw = w; const int dsh = h;
|
---|
296 |
|
---|
297 | mIllumFbo = new FrameBufferObject(dsw, dsh, FrameBufferObject::DEPTH_NONE);
|
---|
298 | //mIllumFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
|
---|
299 |
|
---|
300 | mFBOs.push_back(mIllumFbo);
|
---|
301 |
|
---|
302 | for (int i = 0; i < 4; ++ i)
|
---|
303 | {
|
---|
304 | mIllumFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
|
---|
305 | FrameBufferObject::InitBuffer(mIllumFbo, i);
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | ///////////////
|
---|
310 | //-- the downsampled ssao + color bleeding textures: as gi is inherently low frequency, we can use these to improve performance
|
---|
311 |
|
---|
312 | mDownSampleFbo = new FrameBufferObject(dsw, dsh, FrameBufferObject::DEPTH_NONE);
|
---|
313 | //mDownSampleFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
|
---|
314 | mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
|
---|
315 | // downsample buffer for the normal texture
|
---|
316 | mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
|
---|
317 | // downsample buffer for the offset texture
|
---|
318 | mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
|
---|
319 |
|
---|
320 | for (int i = 0; i < 3; ++ i)
|
---|
321 | {
|
---|
322 | FrameBufferObject::InitBuffer(mDownSampleFbo, i);
|
---|
323 | }
|
---|
324 |
|
---|
325 | mFBOs.push_back(mDownSampleFbo);
|
---|
326 |
|
---|
327 | // create noise texture for ssao
|
---|
328 | // for performance reasons we use a smaller texture and repeat it over the screen
|
---|
329 | CreateNoiseTex2D(mIllumFbo->GetWidth() / 4, mIllumFbo->GetWidth() / 4);
|
---|
330 | //CreateNoiseTex1D(mIllumFbo->GetWidth() / 4);
|
---|
331 |
|
---|
332 | mProjViewMatrix = IdentityMatrix();
|
---|
333 | mOldProjViewMatrix = IdentityMatrix();
|
---|
334 |
|
---|
335 | for (int i = 0; i < 4; ++ i)
|
---|
336 | {
|
---|
337 | mCornersView[i] = mOldCornersView[i] = Vector3::UNIT_X();
|
---|
338 | }
|
---|
339 |
|
---|
340 | mEyePos = mOldEyePos = Vector3::ZERO();
|
---|
341 |
|
---|
342 | InitCg();
|
---|
343 | }
|
---|
344 |
|
---|
345 |
|
---|
346 | DeferredRenderer::~DeferredRenderer()
|
---|
347 | {
|
---|
348 | CLEAR_CONTAINER(mFBOs);
|
---|
349 | glDeleteTextures(1, &noiseTex2D);
|
---|
350 | glDeleteTextures(1, &noiseTex1D);
|
---|
351 | }
|
---|
352 |
|
---|
353 |
|
---|
354 | void DeferredRenderer::SetUseTemporalCoherence(bool temporal)
|
---|
355 | {
|
---|
356 | mUseTemporalCoherence = temporal;
|
---|
357 | }
|
---|
358 |
|
---|
359 |
|
---|
360 |
|
---|
361 | void DeferredRenderer::InitCg()
|
---|
362 | {
|
---|
363 | ShaderManager *sm = ShaderManager::GetSingleton();
|
---|
364 |
|
---|
365 | sCgDeferredProgram = sm->CreateFragmentProgram("deferred", "main", "deferredFrag");
|
---|
366 | sCgDeferredShadowProgram = sm->CreateFragmentProgram("deferred", "main_shadow", "deferredFragShader");
|
---|
367 | sCgSsaoProgram = sm->CreateFragmentProgram("ssao", "main", "ssaoFrag");
|
---|
368 | sCgGiProgram = sm->CreateFragmentProgram("globillum", "main", "giFrag");
|
---|
369 | sCgCombineIllumProgram = sm->CreateFragmentProgram("globillum", "combine", "combineGi");
|
---|
370 | sCgCombineSsaoProgram = sm->CreateFragmentProgram("combineSsao", "combine", "combineSsao");
|
---|
371 | sCgAntiAliasingProgram = sm->CreateFragmentProgram("antialiasing", "main", "antiAliasing");
|
---|
372 | sCgToneProgram = sm->CreateFragmentProgram("tonemap", "ToneMap", "toneMap");
|
---|
373 | sCgDownSampleProgram = sm->CreateFragmentProgram("deferred", "Output", "Output");
|
---|
374 | sCgScaleDepthProgram = sm->CreateFragmentProgram("deferred", "ScaleDepth", "ScaleDepth");
|
---|
375 | sCgLogLumProgram = sm->CreateFragmentProgram("tonemap", "CalcAvgLogLum", "avgLogLum");
|
---|
376 | sCgPrepareSsaoProgram = sm->CreateFragmentProgram("deferred", "PrepareSsao", "PrepareSsao");
|
---|
377 |
|
---|
378 |
|
---|
379 | ///////////////////
|
---|
380 | //-- initialize program parameters
|
---|
381 |
|
---|
382 | string ssaoParams[] =
|
---|
383 | {"colors", "normals", "oldTex", "noiseTex", "temporalCoherence",
|
---|
384 | "samples", "bl", "br", "tl", "tr",
|
---|
385 | "modelViewProj", "oldModelViewProj", "oldEyePos", "oldbl", "oldbr",
|
---|
386 | "oldtl", "oldtr", "attribsTex"};
|
---|
387 | sCgSsaoProgram->AddParameters(ssaoParams, 0, 18);
|
---|
388 |
|
---|
389 | string giParams[] =
|
---|
390 | {"colors", "normals", "noiseTex", "oldSsaoTex", "oldIllumTex",
|
---|
391 | "temporalCoherence", "samples", "bl", "br", "tl",
|
---|
392 | "tr", "oldModelViewProj", "modelViewProj"};
|
---|
393 | sCgGiProgram->AddParameters(giParams, 0, 13);
|
---|
394 |
|
---|
395 | string toneParams[] = {"colors", "imageKey", "whiteLum", "middleGrey"};
|
---|
396 | sCgToneProgram->AddParameters(toneParams, 0, 4);
|
---|
397 |
|
---|
398 |
|
---|
399 | ////////////////
|
---|
400 |
|
---|
401 | string deferredShadowParams[] =
|
---|
402 | {"colors", "normals", "shadowMap", "noiseTex", "shadowMatrix",
|
---|
403 | "sampleWidth", "lightDir", "eyePos", "samples", "weights"};
|
---|
404 |
|
---|
405 | sCgDeferredShadowProgram->AddParameters(deferredShadowParams, 0, 10);
|
---|
406 |
|
---|
407 | ////////////////
|
---|
408 |
|
---|
409 | string combineIllumParams[] = {"colorsTex", "ssaoTex", "illumTex"};
|
---|
410 | sCgCombineIllumProgram->AddParameters(combineIllumParams, 0, 3);
|
---|
411 |
|
---|
412 | ////////////////
|
---|
413 |
|
---|
414 | string combineSsaoParams[] =
|
---|
415 | {"colorsTex", "normalsTex", "ssaoTex", "filterOffs",
|
---|
416 | "filterWeights", "modelViewProj"};
|
---|
417 | sCgCombineSsaoProgram->AddParameters(combineSsaoParams, 0, 6);
|
---|
418 |
|
---|
419 | //////////////
|
---|
420 |
|
---|
421 | string deferredParams[] = {"colors", "normals", "lightDir"};
|
---|
422 | sCgDeferredProgram->AddParameters(deferredParams, 0, 3);
|
---|
423 |
|
---|
424 | ///////////////////
|
---|
425 |
|
---|
426 | string aaParams[] = {"colors", "normals", "offsets"};
|
---|
427 | sCgAntiAliasingProgram->AddParameters(aaParams, 0, 3);
|
---|
428 |
|
---|
429 | /////////////////////
|
---|
430 |
|
---|
431 | string downSampleParams[] = {"colors"};
|
---|
432 | sCgDownSampleProgram->AddParameters(downSampleParams, 0, 1);
|
---|
433 |
|
---|
434 | /////////////////////
|
---|
435 |
|
---|
436 | string scaleDepthParams[] = {"colors"};
|
---|
437 | sCgScaleDepthProgram->AddParameters(scaleDepthParams, 0, 1);
|
---|
438 |
|
---|
439 | ////////////
|
---|
440 |
|
---|
441 | sCgLogLumProgram->AddParameter("colors", 0);
|
---|
442 |
|
---|
443 | ////////////////
|
---|
444 |
|
---|
445 |
|
---|
446 | string prepareSsaoParams[] =
|
---|
447 | {"colorsTex", "oldTex", "oldEyePos", "modelViewProj", "oldModelViewProj",
|
---|
448 | "oldbl", "oldbr", "oldtl", "oldtr", "diffVals"};
|
---|
449 |
|
---|
450 | sCgPrepareSsaoProgram->AddParameters(prepareSsaoParams, 0, 10);
|
---|
451 |
|
---|
452 |
|
---|
453 | ////////////////
|
---|
454 |
|
---|
455 |
|
---|
456 | const float filterWidth = 100.0f;
|
---|
457 | //const float filterWidth = 200.0f;
|
---|
458 | //const float filterWidth = 50.0f;
|
---|
459 |
|
---|
460 | PoissonDiscSampleGenerator2 poisson(NUM_SSAO_FILTERSAMPLES, 1.0f);
|
---|
461 | poisson.Generate((float *)ssaoFilterOffsets);
|
---|
462 |
|
---|
463 | const float xoffs = (float)filterWidth / mWidth;
|
---|
464 | const float yoffs = (float)filterWidth / mHeight;
|
---|
465 |
|
---|
466 | for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
|
---|
467 | {
|
---|
468 | float x = ssaoFilterOffsets[2 * i + 0];
|
---|
469 | float y = ssaoFilterOffsets[2 * i + 1];
|
---|
470 |
|
---|
471 | ssaoFilterWeights[i] = GaussianDistribution(x, y, 1.0f);
|
---|
472 | //ssaoFilterWeights[i] = 1.0f;
|
---|
473 |
|
---|
474 | ssaoFilterOffsets[2 * i + 0] *= xoffs;
|
---|
475 | ssaoFilterOffsets[2 * i + 1] *= yoffs;
|
---|
476 | }
|
---|
477 |
|
---|
478 |
|
---|
479 | /////////
|
---|
480 | //-- pcf tabs for shadowing
|
---|
481 |
|
---|
482 | float filterWeights[NUM_PCF_TABS];
|
---|
483 | PoissonDiscSampleGenerator2 poisson2(NUM_PCF_TABS, 1.0f);
|
---|
484 | poisson2.Generate((float *)pcfSamples);
|
---|
485 |
|
---|
486 | for (int i = 0; i < NUM_PCF_TABS; ++ i)
|
---|
487 | {
|
---|
488 | filterWeights[i] = GaussianDistribution(pcfSamples[i].x, pcfSamples[i].y, 1.0f);
|
---|
489 | }
|
---|
490 |
|
---|
491 | sCgDeferredShadowProgram->SetArray2f(8, (float *)pcfSamples, NUM_PCF_TABS);
|
---|
492 | sCgDeferredShadowProgram->SetArray1f(9, (float *)filterWeights, NUM_PCF_TABS);
|
---|
493 |
|
---|
494 | PrintGLerror("init");
|
---|
495 | }
|
---|
496 |
|
---|
497 |
|
---|
498 | void DeferredRenderer::Render(FrameBufferObject *fbo,
|
---|
499 | float tempCohFactor,
|
---|
500 | DirectionalLight *light,
|
---|
501 | bool useToneMapping,
|
---|
502 | ShadowMap *shadowMap
|
---|
503 | )
|
---|
504 | {
|
---|
505 | InitFrame();
|
---|
506 |
|
---|
507 | if (shadowMap)
|
---|
508 | FirstPassShadow(fbo, light, shadowMap);
|
---|
509 | else
|
---|
510 | FirstPass(fbo, light);
|
---|
511 |
|
---|
512 | if (mShadingMethod != 0)
|
---|
513 | {
|
---|
514 | // downsample fbo buffers
|
---|
515 | // colors
|
---|
516 | //DownSample(fbo, colorBufferIdx, mDownSampleFbo, 0, sCgScaleDepthProgram);
|
---|
517 | PrepareSsao(fbo);
|
---|
518 | DownSample(fbo, 1, mDownSampleFbo, 1, sCgDownSampleProgram); // normals
|
---|
519 | DownSample(fbo, 2, mDownSampleFbo, 2, sCgDownSampleProgram); // offsets
|
---|
520 | }
|
---|
521 |
|
---|
522 | // antialiasing of the color buffer
|
---|
523 | //AntiAliasing(fbo, light);
|
---|
524 |
|
---|
525 | switch (mShadingMethod)
|
---|
526 | {
|
---|
527 | case SSAO:
|
---|
528 | ComputeSsao(fbo, tempCohFactor);
|
---|
529 | CombineSsao(fbo);
|
---|
530 | break;
|
---|
531 | case GI:
|
---|
532 | ComputeGlobIllum(fbo, tempCohFactor);
|
---|
533 | CombineIllum(fbo);
|
---|
534 | break;
|
---|
535 | default: // DEFAULT
|
---|
536 | // do nothing: standard deferred shading
|
---|
537 | break;
|
---|
538 | }
|
---|
539 |
|
---|
540 | if (useToneMapping)
|
---|
541 | {
|
---|
542 | float imageKey, whiteLum, middleGrey;
|
---|
543 |
|
---|
544 | ComputeToneParameters(fbo, light, imageKey, whiteLum, middleGrey);
|
---|
545 | ToneMap(fbo, imageKey, whiteLum, middleGrey);
|
---|
546 | }
|
---|
547 |
|
---|
548 | // as multisampling is difficult / costly with deferred shading,
|
---|
549 | // at least do some antialiasing
|
---|
550 | AntiAliasing(fbo, light);
|
---|
551 |
|
---|
552 | // just output the latest buffer
|
---|
553 | //Output(fbo);
|
---|
554 |
|
---|
555 | glEnable(GL_LIGHTING);
|
---|
556 | glDisable(GL_TEXTURE_2D);
|
---|
557 |
|
---|
558 | glMatrixMode(GL_PROJECTION);
|
---|
559 | glPopMatrix();
|
---|
560 |
|
---|
561 | glMatrixMode(GL_MODELVIEW);
|
---|
562 | glPopMatrix();
|
---|
563 |
|
---|
564 | // viewport
|
---|
565 | glPopAttrib();
|
---|
566 |
|
---|
567 | FrameBufferObject::Release();
|
---|
568 | ShaderManager::GetSingleton()->DisableFragmentProfile();
|
---|
569 | }
|
---|
570 |
|
---|
571 |
|
---|
572 | void DeferredRenderer::ComputeSsao(FrameBufferObject *fbo,
|
---|
573 | float tempCohFactor)
|
---|
574 | {
|
---|
575 | GLuint colorsTex, normalsTex, attribsTex;
|
---|
576 |
|
---|
577 | if (0)
|
---|
578 | {
|
---|
579 | colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
580 | normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
581 | attribsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
582 | }
|
---|
583 | else
|
---|
584 | {
|
---|
585 | colorsTex = mDownSampleFbo->GetColorBuffer(0)->GetTexture();
|
---|
586 | normalsTex = mDownSampleFbo->GetColorBuffer(1)->GetTexture();
|
---|
587 | attribsTex = mDownSampleFbo->GetColorBuffer(2)->GetTexture();
|
---|
588 | //attribsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
589 | }
|
---|
590 |
|
---|
591 | // flip flop between illumination buffers
|
---|
592 | GLuint oldTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();
|
---|
593 |
|
---|
594 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
595 | glViewport(0, 0, mIllumFbo->GetWidth(), mIllumFbo->GetHeight());
|
---|
596 |
|
---|
597 | // read the second buffer, write to the first buffer
|
---|
598 | mIllumFbo->Bind();
|
---|
599 | glDrawBuffers(1, mrt + mIllumFboIndex);
|
---|
600 |
|
---|
601 | int i = 0;
|
---|
602 |
|
---|
603 | sCgSsaoProgram->SetTexture(i ++, colorsTex);
|
---|
604 | sCgSsaoProgram->SetTexture(i ++, normalsTex);
|
---|
605 | sCgSsaoProgram->SetTexture(i ++, oldTex);
|
---|
606 | sCgSsaoProgram->SetTexture(i ++, noiseTex2D);
|
---|
607 |
|
---|
608 | sCgSsaoProgram->SetValue1f(i ++, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
|
---|
609 |
|
---|
610 | if (mUseTemporalCoherence || mRegenerateSamples)
|
---|
611 | {
|
---|
612 | mRegenerateSamples = false;
|
---|
613 |
|
---|
614 | // q: should we generate new samples or only rotate the old ones?
|
---|
615 | // in the first case, the sample patterns look nicer, but the kernel
|
---|
616 | // needs longer to converge
|
---|
617 | GenerateSamples(mSamplingMethod);
|
---|
618 | sCgSsaoProgram->SetArray2f(i, (float *)samples2, NUM_SAMPLES);
|
---|
619 | }
|
---|
620 |
|
---|
621 | ++ i;
|
---|
622 |
|
---|
623 | for (int j = 0; j < 4; ++ j, ++ i)
|
---|
624 | sCgSsaoProgram->SetValue3f(i, mCornersView[j].x, mCornersView[j].y, mCornersView[j].z);
|
---|
625 |
|
---|
626 | sCgSsaoProgram->SetMatrix(i ++, mProjViewMatrix);
|
---|
627 | sCgSsaoProgram->SetMatrix(i ++, mOldProjViewMatrix);
|
---|
628 |
|
---|
629 | Vector3 de;
|
---|
630 | de.x = mOldEyePos.x - mEyePos.x;
|
---|
631 | de.y = mOldEyePos.y - mEyePos.y;
|
---|
632 | de.z = mOldEyePos.z - mEyePos.z;
|
---|
633 |
|
---|
634 | sCgSsaoProgram->SetValue3f(i ++, de.x, de.y, de.z);
|
---|
635 |
|
---|
636 | for (int j = 0; j < 4; ++ j, ++ i)
|
---|
637 | sCgSsaoProgram->SetValue3f(i, mOldCornersView[j].x, mOldCornersView[j].y, mOldCornersView[j].z);
|
---|
638 |
|
---|
639 | sCgSsaoProgram->SetTexture(i ++, attribsTex);
|
---|
640 |
|
---|
641 |
|
---|
642 | DrawQuad(sCgSsaoProgram);
|
---|
643 |
|
---|
644 | glPopAttrib();
|
---|
645 |
|
---|
646 | PrintGLerror("ssao first pass");
|
---|
647 | }
|
---|
648 |
|
---|
649 |
|
---|
650 | static void SetVertex(float x, float y, float x_offs, float y_offs)
|
---|
651 | {
|
---|
652 | glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
|
---|
653 | glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
|
---|
654 | glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
|
---|
655 | glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
|
---|
656 | glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
|
---|
657 |
|
---|
658 | glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
|
---|
659 | glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
|
---|
660 |
|
---|
661 | //glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
|
---|
662 | glVertex2f(x, y);
|
---|
663 | }
|
---|
664 |
|
---|
665 |
|
---|
666 | void DeferredRenderer::AntiAliasing(FrameBufferObject *fbo, DirectionalLight *light)
|
---|
667 | {
|
---|
668 | ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
|
---|
669 | GLuint colorsTex = colorBuffer->GetTexture();
|
---|
670 | GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
671 |
|
---|
672 | FrameBufferObject::Release();
|
---|
673 | // read the second buffer, write to the first buffer
|
---|
674 | //FlipFbos(fbo);
|
---|
675 |
|
---|
676 | // the neighbouring texels
|
---|
677 | float xOffs = 1.0f / fbo->GetWidth();
|
---|
678 | float yOffs = 1.0f / fbo->GetHeight();
|
---|
679 |
|
---|
680 | sCgAntiAliasingProgram->SetTexture(0, colorsTex);
|
---|
681 | sCgAntiAliasingProgram->SetTexture(1, normalsTex);
|
---|
682 |
|
---|
683 | float offsets[16];
|
---|
684 | int i = 0;
|
---|
685 |
|
---|
686 | offsets[i] = -xOffs; offsets[i + 1] = yOffs; i += 2; // left top
|
---|
687 | offsets[i] = xOffs; offsets[i + 1] = -yOffs; i += 2; // right bottom
|
---|
688 | offsets[i] = xOffs; offsets[i + 1] = yOffs; i += 2; // right top
|
---|
689 | offsets[i] = -xOffs; offsets[i + 1] = -yOffs; i += 2; // left bottom
|
---|
690 | offsets[i] = -xOffs; offsets[i + 1] = .0f; i += 2; // left
|
---|
691 | offsets[i] = xOffs; offsets[i + 1] = .0f; i += 2; // right
|
---|
692 | offsets[i] = .0f; offsets[i + 1] = yOffs; i += 2; // top
|
---|
693 | offsets[i] = .0f; offsets[i + 1] = -yOffs; i += 2; // bottom
|
---|
694 |
|
---|
695 | sCgAntiAliasingProgram->SetArray2f(2, offsets, 8);
|
---|
696 |
|
---|
697 | DrawQuad(sCgAntiAliasingProgram);
|
---|
698 |
|
---|
699 | PrintGLerror("antialiasing");
|
---|
700 | }
|
---|
701 |
|
---|
702 |
|
---|
703 | void DeferredRenderer::FirstPass(FrameBufferObject *fbo, DirectionalLight *light)
|
---|
704 | {
|
---|
705 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
706 | GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
707 |
|
---|
708 | FlipFbos(fbo);
|
---|
709 |
|
---|
710 | const Vector3 lightDir = -light->GetDirection();
|
---|
711 |
|
---|
712 | sCgDeferredProgram->SetTexture(0, colorsTex);
|
---|
713 | sCgDeferredProgram->SetTexture(1, normalsTex);
|
---|
714 | sCgDeferredProgram->SetValue3f(2, lightDir.x, lightDir.y, lightDir.z);
|
---|
715 |
|
---|
716 | DrawQuad(sCgDeferredProgram);
|
---|
717 |
|
---|
718 | PrintGLerror("deferred shading");
|
---|
719 | }
|
---|
720 |
|
---|
721 |
|
---|
722 | void DeferredRenderer::ComputeGlobIllum(FrameBufferObject *fbo,
|
---|
723 | float tempCohFactor)
|
---|
724 | {
|
---|
725 | #if 0
|
---|
726 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
727 | GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
728 | #else
|
---|
729 | GLuint colorsTex = mDownSampleFbo->GetColorBuffer(0)->GetTexture();
|
---|
730 | GLuint normalsTex = mDownSampleFbo->GetColorBuffer(1)->GetTexture();
|
---|
731 | #endif
|
---|
732 |
|
---|
733 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
734 | glViewport(0, 0, mIllumFbo->GetWidth(), mIllumFbo->GetHeight());
|
---|
735 |
|
---|
736 | // read the second buffer, write to the first buffer
|
---|
737 | mIllumFbo->Bind();
|
---|
738 |
|
---|
739 | glDrawBuffers(2, mrt + mIllumFboIndex);
|
---|
740 |
|
---|
741 | GLuint oldSsaoTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();
|
---|
742 | GLuint oldIllumTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex + 1)->GetTexture();
|
---|
743 |
|
---|
744 | int i = 0;
|
---|
745 |
|
---|
746 | sCgGiProgram->SetTexture(i ++, colorsTex);
|
---|
747 | sCgGiProgram->SetTexture(i ++, normalsTex);
|
---|
748 | sCgGiProgram->SetTexture(i ++, noiseTex2D);
|
---|
749 | sCgGiProgram->SetTexture(i ++, oldSsaoTex);
|
---|
750 | sCgGiProgram->SetTexture(i ++, oldIllumTex);
|
---|
751 |
|
---|
752 | sCgGiProgram->SetValue1f(i ++,
|
---|
753 | (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
|
---|
754 |
|
---|
755 | if (mUseTemporalCoherence || mRegenerateSamples)
|
---|
756 | {
|
---|
757 | mRegenerateSamples = false;
|
---|
758 |
|
---|
759 | // q: should we generate new samples or only rotate the old ones?
|
---|
760 | // in the first case, the sample patterns look nicer, but the kernel
|
---|
761 | // needs longer to converge
|
---|
762 | GenerateSamples(mSamplingMethod);
|
---|
763 |
|
---|
764 | sCgGiProgram->SetArray2f(i ++, (float *)samples2, NUM_SAMPLES);
|
---|
765 | }
|
---|
766 |
|
---|
767 | Vector3 bl = mCornersView[0];
|
---|
768 | Vector3 br = mCornersView[1];
|
---|
769 | Vector3 tl = mCornersView[2];
|
---|
770 | Vector3 tr = mCornersView[3];
|
---|
771 |
|
---|
772 | sCgGiProgram->SetValue3f(i ++, bl.x, bl.y, bl.z);
|
---|
773 | sCgGiProgram->SetValue3f(i ++, br.x, br.y, br.z);
|
---|
774 | sCgGiProgram->SetValue3f(i ++, tl.x, tl.y, tl.z);
|
---|
775 | sCgGiProgram->SetValue3f(i ++, tr.x, tr.y, tr.z);
|
---|
776 |
|
---|
777 | sCgGiProgram->SetMatrix(i ++, mOldProjViewMatrix);
|
---|
778 | sCgGiProgram->SetMatrix(i ++, mProjViewMatrix);
|
---|
779 |
|
---|
780 |
|
---|
781 | DrawQuad(sCgGiProgram);
|
---|
782 |
|
---|
783 | glPopAttrib();
|
---|
784 |
|
---|
785 | PrintGLerror("globillum first pass");
|
---|
786 | }
|
---|
787 |
|
---|
788 |
|
---|
789 | void DeferredRenderer::CombineIllum(FrameBufferObject *fbo)
|
---|
790 | {
|
---|
791 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
792 |
|
---|
793 | GLuint ssaoTex = mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetTexture();
|
---|
794 | GLuint illumTex = mIllumFbo->GetColorBuffer(mIllumFboIndex + 1)->GetTexture();
|
---|
795 |
|
---|
796 | FlipFbos(fbo);
|
---|
797 |
|
---|
798 | sCgCombineIllumProgram->SetTexture(0, colorsTex);
|
---|
799 | sCgCombineIllumProgram->SetTexture(1, ssaoTex);
|
---|
800 | sCgCombineIllumProgram->SetTexture(2, illumTex);
|
---|
801 |
|
---|
802 | DrawQuad(sCgCombineIllumProgram);
|
---|
803 |
|
---|
804 | PrintGLerror("combine");
|
---|
805 | }
|
---|
806 |
|
---|
807 |
|
---|
808 | void DeferredRenderer::CombineSsao(FrameBufferObject *fbo)
|
---|
809 | {
|
---|
810 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
811 | GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
812 | GLuint ssaoTex = mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetTexture();
|
---|
813 |
|
---|
814 | FlipFbos(fbo);
|
---|
815 |
|
---|
816 | int i = 0;
|
---|
817 |
|
---|
818 | sCgCombineSsaoProgram->SetTexture(i ++, colorsTex);
|
---|
819 | sCgCombineSsaoProgram->SetTexture(i ++, normalsTex);
|
---|
820 | sCgCombineSsaoProgram->SetTexture(i ++, ssaoTex);
|
---|
821 |
|
---|
822 | sCgCombineSsaoProgram->SetArray2f(i ++, (float *)ssaoFilterOffsets, NUM_SSAO_FILTERSAMPLES);
|
---|
823 | sCgCombineSsaoProgram->SetArray1f(i ++, (float *)ssaoFilterWeights, NUM_SSAO_FILTERSAMPLES);
|
---|
824 |
|
---|
825 | sCgCombineSsaoProgram->SetMatrix(i++, mProjViewMatrix);
|
---|
826 |
|
---|
827 |
|
---|
828 | DrawQuad(sCgCombineSsaoProgram);
|
---|
829 |
|
---|
830 | PrintGLerror("combine ssao");
|
---|
831 | }
|
---|
832 |
|
---|
833 |
|
---|
834 | void DeferredRenderer::FirstPassShadow(FrameBufferObject *fbo,
|
---|
835 | DirectionalLight *light,
|
---|
836 | ShadowMap *shadowMap)
|
---|
837 | {
|
---|
838 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
839 | GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
840 |
|
---|
841 | GLuint shadowTex = shadowMap->GetDepthTexture();
|
---|
842 |
|
---|
843 | Matrix4x4 shadowMatrix;
|
---|
844 | shadowMap->GetTextureMatrix(shadowMatrix);
|
---|
845 |
|
---|
846 |
|
---|
847 | FlipFbos(fbo);
|
---|
848 |
|
---|
849 | sCgDeferredShadowProgram->SetTexture(0, colorsTex);
|
---|
850 | sCgDeferredShadowProgram->SetTexture(1, normalsTex);
|
---|
851 | sCgDeferredShadowProgram->SetTexture(2, shadowTex);
|
---|
852 | sCgDeferredShadowProgram->SetTexture(3, noiseTex2D);
|
---|
853 | sCgDeferredShadowProgram->SetMatrix(4, shadowMatrix);
|
---|
854 | sCgDeferredShadowProgram->SetValue1f(5, 2.0f / shadowMap->GetSize());
|
---|
855 |
|
---|
856 | const Vector3 lightDir = -light->GetDirection();
|
---|
857 | sCgDeferredShadowProgram->SetValue3f(6, lightDir.x, lightDir.y, lightDir.z);
|
---|
858 | sCgDeferredShadowProgram->SetValue3f(7, mEyePos.x, mEyePos.y, mEyePos.z);
|
---|
859 |
|
---|
860 | DrawQuad(sCgDeferredShadowProgram);
|
---|
861 |
|
---|
862 | PrintGLerror("deferred shading + shadows");
|
---|
863 | }
|
---|
864 |
|
---|
865 |
|
---|
866 | void DeferredRenderer::SetSamplingMethod(SAMPLING_METHOD s)
|
---|
867 | {
|
---|
868 | if (s != mSamplingMethod)
|
---|
869 | {
|
---|
870 | mSamplingMethod = s;
|
---|
871 | mRegenerateSamples = true;
|
---|
872 | }
|
---|
873 | }
|
---|
874 |
|
---|
875 |
|
---|
876 | void DeferredRenderer::SetShadingMethod(SHADING_METHOD s)
|
---|
877 | {
|
---|
878 | if (s != mShadingMethod)
|
---|
879 | {
|
---|
880 | mShadingMethod = s;
|
---|
881 | mRegenerateSamples = true;
|
---|
882 | }
|
---|
883 | }
|
---|
884 |
|
---|
885 |
|
---|
886 | void DeferredRenderer::ComputeToneParameters(FrameBufferObject *fbo,
|
---|
887 | DirectionalLight *light,
|
---|
888 | float &imageKey,
|
---|
889 | float &whiteLum,
|
---|
890 | float &middleGrey)
|
---|
891 | {
|
---|
892 | // hack: estimate value where sky burns out
|
---|
893 | whiteLum = log(WHITE_LUMINANCE);
|
---|
894 |
|
---|
895 | ////////////////////
|
---|
896 | //-- linear interpolate brightness key depending on the current sun position
|
---|
897 |
|
---|
898 | const float minKey = 0.09f;
|
---|
899 | const float maxKey = 0.36f;
|
---|
900 |
|
---|
901 | const float lightIntensity = DotProd(-light->GetDirection(), Vector3::UNIT_Z());
|
---|
902 | middleGrey = lightIntensity * maxKey + (1.0f - lightIntensity) * minKey;
|
---|
903 |
|
---|
904 |
|
---|
905 | //////////
|
---|
906 | //-- compute avg loglum
|
---|
907 |
|
---|
908 | ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
|
---|
909 | GLuint colorsTex = colorBuffer->GetTexture();
|
---|
910 |
|
---|
911 | FlipFbos(fbo);
|
---|
912 |
|
---|
913 | sCgLogLumProgram->SetTexture(0, colorsTex);
|
---|
914 | DrawQuad(sCgLogLumProgram);
|
---|
915 |
|
---|
916 | PrintGLerror("ToneMapParams");
|
---|
917 |
|
---|
918 |
|
---|
919 | ///////////////////
|
---|
920 | //-- compute avg loglum in scene using mipmapping
|
---|
921 |
|
---|
922 | glBindTexture(GL_TEXTURE_2D, fbo->GetColorBuffer(colorBufferIdx)->GetTexture());
|
---|
923 | glGenerateMipmapEXT(GL_TEXTURE_2D);
|
---|
924 | }
|
---|
925 |
|
---|
926 |
|
---|
927 | static void ExportData(float *data, int w, int h)
|
---|
928 | {
|
---|
929 | startil();
|
---|
930 |
|
---|
931 | cout << "w: " << w << " h: " << h << endl;
|
---|
932 | ILstring filename = ILstring("downsample2.jpg");
|
---|
933 | ilRegisterType(IL_FLOAT);
|
---|
934 |
|
---|
935 | const int depth = 1;
|
---|
936 | const int bpp = 4;
|
---|
937 |
|
---|
938 | if (!ilTexImage(w, h, depth, bpp, IL_RGBA, IL_FLOAT, data))
|
---|
939 | {
|
---|
940 | cerr << "IL error " << ilGetError() << endl;
|
---|
941 | stopil();
|
---|
942 | return;
|
---|
943 | }
|
---|
944 |
|
---|
945 | if (!ilSaveImage(filename))
|
---|
946 | {
|
---|
947 | cerr << "TGA write error " << ilGetError() << endl;
|
---|
948 | }
|
---|
949 |
|
---|
950 | stopil();
|
---|
951 | }
|
---|
952 |
|
---|
953 |
|
---|
954 | void DeferredRenderer::PrepareSsao(FrameBufferObject *fbo)
|
---|
955 | {
|
---|
956 | GLuint colorsTex, diffVals, oldTex;
|
---|
957 |
|
---|
958 | colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
959 | //normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
960 | diffVals = fbo->GetColorBuffer(2)->GetTexture();
|
---|
961 | //diffVals = mDownSampleFbo->GetColorBuffer(2)->GetTexture();
|
---|
962 |
|
---|
963 | // flip flop between illumination buffers
|
---|
964 | oldTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();
|
---|
965 |
|
---|
966 | int i = 0;
|
---|
967 |
|
---|
968 | sCgPrepareSsaoProgram->SetTexture(i ++, colorsTex);
|
---|
969 | //sCgSsaoProgram->SetTexture(i ++, normalsTex);
|
---|
970 | sCgPrepareSsaoProgram->SetTexture(i ++, oldTex);
|
---|
971 |
|
---|
972 | Vector3 de;
|
---|
973 | de.x = mOldEyePos.x - mEyePos.x;
|
---|
974 | de.y = mOldEyePos.y - mEyePos.y;
|
---|
975 | de.z = mOldEyePos.z - mEyePos.z;
|
---|
976 |
|
---|
977 | sCgPrepareSsaoProgram->SetValue3f(i ++, de.x, de.y, de.z);
|
---|
978 |
|
---|
979 | sCgPrepareSsaoProgram->SetMatrix(i ++, mProjViewMatrix);
|
---|
980 | sCgPrepareSsaoProgram->SetMatrix(i ++, mOldProjViewMatrix);
|
---|
981 |
|
---|
982 | for (int j = 0; j < 4; ++ j, ++ i)
|
---|
983 | sCgPrepareSsaoProgram->SetValue3f(i, mOldCornersView[j].x, mOldCornersView[j].y, mOldCornersView[j].z);
|
---|
984 |
|
---|
985 | sCgPrepareSsaoProgram->SetTexture(i ++, diffVals);
|
---|
986 |
|
---|
987 |
|
---|
988 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
989 | glViewport(0, 0, mDownSampleFbo->GetWidth(), mDownSampleFbo->GetHeight());
|
---|
990 |
|
---|
991 | mDownSampleFbo->Bind();
|
---|
992 |
|
---|
993 | glDrawBuffers(1, mrt + 0);
|
---|
994 |
|
---|
995 | DrawQuad(sCgPrepareSsaoProgram);
|
---|
996 |
|
---|
997 | glPopAttrib();
|
---|
998 | PrintGLerror("prepareSsao");
|
---|
999 | }
|
---|
1000 |
|
---|
1001 |
|
---|
1002 |
|
---|
1003 | void DeferredRenderer::DownSample(FrameBufferObject *fbo,
|
---|
1004 | int bufferIdx,
|
---|
1005 | FrameBufferObject *downSampleFbo,
|
---|
1006 | int downSampleBufferIdx,
|
---|
1007 | ShaderProgram *program)
|
---|
1008 | {
|
---|
1009 | ColorBufferObject *buffer = fbo->GetColorBuffer(bufferIdx);
|
---|
1010 | GLuint tex = buffer->GetTexture();
|
---|
1011 |
|
---|
1012 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
1013 | glViewport(0, 0, downSampleFbo->GetWidth(), downSampleFbo->GetHeight());
|
---|
1014 |
|
---|
1015 | downSampleFbo->Bind();
|
---|
1016 |
|
---|
1017 | program->SetTexture(0, tex);
|
---|
1018 | glDrawBuffers(1, mrt + downSampleBufferIdx);
|
---|
1019 |
|
---|
1020 | DrawQuad(program);
|
---|
1021 |
|
---|
1022 | glPopAttrib();
|
---|
1023 | PrintGLerror("downsample");
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 |
|
---|
1027 | void DeferredRenderer::ToneMap(FrameBufferObject *fbo,
|
---|
1028 | float imageKey,
|
---|
1029 | float whiteLum,
|
---|
1030 | float middleGrey)
|
---|
1031 | {
|
---|
1032 | ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
|
---|
1033 | GLuint colorsTex = colorBuffer->GetTexture();
|
---|
1034 | //FrameBufferObject::Release();
|
---|
1035 |
|
---|
1036 | FlipFbos(fbo);
|
---|
1037 |
|
---|
1038 | sCgToneProgram->SetTexture(0, colorsTex);
|
---|
1039 | sCgToneProgram->SetValue1f(1, imageKey);
|
---|
1040 | sCgToneProgram->SetValue1f(2, whiteLum);
|
---|
1041 | sCgToneProgram->SetValue1f(3, middleGrey);
|
---|
1042 |
|
---|
1043 | DrawQuad(sCgToneProgram);
|
---|
1044 |
|
---|
1045 | PrintGLerror("ToneMap");
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 |
|
---|
1049 | void DeferredRenderer::Output(FrameBufferObject *fbo)
|
---|
1050 | {
|
---|
1051 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
1052 | glViewport(0, 0, fbo->GetWidth(), fbo->GetHeight());
|
---|
1053 |
|
---|
1054 | ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
|
---|
1055 | GLuint colorsTex = colorBuffer->GetTexture();
|
---|
1056 |
|
---|
1057 | sCgDownSampleProgram->SetTexture(0, colorsTex);
|
---|
1058 |
|
---|
1059 | FrameBufferObject::Release();
|
---|
1060 | DrawQuad(sCgDownSampleProgram);
|
---|
1061 |
|
---|
1062 | PrintGLerror("output");
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 |
|
---|
1066 | void DeferredRenderer::InitFrame()
|
---|
1067 | {
|
---|
1068 | for (int i = 0; i < 4; ++ i)
|
---|
1069 | mOldCornersView[i] = mCornersView[i];
|
---|
1070 |
|
---|
1071 | mOldProjViewMatrix = mProjViewMatrix;
|
---|
1072 | mOldEyePos = mEyePos;
|
---|
1073 | mEyePos = mCamera->GetPosition();
|
---|
1074 |
|
---|
1075 | // hack: temporarily change far to improve precision
|
---|
1076 | const float oldFar = mCamera->GetFar();
|
---|
1077 | const float oldNear = mCamera->GetNear();
|
---|
1078 |
|
---|
1079 |
|
---|
1080 | Matrix4x4 matViewing, matProjection;
|
---|
1081 |
|
---|
1082 |
|
---|
1083 | ///////////////////
|
---|
1084 |
|
---|
1085 |
|
---|
1086 | mCamera->GetViewOrientationMatrix(matViewing);
|
---|
1087 | mCamera->GetProjectionMatrix(matProjection);
|
---|
1088 |
|
---|
1089 | mProjViewMatrix = matViewing * matProjection;
|
---|
1090 | ComputeViewVectors(mCamera, mCornersView[0], mCornersView[1], mCornersView[2], mCornersView[3]);
|
---|
1091 |
|
---|
1092 |
|
---|
1093 | // switch roles of old and new fbo
|
---|
1094 | // the algorihm uses two input fbos, where the one
|
---|
1095 | // contais the color buffer from the last frame,
|
---|
1096 | // the other one will be written
|
---|
1097 |
|
---|
1098 | mIllumFboIndex = 2 - mIllumFboIndex;
|
---|
1099 |
|
---|
1100 | // enable fragment shading
|
---|
1101 | ShaderManager::GetSingleton()->EnableFragmentProfile();
|
---|
1102 |
|
---|
1103 | glDisable(GL_ALPHA_TEST);
|
---|
1104 | glDisable(GL_TEXTURE_2D);
|
---|
1105 | glDisable(GL_LIGHTING);
|
---|
1106 | glDisable(GL_BLEND);
|
---|
1107 | glDisable(GL_DEPTH_TEST);
|
---|
1108 |
|
---|
1109 | glPolygonMode(GL_FRONT, GL_FILL);
|
---|
1110 |
|
---|
1111 | glMatrixMode(GL_PROJECTION);
|
---|
1112 | glPushMatrix();
|
---|
1113 | glLoadIdentity();
|
---|
1114 |
|
---|
1115 | gluOrtho2D(0, 1, 0, 1);
|
---|
1116 |
|
---|
1117 |
|
---|
1118 | glMatrixMode(GL_MODELVIEW);
|
---|
1119 | glPushMatrix();
|
---|
1120 | glLoadIdentity();
|
---|
1121 |
|
---|
1122 |
|
---|
1123 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
1124 | glViewport(0, 0, mWidth, mHeight);
|
---|
1125 |
|
---|
1126 | // revert to old far and near plane
|
---|
1127 | mCamera->SetFar(oldFar);
|
---|
1128 | mCamera->SetNear(oldNear);
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 |
|
---|
1132 | } // namespace
|
---|