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