source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp @ 3204

Revision 3204, 32.4 KB checked in by mattausch, 16 years ago (diff)

debug version showing a visualization of the confidence

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