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

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