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

Revision 2988, 37.1 KB checked in by mattausch, 16 years ago (diff)

eye linear depth not working

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