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 | #include "Texture.h"
|
---|
13 |
|
---|
14 | #include <math.h>
|
---|
15 |
|
---|
16 | #include <IL/il.h>
|
---|
17 | #include <assert.h>
|
---|
18 |
|
---|
19 |
|
---|
20 | #ifdef _CRT_SET
|
---|
21 | #define _CRTDBG_MAP_ALLOC
|
---|
22 | #include <stdlib.h>
|
---|
23 | #include <crtdbg.h>
|
---|
24 |
|
---|
25 | // redefine new operator
|
---|
26 | #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
|
---|
27 | #define new DEBUG_NEW
|
---|
28 | #endif
|
---|
29 |
|
---|
30 |
|
---|
31 | using namespace std;
|
---|
32 |
|
---|
33 |
|
---|
34 | static void startil()
|
---|
35 | {
|
---|
36 | ilInit();
|
---|
37 | assert(ilGetError() == IL_NO_ERROR);
|
---|
38 | }
|
---|
39 |
|
---|
40 |
|
---|
41 | static void stopil()
|
---|
42 | {
|
---|
43 | ilShutDown();
|
---|
44 | assert(ilGetError() == IL_NO_ERROR);
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | namespace CHCDemoEngine
|
---|
49 | {
|
---|
50 |
|
---|
51 | static ShaderProgram *sCgSsaoProgram = NULL;
|
---|
52 | static ShaderProgram *sCgGiProgram = NULL;
|
---|
53 |
|
---|
54 | static ShaderProgram *sCgDeferredProgram = NULL;
|
---|
55 | static ShaderProgram *sCgAntiAliasingProgram = NULL;
|
---|
56 | static ShaderProgram *sCgDeferredShadowProgram = NULL;
|
---|
57 |
|
---|
58 | static ShaderProgram *sCgCombineSsaoProgram = NULL;
|
---|
59 | static ShaderProgram *sCgCombineIllumProgram = NULL;
|
---|
60 | static ShaderProgram *sCgLogLumProgram = NULL;
|
---|
61 | static ShaderProgram *sCgToneProgram = NULL;
|
---|
62 | static ShaderProgram *sCgDownSampleProgram = NULL;
|
---|
63 | static ShaderProgram *sCgScaleDepthProgram = NULL;
|
---|
64 | static ShaderProgram *sCgPrepareSsaoProgram = NULL;
|
---|
65 | static ShaderProgram *sCgLenseFlareProgram = NULL;
|
---|
66 |
|
---|
67 | static ShaderProgram *sCgDOFProgram = NULL;
|
---|
68 |
|
---|
69 |
|
---|
70 | static GLuint noiseTex2D = 0;
|
---|
71 | static GLuint noiseTex1D = 0;
|
---|
72 |
|
---|
73 |
|
---|
74 | // ssao random spherical samples
|
---|
75 | static Sample2 samples2[NUM_SAMPLES];
|
---|
76 | //#define NUM_PRECOMPUTED_SAMPLES 240
|
---|
77 | //static Sample2 samples2[NUM_PRECOMPUTED_SAMPLES];
|
---|
78 | // pcf samples
|
---|
79 | static Sample2 pcfSamples[NUM_PCF_TABS];
|
---|
80 | // dof samples
|
---|
81 | static Sample2 dofSamples[NUM_DOF_TABS];
|
---|
82 |
|
---|
83 |
|
---|
84 | static float ssaoFilterOffsets[NUM_SSAO_FILTER_SAMPLES * 2];
|
---|
85 | static float ssaoFilterWeights[NUM_SSAO_FILTER_SAMPLES];
|
---|
86 |
|
---|
87 | static Texture *sHaloTex[5];
|
---|
88 |
|
---|
89 | int DeferredRenderer::colorBufferIdx = 0;
|
---|
90 |
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 | /** Helper method that computes the view vectors in the corners of the current view frustum.
|
---|
95 | */
|
---|
96 | static void ComputeViewVectors(PerspectiveCamera *cam, Vector3 &bl, Vector3 &br, Vector3 &tl, Vector3 &tr)
|
---|
97 | {
|
---|
98 | Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr;
|
---|
99 | cam->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
|
---|
100 |
|
---|
101 | bl = Normalize(nbl - fbl);
|
---|
102 | br = Normalize(nbr - fbr);
|
---|
103 | tl = Normalize(ntl - ftl);
|
---|
104 | tr = Normalize(ntr - ftr);
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | static float GaussianDistribution(float x, float y, float rho)
|
---|
109 | {
|
---|
110 | float g = 1.0f / sqrtf(2.0f * M_PI * rho * rho);
|
---|
111 | g *= expf( -(x * x + y * y) / (2.0f * rho * rho));
|
---|
112 |
|
---|
113 | return g;
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | static void PrintGLerror(char *msg)
|
---|
118 | {
|
---|
119 | GLenum errCode;
|
---|
120 | const GLubyte *errStr;
|
---|
121 |
|
---|
122 | if ((errCode = glGetError()) != GL_NO_ERROR)
|
---|
123 | {
|
---|
124 | errStr = gluErrorString(errCode);
|
---|
125 | fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | static Sample2 UnitTest(float x, float y, int wi, int he)
|
---|
131 | {
|
---|
132 | Sample2 s;
|
---|
133 |
|
---|
134 | s.x = float(floor(x * (float)wi - 0.5f) + 1.0f) / (float)wi;
|
---|
135 | s.y = float(floor(y * (float)he - 0.5f) + 1.0f) / (float)he;
|
---|
136 |
|
---|
137 | return s;
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | static void ComputeSampleOffsets(float *sampleOffsets,
|
---|
142 | int imageW, int imageH,
|
---|
143 | float width,
|
---|
144 | int samples)
|
---|
145 | {
|
---|
146 | const float xoffs = width / (float)imageW;
|
---|
147 | const float yoffs = width / (float)imageH;
|
---|
148 |
|
---|
149 | const int numSamples = (int)sqrt((float)samples);
|
---|
150 | const int startSamples = -numSamples / 2;
|
---|
151 | const int endSamples = numSamples + startSamples - 1;
|
---|
152 | //cout << startSamples << " " << endSamples << endl;
|
---|
153 |
|
---|
154 | int idx = 0;
|
---|
155 |
|
---|
156 | for (int x = startSamples; x <= endSamples; ++ x)
|
---|
157 | {
|
---|
158 | for (int y = startSamples; y <= endSamples; ++ y)
|
---|
159 | {
|
---|
160 | sampleOffsets[idx + 0] = (float)x * xoffs;
|
---|
161 | sampleOffsets[idx + 1] = (float)y * yoffs;
|
---|
162 | idx += 2;
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | void DeferredRenderer::FlipFbos(FrameBufferObject *fbo)
|
---|
169 | {
|
---|
170 | fbo->Bind();
|
---|
171 | colorBufferIdx = 3 - colorBufferIdx;
|
---|
172 | glDrawBuffers(1, mrt + colorBufferIdx);
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | void DeferredRenderer::DrawQuad(ShaderProgram *p)
|
---|
177 | {
|
---|
178 | if (p) p->Bind();
|
---|
179 |
|
---|
180 | // interpolate the view vector
|
---|
181 | Vector3 bl = mCornersView[0];
|
---|
182 | Vector3 br = mCornersView[1];
|
---|
183 | Vector3 tl = mCornersView[2];
|
---|
184 | Vector3 tr = mCornersView[3];
|
---|
185 |
|
---|
186 | // note: slightly larger texture could hide ambient occlusion error on border but costs resolution
|
---|
187 | glBegin(GL_QUADS);
|
---|
188 |
|
---|
189 | glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex2f( .0f, .0f);
|
---|
190 | glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex2f(1.0f, .0f);
|
---|
191 | glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex2f(1.0f, 1.0f);
|
---|
192 | glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex2f( .0f, 1.0f);
|
---|
193 |
|
---|
194 | glEnd();
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | /** Generate poisson disc distributed sample points on the unit disc
|
---|
199 | */
|
---|
200 | static void GenerateSamples(int sampling)
|
---|
201 | {
|
---|
202 | switch (sampling)
|
---|
203 | {
|
---|
204 | case DeferredRenderer::SAMPLING_POISSON:
|
---|
205 | {
|
---|
206 | static PoissonDiscSampleGenerator2D poisson(NUM_SAMPLES, 1.0f);
|
---|
207 | poisson.Generate((float *)samples2);
|
---|
208 | }
|
---|
209 | break;
|
---|
210 | case DeferredRenderer::SAMPLING_QUADRATIC:
|
---|
211 | {
|
---|
212 | //static QuadraticDiscSampleGenerator2D g(NUM_PRECOMPUTED_SAMPLES, 1.0f);
|
---|
213 | static QuadraticDiscSampleGenerator2D g(NUM_SAMPLES, 1.0f);
|
---|
214 | g.Generate((float *)samples2);
|
---|
215 | }
|
---|
216 | break;
|
---|
217 | default: // SAMPLING_DEFAULT
|
---|
218 | {
|
---|
219 | static RandomSampleGenerator2D g(NUM_SAMPLES, 1.0f);
|
---|
220 | g.Generate((float *)samples2);
|
---|
221 | }
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | static void CreateNoiseTex2D(int w, int h)
|
---|
227 | {
|
---|
228 | //GLubyte *randomNormals = new GLubyte[mWidth * mHeight * 3];
|
---|
229 | Vector3 *randomNormals = new Vector3[w * h];
|
---|
230 |
|
---|
231 | for (int i = 0; i < w * h; ++ i)
|
---|
232 | {
|
---|
233 | // create random samples on a circle
|
---|
234 | const float r = RandomValue(0, 1);
|
---|
235 |
|
---|
236 | const float theta = 2.0f * acos(sqrt(1.0f - r));
|
---|
237 | //randomNormals[i] = Vector3(cos(theta), sin(theta), 0);
|
---|
238 | randomNormals[i] = Vector3(RandomValue(-M_PI, M_PI), 0, 0);
|
---|
239 | //Normalize(randomNormals[i]);
|
---|
240 | }
|
---|
241 |
|
---|
242 |
|
---|
243 | glEnable(GL_TEXTURE_2D);
|
---|
244 | glGenTextures(1, &noiseTex2D);
|
---|
245 | glBindTexture(GL_TEXTURE_2D, noiseTex2D);
|
---|
246 |
|
---|
247 | //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
248 | //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
249 |
|
---|
250 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
251 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
252 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
---|
253 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
---|
254 |
|
---|
255 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, w, h, 0, GL_RGB, GL_FLOAT, (float *)randomNormals);
|
---|
256 |
|
---|
257 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
258 | glDisable(GL_TEXTURE_2D);
|
---|
259 |
|
---|
260 | delete [] randomNormals;
|
---|
261 |
|
---|
262 | cout << "created noise texture" << endl;
|
---|
263 |
|
---|
264 | PrintGLerror("noisetexture");
|
---|
265 | }
|
---|
266 |
|
---|
267 |
|
---|
268 | static void PrepareLenseFlare()
|
---|
269 | {
|
---|
270 | string textures[] = {"flare4.tga", "lens2.jpg", "lens3.jpg", "lens4.jpg", "lens1.jpg"};
|
---|
271 |
|
---|
272 | // todo: delete halo textures
|
---|
273 | for (int i = 0; i < 5; ++ i)
|
---|
274 | {
|
---|
275 | sHaloTex[i] = new Texture(model_path + textures[i]);
|
---|
276 |
|
---|
277 | sHaloTex[i]->SetBoundaryModeS(Texture::BORDER);
|
---|
278 | sHaloTex[i]->SetBoundaryModeT(Texture::BORDER);
|
---|
279 |
|
---|
280 | sHaloTex[i]->Create();
|
---|
281 | }
|
---|
282 |
|
---|
283 | cout << "prepared lense flare textures" << endl;
|
---|
284 |
|
---|
285 | PrintGLerror("prepare lense flare");
|
---|
286 | }
|
---|
287 |
|
---|
288 |
|
---|
289 | DeferredRenderer::DeferredRenderer(int w, int h,
|
---|
290 | PerspectiveCamera *cam,
|
---|
291 | bool ssaoUseFullResolution):
|
---|
292 | mWidth(w), mHeight(h),
|
---|
293 | mCamera(cam),
|
---|
294 | mUseTemporalCoherence(true),
|
---|
295 | mRegenerateSamples(true),
|
---|
296 | mSamplingMethod(SAMPLING_POISSON),
|
---|
297 | mShadingMethod(DEFAULT),
|
---|
298 | mIllumFboIndex(0),
|
---|
299 | mSortSamples(true),
|
---|
300 | mKernelRadius(1e-8f),
|
---|
301 | mSsaoFilterRadius(12.0f),
|
---|
302 | mSampleIntensity(0.2f),
|
---|
303 | mSunVisiblePixels(0),
|
---|
304 | mSavedFrameNumber(-1),
|
---|
305 | mSavedFrameSuffix(""),
|
---|
306 | mMaxDistance(1e6f),
|
---|
307 | mTempCohFactor(.0f),
|
---|
308 | mUseToneMapping(false),
|
---|
309 | mUseAntiAliasing(false),
|
---|
310 | mUseDepthOfField(false)
|
---|
311 | {
|
---|
312 | ///////////
|
---|
313 | //-- the flip-flop fbos
|
---|
314 |
|
---|
315 | int downSampledWidth, downSampledHeight;
|
---|
316 |
|
---|
317 | if (ssaoUseFullResolution)
|
---|
318 | {
|
---|
319 | downSampledWidth = w; downSampledHeight = h;
|
---|
320 | cout << "using full resolution ssao" << endl;
|
---|
321 | }
|
---|
322 | else
|
---|
323 | {
|
---|
324 | downSampledWidth = w / 2; downSampledHeight = h / 2;
|
---|
325 | cout << "using half resolution ssao" << endl;
|
---|
326 | }
|
---|
327 |
|
---|
328 | mIllumFbo = new FrameBufferObject(downSampledWidth, downSampledHeight, FrameBufferObject::DEPTH_NONE);
|
---|
329 | //mIllumFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
|
---|
330 |
|
---|
331 | mFBOs.push_back(mIllumFbo);
|
---|
332 |
|
---|
333 | for (int i = 0; i < 4; ++ i)
|
---|
334 | {
|
---|
335 | mIllumFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
|
---|
336 | FrameBufferObject::InitBuffer(mIllumFbo, i);
|
---|
337 | }
|
---|
338 |
|
---|
339 |
|
---|
340 | ///////////////
|
---|
341 | //-- the downsampled ssao + color bleeding textures:
|
---|
342 | //-- as GI is mostly low frequency, we can use lower resolution toimprove performance
|
---|
343 |
|
---|
344 | mDownSampleFbo = new FrameBufferObject(downSampledWidth, downSampledHeight, FrameBufferObject::DEPTH_NONE);
|
---|
345 | // the downsampled color + depth buffer
|
---|
346 | mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
|
---|
347 | // downsample buffer for the normal texture
|
---|
348 | mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
|
---|
349 |
|
---|
350 |
|
---|
351 | for (int i = 0; i < 2; ++ i)
|
---|
352 | {
|
---|
353 | FrameBufferObject::InitBuffer(mDownSampleFbo, i);
|
---|
354 | }
|
---|
355 |
|
---|
356 | mFBOs.push_back(mDownSampleFbo);
|
---|
357 |
|
---|
358 | // create noise texture for ssao
|
---|
359 | // for performance reasons we use a smaller texture and repeat it over the screen
|
---|
360 | CreateNoiseTex2D(mIllumFbo->GetWidth() / 4, mIllumFbo->GetWidth() / 4);
|
---|
361 | //CreateNoiseTex2D(mIllumFbo->GetWidth(), mIllumFbo->GetWidth());
|
---|
362 |
|
---|
363 | mProjViewMatrix = IdentityMatrix();
|
---|
364 | mOldProjViewMatrix = IdentityMatrix();
|
---|
365 |
|
---|
366 | for (int i = 0; i < 4; ++ i)
|
---|
367 | {
|
---|
368 | mCornersView[i] = mOldCornersView[i] = Vector3::UNIT_X();
|
---|
369 | }
|
---|
370 |
|
---|
371 | mEyePos = mOldEyePos = Vector3::ZERO();
|
---|
372 |
|
---|
373 | PrepareLenseFlare();
|
---|
374 |
|
---|
375 | InitCg();
|
---|
376 | }
|
---|
377 |
|
---|
378 |
|
---|
379 | DeferredRenderer::~DeferredRenderer()
|
---|
380 | {
|
---|
381 | CLEAR_CONTAINER(mFBOs);
|
---|
382 |
|
---|
383 | glDeleteTextures(1, &noiseTex2D);
|
---|
384 | glDeleteTextures(1, &noiseTex1D);
|
---|
385 | }
|
---|
386 |
|
---|
387 |
|
---|
388 | void DeferredRenderer::InitCg()
|
---|
389 | {
|
---|
390 | ShaderManager *sm = ShaderManager::GetSingleton();
|
---|
391 |
|
---|
392 | sCgDeferredProgram = sm->CreateFragmentProgram("deferred", "main", "DeferredFrag");
|
---|
393 | sCgDeferredShadowProgram = sm->CreateFragmentProgram("deferred", "main_shadow", "DeferredFragShadow");
|
---|
394 | sCgSsaoProgram = sm->CreateFragmentProgram("ssao", "main", "SsaoFrag");
|
---|
395 | sCgGiProgram = sm->CreateFragmentProgram("globillum", "main", "GiFrag");
|
---|
396 | sCgCombineIllumProgram = sm->CreateFragmentProgram("globillum", "combine", "CombineGi");
|
---|
397 | //sCgCombineSsaoProgram = sm->CreateFragmentProgram("combineSsao", "CombineSsaoHalfRes", "CombineSsao");
|
---|
398 | sCgCombineSsaoProgram = sm->CreateFragmentProgram("combineSsao", "CombineSsaoFullRes", "CombineSsao");
|
---|
399 | sCgAntiAliasingProgram = sm->CreateFragmentProgram("antialiasing", "main", "AntiAliasing");
|
---|
400 | sCgToneProgram = sm->CreateFragmentProgram("tonemap", "ToneMap", "ToneMap");
|
---|
401 | sCgDownSampleProgram = sm->CreateFragmentProgram("deferred", "Output", "Output");
|
---|
402 | sCgScaleDepthProgram = sm->CreateFragmentProgram("deferred", "ScaleDepth", "ScaleDepth");
|
---|
403 | sCgLogLumProgram = sm->CreateFragmentProgram("tonemap", "CalcAvgLogLum", "AvgLogLum");
|
---|
404 | sCgPrepareSsaoProgram = sm->CreateFragmentProgram("deferred", "PrepareSsao", "PrepareSsao");
|
---|
405 | sCgLenseFlareProgram = sm->CreateFragmentProgram("lenseFlare", "LenseFlare", "LenseFlare");
|
---|
406 | sCgDOFProgram = sm->CreateFragmentProgram("depthOfField", "DepthOfField", "DepthOfField");
|
---|
407 |
|
---|
408 |
|
---|
409 | ///////////////////
|
---|
410 | //-- initialize program parameters
|
---|
411 |
|
---|
412 | string ssaoParams[] =
|
---|
413 | {"colors", "normals", "oldTex", "noiseTex", "temporalCoherence",
|
---|
414 | "samples", "bl", "br", "tl", "tr",
|
---|
415 | "modelViewProj", "oldModelViewProj", "oldEyePos", "oldbl", "oldbr",
|
---|
416 | "oldtl", "oldtr", "attribsTex", "kernelRadius", "sampleIntensity"};
|
---|
417 | sCgSsaoProgram->AddParameters(ssaoParams, 0, 20);
|
---|
418 |
|
---|
419 | string giParams[] =
|
---|
420 | {"colors", "normals", "noiseTex", "oldSsaoTex", "oldIllumTex",
|
---|
421 | "temporalCoherence", "samples", "bl", "br", "tl",
|
---|
422 | "tr", "oldModelViewProj", "modelViewProj"};
|
---|
423 |
|
---|
424 | sCgGiProgram->AddParameters(giParams, 0, 13);
|
---|
425 |
|
---|
426 | string toneParams[] = {"colors", "imageKey", "whiteLum", "middleGrey"};
|
---|
427 | sCgToneProgram->AddParameters(toneParams, 0, 4);
|
---|
428 |
|
---|
429 |
|
---|
430 | ////////////////
|
---|
431 |
|
---|
432 | string deferredShadowParams[] =
|
---|
433 | {"colors", "normals", "shadowMap", "noiseTex", "shadowMatrix",
|
---|
434 | "sampleWidth", "lightDir", "eyePos", "samples", "weights"};
|
---|
435 |
|
---|
436 | sCgDeferredShadowProgram->AddParameters(deferredShadowParams, 0, 10);
|
---|
437 |
|
---|
438 | ////////////////
|
---|
439 |
|
---|
440 | string combineIllumParams[] = {"colorsTex", "ssaoTex", "illumTex"};
|
---|
441 | sCgCombineIllumProgram->AddParameters(combineIllumParams, 0, 3);
|
---|
442 |
|
---|
443 | ////////////////
|
---|
444 |
|
---|
445 | string combineSsaoParams[] =
|
---|
446 | {"colorsTex", "normalsTex", "ssaoTex", "filterOffs", "filterWeights",
|
---|
447 | "ssaoFilterRadius", "modelViewProj", "bl", "br", "tl",
|
---|
448 | "tr", "w", "h"};
|
---|
449 |
|
---|
450 | sCgCombineSsaoProgram->AddParameters(combineSsaoParams, 0, 13);
|
---|
451 |
|
---|
452 | //////////////
|
---|
453 |
|
---|
454 | string deferredParams[] = {"colors", "normals", "lightDir"};
|
---|
455 | sCgDeferredProgram->AddParameters(deferredParams, 0, 3);
|
---|
456 |
|
---|
457 | ///////////////////
|
---|
458 |
|
---|
459 | string aaParams[] = {"colors", "normals", "offsets"};
|
---|
460 | sCgAntiAliasingProgram->AddParameters(aaParams, 0, 3);
|
---|
461 |
|
---|
462 | /////////////////////
|
---|
463 |
|
---|
464 | string downSampleParams[] = {"colors"};
|
---|
465 | sCgDownSampleProgram->AddParameters(downSampleParams, 0, 1);
|
---|
466 |
|
---|
467 | /////////////////////
|
---|
468 |
|
---|
469 | string scaleDepthParams[] = {"colors"};
|
---|
470 | sCgScaleDepthProgram->AddParameters(scaleDepthParams, 0, 1);
|
---|
471 |
|
---|
472 | ////////////
|
---|
473 |
|
---|
474 | sCgLogLumProgram->AddParameter("colors", 0);
|
---|
475 |
|
---|
476 | ////////////////
|
---|
477 |
|
---|
478 |
|
---|
479 | string prepareSsaoParams[] =
|
---|
480 | {"colorsTex", "normalsTex", "diffVals", "oldTex",
|
---|
481 | "oldEyePos", "modelViewProj", "oldModelViewProj",
|
---|
482 | "oldbl", "oldbr", "oldtl", "oldtr"};
|
---|
483 |
|
---|
484 | sCgPrepareSsaoProgram->AddParameters(prepareSsaoParams, 0, 11);
|
---|
485 |
|
---|
486 |
|
---|
487 | ////////////////
|
---|
488 |
|
---|
489 | string lenseFlareParams[] =
|
---|
490 | {"colorsTex", "flareTex1", "flareTex2", "flareTex3", "flareTex4",
|
---|
491 | "flareTex5", "vectorToLight", "distanceToLight", "sunVisiblePixels"};
|
---|
492 |
|
---|
493 | sCgLenseFlareProgram->AddParameters(lenseFlareParams, 0, 9);
|
---|
494 |
|
---|
495 |
|
---|
496 | ////////////////
|
---|
497 |
|
---|
498 | string dofParams[] = {"colorsTex", "filterOffs", "sceneRange", "zFocus"};
|
---|
499 |
|
---|
500 | sCgDOFProgram->AddParameters(dofParams, 0, 4);
|
---|
501 |
|
---|
502 |
|
---|
503 | ////////////////
|
---|
504 | //-- prepare filter for ssao
|
---|
505 |
|
---|
506 | PrepareSsaoFilter();
|
---|
507 |
|
---|
508 |
|
---|
509 | /////////
|
---|
510 | //-- pcf tabs for shadowing
|
---|
511 |
|
---|
512 | float filterWeights[NUM_PCF_TABS];
|
---|
513 | PoissonDiscSampleGenerator2D poisson2(NUM_PCF_TABS, 1.0f);
|
---|
514 | poisson2.Generate((float *)pcfSamples);
|
---|
515 |
|
---|
516 | for (int i = 0; i < NUM_PCF_TABS; ++ i)
|
---|
517 | {
|
---|
518 | filterWeights[i] = GaussianDistribution(pcfSamples[i].x, pcfSamples[i].y, 1.0f);
|
---|
519 | }
|
---|
520 |
|
---|
521 | sCgDeferredShadowProgram->SetArray2f(8, (float *)pcfSamples, NUM_PCF_TABS);
|
---|
522 | sCgDeferredShadowProgram->SetArray1f(9, (float *)filterWeights, NUM_PCF_TABS);
|
---|
523 |
|
---|
524 |
|
---|
525 | /////////
|
---|
526 | //-- pcf tabs for depth of field
|
---|
527 |
|
---|
528 | // todo matt: it is stupid to put num samples and width of kernel into constructor => change this!!!
|
---|
529 | PoissonDiscSampleGenerator2D poisson3(NUM_DOF_TABS, 1.0f);
|
---|
530 | poisson3.Generate((float *)dofSamples);
|
---|
531 |
|
---|
532 | for (int i = 0; i < NUM_DOF_TABS; ++ i)
|
---|
533 | {
|
---|
534 | dofSamples[i].x *= 1.0f / mWidth;
|
---|
535 | dofSamples[i].y *= 1.0f / mHeight;
|
---|
536 | }
|
---|
537 |
|
---|
538 | //float dofWeights[NUM_PCF_TABS];
|
---|
539 | //sCgDOFProgram->SetArray1f(2, (float *)dofWeights, NUM_DOF_TABS);
|
---|
540 |
|
---|
541 | PrintGLerror("init");
|
---|
542 | }
|
---|
543 |
|
---|
544 |
|
---|
545 | void DeferredRenderer::Render(FrameBufferObject *fbo,
|
---|
546 | DirectionalLight *light,
|
---|
547 | ShadowMap *shadowMap
|
---|
548 | )
|
---|
549 | {
|
---|
550 | InitFrame();
|
---|
551 |
|
---|
552 | if (shadowMap)
|
---|
553 | FirstPassShadow(fbo, light, shadowMap);
|
---|
554 | else
|
---|
555 | FirstPass(fbo, light);
|
---|
556 |
|
---|
557 | if (mShadingMethod != 0)
|
---|
558 | {
|
---|
559 | PrepareSsao(fbo); // downsample fbo buffers
|
---|
560 | }
|
---|
561 |
|
---|
562 | // q: use antialiasing before or after ssao?
|
---|
563 | //if (useAntiAliasing) AntiAliasing(fbo, light);
|
---|
564 |
|
---|
565 | switch (mShadingMethod)
|
---|
566 | {
|
---|
567 | case SSAO:
|
---|
568 | ComputeSsao(fbo, mTempCohFactor);
|
---|
569 | CombineSsao(fbo);
|
---|
570 | break;
|
---|
571 | case GI:
|
---|
572 | ComputeGlobIllum(fbo, mTempCohFactor);
|
---|
573 | CombineIllum(fbo);
|
---|
574 | break;
|
---|
575 | default:
|
---|
576 | // do nothing: standard deferred shading
|
---|
577 | break;
|
---|
578 | }
|
---|
579 |
|
---|
580 | /// depth of field
|
---|
581 | if (mUseDepthOfField)
|
---|
582 | {
|
---|
583 | DepthOfField(fbo);
|
---|
584 | }
|
---|
585 |
|
---|
586 | if (mUseToneMapping)
|
---|
587 | {
|
---|
588 | float imageKey, whiteLum, middleGrey;
|
---|
589 |
|
---|
590 | ComputeToneParameters(fbo, light, imageKey, whiteLum, middleGrey);
|
---|
591 | ToneMap(fbo, imageKey, whiteLum, middleGrey);
|
---|
592 | }
|
---|
593 |
|
---|
594 | /// compute lense flare
|
---|
595 | LenseFlare(fbo, light);
|
---|
596 |
|
---|
597 | const bool saveFrame = (mSavedFrameNumber != -1);
|
---|
598 | const bool displayAfterAA = !saveFrame;
|
---|
599 |
|
---|
600 | // multisampling is difficult / costly with deferred shading
|
---|
601 | // at least do some edge blurring
|
---|
602 | if (mUseAntiAliasing) AntiAliasing(fbo, light, displayAfterAA);
|
---|
603 |
|
---|
604 | /// store the current frame
|
---|
605 | if (saveFrame) SaveFrame(fbo);
|
---|
606 |
|
---|
607 | // if it hasn't been done yet => just output the latest buffer
|
---|
608 | if (!mUseAntiAliasing || !displayAfterAA)
|
---|
609 | Output(fbo);
|
---|
610 |
|
---|
611 | glEnable(GL_LIGHTING);
|
---|
612 | glDisable(GL_TEXTURE_2D);
|
---|
613 |
|
---|
614 | glMatrixMode(GL_PROJECTION);
|
---|
615 | glPopMatrix();
|
---|
616 |
|
---|
617 | glMatrixMode(GL_MODELVIEW);
|
---|
618 | glPopMatrix();
|
---|
619 |
|
---|
620 | // viewport
|
---|
621 | glPopAttrib();
|
---|
622 |
|
---|
623 | FrameBufferObject::Release();
|
---|
624 | ShaderManager::GetSingleton()->DisableFragmentProfile();
|
---|
625 | }
|
---|
626 |
|
---|
627 | #if 0
|
---|
628 |
|
---|
629 | void DeferredRenderer::PrepareSsaoFilter()
|
---|
630 | {
|
---|
631 | const float filterWidth = 1.0f;
|
---|
632 |
|
---|
633 | PoissonDiscSampleGenerator2D poisson(NUM_SSAO_FILTER_SAMPLES, 1.0f);
|
---|
634 | poisson.Generate((float *)ssaoFilterOffsets);
|
---|
635 |
|
---|
636 | const float xoffs = (float)filterWidth / mWidth;
|
---|
637 | const float yoffs = (float)filterWidth / mHeight;
|
---|
638 |
|
---|
639 | for (int i = 0; i < NUM_SSAO_FILTER_SAMPLES; ++ i)
|
---|
640 | {
|
---|
641 | float x = ssaoFilterOffsets[2 * i + 0];
|
---|
642 | float y = ssaoFilterOffsets[2 * i + 1];
|
---|
643 |
|
---|
644 | ssaoFilterWeights[i] = GaussianDistribution(x, y, 1.0f);
|
---|
645 | //ssaoFilterWeights[i] = 1.0f;
|
---|
646 |
|
---|
647 | ssaoFilterOffsets[2 * i + 0] *= xoffs;
|
---|
648 | ssaoFilterOffsets[2 * i + 1] *= yoffs;
|
---|
649 | }
|
---|
650 | }
|
---|
651 |
|
---|
652 | #else
|
---|
653 |
|
---|
654 | /** star filter
|
---|
655 | */
|
---|
656 | void DeferredRenderer::PrepareSsaoFilter()
|
---|
657 | {
|
---|
658 | const float filterWidth = 1.0f;
|
---|
659 |
|
---|
660 | PoissonDiscSampleGenerator2D poisson(NUM_SSAO_FILTER_SAMPLES, 1.0f);
|
---|
661 | poisson.Generate((float *)ssaoFilterOffsets);
|
---|
662 |
|
---|
663 | const float xoffs = (float)filterWidth / mWidth;
|
---|
664 | const float yoffs = (float)filterWidth / mHeight;
|
---|
665 |
|
---|
666 | const int n = NUM_SSAO_FILTER_SAMPLES / 2 + 1;
|
---|
667 |
|
---|
668 | for (int i = 0; i < n; ++ i)
|
---|
669 | {
|
---|
670 | const int idx = i - NUM_SSAO_FILTER_SAMPLES / 4;
|
---|
671 |
|
---|
672 | const float x = xoffs * (float)idx;
|
---|
673 | const float y = 0;
|
---|
674 |
|
---|
675 | ssaoFilterWeights[i] = GaussianDistribution(x, y, 1.0f);
|
---|
676 |
|
---|
677 | ssaoFilterOffsets[2 * i + 0] = x;
|
---|
678 | ssaoFilterOffsets[2 * i + 1] = y;
|
---|
679 | }
|
---|
680 |
|
---|
681 | int j = 2 * n;
|
---|
682 |
|
---|
683 | for (int i = 0; i < n; ++ i)
|
---|
684 | {
|
---|
685 | const int idx = i - NUM_SSAO_FILTER_SAMPLES / 4;
|
---|
686 |
|
---|
687 | if (idx)
|
---|
688 | {
|
---|
689 | const float x = 0;
|
---|
690 | const float y = yoffs * (float)idx;
|
---|
691 |
|
---|
692 | ssaoFilterWeights[i] = GaussianDistribution(x, y, 1.0f);
|
---|
693 | //ssaoFilterWeights[i] = 1.0f;
|
---|
694 |
|
---|
695 | ssaoFilterOffsets[j + 0] = x;
|
---|
696 | ssaoFilterOffsets[j + 1] = y;
|
---|
697 |
|
---|
698 | j += 2;
|
---|
699 | }
|
---|
700 | }
|
---|
701 |
|
---|
702 | for (int i = 0; i < NUM_SSAO_FILTER_SAMPLES; ++ i)
|
---|
703 | {
|
---|
704 | cout << "samples " << i << ": " << ssaoFilterOffsets[i * 2] << " " << ssaoFilterOffsets[i * 2 + 1] << endl;
|
---|
705 | }
|
---|
706 | }
|
---|
707 |
|
---|
708 | #endif
|
---|
709 |
|
---|
710 |
|
---|
711 | static inline float SqrMag(const Sample2 &s)
|
---|
712 | {
|
---|
713 | return (s.x * s.x + s.y * s.y);
|
---|
714 | }
|
---|
715 |
|
---|
716 |
|
---|
717 | static inline float SqrDist(const Sample2 &a, const Sample2 &b)
|
---|
718 | {
|
---|
719 | float x = a.x - b.x;
|
---|
720 | float y = a.y - b.y;
|
---|
721 |
|
---|
722 | return x * x + y * y;
|
---|
723 | }
|
---|
724 |
|
---|
725 |
|
---|
726 | static inline bool lt(const Sample2 &a, const Sample2 &b)
|
---|
727 | {
|
---|
728 | return SqrMag(a) < SqrMag(b);
|
---|
729 | }
|
---|
730 |
|
---|
731 |
|
---|
732 | void DeferredRenderer::SortSamples()
|
---|
733 | {
|
---|
734 | static Sample2 tempSamples[NUM_SAMPLES];
|
---|
735 | static bool checked[NUM_SAMPLES];
|
---|
736 |
|
---|
737 | for (int i = 0; i < NUM_SAMPLES; ++ i)
|
---|
738 | checked[i] = false;
|
---|
739 |
|
---|
740 | Sample2 currentSample;
|
---|
741 | currentSample.x = 0; currentSample.y = 0;
|
---|
742 | int ns = 0; // the next sample index
|
---|
743 |
|
---|
744 | for (int i = 0; i < NUM_SAMPLES; ++ i)
|
---|
745 | {
|
---|
746 | float minLen = 1e20f;
|
---|
747 |
|
---|
748 | for (int j = 0; j < NUM_SAMPLES; ++ j)
|
---|
749 | {
|
---|
750 | if (checked[j]) continue;
|
---|
751 |
|
---|
752 | Sample2 s = samples2[j];
|
---|
753 | const float len = SqrDist(s, currentSample);
|
---|
754 |
|
---|
755 | if (len < minLen)
|
---|
756 | {
|
---|
757 | minLen = len;
|
---|
758 | ns = j;
|
---|
759 | }
|
---|
760 | }
|
---|
761 |
|
---|
762 | tempSamples[i] = samples2[ns];
|
---|
763 | currentSample = samples2[ns];
|
---|
764 | checked[ns] = true;
|
---|
765 | }
|
---|
766 |
|
---|
767 | for (int i = 0; i < NUM_SAMPLES; ++ i)
|
---|
768 | samples2[i] = tempSamples[i];
|
---|
769 | }
|
---|
770 |
|
---|
771 |
|
---|
772 | void DeferredRenderer::ComputeSsao(FrameBufferObject *fbo, float tempCohFactor)
|
---|
773 | {
|
---|
774 | GLuint colorsTex, normalsTex, attribsTex;
|
---|
775 |
|
---|
776 | if (0)
|
---|
777 | {
|
---|
778 | colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
779 | normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
780 | }
|
---|
781 | else
|
---|
782 | {
|
---|
783 | normalsTex = mDownSampleFbo->GetColorBuffer(1)->GetTexture();
|
---|
784 | colorsTex = mDownSampleFbo->GetColorBuffer(0)->GetTexture();
|
---|
785 | }
|
---|
786 |
|
---|
787 | attribsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
788 |
|
---|
789 | // flip flop between illumination buffers
|
---|
790 | GLuint oldTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();
|
---|
791 |
|
---|
792 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
793 | glViewport(0, 0, mIllumFbo->GetWidth(), mIllumFbo->GetHeight());
|
---|
794 |
|
---|
795 | // read the second buffer, write to the first buffer
|
---|
796 | mIllumFbo->Bind();
|
---|
797 | glDrawBuffers(1, mrt + mIllumFboIndex);
|
---|
798 |
|
---|
799 | int i = 0;
|
---|
800 |
|
---|
801 | sCgSsaoProgram->SetTexture(i ++, colorsTex);
|
---|
802 | sCgSsaoProgram->SetTexture(i ++, normalsTex);
|
---|
803 | sCgSsaoProgram->SetTexture(i ++, oldTex);
|
---|
804 | sCgSsaoProgram->SetTexture(i ++, noiseTex2D);
|
---|
805 |
|
---|
806 | sCgSsaoProgram->SetValue1f(i ++, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
|
---|
807 |
|
---|
808 | static int currentPos = 0;
|
---|
809 |
|
---|
810 | if (mUseTemporalCoherence || mRegenerateSamples)
|
---|
811 | {
|
---|
812 | mRegenerateSamples = false;
|
---|
813 |
|
---|
814 | // q: should we generate new samples or only rotate the old ones?
|
---|
815 | // in the first case, the sample patterns look nicer, but the kernel
|
---|
816 | // needs longer to converge
|
---|
817 | //if (currentPos + NUM_SAMPLES >= NUM_PRECOMPUTED_SAMPLES) {
|
---|
818 | currentPos = 0;
|
---|
819 | GenerateSamples(mSamplingMethod);
|
---|
820 | //}
|
---|
821 |
|
---|
822 | //if (mSortSamples) { SortSamples(); }
|
---|
823 | sCgSsaoProgram->SetArray2f(i, (float *)samples2 + currentPos, NUM_SAMPLES);
|
---|
824 |
|
---|
825 | currentPos += NUM_SAMPLES;
|
---|
826 | }
|
---|
827 |
|
---|
828 | ++ i;
|
---|
829 |
|
---|
830 | for (int j = 0; j < 4; ++ j, ++ i)
|
---|
831 | sCgSsaoProgram->SetValue3f(i, mCornersView[j].x, mCornersView[j].y, mCornersView[j].z);
|
---|
832 |
|
---|
833 | sCgSsaoProgram->SetMatrix(i ++, mProjViewMatrix);
|
---|
834 | sCgSsaoProgram->SetMatrix(i ++, mOldProjViewMatrix);
|
---|
835 |
|
---|
836 | Vector3 de;
|
---|
837 | de.x = mOldEyePos.x - mEyePos.x;
|
---|
838 | de.y = mOldEyePos.y - mEyePos.y;
|
---|
839 | de.z = mOldEyePos.z - mEyePos.z;
|
---|
840 |
|
---|
841 | sCgSsaoProgram->SetValue3f(i ++, de.x, de.y, de.z);
|
---|
842 |
|
---|
843 | for (int j = 0; j < 4; ++ j, ++ i)
|
---|
844 | {
|
---|
845 | sCgSsaoProgram->SetValue3f(i, mOldCornersView[j].x, mOldCornersView[j].y, mOldCornersView[j].z);
|
---|
846 | }
|
---|
847 |
|
---|
848 | sCgSsaoProgram->SetTexture(i ++, attribsTex);
|
---|
849 | sCgSsaoProgram->SetValue1f(i ++, mKernelRadius);
|
---|
850 | sCgSsaoProgram->SetValue1f(i ++, mSampleIntensity * mKernelRadius);
|
---|
851 |
|
---|
852 |
|
---|
853 | DrawQuad(sCgSsaoProgram);
|
---|
854 |
|
---|
855 | glPopAttrib();
|
---|
856 |
|
---|
857 | PrintGLerror("ssao first pass");
|
---|
858 | }
|
---|
859 |
|
---|
860 |
|
---|
861 | static void SetVertex(float x, float y, float x_offs, float y_offs)
|
---|
862 | {
|
---|
863 | glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
|
---|
864 | glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
|
---|
865 | glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
|
---|
866 | glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
|
---|
867 | glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
|
---|
868 |
|
---|
869 | glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
|
---|
870 | glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
|
---|
871 |
|
---|
872 | //glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
|
---|
873 | glVertex2f(x, y);
|
---|
874 | }
|
---|
875 |
|
---|
876 |
|
---|
877 | void DeferredRenderer::AntiAliasing(FrameBufferObject *fbo,
|
---|
878 | DirectionalLight *light,
|
---|
879 | bool displayFrame)
|
---|
880 | {
|
---|
881 | ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
|
---|
882 | GLuint colorsTex = colorBuffer->GetTexture();
|
---|
883 | GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
884 |
|
---|
885 | // read the second buffer, write to the first buffer
|
---|
886 | if (!displayFrame)
|
---|
887 | FlipFbos(fbo);
|
---|
888 | else
|
---|
889 | // end of the pipeline => just draw image to screen
|
---|
890 | FrameBufferObject::Release();
|
---|
891 |
|
---|
892 | // the neighbouring texels
|
---|
893 | float xOffs = 1.0f / fbo->GetWidth();
|
---|
894 | float yOffs = 1.0f / fbo->GetHeight();
|
---|
895 |
|
---|
896 | sCgAntiAliasingProgram->SetTexture(0, colorsTex);
|
---|
897 | sCgAntiAliasingProgram->SetTexture(1, normalsTex);
|
---|
898 |
|
---|
899 | float offsets[16];
|
---|
900 | int i = 0;
|
---|
901 |
|
---|
902 | offsets[i] = -xOffs; offsets[i + 1] = yOffs; i += 2; // left top
|
---|
903 | offsets[i] = xOffs; offsets[i + 1] = -yOffs; i += 2; // right bottom
|
---|
904 | offsets[i] = xOffs; offsets[i + 1] = yOffs; i += 2; // right top
|
---|
905 | offsets[i] = -xOffs; offsets[i + 1] = -yOffs; i += 2; // left bottom
|
---|
906 | offsets[i] = -xOffs; offsets[i + 1] = .0f; i += 2; // left
|
---|
907 | offsets[i] = xOffs; offsets[i + 1] = .0f; i += 2; // right
|
---|
908 | offsets[i] = .0f; offsets[i + 1] = yOffs; i += 2; // top
|
---|
909 | offsets[i] = .0f; offsets[i + 1] = -yOffs; i += 2; // bottom
|
---|
910 |
|
---|
911 | sCgAntiAliasingProgram->SetArray2f(2, offsets, 8);
|
---|
912 |
|
---|
913 | DrawQuad(sCgAntiAliasingProgram);
|
---|
914 |
|
---|
915 | PrintGLerror("antialiasing");
|
---|
916 | }
|
---|
917 |
|
---|
918 |
|
---|
919 | void DeferredRenderer::FirstPass(FrameBufferObject *fbo, DirectionalLight *light)
|
---|
920 | {
|
---|
921 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
922 | GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
923 |
|
---|
924 | FlipFbos(fbo);
|
---|
925 |
|
---|
926 | const Vector3 lightDir = -light->GetDirection();
|
---|
927 |
|
---|
928 | sCgDeferredProgram->SetTexture(0, colorsTex);
|
---|
929 | sCgDeferredProgram->SetTexture(1, normalsTex);
|
---|
930 | sCgDeferredProgram->SetValue3f(2, lightDir.x, lightDir.y, lightDir.z);
|
---|
931 |
|
---|
932 | DrawQuad(sCgDeferredProgram);
|
---|
933 |
|
---|
934 | PrintGLerror("deferred shading");
|
---|
935 | }
|
---|
936 |
|
---|
937 |
|
---|
938 | void DeferredRenderer::ComputeGlobIllum(FrameBufferObject *fbo,
|
---|
939 | float tempCohFactor)
|
---|
940 | {
|
---|
941 | #if 0
|
---|
942 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
943 | GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
944 | #else
|
---|
945 | GLuint colorsTex = mDownSampleFbo->GetColorBuffer(0)->GetTexture();
|
---|
946 | GLuint normalsTex = mDownSampleFbo->GetColorBuffer(1)->GetTexture();
|
---|
947 | #endif
|
---|
948 |
|
---|
949 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
950 | glViewport(0, 0, mIllumFbo->GetWidth(), mIllumFbo->GetHeight());
|
---|
951 |
|
---|
952 | // read the second buffer, write to the first buffer
|
---|
953 | mIllumFbo->Bind();
|
---|
954 |
|
---|
955 | glDrawBuffers(2, mrt + mIllumFboIndex);
|
---|
956 |
|
---|
957 | GLuint oldSsaoTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();
|
---|
958 | GLuint oldIllumTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex + 1)->GetTexture();
|
---|
959 |
|
---|
960 | int i = 0;
|
---|
961 |
|
---|
962 | sCgGiProgram->SetTexture(i ++, colorsTex);
|
---|
963 | sCgGiProgram->SetTexture(i ++, normalsTex);
|
---|
964 | sCgGiProgram->SetTexture(i ++, noiseTex2D);
|
---|
965 | sCgGiProgram->SetTexture(i ++, oldSsaoTex);
|
---|
966 | sCgGiProgram->SetTexture(i ++, oldIllumTex);
|
---|
967 |
|
---|
968 | sCgGiProgram->SetValue1f(i ++,
|
---|
969 | (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
|
---|
970 |
|
---|
971 | if (mUseTemporalCoherence || mRegenerateSamples)
|
---|
972 | {
|
---|
973 | mRegenerateSamples = false;
|
---|
974 |
|
---|
975 | // q: should we generate new samples or only rotate the old ones?
|
---|
976 | // in the first case, the sample patterns look nicer, but the kernel
|
---|
977 | // needs longer to converge
|
---|
978 | GenerateSamples(mSamplingMethod);
|
---|
979 |
|
---|
980 | sCgGiProgram->SetArray2f(i ++, (float *)samples2, NUM_SAMPLES);
|
---|
981 | }
|
---|
982 |
|
---|
983 | Vector3 bl = mCornersView[0];
|
---|
984 | Vector3 br = mCornersView[1];
|
---|
985 | Vector3 tl = mCornersView[2];
|
---|
986 | Vector3 tr = mCornersView[3];
|
---|
987 |
|
---|
988 | sCgGiProgram->SetValue3f(i ++, bl.x, bl.y, bl.z);
|
---|
989 | sCgGiProgram->SetValue3f(i ++, br.x, br.y, br.z);
|
---|
990 | sCgGiProgram->SetValue3f(i ++, tl.x, tl.y, tl.z);
|
---|
991 | sCgGiProgram->SetValue3f(i ++, tr.x, tr.y, tr.z);
|
---|
992 |
|
---|
993 | sCgGiProgram->SetMatrix(i ++, mOldProjViewMatrix);
|
---|
994 | sCgGiProgram->SetMatrix(i ++, mProjViewMatrix);
|
---|
995 |
|
---|
996 |
|
---|
997 | DrawQuad(sCgGiProgram);
|
---|
998 |
|
---|
999 | glPopAttrib();
|
---|
1000 |
|
---|
1001 | PrintGLerror("globillum first pass");
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 |
|
---|
1005 | void DeferredRenderer::CombineIllum(FrameBufferObject *fbo)
|
---|
1006 | {
|
---|
1007 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
1008 |
|
---|
1009 | GLuint ssaoTex = mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetTexture();
|
---|
1010 | GLuint illumTex = mIllumFbo->GetColorBuffer(mIllumFboIndex + 1)->GetTexture();
|
---|
1011 |
|
---|
1012 | FlipFbos(fbo);
|
---|
1013 |
|
---|
1014 | sCgCombineIllumProgram->SetTexture(0, colorsTex);
|
---|
1015 | sCgCombineIllumProgram->SetTexture(1, ssaoTex);
|
---|
1016 | sCgCombineIllumProgram->SetTexture(2, illumTex);
|
---|
1017 |
|
---|
1018 | DrawQuad(sCgCombineIllumProgram);
|
---|
1019 |
|
---|
1020 | PrintGLerror("combine");
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 |
|
---|
1024 | void DeferredRenderer::CombineSsao(FrameBufferObject *fbo)
|
---|
1025 | {
|
---|
1026 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
1027 | GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
1028 | GLuint ssaoTex = mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetTexture();
|
---|
1029 |
|
---|
1030 | FlipFbos(fbo);
|
---|
1031 |
|
---|
1032 | int i = 0;
|
---|
1033 |
|
---|
1034 | sCgCombineSsaoProgram->SetTexture(i ++, colorsTex);
|
---|
1035 | sCgCombineSsaoProgram->SetTexture(i ++, normalsTex);
|
---|
1036 | sCgCombineSsaoProgram->SetTexture(i ++, ssaoTex);
|
---|
1037 |
|
---|
1038 | sCgCombineSsaoProgram->SetArray2f(i ++, (float *)ssaoFilterOffsets, NUM_SSAO_FILTER_SAMPLES);
|
---|
1039 | sCgCombineSsaoProgram->SetArray1f(i ++, (float *)ssaoFilterWeights, NUM_SSAO_FILTER_SAMPLES);
|
---|
1040 | sCgCombineSsaoProgram->SetValue1f(i ++, mSsaoFilterRadius);
|
---|
1041 |
|
---|
1042 | sCgCombineSsaoProgram->SetMatrix(i++, mProjViewMatrix);
|
---|
1043 |
|
---|
1044 | for (int j = 0; j < 4; ++ j, ++ i)
|
---|
1045 | {
|
---|
1046 | sCgCombineSsaoProgram->SetValue3f(i, mCornersView[j].x, mCornersView[j].y, mCornersView[j].z);
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | sCgCombineSsaoProgram->SetValue1f(i ++, mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetWidth());
|
---|
1050 | sCgCombineSsaoProgram->SetValue1f(i ++, mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetHeight());
|
---|
1051 |
|
---|
1052 | DrawQuad(sCgCombineSsaoProgram);
|
---|
1053 |
|
---|
1054 | PrintGLerror("combine ssao");
|
---|
1055 | }
|
---|
1056 |
|
---|
1057 |
|
---|
1058 | void DeferredRenderer::CombineSsao2(FrameBufferObject *fbo)
|
---|
1059 | {
|
---|
1060 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
1061 | GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
1062 | GLuint ssaoTex = mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetTexture();
|
---|
1063 |
|
---|
1064 | FlipFbos(fbo);
|
---|
1065 |
|
---|
1066 | int i = 0;
|
---|
1067 |
|
---|
1068 | sCgCombineSsaoProgram->SetTexture(i ++, colorsTex);
|
---|
1069 | sCgCombineSsaoProgram->SetTexture(i ++, normalsTex);
|
---|
1070 | sCgCombineSsaoProgram->SetTexture(i ++, ssaoTex);
|
---|
1071 |
|
---|
1072 | sCgCombineSsaoProgram->SetArray2f(i ++, (float *)ssaoFilterOffsets, NUM_SSAO_FILTER_SAMPLES);
|
---|
1073 | sCgCombineSsaoProgram->SetArray1f(i ++, (float *)ssaoFilterWeights, NUM_SSAO_FILTER_SAMPLES);
|
---|
1074 | sCgCombineSsaoProgram->SetValue1f(i ++, mSsaoFilterRadius);
|
---|
1075 |
|
---|
1076 | sCgCombineSsaoProgram->SetMatrix(i++, mProjViewMatrix);
|
---|
1077 |
|
---|
1078 | for (int j = 0; j < 4; ++ j, ++ i)
|
---|
1079 | {
|
---|
1080 | sCgCombineSsaoProgram->SetValue3f(i, mCornersView[j].x, mCornersView[j].y, mCornersView[j].z);
|
---|
1081 | }
|
---|
1082 |
|
---|
1083 | sCgCombineSsaoProgram->SetValue1f(i ++, mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetWidth());
|
---|
1084 | sCgCombineSsaoProgram->SetValue1f(i ++, mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetHeight());
|
---|
1085 |
|
---|
1086 | DrawQuad(sCgCombineSsaoProgram);
|
---|
1087 |
|
---|
1088 | PrintGLerror("combine ssao");
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 |
|
---|
1092 | void DeferredRenderer::FirstPassShadow(FrameBufferObject *fbo,
|
---|
1093 | DirectionalLight *light,
|
---|
1094 | ShadowMap *shadowMap)
|
---|
1095 | {
|
---|
1096 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
1097 | GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
1098 |
|
---|
1099 | GLuint shadowTex = shadowMap->GetDepthTexture();
|
---|
1100 |
|
---|
1101 | Matrix4x4 shadowMatrix;
|
---|
1102 | shadowMap->GetTextureMatrix(shadowMatrix);
|
---|
1103 |
|
---|
1104 |
|
---|
1105 | FlipFbos(fbo);
|
---|
1106 |
|
---|
1107 | sCgDeferredShadowProgram->SetTexture(0, colorsTex);
|
---|
1108 | sCgDeferredShadowProgram->SetTexture(1, normalsTex);
|
---|
1109 | sCgDeferredShadowProgram->SetTexture(2, shadowTex);
|
---|
1110 | sCgDeferredShadowProgram->SetTexture(3, noiseTex2D);
|
---|
1111 | sCgDeferredShadowProgram->SetMatrix(4, shadowMatrix);
|
---|
1112 | sCgDeferredShadowProgram->SetValue1f(5, 2.0f / shadowMap->GetSize());
|
---|
1113 |
|
---|
1114 | const Vector3 lightDir = -light->GetDirection();
|
---|
1115 | sCgDeferredShadowProgram->SetValue3f(6, lightDir.x, lightDir.y, lightDir.z);
|
---|
1116 | sCgDeferredShadowProgram->SetValue3f(7, mEyePos.x, mEyePos.y, mEyePos.z);
|
---|
1117 |
|
---|
1118 | DrawQuad(sCgDeferredShadowProgram);
|
---|
1119 |
|
---|
1120 | PrintGLerror("deferred shading + shadows");
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 |
|
---|
1124 | void DeferredRenderer::ComputeToneParameters(FrameBufferObject *fbo,
|
---|
1125 | DirectionalLight *light,
|
---|
1126 | float &imageKey,
|
---|
1127 | float &whiteLum,
|
---|
1128 | float &middleGrey)
|
---|
1129 | {
|
---|
1130 | // hack: estimate value where sky burns out
|
---|
1131 | whiteLum = log(WHITE_LUMINANCE);
|
---|
1132 |
|
---|
1133 | ////////////////////
|
---|
1134 | //-- linear interpolate brightness key depending on the current sun position
|
---|
1135 |
|
---|
1136 | const float minKey = 0.09f;
|
---|
1137 | const float maxKey = 0.36f;
|
---|
1138 |
|
---|
1139 | const float lightIntensity = DotProd(-light->GetDirection(), Vector3::UNIT_Z());
|
---|
1140 | middleGrey = lightIntensity * maxKey + (1.0f - lightIntensity) * minKey;
|
---|
1141 |
|
---|
1142 |
|
---|
1143 | //////////
|
---|
1144 | //-- compute avg loglum
|
---|
1145 |
|
---|
1146 | ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
|
---|
1147 | GLuint colorsTex = colorBuffer->GetTexture();
|
---|
1148 |
|
---|
1149 | FlipFbos(fbo);
|
---|
1150 |
|
---|
1151 | sCgLogLumProgram->SetTexture(0, colorsTex);
|
---|
1152 | DrawQuad(sCgLogLumProgram);
|
---|
1153 |
|
---|
1154 | PrintGLerror("ToneMapParams");
|
---|
1155 |
|
---|
1156 |
|
---|
1157 | ///////////////////
|
---|
1158 | //-- compute avg loglum in scene using mipmapping
|
---|
1159 |
|
---|
1160 | glBindTexture(GL_TEXTURE_2D, fbo->GetColorBuffer(colorBufferIdx)->GetTexture());
|
---|
1161 | glGenerateMipmapEXT(GL_TEXTURE_2D);
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 |
|
---|
1165 | static void ExportData(float *data, int w, int h)
|
---|
1166 | {
|
---|
1167 | startil();
|
---|
1168 |
|
---|
1169 | cout << "w: " << w << " h: " << h << endl;
|
---|
1170 | ILstring filename = ILstring("downsample2.jpg");
|
---|
1171 | ilRegisterType(IL_FLOAT);
|
---|
1172 |
|
---|
1173 | const int depth = 1;
|
---|
1174 | const int bpp = 4;
|
---|
1175 |
|
---|
1176 | if (!ilTexImage(w, h, depth, bpp, IL_RGBA, IL_FLOAT, data))
|
---|
1177 | {
|
---|
1178 | cerr << "IL error " << ilGetError() << endl;
|
---|
1179 | stopil();
|
---|
1180 | return;
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | if (!ilSaveImage(filename))
|
---|
1184 | {
|
---|
1185 | cerr << "TGA write error " << ilGetError() << endl;
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 | stopil();
|
---|
1189 | }
|
---|
1190 |
|
---|
1191 |
|
---|
1192 | void DeferredRenderer::PrepareSsao(FrameBufferObject *fbo)
|
---|
1193 | {
|
---|
1194 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
1195 | GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
1196 | GLuint diffVals = fbo->GetColorBuffer(2)->GetTexture();
|
---|
1197 | // flip flop between illumination buffers
|
---|
1198 | GLuint oldTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();
|
---|
1199 |
|
---|
1200 | int i = 0;
|
---|
1201 |
|
---|
1202 | sCgPrepareSsaoProgram->SetTexture(i ++, colorsTex);
|
---|
1203 | sCgPrepareSsaoProgram->SetTexture(i ++, normalsTex);
|
---|
1204 | sCgPrepareSsaoProgram->SetTexture(i ++, diffVals);
|
---|
1205 | sCgPrepareSsaoProgram->SetTexture(i ++, oldTex);
|
---|
1206 |
|
---|
1207 | Vector3 de;
|
---|
1208 | de.x = mOldEyePos.x - mEyePos.x;
|
---|
1209 | de.y = mOldEyePos.y - mEyePos.y;
|
---|
1210 | de.z = mOldEyePos.z - mEyePos.z;
|
---|
1211 |
|
---|
1212 | sCgPrepareSsaoProgram->SetValue3f(i ++, de.x, de.y, de.z);
|
---|
1213 |
|
---|
1214 | sCgPrepareSsaoProgram->SetMatrix(i ++, mProjViewMatrix);
|
---|
1215 | sCgPrepareSsaoProgram->SetMatrix(i ++, mOldProjViewMatrix);
|
---|
1216 |
|
---|
1217 | for (int j = 0; j < 4; ++ j, ++ i)
|
---|
1218 | sCgPrepareSsaoProgram->SetValue3f(i, mOldCornersView[j].x, mOldCornersView[j].y, mOldCornersView[j].z);
|
---|
1219 |
|
---|
1220 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
1221 | glViewport(0, 0, mDownSampleFbo->GetWidth(), mDownSampleFbo->GetHeight());
|
---|
1222 |
|
---|
1223 | mDownSampleFbo->Bind();
|
---|
1224 |
|
---|
1225 | // prepare downsampled depth and normal texture for ssao
|
---|
1226 | glDrawBuffers(2, mrt);
|
---|
1227 |
|
---|
1228 | DrawQuad(sCgPrepareSsaoProgram);
|
---|
1229 |
|
---|
1230 | glPopAttrib();
|
---|
1231 | PrintGLerror("prepareSsao");
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 |
|
---|
1235 | void DeferredRenderer::DownSample(FrameBufferObject *fbo,
|
---|
1236 | int bufferIdx,
|
---|
1237 | FrameBufferObject *downSampleFbo,
|
---|
1238 | int downSampleBufferIdx,
|
---|
1239 | ShaderProgram *program)
|
---|
1240 | {
|
---|
1241 | ColorBufferObject *buffer = fbo->GetColorBuffer(bufferIdx);
|
---|
1242 | GLuint tex = buffer->GetTexture();
|
---|
1243 |
|
---|
1244 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
1245 | glViewport(0, 0, downSampleFbo->GetWidth(), downSampleFbo->GetHeight());
|
---|
1246 |
|
---|
1247 | downSampleFbo->Bind();
|
---|
1248 |
|
---|
1249 | program->SetTexture(0, tex);
|
---|
1250 | glDrawBuffers(1, mrt + downSampleBufferIdx);
|
---|
1251 |
|
---|
1252 | DrawQuad(program);
|
---|
1253 |
|
---|
1254 | glPopAttrib();
|
---|
1255 | PrintGLerror("downsample");
|
---|
1256 | }
|
---|
1257 |
|
---|
1258 |
|
---|
1259 | void DeferredRenderer::ToneMap(FrameBufferObject *fbo,
|
---|
1260 | float imageKey,
|
---|
1261 | float whiteLum,
|
---|
1262 | float middleGrey)
|
---|
1263 | {
|
---|
1264 | ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
|
---|
1265 | GLuint colorsTex = colorBuffer->GetTexture();
|
---|
1266 | //FrameBufferObject::Release();
|
---|
1267 |
|
---|
1268 | FlipFbos(fbo);
|
---|
1269 |
|
---|
1270 | sCgToneProgram->SetTexture(0, colorsTex);
|
---|
1271 | sCgToneProgram->SetValue1f(1, imageKey);
|
---|
1272 | sCgToneProgram->SetValue1f(2, whiteLum);
|
---|
1273 | sCgToneProgram->SetValue1f(3, middleGrey);
|
---|
1274 |
|
---|
1275 | DrawQuad(sCgToneProgram);
|
---|
1276 |
|
---|
1277 | PrintGLerror("ToneMap");
|
---|
1278 | }
|
---|
1279 |
|
---|
1280 |
|
---|
1281 | void DeferredRenderer::Output(FrameBufferObject *fbo)
|
---|
1282 | {
|
---|
1283 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
1284 | glViewport(0, 0, fbo->GetWidth(), fbo->GetHeight());
|
---|
1285 |
|
---|
1286 | ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
|
---|
1287 | GLuint colorsTex = colorBuffer->GetTexture();
|
---|
1288 |
|
---|
1289 | sCgDownSampleProgram->SetTexture(0, colorsTex);
|
---|
1290 |
|
---|
1291 | FrameBufferObject::Release();
|
---|
1292 | DrawQuad(sCgDownSampleProgram);
|
---|
1293 |
|
---|
1294 | PrintGLerror("output");
|
---|
1295 | }
|
---|
1296 |
|
---|
1297 |
|
---|
1298 | void DeferredRenderer::InitFrame()
|
---|
1299 | {
|
---|
1300 | for (int i = 0; i < 4; ++ i)
|
---|
1301 | mOldCornersView[i] = mCornersView[i];
|
---|
1302 |
|
---|
1303 | mOldProjViewMatrix = mProjViewMatrix;
|
---|
1304 | mOldEyePos = mEyePos;
|
---|
1305 | mEyePos = mCamera->GetPosition();
|
---|
1306 |
|
---|
1307 | // hack: temporarily change far to improve precision
|
---|
1308 | const float oldFar = mCamera->GetFar();
|
---|
1309 | const float oldNear = mCamera->GetNear();
|
---|
1310 |
|
---|
1311 |
|
---|
1312 | Matrix4x4 matViewing, matProjection;
|
---|
1313 |
|
---|
1314 |
|
---|
1315 | ///////////////////
|
---|
1316 |
|
---|
1317 | // use view orientation as we assume that the eye point is always in the center
|
---|
1318 | // of our coordinate system, this way we have the highest precision near the eye point
|
---|
1319 | mCamera->GetViewOrientationMatrix(matViewing);
|
---|
1320 | mCamera->GetProjectionMatrix(matProjection);
|
---|
1321 |
|
---|
1322 | mProjViewMatrix = matViewing * matProjection;
|
---|
1323 | ComputeViewVectors(mCamera, mCornersView[0], mCornersView[1], mCornersView[2], mCornersView[3]);
|
---|
1324 |
|
---|
1325 |
|
---|
1326 | // switch roles of old and new fbo
|
---|
1327 | // the algorihm uses two input fbos, where the one
|
---|
1328 | // contais the color buffer from the last frame,
|
---|
1329 | // the other one will be written
|
---|
1330 |
|
---|
1331 | mIllumFboIndex = 2 - mIllumFboIndex;
|
---|
1332 |
|
---|
1333 | // enable fragment shading
|
---|
1334 | ShaderManager::GetSingleton()->EnableFragmentProfile();
|
---|
1335 |
|
---|
1336 | glDisable(GL_ALPHA_TEST);
|
---|
1337 | glDisable(GL_TEXTURE_2D);
|
---|
1338 | glDisable(GL_LIGHTING);
|
---|
1339 | glDisable(GL_BLEND);
|
---|
1340 | glDisable(GL_DEPTH_TEST);
|
---|
1341 |
|
---|
1342 | glPolygonMode(GL_FRONT, GL_FILL);
|
---|
1343 |
|
---|
1344 | glMatrixMode(GL_PROJECTION);
|
---|
1345 | glPushMatrix();
|
---|
1346 | glLoadIdentity();
|
---|
1347 |
|
---|
1348 | gluOrtho2D(0, 1, 0, 1);
|
---|
1349 |
|
---|
1350 |
|
---|
1351 | glMatrixMode(GL_MODELVIEW);
|
---|
1352 | glPushMatrix();
|
---|
1353 | glLoadIdentity();
|
---|
1354 |
|
---|
1355 |
|
---|
1356 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
1357 | glViewport(0, 0, mWidth, mHeight);
|
---|
1358 |
|
---|
1359 | // revert to old far and near plane
|
---|
1360 | mCamera->SetFar(oldFar);
|
---|
1361 | mCamera->SetNear(oldNear);
|
---|
1362 | }
|
---|
1363 |
|
---|
1364 |
|
---|
1365 | void DeferredRenderer::LenseFlare(FrameBufferObject *fbo,
|
---|
1366 | DirectionalLight *light
|
---|
1367 | )
|
---|
1368 | {
|
---|
1369 | // light source visible?
|
---|
1370 | if (!mSunVisiblePixels) return;
|
---|
1371 |
|
---|
1372 | // the sun is a large distance in the reverse light direction
|
---|
1373 | // => extrapolate light pos
|
---|
1374 | const Vector3 lightPos = light->GetDirection() * -1e3f;
|
---|
1375 |
|
---|
1376 | float w;
|
---|
1377 | Vector3 projLightPos = mProjViewMatrix.Transform(w, lightPos, 1.0f);
|
---|
1378 | projLightPos /= w;
|
---|
1379 | projLightPos = projLightPos * 0.5f + Vector3(0.5f);
|
---|
1380 |
|
---|
1381 | // vector to light from screen center in texture space
|
---|
1382 | Vector3 vectorToLight = projLightPos - Vector3(0.5f);
|
---|
1383 | vectorToLight.z = .0f;
|
---|
1384 |
|
---|
1385 | const float distanceToLight = Magnitude(vectorToLight);
|
---|
1386 | vectorToLight /= distanceToLight;
|
---|
1387 |
|
---|
1388 | //cout << "dist " << distanceToLight << " v " << vectorToLight << endl;
|
---|
1389 |
|
---|
1390 |
|
---|
1391 | ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
|
---|
1392 | GLuint colorsTex = colorBuffer->GetTexture();
|
---|
1393 |
|
---|
1394 | FlipFbos(fbo);
|
---|
1395 |
|
---|
1396 | int i = 0;
|
---|
1397 |
|
---|
1398 | sCgLenseFlareProgram->SetTexture(i ++, colorsTex);
|
---|
1399 | sCgLenseFlareProgram->SetTexture(i ++, sHaloTex[0]->GetId());
|
---|
1400 | sCgLenseFlareProgram->SetTexture(i ++, sHaloTex[1]->GetId());
|
---|
1401 | sCgLenseFlareProgram->SetTexture(i ++, sHaloTex[2]->GetId());
|
---|
1402 | sCgLenseFlareProgram->SetTexture(i ++, sHaloTex[3]->GetId());
|
---|
1403 | sCgLenseFlareProgram->SetTexture(i ++, sHaloTex[4]->GetId());
|
---|
1404 | sCgLenseFlareProgram->SetValue2f(i ++, vectorToLight.x, vectorToLight.y);
|
---|
1405 | sCgLenseFlareProgram->SetValue1f(i ++, distanceToLight);
|
---|
1406 | sCgLenseFlareProgram->SetValue1f(i ++, (float)mSunVisiblePixels);
|
---|
1407 |
|
---|
1408 | DrawQuad(sCgLenseFlareProgram);
|
---|
1409 |
|
---|
1410 | PrintGLerror("LenseFlare");
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 |
|
---|
1414 | void DeferredRenderer::DepthOfField(FrameBufferObject *fbo)
|
---|
1415 | {
|
---|
1416 | ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
|
---|
1417 | GLuint colorsTex = colorBuffer->GetTexture();
|
---|
1418 |
|
---|
1419 | FlipFbos(fbo);
|
---|
1420 |
|
---|
1421 | int i = 0;
|
---|
1422 |
|
---|
1423 | const float zFocus = 7.0f;
|
---|
1424 |
|
---|
1425 | sCgDOFProgram->SetTexture(i ++, colorsTex);
|
---|
1426 | sCgDOFProgram->SetArray2f(i ++, (float *)dofSamples, NUM_DOF_TABS);
|
---|
1427 | sCgDOFProgram->SetValue1f(i ++, min(mCamera->GetFar(), mMaxDistance) - mCamera->GetNear());
|
---|
1428 | sCgDOFProgram->SetValue1f(i ++, zFocus);
|
---|
1429 |
|
---|
1430 | DrawQuad(sCgDOFProgram);
|
---|
1431 |
|
---|
1432 | PrintGLerror("LenseFlare");
|
---|
1433 | }
|
---|
1434 |
|
---|
1435 |
|
---|
1436 | void DeferredRenderer::SaveFrame(FrameBufferObject *fbo)
|
---|
1437 | {
|
---|
1438 | ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
|
---|
1439 | GLuint colorsTex = colorBuffer->GetTexture();
|
---|
1440 |
|
---|
1441 | GLubyte *data = new GLubyte[mWidth * mHeight * 4];
|
---|
1442 |
|
---|
1443 | // grab texture data
|
---|
1444 | glEnable(GL_TEXTURE_2D);
|
---|
1445 | glBindTexture(GL_TEXTURE_2D, colorsTex);
|
---|
1446 | glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
---|
1447 |
|
---|
1448 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
1449 | glDisable(GL_TEXTURE_2D);
|
---|
1450 |
|
---|
1451 | /////////////////
|
---|
1452 |
|
---|
1453 | startil();
|
---|
1454 |
|
---|
1455 | static char imageName[200];
|
---|
1456 | sprintf(imageName, "%s_%05d.tga", mSavedFrameSuffix.c_str(), mSavedFrameNumber);
|
---|
1457 |
|
---|
1458 | ILstring fileName = ILstring(imageName);
|
---|
1459 | ilRegisterType(IL_FLOAT);
|
---|
1460 |
|
---|
1461 | const int depth = 1;
|
---|
1462 | const int bpp = 4;
|
---|
1463 |
|
---|
1464 | if (!ilTexImage(mWidth, mHeight, depth, bpp, IL_RGBA, IL_UNSIGNED_BYTE, data))
|
---|
1465 | {
|
---|
1466 | cerr << "IL error " << ilGetError() << endl;
|
---|
1467 | stopil();
|
---|
1468 | return;
|
---|
1469 | }
|
---|
1470 |
|
---|
1471 | ilEnable(IL_FILE_OVERWRITE);
|
---|
1472 |
|
---|
1473 | if (!ilSaveImage(fileName))
|
---|
1474 | {
|
---|
1475 | cerr << "TGA write error " << ilGetError() << endl;
|
---|
1476 | }
|
---|
1477 |
|
---|
1478 | delete [] data;
|
---|
1479 |
|
---|
1480 | stopil();
|
---|
1481 |
|
---|
1482 | PrintGLerror("Store frame");
|
---|
1483 | }
|
---|
1484 |
|
---|
1485 |
|
---|
1486 | void DeferredRenderer::SetUseTemporalCoherence(bool temporal)
|
---|
1487 | {
|
---|
1488 | mUseTemporalCoherence = temporal;
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 |
|
---|
1492 | void DeferredRenderer::SetSamplingMethod(SAMPLING_METHOD s)
|
---|
1493 | {
|
---|
1494 | if (s != mSamplingMethod)
|
---|
1495 | {
|
---|
1496 | mSamplingMethod = s;
|
---|
1497 | mRegenerateSamples = true;
|
---|
1498 | }
|
---|
1499 | }
|
---|
1500 |
|
---|
1501 |
|
---|
1502 | void DeferredRenderer::SetShadingMethod(SHADING_METHOD s)
|
---|
1503 | {
|
---|
1504 | if (s != mShadingMethod)
|
---|
1505 | {
|
---|
1506 | mShadingMethod = s;
|
---|
1507 | mRegenerateSamples = true;
|
---|
1508 | }
|
---|
1509 | }
|
---|
1510 |
|
---|
1511 |
|
---|
1512 | #if TODO
|
---|
1513 |
|
---|
1514 | void DeferredRenderer::SetNumSamples(int numSamples)
|
---|
1515 | {
|
---|
1516 | mNumSamples = numSamples;
|
---|
1517 | }
|
---|
1518 |
|
---|
1519 | #endif
|
---|
1520 |
|
---|
1521 |
|
---|
1522 | void DeferredRenderer::SetSampleIntensity(float sampleIntensity)
|
---|
1523 | {
|
---|
1524 | mSampleIntensity = sampleIntensity;
|
---|
1525 | mRegenerateSamples = true;
|
---|
1526 | }
|
---|
1527 |
|
---|
1528 |
|
---|
1529 | void DeferredRenderer::SetKernelRadius(float kernelRadius)
|
---|
1530 | {
|
---|
1531 | mKernelRadius = kernelRadius;
|
---|
1532 | mRegenerateSamples = true;
|
---|
1533 | }
|
---|
1534 |
|
---|
1535 |
|
---|
1536 | void DeferredRenderer::SetSsaoFilterRadius(float ssaoFilterRadius)
|
---|
1537 | {
|
---|
1538 | mSsaoFilterRadius = ssaoFilterRadius;
|
---|
1539 | mRegenerateSamples = true;
|
---|
1540 | }
|
---|
1541 |
|
---|
1542 |
|
---|
1543 | /*void DeferredRenderer::SetSortSamples(bool sortSamples)
|
---|
1544 | {
|
---|
1545 | mSortSamples = sortSamples;
|
---|
1546 | }*/
|
---|
1547 |
|
---|
1548 |
|
---|
1549 | void DeferredRenderer::SetSunVisiblePixels(int pixels)
|
---|
1550 | {
|
---|
1551 | mSunVisiblePixels = pixels;
|
---|
1552 | }
|
---|
1553 |
|
---|
1554 |
|
---|
1555 | void DeferredRenderer::SetMaxDistance(float maxDist)
|
---|
1556 | {
|
---|
1557 | mMaxDistance = maxDist;
|
---|
1558 | }
|
---|
1559 |
|
---|
1560 |
|
---|
1561 | void DeferredRenderer::SetUseToneMapping(bool toneMapping)
|
---|
1562 | {
|
---|
1563 | mUseToneMapping = toneMapping;
|
---|
1564 | }
|
---|
1565 |
|
---|
1566 |
|
---|
1567 | void DeferredRenderer::SetUseAntiAliasing(bool antiAliasing)
|
---|
1568 | {
|
---|
1569 | mUseAntiAliasing = antiAliasing;
|
---|
1570 | }
|
---|
1571 |
|
---|
1572 |
|
---|
1573 | void DeferredRenderer::SetUseDepthOfField(bool dof)
|
---|
1574 | {
|
---|
1575 | mUseDepthOfField = dof;
|
---|
1576 | }
|
---|
1577 |
|
---|
1578 |
|
---|
1579 | void DeferredRenderer::SetTemporalCoherenceFactorForSsao(float factor)
|
---|
1580 | {
|
---|
1581 | mTempCohFactor = factor;
|
---|
1582 | }
|
---|
1583 |
|
---|
1584 |
|
---|
1585 | void DeferredRenderer::SetSaveFrame(const string &suffix, int frameNumber)
|
---|
1586 | {
|
---|
1587 | mSavedFrameSuffix = suffix;
|
---|
1588 | mSavedFrameNumber = frameNumber;
|
---|
1589 | }
|
---|
1590 |
|
---|
1591 |
|
---|
1592 | } // namespace
|
---|