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

Revision 2993, 38.1 KB checked in by mattausch, 16 years ago (diff)
RevLine 
[2896]1#include "DeferredRenderer.h"
[2859]2#include "FrameBufferObject.h"
3#include "RenderState.h"
4#include "SampleGenerator.h"
[2860]5#include "Vector3.h"
6#include "Camera.h"
[2884]7#include "shaderenv.h"
[2886]8#include "Halton.h"
[2895]9#include "ShadowMapping.h"
[2952]10#include "Light.h"
[2858]11
[2859]12
[2858]13using namespace std;
14
15
16namespace CHCDemoEngine
17{
18
[2859]19static CGprogram sCgSsaoProgram = NULL;
[2873]20static CGprogram sCgGiProgram = NULL;
21
22static CGprogram sCgDeferredProgram = NULL;
[2868]23static CGprogram sCgAntiAliasingProgram = NULL;
[2895]24static CGprogram sCgDeferredShadowProgram = NULL;
[2859]25
[2869]26static CGparameter sColorsTexCombineParam;
27static CGparameter sSsaoTexCombineParam;
28
[2868]29static CGparameter sColorsTexDeferredParam;
30static CGparameter sPositionsTexDeferredParam;
31static CGparameter sNormalsTexDeferredParam;
32
[2880]33static CGprogram sCgCombinedSsaoProgram = NULL;
34static CGprogram sCgCombinedIllumProgram = NULL;
35
[2992]36static CGparameter sColorsTexLogLumParam;
[2967]37
[2992]38
39
[2972]40static CGprogram sCgInitialIntensityProgram;
41static CGprogram sCgDownSampleProgram;
42static CGprogram sCgToneProgram;
[2992]43static CGprogram sCgLogLumProgram;
[2972]44
45
[2992]46
[2944]47///////////////////////////////////////
[2873]48
49
[2859]50static CGparameter sColorsTexParam;
51static CGparameter sPositionsTexParam;
52static CGparameter sNormalsTexParam;
[2868]53
[2859]54static CGparameter sOldModelViewProjMatrixParam;
[2900]55static CGparameter sModelViewProjMatrixParam;
[2859]56static CGparameter sMaxDepthParam;
[2985]57static CGparameter sEyePosParam;
[2859]58static CGparameter sSamplesParam;
59static CGparameter sOldTexParam;
60static CGparameter sNoiseTexParam;
[2897]61static CGparameter sTemporalCoherenceParam;
[2859]62
[2873]63
64///////////////////////////////////////
65
66
67static CGparameter sColorsTexGiParam;
68static CGparameter sPositionsTexGiParam;
69static CGparameter sNormalsTexGiParam;
70
71
72static CGparameter sOldModelViewProjMatrixGiParam;
73static CGparameter sMaxDepthGiParam;
74static CGparameter sSamplesGiParam;
75static CGparameter sOldSsaoTexGiParam;
76static CGparameter sOldIllumTexGiParam;
77static CGparameter sNoiseTexGiParam;
[2897]78static CGparameter sTemporalCoherenceGiParam;
[2873]79
80
[2880]81static CGparameter sColorsTexCombinedIllumParam;
82static CGparameter sSsaoTexCombinedIllumParam;
83static CGparameter sIllumTexCombinedIllumParam;
84
85static CGparameter sColorsTexCombinedSsaoParam;
86static CGparameter sSsaoTexCombinedSsaoParam;
[2974]87static CGparameter sPositionsTexCombinedSsaoParam;
[2880]88
[2987]89static CGparameter sTLParam;
90static CGparameter sTRParam;
91static CGparameter sBRParam;
92static CGparameter sBLParam;
[2880]93
[2990]94
95static CGparameter sTLGiParam;
96static CGparameter sTRGiParam;
97static CGparameter sBRGiParam;
98static CGparameter sBLGiParam;
99
100static CGparameter sEyePosGiParam;
101
102
103
[2873]104////////////
105
[2865]106static CGparameter sColorsTexAntiAliasingParam;
107static CGparameter sNormalsTexAntiAliasingParam;
108
[2895]109
110static CGparameter sShadowMapParam;
111static CGparameter sPositionsTexShadowParam; 
112static CGparameter sColorsTexShadowParam; 
113static CGparameter sNormalsTexShadowParam;
114
115static CGparameter sShadowMatrixParam;
116static CGparameter sMaxDepthShadowParam;
117static CGparameter sSampleWidthParam;
118
[2944]119static CGparameter sNoiseTexShadowParam;
120static CGparameter sSamplesShadowParam;
121
[2952]122static CGparameter sLightDirParam;
123static CGparameter sLightDirShadowParam;
[2966]124static CGparameter sImageKeyParam;
[2970]125static CGparameter sMiddleGreyParam;
[2968]126static CGparameter sWhiteLumParam;
[2952]127
[2972]128
129static CGparameter sColorsTexInitialParam;
130static CGparameter sColorsTexToneParam;
131static CGparameter sIntensityTexDownSampleParam;
132
[2903]133//#define USE_3D_SSAO
[2895]134
135
[2879]136static GLuint noiseTex = 0;
[2865]137
[2859]138// ssao random spherical samples
[2903]139#ifdef USE_3D_SSAO
[2900]140static Sample2 samples3[NUM_SAMPLES];
[2903]141#else
142static Sample2 samples2[NUM_SAMPLES];
143#endif
144
[2966]145// number of pcf tabs
146Sample2 pcfSamples[NUM_PCF_TABS];
147
[2976]148int DeferredRenderer::colorBufferIdx = 0;
[2859]149
[2992]150
[2859]151static void PrintGLerror(char *msg)
152{
153        GLenum errCode;
154        const GLubyte *errStr;
155       
156        if ((errCode = glGetError()) != GL_NO_ERROR)
157        {
158                errStr = gluErrorString(errCode);
159                fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
160        }
161}
162
163
[2976]164
[2859]165/** Generate poisson disc distributed sample points on the unit disc
166*/
[2887]167static void GenerateSamples(int sampling)
[2859]168{
[2903]169#ifdef USE_3D_SSAO
170
171        SphericalSampleGenerator sph(NUM_SAMPLES, 1.0f);
172        sph.Generate((float *)samples3);
173
174#else
[2887]175        switch (sampling)
176        {
[2930]177        case DeferredRenderer::SAMPLING_POISSON:
[2887]178                {
[2930]179                        PoissonDiscSampleGenerator2 poisson(NUM_SAMPLES, 1.0f);
[2900]180                        poisson.Generate((float *)samples2);
[2887]181                }
182                break;
[2930]183        case DeferredRenderer::SAMPLING_QUADRATIC:
[2887]184                {
[2930]185                        QuadraticDiscSampleGenerator2 g(NUM_SAMPLES, 1.0f);
186                        g.Generate((float *)samples2);
[2887]187                }
188                break;
[2930]189        default: // SAMPLING_DEFAULT
190
191                RandomSampleGenerator2 g(NUM_SAMPLES, 1.0f);
192                g.Generate((float *)samples2);
[2903]193        }
194#endif
[2859]195}
196
197
[2879]198static void CreateNoiseTex2D(int w, int h)
199{
200        //GLubyte *randomNormals = new GLubyte[mWidth * mHeight * 3];
201        float *randomNormals = new float[w * h * 3];
202
[2900]203        static HaltonSequence halton;
204        float r[2];
205
[2879]206        for (int i = 0; i < w * h * 3; i += 3)
207        {
[2901]208               
[2903]209#ifdef USE_3D_SSAO
210                //halton.GetNext(2, r);
211                r[0] = RandomValue(0, 1);
212                r[1] = RandomValue(0, 1);
[2879]213
[2900]214                const float theta = 2.0f * acos(sqrt(1.0f - r[0]));
215                const float phi = 2.0f * M_PI * r[1];
216
217                randomNormals[i + 0] = sin(theta) * cos(phi);
218                randomNormals[i + 1] = sin(theta) * sin(phi);
219                randomNormals[i + 2] = cos(theta);
[2903]220#else
221                // create random samples on a circle
222                r[0] = RandomValue(0, 1);
223                //halton.GetNext(1, r);
224
225                const float theta = 2.0f * acos(sqrt(1.0f - r[0]));
226               
227                randomNormals[i + 0] = cos(theta);
228                randomNormals[i + 1] = sin(theta);
229                randomNormals[i + 2] = 0;
230#endif
[2879]231        }
232
233        glEnable(GL_TEXTURE_2D);
234        glGenTextures(1, &noiseTex);
235        glBindTexture(GL_TEXTURE_2D, noiseTex);
236               
237        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
238        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
239        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
240        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
241
[2884]242        //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);
[2879]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
[2896]256DeferredRenderer::DeferredRenderer(int w, int h, Camera *cam, float scaleFactor):
[2860]257mWidth(w), mHeight(h),
258mCamera(cam),
[2875]259mScaleFactor(scaleFactor),
260mUseTemporalCoherence(true),
[2895]261mRegenerateSamples(true),
[2930]262mSamplingMethod(SAMPLING_POISSON),
[2895]263mShadingMethod(DEFAULT),
[2891]264mFboIndex(0)
[2861]265{
[2879]266        // create noise texture for ssao
267        CreateNoiseTex2D(w, h);
268
[2965]269
[2861]270        ///////////
271        //-- the flip-flop fbos
[2859]272
[2891]273        mFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
274
[2965]275        mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
276        mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
277        mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
278        mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
[2861]279}
280
281
[2896]282DeferredRenderer::~DeferredRenderer()
[2861]283{
[2879]284        if (sCgSsaoProgram)     cgDestroyProgram(sCgSsaoProgram);
285        if (sCgDeferredProgram) cgDestroyProgram(sCgDeferredProgram);
286        if (sCgSsaoProgram)     cgDestroyProgram(sCgSsaoProgram);
287        if (sCgGiProgram) cgDestroyProgram(sCgGiProgram);
288        if (sCgAntiAliasingProgram) cgDestroyProgram(sCgAntiAliasingProgram);
[2861]289
[2891]290        DEL_PTR(mFbo);
[2861]291
292        glDeleteTextures(1, &noiseTex);
293}
294
295
[2896]296void DeferredRenderer::SetUseTemporalCoherence(bool temporal)
[2875]297{
298        mUseTemporalCoherence = temporal;
299}
300
301
[2896]302void DeferredRenderer::Init(CGcontext context)
[2861]303{       
[2873]304        sCgDeferredProgram =
[2868]305                cgCreateProgramFromFile(context,
306                                                                CG_SOURCE,
307                                                                "src/shaders/deferred.cg",
308                                                                RenderState::sCgFragmentProfile,
[2873]309                                                                "main",
[2868]310                                                                NULL);
311
[2873]312        if (sCgDeferredProgram != NULL)
[2868]313        {
[2873]314                cgGLLoadProgram(sCgDeferredProgram);
[2868]315
316                // we need size of texture for scaling
[2873]317                sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "positions"); 
318                sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "colors"); 
[2944]319                sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals");
[2968]320               
[2952]321                sLightDirParam = cgGetNamedParameter(sCgDeferredProgram, "lightDir");
[2868]322        }
323        else
324                cerr << "deferred program failed to load" << endl;
325
326
[2859]327        ///////////////
328
329        sCgSsaoProgram =
330                cgCreateProgramFromFile(context,
331                                                                CG_SOURCE,
[2903]332#ifdef USE_3D_SSAO
[2932]333                                                                "src/shaders/ssao3d.cg",
[2903]334#else
335                                                                "src/shaders/ssao.cg",
336#endif
[2859]337                                                                RenderState::sCgFragmentProfile,
[2868]338                                                                "main",
[2859]339                                                                NULL);
340
341        if (sCgSsaoProgram != NULL)
342        {
343                cgGLLoadProgram(sCgSsaoProgram);
344
345                sPositionsTexParam = cgGetNamedParameter(sCgSsaoProgram, "positions"); 
346                sColorsTexParam = cgGetNamedParameter(sCgSsaoProgram, "colors"); 
[2985]347                sEyePosParam = cgGetNamedParameter(sCgSsaoProgram, "eyePos");
[2859]348                sNormalsTexParam = cgGetNamedParameter(sCgSsaoProgram, "normals"); 
349                sNoiseTexParam = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture");
[2868]350               
[2859]351                sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj");
[2900]352                sModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "mymodelViewProj");
[2859]353                sMaxDepthParam = cgGetNamedParameter(sCgSsaoProgram, "maxDepth");
[2897]354                sTemporalCoherenceParam = cgGetNamedParameter(sCgSsaoProgram, "temporalCoherence");
[2859]355
356                sOldTexParam = cgGetNamedParameter(sCgSsaoProgram, "oldTex"); 
[2884]357                sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples");
[2987]358
[2988]359                sTLParam = cgGetNamedParameter(sCgSsaoProgram, "tl");
360                sTRParam = cgGetNamedParameter(sCgSsaoProgram, "tr");
361                sBRParam = cgGetNamedParameter(sCgSsaoProgram, "br");
362                sBLParam = cgGetNamedParameter(sCgSsaoProgram, "bl");
[2859]363        }
364        else
365                cerr << "ssao program failed to load" << endl;
366
[2873]367        sCgGiProgram =
368                cgCreateProgramFromFile(context,
369                                                                CG_SOURCE,
370                                                                "src/shaders/globillum.cg",
371                                                                RenderState::sCgFragmentProfile,
372                                                                "main",
373                                                                NULL);
374
375        if (sCgGiProgram != NULL)
376        {
377                cgGLLoadProgram(sCgGiProgram);
378
379                // we need size of texture for scaling
380                sPositionsTexGiParam = cgGetNamedParameter(sCgGiProgram, "positions"); 
381                sColorsTexGiParam = cgGetNamedParameter(sCgGiProgram, "colors"); 
382                sNormalsTexGiParam = cgGetNamedParameter(sCgGiProgram, "normals"); 
383               
[2874]384                sOldModelViewProjMatrixGiParam = cgGetNamedParameter(sCgGiProgram, "oldModelViewProj");
[2873]385                sMaxDepthGiParam = cgGetNamedParameter(sCgGiProgram, "maxDepth");
[2897]386                sTemporalCoherenceGiParam = cgGetNamedParameter(sCgGiProgram, "temporalCoherence");
[2873]387
[2944]388                sNoiseTexGiParam = cgGetNamedParameter(sCgGiProgram, "noiseTexture");
[2873]389                sSamplesGiParam = cgGetNamedParameter(sCgGiProgram, "samples");
390               
391                sOldSsaoTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldSsaoTex"); 
[2990]392                sOldIllumTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldIllumTex");
393
394                sTLGiParam = cgGetNamedParameter(sCgGiProgram, "tl");
395                sTRGiParam = cgGetNamedParameter(sCgGiProgram, "tr");
396                sBRGiParam = cgGetNamedParameter(sCgGiProgram, "br");
397                sBLGiParam = cgGetNamedParameter(sCgGiProgram, "bl");
398
399                sEyePosGiParam = cgGetNamedParameter(sCgGiProgram, "eyePos");
[2873]400        }
401        else
402                cerr << "globillum program failed to load" << endl;
403
[2880]404        sCgCombinedIllumProgram =
405                cgCreateProgramFromFile(context,
406                                                                CG_SOURCE,
407                                                                "src/shaders/globillum.cg",
408                                                                RenderState::sCgFragmentProfile,
409                                                                "combine",
410                                                                NULL);
411
412        if (sCgCombinedIllumProgram != NULL)
413        {
414                cgGLLoadProgram(sCgCombinedIllumProgram);
415
416                sColorsTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "colors"); 
417                sSsaoTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "ssaoTex");
418                sIllumTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "illumTex");
419        }
420        else
421                cerr << "combined illum program failed to load" << endl;
422
423
424        sCgCombinedSsaoProgram =
425                cgCreateProgramFromFile(context,
426                                                                CG_SOURCE,
427                                                                "src/shaders/ssao.cg",
428                                                                RenderState::sCgFragmentProfile,
429                                                                "combine",
430                                                                NULL);
431
432        if (sCgCombinedSsaoProgram != NULL)
433        {
434                cgGLLoadProgram(sCgCombinedSsaoProgram);
435
436                sColorsTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "colors"); 
437                sSsaoTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "ssaoTex");
[2974]438                sPositionsTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "positions");
[2880]439        }
440        else
441                cerr << "combied illum program failed to load" << endl;
442
443       
[2865]444        sCgAntiAliasingProgram =
445                cgCreateProgramFromFile(context,
446                                                                CG_SOURCE,
447                                                                "src/shaders/antialiasing.cg",
448                                                                RenderState::sCgFragmentProfile,
449                                                                "main",
450                                                                NULL);
451
452        if (sCgAntiAliasingProgram != NULL)
453        {
454                cgGLLoadProgram(sCgAntiAliasingProgram);
455
456                sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors"); 
457                sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals");
458        }
459        else
460                cerr << "antialiasing program failed to load" << endl;
461
[2895]462        sCgDeferredShadowProgram =
463                cgCreateProgramFromFile(context,
464                                                                CG_SOURCE,
465                                                                "src/shaders/deferred.cg",
466                                                                RenderState::sCgFragmentProfile,
467                                                                "main_shadow",
468                                                                NULL);
469
470        if (sCgDeferredShadowProgram != NULL)
471        {
472                cgGLLoadProgram(sCgDeferredShadowProgram);
473
474                // we need size of texture for scaling
475                sPositionsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "positions"); 
476                sColorsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "colors"); 
477                sNormalsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "normals");
478
479                sShadowMapParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMap"); 
480                sMaxDepthShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "maxDepth");
481                sSampleWidthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "sampleWidth");
482                sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix");
[2928]483
[2944]484                sNoiseTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "noiseTexture");
485                sSamplesShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "samples");
[2952]486                sLightDirShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "lightDir");
[2966]487
488                PoissonDiscSampleGenerator2 poisson(NUM_PCF_TABS, 1.0f);
[2944]489                poisson.Generate((float *)pcfSamples);
[2928]490
[2966]491                cgGLSetParameterArray2f(sSamplesShadowParam, 0, NUM_PCF_TABS, (const float *)pcfSamples);
[2928]492        }
493        else
[2944]494                cerr << "deferred program failed to load" << endl;
[2928]495
[2992]496        sCgLogLumProgram =
497                cgCreateProgramFromFile(context,
498                                                                CG_SOURCE,
499                                                                "src/shaders/tonemap.cg",
500                                                                RenderState::sCgFragmentProfile,
501                                                                "CalcAvgLogLum",
502                                                                NULL);
503
504        if (sCgLogLumProgram != NULL)
505        {
506                cgGLLoadProgram(sCgLogLumProgram);
507                sColorsTexLogLumParam = cgGetNamedParameter(sCgLogLumProgram, "colors"); 
508        }
509        else
510                cerr << "avg loglum program failed to load" << endl;
511
512
[2973]513        sCgToneProgram =
514                cgCreateProgramFromFile(context,
515                                                                CG_SOURCE,
516                                                                "src/shaders/tonemap.cg",
517                                                                RenderState::sCgFragmentProfile,
518                                                                "ToneMap",
519                                                                NULL);
520
521        if (sCgToneProgram != NULL)
522        {
523                cgGLLoadProgram(sCgToneProgram);
524
525                sImageKeyParam = cgGetNamedParameter(sCgToneProgram, "imageKey");
526                sMiddleGreyParam = cgGetNamedParameter(sCgToneProgram, "middleGrey");
527                sWhiteLumParam = cgGetNamedParameter(sCgToneProgram, "whiteLum");
528
529                sColorsTexToneParam = cgGetNamedParameter(sCgToneProgram, "colors"); 
530        }
531        else
532                cerr << "tone program failed to load" << endl;
533
[2992]534/*
[2973]535        sCgInitialIntensityProgram =
536                cgCreateProgramFromFile(context,
537                                                                CG_SOURCE,
538                                                                "src/shaders/tonemap.cg",
539                                                                RenderState::sCgFragmentProfile,
540                                                                "GreyScaleDownSample",
541                                                                NULL);
542
543        if (sCgInitialIntensityProgram != NULL)
544        {
545                cgGLLoadProgram(sCgInitialIntensityProgram);
546
547                // we need size of texture for scaling
548                sColorsTexInitialParam = cgGetNamedParameter(sCgInitialIntensityProgram, "colors"); 
549        }
550        else
551                cerr << "intensity program failed to load" << endl;
552
[2992]553*/
[2973]554
[2859]555        PrintGLerror("init");
556}
557
558
[2896]559void DeferredRenderer::Render(FrameBufferObject *fbo,
[2897]560                                                          const Matrix4x4 &oldProjViewMatrix,
[2900]561                                                          const Matrix4x4 &projViewMatrix,
[2901]562                                                          float tempCohFactor,
[2952]563                                                          DirectionalLight *light,
[2991]564                                                          bool useToneMapping,
565                                                          ShadowMap *shadowMap
566                                                          )
[2859]567{
[2868]568        // switch roles of old and new fbo
569        // the algorihm uses two input fbos, where the one
570        // contais the color buffer from the last frame,
571        // the other one will be written
[2897]572
573        mFboIndex = 2 - mFboIndex;
[2976]574       
[2868]575        FrameBufferObject::Release();
576
[2867]577        cgGLEnableProfile(RenderState::sCgFragmentProfile);
578
579        glDisable(GL_ALPHA_TEST);
580        glDisable(GL_TEXTURE_2D);
581        glDisable(GL_LIGHTING);
582
[2880]583        glPushAttrib(GL_VIEWPORT_BIT);
584        glViewport(0, 0, mWidth, mHeight);
585
[2867]586        glMatrixMode(GL_PROJECTION);
587        glPushMatrix();
588        glLoadIdentity();
589
[2897]590        const float offs = 0.5f;
591        glOrtho(-offs, offs, -offs, offs, 0, 1);
592
[2867]593        glMatrixMode(GL_MODELVIEW);
594        glPushMatrix();
595        glLoadIdentity();
596
[2944]597        if (shadowMap)
[2952]598                FirstPassShadow(fbo, light, shadowMap);
[2895]599        else
[2952]600                FirstPass(fbo, light);
[2944]601
602        switch (mShadingMethod)
[2880]603        {
[2944]604        case SSAO:
605                ComputeSsao(fbo, tempCohFactor, oldProjViewMatrix, projViewMatrix);
606                CombineSsao(fbo);
607                break;
608        case GI:
609                ComputeGlobIllum(fbo, tempCohFactor, oldProjViewMatrix);
610                CombineIllum(fbo);
611                break;
612        default: // DEFAULT
613                // do nothing: standard deferred shading
614                break;
615        }
[2884]616
[2991]617        if (useToneMapping)
618        {
619                float imageKey, whiteLum, middleGrey;
620
621                ComputeToneParameters(fbo, light, imageKey, whiteLum, middleGrey);
622                ToneMap(fbo, light, imageKey, whiteLum, middleGrey);
623        }
624
[2970]625        AntiAliasing(fbo, light);
[2867]626
627        glEnable(GL_LIGHTING);
628        glDisable(GL_TEXTURE_2D);
629
630        glMatrixMode(GL_PROJECTION);
631        glPopMatrix();
632
633        glMatrixMode(GL_MODELVIEW);
634        glPopMatrix();
635
636        glPopAttrib();
637
638        cgGLDisableProfile(RenderState::sCgFragmentProfile);
[2859]639}
640
641
[2896]642void DeferredRenderer::ComputeSsao(FrameBufferObject *fbo,
[2901]643                                                                   float tempCohFactor,
[2900]644                                                                   const Matrix4x4 &oldProjViewMatrix,
645                                                                   const Matrix4x4 &projViewMatrix
646                                                                   )
[2859]647{
[2903]648#ifdef USE_3D_SSAO
[2975]649        // bias from [-1, 1] to [0, 1]
[2900]650        static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.5f,
651                                                                0.0f, 0.5f, 0.0f, 0.5f,
652                                                                0.0f, 0.0f, 0.5f, 0.5f,
[2975]653                                                                0.0f, 0.0f, 0.0f, 1.0f);
[2900]654
655        Matrix4x4 m = projViewMatrix * biasMatrix;
656
657        cgGLSetMatrixParameterfc(sModelViewProjMatrixParam, (const float *)m.x);
[2903]658#endif
[2874]659
[2903]660        cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x);
661
[2975]662        GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
[2861]663        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
664        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
[2859]665
[2992]666        // generate mip map levels of position in order to improve texture lookup performance
667        //glBindTexture(GL_TEXTURE_2D, positionsTex); glGenerateMipmapEXT(GL_TEXTURE_2D);
[2993]668        //glBindTexture(GL_TEXTURE_2D, colorsTex); glGenerateMipmapEXT(GL_TEXTURE_2D);
669
[2861]670        // read the second buffer, write to the first buffer
[2891]671        mFbo->Bind();
[2899]672        glDrawBuffers(1, mrt + mFboIndex);
[2868]673
[2891]674        GLuint oldTex = mFbo->GetColorBuffer(2 - mFboIndex)->GetTexture();
[2860]675
[2859]676        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
677
[2868]678        cgGLEnableProfile(RenderState::sCgFragmentProfile);
[2859]679        cgGLBindProgram(sCgSsaoProgram);
680
681        cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
682        cgGLEnableTextureParameter(sPositionsTexParam);
683
684        cgGLSetTextureParameter(sColorsTexParam, colorsTex);
685        cgGLEnableTextureParameter(sColorsTexParam);
686
687        cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
688        cgGLEnableTextureParameter(sNormalsTexParam);
689
690        cgGLSetTextureParameter(sNoiseTexParam, noiseTex);
691        cgGLEnableTextureParameter(sNoiseTexParam);
692
693        cgGLSetTextureParameter(sOldTexParam, oldTex);
694        cgGLEnableTextureParameter(sOldTexParam);
695       
696        cgGLSetParameter1f(sMaxDepthParam, mScaleFactor);
[2985]697
[2988]698        Vector3 pos = mCamera->GetPosition() / mScaleFactor;
[2985]699        cgGLSetParameter3f(sEyePosParam, pos.x, pos.y, pos.z);
[2875]700       
[2901]701        cgGLSetParameter1f(sTemporalCoherenceParam, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
[2893]702
[2895]703        if (mUseTemporalCoherence || mRegenerateSamples)
[2875]704        {
[2895]705                mRegenerateSamples = false;
[2859]706
[2875]707                // q: should we generate new samples or only rotate the old ones?
708                // in the first case, the sample patterns look nicer, but the kernel
709                // needs longer to converge
[2895]710                GenerateSamples(mSamplingMethod);
[2900]711
[2903]712#ifdef USE_3D_SSAO
[2900]713                cgGLSetParameterArray3f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples3);
[2903]714#else
715                cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples2);
716#endif
[2875]717        }
[2859]718
719        Vector3 tl, tr, bl, br;
720        ComputeViewVectors(tl, tr, bl, br);
721
[2987]722        cgGLSetParameter3f(sBLParam, bl.x, bl.y, bl.z);
723        cgGLSetParameter3f(sBRParam, br.x, br.y, br.z);
[2988]724        cgGLSetParameter3f(sTLParam, tl.x, tl.y, tl.z);
[2987]725        cgGLSetParameter3f(sTRParam, tr.x, tr.y, tr.z);
726
727
[2859]728        glBegin(GL_QUADS);
729
[2975]730        // note: slightly larger texture could hide ambient occlusion error on border but costs resolution
731        const float offs = 0.5f;
[2891]732       
[2975]733        glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-offs, -offs, -0.5f);
734        glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( offs, -offs, -0.5f);
735        glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( offs,  offs, -0.5f);
736        glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-offs,  offs, -0.5f);
[2859]737
738        glEnd();
739
[2860]740        cgGLDisableTextureParameter(sColorsTexParam);
741        cgGLDisableTextureParameter(sPositionsTexParam);
742        cgGLDisableTextureParameter(sNormalsTexParam);
743        cgGLDisableTextureParameter(sNoiseTexParam);
744        cgGLDisableTextureParameter(sOldTexParam);
[2859]745
[2880]746
[2861]747        FrameBufferObject::Release();
[2859]748
749        PrintGLerror("ssao first pass");
750}
751
752
[2896]753void DeferredRenderer::ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br)
[2860]754{
755        Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr;
[2859]756
[2860]757        mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
758
[2888]759        bl = Normalize(nbl - fbl);
760        br = Normalize(nbr - fbr);
761        tl = Normalize(ntl - ftl);
762        tr = Normalize(ntr - ftr);
[2860]763}
764
765
[2865]766static void SetVertex(float x, float y, float x_offs, float y_offs)
767{
768        glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
769        glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
770        glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
771        glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
772        glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
773
774        glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
775        glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
776
777        glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
778}
779
780
[2970]781void DeferredRenderer::AntiAliasing(FrameBufferObject *fbo, DirectionalLight *light)
[2865]782{
[2968]783        ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
[2967]784
[2968]785        GLuint colorsTex = colorBuffer->GetTexture();
[2865]786        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
787       
788        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
789
790        cgGLEnableProfile(RenderState::sCgFragmentProfile);
791        cgGLBindProgram(sCgAntiAliasingProgram);
[2868]792       
[2865]793        cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
794        cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
795
796        cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
797        cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
[2868]798
[2967]799
[2865]800        glColor3f(1.0f, 1.0f, 1.0f);
801
802        glBegin(GL_QUADS);
803
804        // the neighbouring texels
805        float x_offs = 1.0f / mWidth;
806        float y_offs = 1.0f / mHeight;
807
808        SetVertex(0, 0, x_offs, y_offs);
809        SetVertex(1, 0, x_offs, y_offs);
810        SetVertex(1, 1, x_offs, y_offs);
811        SetVertex(0, 1, x_offs, y_offs);
812
813        glEnd();
814
815        cgGLDisableTextureParameter(sColorsTexAntiAliasingParam);
816        cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam);
817
[2867]818        PrintGLerror("antialiasing");
[2865]819}
820
821
[2952]822void DeferredRenderer::FirstPass(FrameBufferObject *fbo, DirectionalLight *light)
[2868]823{
[2973]824        GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
[2868]825        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
826        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
827
[2879]828        fbo->Bind();
[2868]829
[2973]830        colorBufferIdx = 3 - colorBufferIdx;
[2899]831        glDrawBuffers(1, mrt + colorBufferIdx);
[2879]832
[2868]833        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
834       
835        cgGLEnableProfile(RenderState::sCgFragmentProfile);
836
[2873]837        cgGLBindProgram(sCgDeferredProgram);
[2868]838
839        cgGLSetTextureParameter(sColorsTexDeferredParam, colorsTex);
840        cgGLEnableTextureParameter(sColorsTexDeferredParam);
841
842        cgGLSetTextureParameter(sPositionsTexDeferredParam, positionsTex);
843        cgGLEnableTextureParameter(sPositionsTexDeferredParam);
844
845        cgGLSetTextureParameter(sNormalsTexDeferredParam, normalsTex);
846        cgGLEnableTextureParameter(sNormalsTexDeferredParam);
847       
[2952]848        Vector3 lightDir = -light->GetDirection();
849        cgGLSetParameter3f(sLightDirParam, lightDir.x, lightDir.y, lightDir.z);
850
[2966]851
[2868]852        glColor3f(1.0f, 1.0f, 1.0f);
853
854        const float offs = 0.5f;
855
856        glBegin(GL_QUADS);
857
858        glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
859        glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
860        glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f);
861        glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f);
862
863        glEnd();
864
865        cgGLDisableTextureParameter(sColorsTexDeferredParam);
866        cgGLDisableTextureParameter(sPositionsTexDeferredParam);
867        cgGLDisableTextureParameter(sNormalsTexDeferredParam);
868
869        cgGLDisableProfile(RenderState::sCgFragmentProfile);
870
871        FrameBufferObject::Release();
872
873        PrintGLerror("deferred shading");
874}
875
[2869]876
[2928]877
[2896]878void DeferredRenderer::ComputeGlobIllum(FrameBufferObject *fbo,
[2901]879                                                                                float tempCohFactor,
880                                                                                const Matrix4x4 &oldProjViewMatrix)
[2869]881{
[2874]882        cgGLSetMatrixParameterfc(sOldModelViewProjMatrixGiParam, (const float *)oldProjViewMatrix.x);
883
[2974]884        GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
[2873]885        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
886        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
[2869]887
[2974]888        // generate mip map levels for position texture
[2991]889        //glBindTexture(GL_TEXTURE_2D, positionsTex); glGenerateMipmapEXT(GL_TEXTURE_2D);
[2869]890
[2873]891        // read the second buffer, write to the first buffer
[2891]892        mFbo->Bind();
[2873]893
[2899]894        glDrawBuffers(2, mrt + mFboIndex);
[2873]895
[2891]896        GLuint oldSsaoTex = mFbo->GetColorBuffer(2 - mFboIndex)->GetTexture();
897        GLuint oldIllumTex = mFbo->GetColorBuffer(2 - mFboIndex + 1)->GetTexture();
898
[2869]899        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
900
901        cgGLEnableProfile(RenderState::sCgFragmentProfile);
[2873]902        cgGLBindProgram(sCgGiProgram);
[2869]903
[2873]904        cgGLSetTextureParameter(sPositionsTexGiParam, positionsTex);
905        cgGLEnableTextureParameter(sPositionsTexGiParam);
[2869]906
[2873]907        cgGLSetTextureParameter(sColorsTexGiParam, colorsTex);
908        cgGLEnableTextureParameter(sColorsTexGiParam);
[2869]909
[2873]910        cgGLSetTextureParameter(sNormalsTexGiParam, normalsTex);
911        cgGLEnableTextureParameter(sNormalsTexGiParam);
[2869]912
[2873]913        cgGLSetTextureParameter(sNoiseTexGiParam, noiseTex);
914        cgGLEnableTextureParameter(sNoiseTexGiParam);
915
916        cgGLSetTextureParameter(sOldSsaoTexGiParam, oldSsaoTex);
917        cgGLEnableTextureParameter(sOldSsaoTexGiParam);
918
919        cgGLSetTextureParameter(sOldIllumTexGiParam, oldIllumTex);
920        cgGLEnableTextureParameter(sOldIllumTexGiParam);
921
922        cgGLSetParameter1f(sMaxDepthGiParam, mScaleFactor);
923
[2990]924
[2901]925        cgGLSetParameter1f(sTemporalCoherenceGiParam, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
[2873]926
[2897]927
[2895]928        if (mUseTemporalCoherence || mRegenerateSamples)
[2875]929        {
[2895]930                mRegenerateSamples = false;
[2873]931
[2875]932                // q: should we generate new samples or only rotate the old ones?
933                // in the first case, the sample patterns look nicer, but the kernel
934                // needs longer to converge
[2895]935                GenerateSamples(mSamplingMethod);
[2903]936
937#ifdef USE_3D_SSAO
938                cgGLSetParameterArray3f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples3);
939#else
[2900]940                cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples2);
[2903]941#endif
[2875]942        }
943
[2895]944
[2873]945        Vector3 tl, tr, bl, br;
946        ComputeViewVectors(tl, tr, bl, br);
[2990]947       
948        const Vector3 pos = mCamera->GetPosition() / mScaleFactor;
949        cgGLSetParameter3f(sEyePosGiParam, pos.x, pos.y, pos.z);
[2873]950
[2990]951        cgGLSetParameter3f(sBLGiParam, bl.x, bl.y, bl.z);
952        cgGLSetParameter3f(sBRGiParam, br.x, br.y, br.z);
953        cgGLSetParameter3f(sTLGiParam, tl.x, tl.y, tl.z);
954        cgGLSetParameter3f(sTRGiParam, tr.x, tr.y, tr.z);
955
956
[2869]957        glBegin(GL_QUADS);
958
[2873]959        // note: slightly larger texture hides ambient occlusion error on border but costs resolution
960        //const float new_offs = 0.55f;
961        const float new_offs = 0.5f;
[2891]962               
963        glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-new_offs, -new_offs, -0.5f);
964        glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( new_offs, -new_offs, -0.5f);
965        glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( new_offs,  new_offs, -0.5f);
966        glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-new_offs,  new_offs, -0.5f);
[2869]967
968        glEnd();
969
[2873]970        cgGLDisableTextureParameter(sColorsTexGiParam);
971        cgGLDisableTextureParameter(sPositionsTexGiParam);
972        cgGLDisableTextureParameter(sNormalsTexGiParam);
973        cgGLDisableTextureParameter(sNoiseTexGiParam);
974        cgGLDisableTextureParameter(sOldSsaoTexGiParam);
975        cgGLDisableTextureParameter(sOldIllumTexGiParam);
[2869]976
977        FrameBufferObject::Release();
978
[2873]979        PrintGLerror("globillum first pass");
[2869]980}
981
982
[2896]983void DeferredRenderer::CombineIllum(FrameBufferObject *fbo)
[2880]984{
[2973]985        GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
[2884]986
[2891]987        GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
988        GLuint illumTex = mFbo->GetColorBuffer(mFboIndex + 1)->GetTexture();
[2880]989
[2891]990
[2880]991        fbo->Bind();
992
[2884]993        // overwrite old color texture
[2973]994        colorBufferIdx = 3 - colorBufferIdx;
995
[2899]996        glDrawBuffers(1, mrt + colorBufferIdx);
[2880]997
998        cgGLEnableProfile(RenderState::sCgFragmentProfile);
999
1000        cgGLBindProgram(sCgCombinedIllumProgram);
1001
[2881]1002        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1003       
1004
[2880]1005        cgGLSetTextureParameter(sColorsTexCombinedIllumParam, colorsTex);
1006        cgGLEnableTextureParameter(sColorsTexCombinedIllumParam);
1007
1008        cgGLSetTextureParameter(sSsaoTexCombinedIllumParam, ssaoTex);
1009        cgGLEnableTextureParameter(sSsaoTexCombinedIllumParam);
1010
1011        cgGLSetTextureParameter(sIllumTexCombinedIllumParam, illumTex);
1012        cgGLEnableTextureParameter(sIllumTexCombinedIllumParam);
1013       
1014        glColor3f(1.0f, 1.0f, 1.0f);
1015
1016        const float offs = 0.5f;
1017
1018        glBegin(GL_QUADS);
1019
1020        glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
1021        glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
1022        glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f);
1023        glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f);
1024
1025        glEnd();
1026
1027        cgGLDisableTextureParameter(sColorsTexCombinedIllumParam);
1028        cgGLDisableTextureParameter(sSsaoTexCombinedIllumParam);
1029        cgGLDisableTextureParameter(sIllumTexCombinedIllumParam);
1030
1031        cgGLDisableProfile(RenderState::sCgFragmentProfile);
1032
1033        FrameBufferObject::Release();
1034
1035        PrintGLerror("combine");
1036}
1037
1038
[2896]1039void DeferredRenderer::CombineSsao(FrameBufferObject *fbo)
[2880]1040{
[2973]1041        fbo->Bind();
1042
1043        GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
[2974]1044        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
[2891]1045        GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
[2880]1046
[2895]1047        // overwrite old color texture
[2973]1048        colorBufferIdx = 3 - colorBufferIdx;
[2899]1049        glDrawBuffers(1, mrt + colorBufferIdx);
[2880]1050
1051        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1052       
1053        cgGLEnableProfile(RenderState::sCgFragmentProfile);
1054
1055        cgGLBindProgram(sCgCombinedSsaoProgram);
1056
1057        cgGLSetTextureParameter(sColorsTexCombinedSsaoParam, colorsTex);
1058        cgGLEnableTextureParameter(sColorsTexCombinedSsaoParam);
1059
1060        cgGLSetTextureParameter(sSsaoTexCombinedSsaoParam, ssaoTex);
1061        cgGLEnableTextureParameter(sSsaoTexCombinedSsaoParam);
1062
[2974]1063        cgGLSetTextureParameter(sPositionsTexCombinedSsaoParam, positionsTex);
1064        cgGLEnableTextureParameter(sPositionsTexCombinedSsaoParam);
[2880]1065       
1066        glColor3f(1.0f, 1.0f, 1.0f);
1067
1068        const float offs = 0.5f;
1069
1070        glBegin(GL_QUADS);
1071
1072        glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
1073        glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
1074        glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f);
1075        glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f);
1076
1077        glEnd();
1078
1079        cgGLDisableTextureParameter(sColorsTexCombinedSsaoParam);
1080        cgGLDisableTextureParameter(sSsaoTexCombinedSsaoParam);
[2974]1081        cgGLDisableTextureParameter(sPositionsTexCombinedSsaoParam);
1082       
[2880]1083        cgGLDisableProfile(RenderState::sCgFragmentProfile);
1084
1085        FrameBufferObject::Release();
1086
1087        PrintGLerror("combine ssao");
1088}
1089
1090
[2952]1091void DeferredRenderer::FirstPassShadow(FrameBufferObject *fbo,
1092                                                                           DirectionalLight *light,
1093                                                                           ShadowMap *shadowMap)
[2895]1094{
[2973]1095        fbo->Bind();
[2880]1096
[2973]1097        GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
1098
[2895]1099        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
1100        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
[2928]1101        GLuint shadowTex = shadowMap->GetDepthTexture();
[2895]1102
1103        Matrix4x4 shadowMatrix;
1104        shadowMap->GetTextureMatrix(shadowMatrix);
1105
[2973]1106        colorBufferIdx = 3 - colorBufferIdx;
[2899]1107        glDrawBuffers(1, mrt + colorBufferIdx);
[2895]1108
1109       
1110        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1111
1112        cgGLBindProgram(sCgDeferredShadowProgram);
1113
1114        cgGLSetTextureParameter(sColorsTexShadowParam, colorsTex);
1115        cgGLEnableTextureParameter(sColorsTexShadowParam);
1116
1117        cgGLSetTextureParameter(sPositionsTexShadowParam, positionsTex);
1118        cgGLEnableTextureParameter(sPositionsTexShadowParam);
1119
1120        cgGLSetTextureParameter(sNormalsTexShadowParam, normalsTex);
1121        cgGLEnableTextureParameter(sNormalsTexShadowParam);
1122       
1123        cgGLSetTextureParameter(sShadowMapParam, shadowTex);
1124        cgGLEnableTextureParameter(sShadowMapParam);
1125
1126        cgGLSetParameter1f(sMaxDepthShadowParam, mScaleFactor);
[2973]1127
[2946]1128        cgGLSetParameter1f(sSampleWidthParam, 2.0f / shadowMap->GetSize());
[2973]1129       
[2895]1130         
1131        cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x);
1132
[2944]1133        cgGLSetTextureParameter(sNoiseTexShadowParam, noiseTex);
1134        cgGLEnableTextureParameter(sNoiseTexShadowParam);
1135
[2952]1136        Vector3 lightDir = -light->GetDirection();
1137        cgGLSetParameter3f(sLightDirShadowParam, lightDir.x, lightDir.y, lightDir.z);
[2944]1138
[2952]1139
[2895]1140        glColor3f(1.0f, 1.0f, 1.0f);
1141       
1142        glBegin(GL_QUADS);
1143
1144        float offs2 = 0.5f;
1145
1146        glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
1147        glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
1148        glTexCoord2f(1, 1); glVertex3f( offs2,  offs2, -0.5f);
1149        glTexCoord2f(0, 1); glVertex3f(-offs2,  offs2, -0.5f);
1150
1151        glEnd();
1152
1153        cgGLDisableTextureParameter(sColorsTexShadowParam);
1154        cgGLDisableTextureParameter(sPositionsTexShadowParam);
1155        cgGLDisableTextureParameter(sNormalsTexShadowParam);
1156        cgGLDisableTextureParameter(sShadowMapParam);
[2944]1157        cgGLDisableTextureParameter(sNoiseTexShadowParam);
1158
[2895]1159        FrameBufferObject::Release();
1160
1161        PrintGLerror("deferred shading + shadows");
1162}
1163
1164
[2896]1165void DeferredRenderer::SetSamplingMethod(SAMPLING_METHOD s)
[2895]1166{
1167        if (s != mSamplingMethod)
1168        {
1169                mSamplingMethod = s;
1170                mRegenerateSamples = true;
1171        }
1172}
1173
[2897]1174
1175void DeferredRenderer::SetShadingMethod(SHADING_METHOD s)
1176{
1177        if (s != mShadingMethod)
1178        {
1179                mShadingMethod = s;
1180                mRegenerateSamples = true;
1181        }
1182}
1183
[2965]1184
[2973]1185void DeferredRenderer::ComputeToneParameters(FrameBufferObject *fbo,
1186                                                                                         DirectionalLight *light,
1187                                                                                         float &imageKey,
1188                                                                                         float &whiteLum,
1189                                                                                         float &middleGrey)
[2972]1190{
[2975]1191        // hack: estimate value where sky burns out
1192        whiteLum = log(1e4f);
[2973]1193       
[2975]1194        ////////////////////
1195        //-- linear interpolate brightness key depending on the current sun position
[2973]1196
1197        const float minKey = 0.09f;
1198        const float maxKey = 0.5f;
1199
1200        const float lightIntensity = DotProd(-light->GetDirection(), Vector3::UNIT_Z());
1201        middleGrey = lightIntensity * maxKey + (1.0f - lightIntensity) * minKey;
[2991]1202
[2993]1203
[2992]1204#if 1
[2991]1205
1206        //////////
1207        //-- compute avg loglum
1208
1209        ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
[2992]1210        GLuint colorsTex = colorBuffer->GetTexture();
[2991]1211
1212        fbo->Bind();
1213
1214        colorBufferIdx = 3 - colorBufferIdx;
1215        glDrawBuffers(1, mrt + colorBufferIdx);
1216       
[2992]1217
[2991]1218        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1219
1220        cgGLEnableProfile(RenderState::sCgFragmentProfile);
[2992]1221        cgGLBindProgram(sCgLogLumProgram);
[2991]1222       
1223        cgGLSetTextureParameter(sColorsTexLogLumParam, colorsTex);
[2992]1224        cgGLEnableTextureParameter(sColorsTexLogLumParam);
[2991]1225       
1226        const float offs = 0.5f;
1227
1228        glBegin(GL_QUADS);
1229
1230        glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
1231        glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
1232        glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f);
1233        glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f);
1234
1235        glEnd();
1236
1237        cgGLDisableTextureParameter(sColorsTexLogLumParam);
1238
1239        FrameBufferObject::Release();
1240
1241        PrintGLerror("ToneMapParams");
1242
[2992]1243#endif
[2991]1244
1245        ///////////////////
1246        //-- compute avg loglum in scene using mipmapping
1247
1248        glBindTexture(GL_TEXTURE_2D, fbo->GetColorBuffer(colorBufferIdx)->GetTexture());
1249        glGenerateMipmapEXT(GL_TEXTURE_2D);
[2972]1250}
1251
1252
1253void DeferredRenderer::DownSample(FrameBufferObject *fbo)
1254{
[2973]1255        GLuint intensityTex=0;
[2972]1256       
1257        cgGLEnableProfile(RenderState::sCgFragmentProfile);
1258        cgGLBindProgram(sCgDownSampleProgram);
1259       
1260        cgGLSetTextureParameter(sIntensityTexDownSampleParam, intensityTex);
1261        cgGLEnableTextureParameter(sIntensityTexDownSampleParam);
1262}
1263
1264
[2973]1265void DeferredRenderer::ToneMap(FrameBufferObject *fbo,
1266                                                           DirectionalLight *light,
1267                                                           float imageKey,
1268                                                           float whiteLum,
1269                                                           float middleGrey)
[2972]1270{
1271        ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
1272
[2973]1273        fbo->Bind();
[2972]1274
[2973]1275        colorBufferIdx = 3 - colorBufferIdx;
1276        glDrawBuffers(1, mrt + colorBufferIdx);
[2972]1277
1278
1279        GLuint colorsTex = colorBuffer->GetTexture();
1280       
1281        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1282
1283        cgGLEnableProfile(RenderState::sCgFragmentProfile);
1284        cgGLBindProgram(sCgToneProgram);
1285       
1286        cgGLSetTextureParameter(sColorsTexToneParam, colorsTex);
1287        cgGLEnableTextureParameter(sColorsTexToneParam);
1288
1289        cgGLSetParameter1f(sImageKeyParam, imageKey);
1290        cgGLSetParameter1f(sWhiteLumParam, whiteLum);
1291        cgGLSetParameter1f(sMiddleGreyParam, middleGrey);
1292
1293        glColor3f(1.0f, 1.0f, 1.0f);
1294
1295        glBegin(GL_QUADS);
1296
1297        // the neighbouring texels
[2974]1298        const float x_offs = 1.0f / mWidth;
1299        const float y_offs = 1.0f / mHeight;
[2972]1300
1301        SetVertex(0, 0, x_offs, y_offs);
1302        SetVertex(1, 0, x_offs, y_offs);
1303        SetVertex(1, 1, x_offs, y_offs);
1304        SetVertex(0, 1, x_offs, y_offs);
1305
1306        glEnd();
1307
1308        cgGLDisableTextureParameter(sColorsTexToneParam);
[2973]1309        FrameBufferObject::Release();
[2972]1310
[2973]1311        PrintGLerror("ToneMap");
[2972]1312}
1313
1314
1315
1316
[2858]1317} // namespace
Note: See TracBrowser for help on using the repository browser.