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