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

Revision 2897, 30.4 KB checked in by mattausch, 16 years ago (diff)

improved shadow mapping

RevLine 
[2896]1#include "DeferredRenderer.h"
[2859]2#include "FrameBufferObject.h"
3#include "RenderState.h"
4#include "SampleGenerator.h"
[2860]5#include "Vector3.h"
6#include "Camera.h"
[2884]7#include "shaderenv.h"
[2886]8#include "RndGauss.h"
9#include "Halton.h"
[2895]10#include "ShadowMapping.h"
[2858]11
[2859]12
[2858]13using namespace std;
14
[2879]15static GLenum mymrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT};
[2858]16
[2886]17
[2858]18namespace CHCDemoEngine
19{
20
[2859]21static CGprogram sCgSsaoProgram = NULL;
[2873]22static CGprogram sCgGiProgram = NULL;
23
24static CGprogram sCgDeferredProgram = NULL;
[2868]25static CGprogram sCgAntiAliasingProgram = NULL;
[2895]26static CGprogram sCgDeferredShadowProgram = NULL;
[2859]27
[2869]28static CGparameter sColorsTexCombineParam;
29static CGparameter sSsaoTexCombineParam;
30
[2868]31static CGparameter sColorsTexDeferredParam;
32static CGparameter sPositionsTexDeferredParam;
33static CGparameter sNormalsTexDeferredParam;
34
[2873]35
[2880]36static CGprogram sCgCombinedSsaoProgram = NULL;
37static CGprogram sCgCombinedIllumProgram = NULL;
38
[2873]39///////////////////////////////////////7
40
41
[2859]42static CGparameter sColorsTexParam;
43static CGparameter sPositionsTexParam;
44static CGparameter sNormalsTexParam;
[2868]45
[2859]46static CGparameter sOldModelViewProjMatrixParam;
47static CGparameter sMaxDepthParam;
48static CGparameter sSamplesParam;
49static CGparameter sOldTexParam;
50static CGparameter sNoiseTexParam;
51static CGparameter sNoiseMultiplierParam;
[2897]52static CGparameter sTemporalCoherenceParam;
[2859]53
[2873]54
55///////////////////////////////////////
56
57
58static CGparameter sColorsTexGiParam;
59static CGparameter sPositionsTexGiParam;
60static CGparameter sNormalsTexGiParam;
61
62
63static CGparameter sOldModelViewProjMatrixGiParam;
64static CGparameter sMaxDepthGiParam;
65static CGparameter sSamplesGiParam;
66static CGparameter sOldSsaoTexGiParam;
67static CGparameter sOldIllumTexGiParam;
68static CGparameter sNoiseTexGiParam;
69static CGparameter sNoiseMultiplierGiParam;
[2897]70static CGparameter sTemporalCoherenceGiParam;
[2873]71
72
[2880]73static CGparameter sColorsTexCombinedIllumParam;
74static CGparameter sSsaoTexCombinedIllumParam;
75static CGparameter sIllumTexCombinedIllumParam;
76
77static CGparameter sColorsTexCombinedSsaoParam;
78static CGparameter sSsaoTexCombinedSsaoParam;
79
80
[2873]81////////////
82
[2865]83static CGparameter sColorsTexAntiAliasingParam;
84static CGparameter sNormalsTexAntiAliasingParam;
85
[2895]86
87static CGparameter sShadowMapParam;
88static CGparameter sPositionsTexShadowParam; 
89static CGparameter sColorsTexShadowParam; 
90static CGparameter sNormalsTexShadowParam;
91
92static CGparameter sShadowMatrixParam;
93static CGparameter sMaxDepthShadowParam;
94static CGparameter sSampleWidthParam;
95
96
97
[2879]98static GLuint noiseTex = 0;
[2865]99
[2859]100// ssao random spherical samples
101static Sample2 samples[NUM_SAMPLES];
102
[2895]103static int colorBufferIdx = 0;
[2859]104
105static void PrintGLerror(char *msg)
106{
107        GLenum errCode;
108        const GLubyte *errStr;
109       
110        if ((errCode = glGetError()) != GL_NO_ERROR)
111        {
112                errStr = gluErrorString(errCode);
113                fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
114        }
115}
116
117
118/** Generate poisson disc distributed sample points on the unit disc
119*/
[2887]120static void GenerateSamples(int sampling)
[2859]121{
[2887]122        switch (sampling)
123        {
[2896]124        case DeferredRenderer::POISSON:
[2887]125                {
126                        static PoissonDiscSampleGenerator poisson(NUM_SAMPLES, 1.0f);
127                        poisson.Generate((Sample2 *)samples);
128                }
129                break;
[2896]130        case DeferredRenderer::GAUSS:
[2887]131                {
132                        const float radius = 1.0f;
133                        const float sigma = 0.25f;//ComputeSigmaFromRadius(1.0f);
134
135                        static HaltonSequence halton;
136
137                        for (int i = 0; i < NUM_SAMPLES; ++ i)
138                        {
139                                Sample2 s;
140                                Vector3 input;
141
142                                halton.GetNext(3, (float *)&input.x);
143
144                                //GaussianOn2D(cinput, // input (RND in interval [0-1)x[0-1))
145                                //               sigma,            // standard deviation of gaussian distribution
146                                //                       outputVec)   // resulting vector according to gaussian distr.
147
148                                PolarGaussianOnDisk(input,  // input (RND in interval [0-1)x[0-1))
149                                        sigma,  // standard deviation of gaussian distribution
150                                        radius, // standard deviation of gaussian distribution
151                                        s);     // result
152                                samples[i] = s;
153                        }
154                }
155                break;
156        default:
157                break;
[2886]158        }
[2859]159}
160
161
[2879]162static void CreateNoiseTex2D(int w, int h)
163{
164        //GLubyte *randomNormals = new GLubyte[mWidth * mHeight * 3];
165        float *randomNormals = new float[w * h * 3];
166
167        for (int i = 0; i < w * h * 3; i += 3)
168        {
169                // create random samples on a circle
170                const float rx = RandomValue(0, 1);
171                const float theta = 2.0f * acos(sqrt(1.0f - rx));
172
173                //randomNormals[i + 0] = (GLubyte)((cos(theta) * 0.5f + 0.5f) * 255.0f);
174                //randomNormals[i + 1] = (GLubyte)((sin(theta) * 0.5f + 0.5f) * 255.0f);
175                randomNormals[i + 0] = cos(theta);
176                randomNormals[i + 1] = sin(theta);
177                randomNormals[i + 2] = 0;
178        }
179
180        glEnable(GL_TEXTURE_2D);
181        glGenTextures(1, &noiseTex);
182        glBindTexture(GL_TEXTURE_2D, noiseTex);
183               
184        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
185        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
186        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
187        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
188
[2884]189        //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);
[2879]190        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, w, h, 0, GL_RGB, GL_FLOAT, randomNormals);
191
192        glBindTexture(GL_TEXTURE_2D, 0);
193        glDisable(GL_TEXTURE_2D);
194
195        delete [] randomNormals;
196
197        cout << "created noise texture" << endl;
198
199        PrintGLerror("noisetexture");
200}
201
202
[2896]203DeferredRenderer::DeferredRenderer(int w, int h, Camera *cam, float scaleFactor):
[2860]204mWidth(w), mHeight(h),
205mCamera(cam),
[2875]206mScaleFactor(scaleFactor),
207mUseTemporalCoherence(true),
[2895]208mRegenerateSamples(true),
209mSamplingMethod(POISSON),
210mShadingMethod(DEFAULT),
[2891]211mFboIndex(0)
[2861]212{
[2879]213        // create noise texture for ssao
214        CreateNoiseTex2D(w, h);
215
[2861]216        ///////////
217        //-- the flip-flop fbos
[2859]218
[2891]219        /*mNewFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
[2880]220       
[2867]221        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
[2871]222        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
[2861]223       
[2881]224
225        ///////////////////////
226
[2861]227        mOldFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
[2880]228       
[2867]229        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
[2891]230        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);*/
[2861]231       
[2891]232        mFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
233
234        mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
235        mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
236        mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
237        mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
[2861]238}
239
240
[2896]241DeferredRenderer::~DeferredRenderer()
[2861]242{
[2879]243        if (sCgSsaoProgram)     cgDestroyProgram(sCgSsaoProgram);
244        if (sCgDeferredProgram) cgDestroyProgram(sCgDeferredProgram);
245        if (sCgSsaoProgram)     cgDestroyProgram(sCgSsaoProgram);
246        if (sCgGiProgram) cgDestroyProgram(sCgGiProgram);
247        if (sCgAntiAliasingProgram) cgDestroyProgram(sCgAntiAliasingProgram);
[2861]248
[2891]249        //DEL_PTR(mNewFbo);
250        //DEL_PTR(mOldFbo);
251        DEL_PTR(mFbo);
[2861]252
253        glDeleteTextures(1, &noiseTex);
254}
255
256
[2896]257void DeferredRenderer::SetUseTemporalCoherence(bool temporal)
[2875]258{
259        mUseTemporalCoherence = temporal;
260}
261
262
[2896]263void DeferredRenderer::Init(CGcontext context)
[2861]264{       
[2873]265        sCgDeferredProgram =
[2868]266                cgCreateProgramFromFile(context,
267                                                                CG_SOURCE,
268                                                                "src/shaders/deferred.cg",
269                                                                RenderState::sCgFragmentProfile,
[2873]270                                                                "main",
[2868]271                                                                NULL);
272
[2873]273        if (sCgDeferredProgram != NULL)
[2868]274        {
[2873]275                cgGLLoadProgram(sCgDeferredProgram);
[2868]276
277                // we need size of texture for scaling
[2873]278                sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "positions"); 
279                sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "colors"); 
280                sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals");
[2868]281        }
282        else
283                cerr << "deferred program failed to load" << endl;
284
285
[2859]286        ///////////////
287
288        sCgSsaoProgram =
289                cgCreateProgramFromFile(context,
290                                                                CG_SOURCE,
[2868]291                                                                "src/shaders/ssao.cg",
[2859]292                                                                RenderState::sCgFragmentProfile,
[2868]293                                                                "main",
[2859]294                                                                NULL);
295
296        if (sCgSsaoProgram != NULL)
297        {
298                cgGLLoadProgram(sCgSsaoProgram);
299
300                sPositionsTexParam = cgGetNamedParameter(sCgSsaoProgram, "positions"); 
301                sColorsTexParam = cgGetNamedParameter(sCgSsaoProgram, "colors"); 
302                sNormalsTexParam = cgGetNamedParameter(sCgSsaoProgram, "normals"); 
303                sNoiseTexParam = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture");
304                sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier");
[2868]305               
[2859]306                sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj");
307                sMaxDepthParam = cgGetNamedParameter(sCgSsaoProgram, "maxDepth");
[2897]308                sTemporalCoherenceParam = cgGetNamedParameter(sCgSsaoProgram, "temporalCoherence");
[2859]309
310                sOldTexParam = cgGetNamedParameter(sCgSsaoProgram, "oldTex"); 
[2873]311               
[2884]312                cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));
313                sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples");
[2859]314        }
315        else
316                cerr << "ssao program failed to load" << endl;
317
[2873]318        sCgGiProgram =
319                cgCreateProgramFromFile(context,
320                                                                CG_SOURCE,
321                                                                "src/shaders/globillum.cg",
322                                                                RenderState::sCgFragmentProfile,
323                                                                "main",
324                                                                NULL);
325
326        if (sCgGiProgram != NULL)
327        {
328                cgGLLoadProgram(sCgGiProgram);
329
330                // we need size of texture for scaling
331                sPositionsTexGiParam = cgGetNamedParameter(sCgGiProgram, "positions"); 
332                sColorsTexGiParam = cgGetNamedParameter(sCgGiProgram, "colors"); 
333                sNormalsTexGiParam = cgGetNamedParameter(sCgGiProgram, "normals"); 
334                sNoiseTexGiParam = cgGetNamedParameter(sCgGiProgram, "noiseTexture");
335                sNoiseMultiplierGiParam = cgGetNamedParameter(sCgGiProgram, "noiseMultiplier");
336               
[2874]337                sOldModelViewProjMatrixGiParam = cgGetNamedParameter(sCgGiProgram, "oldModelViewProj");
[2873]338                sMaxDepthGiParam = cgGetNamedParameter(sCgGiProgram, "maxDepth");
[2897]339                sTemporalCoherenceGiParam = cgGetNamedParameter(sCgGiProgram, "temporalCoherence");
[2873]340
341                sSamplesGiParam = cgGetNamedParameter(sCgGiProgram, "samples");
342               
343                sOldSsaoTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldSsaoTex"); 
344                sOldIllumTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldIllumTex"); 
345
[2875]346                cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f));
[2873]347        }
348        else
349                cerr << "globillum program failed to load" << endl;
350
[2880]351        sCgCombinedIllumProgram =
352                cgCreateProgramFromFile(context,
353                                                                CG_SOURCE,
354                                                                "src/shaders/globillum.cg",
355                                                                RenderState::sCgFragmentProfile,
356                                                                "combine",
357                                                                NULL);
358
359        if (sCgCombinedIllumProgram != NULL)
360        {
361                cgGLLoadProgram(sCgCombinedIllumProgram);
362
363                sColorsTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "colors"); 
364                sSsaoTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "ssaoTex");
365                sIllumTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "illumTex");
366        }
367        else
368                cerr << "combined illum program failed to load" << endl;
369
370
371        sCgCombinedSsaoProgram =
372                cgCreateProgramFromFile(context,
373                                                                CG_SOURCE,
374                                                                "src/shaders/ssao.cg",
375                                                                RenderState::sCgFragmentProfile,
376                                                                "combine",
377                                                                NULL);
378
379        if (sCgCombinedSsaoProgram != NULL)
380        {
381                cgGLLoadProgram(sCgCombinedSsaoProgram);
382
383                sColorsTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "colors"); 
384                sSsaoTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "ssaoTex");
385        }
386        else
387                cerr << "combied illum program failed to load" << endl;
388
389       
[2865]390        sCgAntiAliasingProgram =
391                cgCreateProgramFromFile(context,
392                                                                CG_SOURCE,
393                                                                "src/shaders/antialiasing.cg",
394                                                                RenderState::sCgFragmentProfile,
395                                                                "main",
396                                                                NULL);
397
398        if (sCgAntiAliasingProgram != NULL)
399        {
400                cgGLLoadProgram(sCgAntiAliasingProgram);
401
402                sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors"); 
403                sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals");
404        }
405        else
406                cerr << "antialiasing program failed to load" << endl;
407
[2895]408        sCgDeferredShadowProgram =
409                cgCreateProgramFromFile(context,
410                                                                CG_SOURCE,
411                                                                "src/shaders/deferred.cg",
412                                                                RenderState::sCgFragmentProfile,
413                                                                "main_shadow",
414                                                                NULL);
415
416        if (sCgDeferredShadowProgram != NULL)
417        {
418                cgGLLoadProgram(sCgDeferredShadowProgram);
419
420                // we need size of texture for scaling
421                sPositionsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "positions"); 
422                sColorsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "colors"); 
423                sNormalsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "normals");
424
425                sShadowMapParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMap"); 
426                sMaxDepthShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "maxDepth");
427                sSampleWidthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "sampleWidth");
428                sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix");
429        }
430        else
431                cerr << "deferred program failed to load" << endl;
[2859]432        PrintGLerror("init");
433}
434
435
[2896]436void DeferredRenderer::Render(FrameBufferObject *fbo,
[2897]437                                                          const Matrix4x4 &oldProjViewMatrix,
438                                                          float expFactor,
439                                                          ShadowMap *shadowMap)
[2859]440{
[2868]441        // switch roles of old and new fbo
442        // the algorihm uses two input fbos, where the one
443        // contais the color buffer from the last frame,
444        // the other one will be written
[2897]445
446        mFboIndex = 2 - mFboIndex;
[2891]447        //swap(mNewFbo, mOldFbo);       
[2897]448
[2868]449        FrameBufferObject::Release();
450
[2867]451        cgGLEnableProfile(RenderState::sCgFragmentProfile);
452
453        glDisable(GL_ALPHA_TEST);
454        glDisable(GL_TEXTURE_2D);
455        glDisable(GL_LIGHTING);
456
[2880]457        glPushAttrib(GL_VIEWPORT_BIT);
458        glViewport(0, 0, mWidth, mHeight);
459
[2867]460        glMatrixMode(GL_PROJECTION);
461        glPushMatrix();
462        glLoadIdentity();
463
[2897]464        const float offs = 0.5f;
465        glOrtho(-offs, offs, -offs, offs, 0, 1);
466
[2867]467        glMatrixMode(GL_MODELVIEW);
468        glPushMatrix();
469        glLoadIdentity();
470
[2895]471        if (shadowMap)
472                FirstPassShadow(fbo, shadowMap);
473        else
474                FirstPass(fbo);
[2873]475       
[2895]476        switch (mShadingMethod)
[2880]477        {
[2895]478        case SSAO:
[2875]479                ComputeSsao(fbo, expFactor, oldProjViewMatrix);
[2880]480                CombineSsao(fbo);
[2895]481                break;
482        case GI:
[2875]483                ComputeGlobIllum(fbo, expFactor, oldProjViewMatrix);
[2880]484                CombineIllum(fbo);
[2895]485                break;
486        default: // DEFAULT
487                // do nothing: standard deferred shading
488                break;
[2880]489        }
[2884]490
[2865]491        AntiAliasing(fbo);
[2867]492
493        glEnable(GL_LIGHTING);
494        glDisable(GL_TEXTURE_2D);
495
496        glMatrixMode(GL_PROJECTION);
497        glPopMatrix();
498
499        glMatrixMode(GL_MODELVIEW);
500        glPopMatrix();
501
502        glPopAttrib();
503
504        cgGLDisableProfile(RenderState::sCgFragmentProfile);
[2859]505}
506
507
[2896]508void DeferredRenderer::ComputeSsao(FrameBufferObject *fbo,
[2874]509                                                         float expFactor,
510                                                         const Matrix4x4 &oldProjViewMatrix
511                                                         )
[2859]512{
[2874]513        cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x);
514
[2879]515        GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
[2861]516        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
517        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
[2859]518
[2867]519        if (1)
520        {
521                // generate mip map levels for position texture
522                glBindTexture(GL_TEXTURE_2D, positionsTex);
523                glGenerateMipmapEXT(GL_TEXTURE_2D);
524        }
525
526
[2861]527        // read the second buffer, write to the first buffer
[2891]528        //mNewFbo->Bind();
529        mFbo->Bind();
530        glDrawBuffers(1, mymrt + mFboIndex);
[2868]531
[2880]532
[2891]533        //GLuint oldTex = mOldFbo->GetColorBuffer(0)->GetTexture();
534        GLuint oldTex = mFbo->GetColorBuffer(2 - mFboIndex)->GetTexture();
[2860]535
[2859]536        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
537
[2868]538        cgGLEnableProfile(RenderState::sCgFragmentProfile);
[2859]539        cgGLBindProgram(sCgSsaoProgram);
540
541        cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
542        cgGLEnableTextureParameter(sPositionsTexParam);
543
544        cgGLSetTextureParameter(sColorsTexParam, colorsTex);
545        cgGLEnableTextureParameter(sColorsTexParam);
546
547        cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
548        cgGLEnableTextureParameter(sNormalsTexParam);
549
550        cgGLSetTextureParameter(sNoiseTexParam, noiseTex);
551        cgGLEnableTextureParameter(sNoiseTexParam);
552
553        cgGLSetTextureParameter(sOldTexParam, oldTex);
554        cgGLEnableTextureParameter(sOldTexParam);
555       
556        cgGLSetParameter1f(sMaxDepthParam, mScaleFactor);
[2875]557       
[2897]558        cgGLSetParameter1f(sTemporalCoherenceParam, (mUseTemporalCoherence && !mRegenerateSamples) ? 255 : 0);
[2893]559
[2895]560        if (mUseTemporalCoherence || mRegenerateSamples)
[2875]561        {
[2895]562                mRegenerateSamples = false;
[2875]563                cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));
[2859]564
[2875]565                // q: should we generate new samples or only rotate the old ones?
566                // in the first case, the sample patterns look nicer, but the kernel
567                // needs longer to converge
[2895]568                GenerateSamples(mSamplingMethod);
[2875]569                cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);
570        }
[2859]571
572        Vector3 tl, tr, bl, br;
573        ComputeViewVectors(tl, tr, bl, br);
574
575        glBegin(GL_QUADS);
576
577        // note: slightly larger texture hides ambient occlusion error on border but costs resolution
[2860]578        //const float new_offs = 0.55f;
579        const float new_offs = 0.5f;
[2891]580       
[2890]581        glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-new_offs, -new_offs, -0.5f);
582        glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( new_offs, -new_offs, -0.5f);
583        glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( new_offs,  new_offs, -0.5f);
584        glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-new_offs,  new_offs, -0.5f);
[2859]585
586        glEnd();
587
[2860]588        cgGLDisableTextureParameter(sColorsTexParam);
589        cgGLDisableTextureParameter(sPositionsTexParam);
590        cgGLDisableTextureParameter(sNormalsTexParam);
591        cgGLDisableTextureParameter(sNoiseTexParam);
592        cgGLDisableTextureParameter(sOldTexParam);
[2859]593
[2880]594
[2861]595        FrameBufferObject::Release();
[2859]596
597        PrintGLerror("ssao first pass");
598}
599
600
[2896]601void DeferredRenderer::ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br)
[2860]602{
603        Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr;
[2859]604
[2860]605        mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
606
[2888]607        bl = Normalize(nbl - fbl);
608        br = Normalize(nbr - fbr);
609        tl = Normalize(ntl - ftl);
610        tr = Normalize(ntr - ftr);
[2860]611}
612
613
614
[2865]615static void SetVertex(float x, float y, float x_offs, float y_offs)
616{
617        glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
618        glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
619        glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
620        glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
621        glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
622
623        glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
624        glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
625
626        glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
627}
628
629
[2896]630void DeferredRenderer::AntiAliasing(FrameBufferObject *fbo)
[2865]631{
[2895]632        GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
[2865]633        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
634       
635        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
636
637        cgGLEnableProfile(RenderState::sCgFragmentProfile);
638        cgGLBindProgram(sCgAntiAliasingProgram);
[2868]639       
[2865]640        cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
641        cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
642
643        cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
644        cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
[2868]645
[2865]646        glColor3f(1.0f, 1.0f, 1.0f);
647
648        float offs2 = 0.5f;
649
650        glBegin(GL_QUADS);
651
652        // the neighbouring texels
653        float x_offs = 1.0f / mWidth;
654        float y_offs = 1.0f / mHeight;
655
656        SetVertex(0, 0, x_offs, y_offs);
657        SetVertex(1, 0, x_offs, y_offs);
658        SetVertex(1, 1, x_offs, y_offs);
659        SetVertex(0, 1, x_offs, y_offs);
660
661        glEnd();
662
663        cgGLDisableTextureParameter(sColorsTexAntiAliasingParam);
664        cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam);
665
[2867]666        PrintGLerror("antialiasing");
[2865]667}
668
669
[2896]670void DeferredRenderer::FirstPass(FrameBufferObject *fbo)
[2868]671{
672        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
673        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
674        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
675
[2879]676        fbo->Bind();
[2868]677
[2895]678        colorBufferIdx = 3;
679        glDrawBuffers(1, mymrt + colorBufferIdx);
[2879]680
[2868]681        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
682       
683        cgGLEnableProfile(RenderState::sCgFragmentProfile);
684
[2873]685        cgGLBindProgram(sCgDeferredProgram);
[2868]686
687        cgGLSetTextureParameter(sColorsTexDeferredParam, colorsTex);
688        cgGLEnableTextureParameter(sColorsTexDeferredParam);
689
690        cgGLSetTextureParameter(sPositionsTexDeferredParam, positionsTex);
691        cgGLEnableTextureParameter(sPositionsTexDeferredParam);
692
693        cgGLSetTextureParameter(sNormalsTexDeferredParam, normalsTex);
694        cgGLEnableTextureParameter(sNormalsTexDeferredParam);
695       
696        glColor3f(1.0f, 1.0f, 1.0f);
697
698        const float offs = 0.5f;
699
700        glBegin(GL_QUADS);
701
702        glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
703        glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
704        glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f);
705        glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f);
706
707        glEnd();
708
709        cgGLDisableTextureParameter(sColorsTexDeferredParam);
710        cgGLDisableTextureParameter(sPositionsTexDeferredParam);
711        cgGLDisableTextureParameter(sNormalsTexDeferredParam);
712
713        cgGLDisableProfile(RenderState::sCgFragmentProfile);
714
715        FrameBufferObject::Release();
716
717        PrintGLerror("deferred shading");
718}
719
[2869]720
[2896]721void DeferredRenderer::ComputeGlobIllum(FrameBufferObject *fbo,
[2874]722                                                                  float expFactor,
723                                                                  const Matrix4x4 &oldProjViewMatrix
724                                                                  )
[2869]725{
[2874]726        cgGLSetMatrixParameterfc(sOldModelViewProjMatrixGiParam, (const float *)oldProjViewMatrix.x);
727
[2879]728        //GLuint colorsTex = mFbo->GetColorBuffer(0)->GetTexture();
729        GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
[2873]730        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
731        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
[2869]732
[2873]733        if (1)
734        {
735                // generate mip map levels for position texture
736                glBindTexture(GL_TEXTURE_2D, positionsTex);
737                glGenerateMipmapEXT(GL_TEXTURE_2D);
[2869]738
[2873]739                // generate mip map levels for position texture
740                glBindTexture(GL_TEXTURE_2D, colorsTex);
741                glGenerateMipmapEXT(GL_TEXTURE_2D);
742        }
743
744
745        // read the second buffer, write to the first buffer
[2891]746        //mNewFbo->Bind();
747        mFbo->Bind();
[2873]748
[2891]749        glDrawBuffers(2, mymrt + mFboIndex);
[2873]750
[2891]751        //GLuint oldSsaoTex = mOldFbo->GetColorBuffer(0)->GetTexture();
752        //GLuint oldIllumTex = mOldFbo->GetColorBuffer(1)->GetTexture();
753       
754        GLuint oldSsaoTex = mFbo->GetColorBuffer(2 - mFboIndex)->GetTexture();
755        GLuint oldIllumTex = mFbo->GetColorBuffer(2 - mFboIndex + 1)->GetTexture();
756
[2869]757        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
758
759        cgGLEnableProfile(RenderState::sCgFragmentProfile);
[2873]760        cgGLBindProgram(sCgGiProgram);
[2869]761
[2873]762        cgGLSetTextureParameter(sPositionsTexGiParam, positionsTex);
763        cgGLEnableTextureParameter(sPositionsTexGiParam);
[2869]764
[2873]765        cgGLSetTextureParameter(sColorsTexGiParam, colorsTex);
766        cgGLEnableTextureParameter(sColorsTexGiParam);
[2869]767
[2873]768        cgGLSetTextureParameter(sNormalsTexGiParam, normalsTex);
769        cgGLEnableTextureParameter(sNormalsTexGiParam);
[2869]770
[2873]771        cgGLSetTextureParameter(sNoiseTexGiParam, noiseTex);
772        cgGLEnableTextureParameter(sNoiseTexGiParam);
773
774        cgGLSetTextureParameter(sOldSsaoTexGiParam, oldSsaoTex);
775        cgGLEnableTextureParameter(sOldSsaoTexGiParam);
776
777        cgGLSetTextureParameter(sOldIllumTexGiParam, oldIllumTex);
778        cgGLEnableTextureParameter(sOldIllumTexGiParam);
779
780        cgGLSetParameter1f(sMaxDepthGiParam, mScaleFactor);
781
[2897]782        cgGLSetParameter1f(sTemporalCoherenceGiParam, (mUseTemporalCoherence && !mRegenerateSamples) ? 255 : 0);
[2873]783
[2897]784
[2895]785        if (mUseTemporalCoherence || mRegenerateSamples)
[2875]786        {
[2895]787                mRegenerateSamples = false;
[2875]788                cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f));
[2873]789
[2875]790                // q: should we generate new samples or only rotate the old ones?
791                // in the first case, the sample patterns look nicer, but the kernel
792                // needs longer to converge
[2895]793                GenerateSamples(mSamplingMethod);
[2875]794                cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples);
795        }
796
[2895]797
[2873]798        Vector3 tl, tr, bl, br;
799        ComputeViewVectors(tl, tr, bl, br);
800
[2869]801        glBegin(GL_QUADS);
802
[2873]803        // note: slightly larger texture hides ambient occlusion error on border but costs resolution
804        //const float new_offs = 0.55f;
805        const float new_offs = 0.5f;
[2891]806               
807        glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-new_offs, -new_offs, -0.5f);
808        glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( new_offs, -new_offs, -0.5f);
809        glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( new_offs,  new_offs, -0.5f);
810        glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-new_offs,  new_offs, -0.5f);
[2869]811
812        glEnd();
813
[2873]814        cgGLDisableTextureParameter(sColorsTexGiParam);
815        cgGLDisableTextureParameter(sPositionsTexGiParam);
816        cgGLDisableTextureParameter(sNormalsTexGiParam);
817        cgGLDisableTextureParameter(sNoiseTexGiParam);
818        cgGLDisableTextureParameter(sOldSsaoTexGiParam);
819        cgGLDisableTextureParameter(sOldIllumTexGiParam);
[2869]820
821        FrameBufferObject::Release();
822
[2873]823        PrintGLerror("globillum first pass");
[2869]824}
825
826
[2896]827void DeferredRenderer::CombineIllum(FrameBufferObject *fbo)
[2880]828{
829        GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
[2884]830
[2891]831        //GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
832        //GLuint illumTex = mNewFbo->GetColorBuffer(1)->GetTexture();
833        GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
834        GLuint illumTex = mFbo->GetColorBuffer(mFboIndex + 1)->GetTexture();
[2880]835
[2891]836
[2880]837        fbo->Bind();
838
[2884]839        // overwrite old color texture
[2895]840        colorBufferIdx = 0;
841        glDrawBuffers(1, mymrt + colorBufferIdx);
[2880]842
843        cgGLEnableProfile(RenderState::sCgFragmentProfile);
844
845        cgGLBindProgram(sCgCombinedIllumProgram);
846
[2881]847        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
848       
849
[2880]850        cgGLSetTextureParameter(sColorsTexCombinedIllumParam, colorsTex);
851        cgGLEnableTextureParameter(sColorsTexCombinedIllumParam);
852
853        cgGLSetTextureParameter(sSsaoTexCombinedIllumParam, ssaoTex);
854        cgGLEnableTextureParameter(sSsaoTexCombinedIllumParam);
855
856        cgGLSetTextureParameter(sIllumTexCombinedIllumParam, illumTex);
857        cgGLEnableTextureParameter(sIllumTexCombinedIllumParam);
858       
859        glColor3f(1.0f, 1.0f, 1.0f);
860
861        const float offs = 0.5f;
862
863        glBegin(GL_QUADS);
864
865        glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
866        glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
867        glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f);
868        glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f);
869
870        glEnd();
871
872        cgGLDisableTextureParameter(sColorsTexCombinedIllumParam);
873        cgGLDisableTextureParameter(sSsaoTexCombinedIllumParam);
874        cgGLDisableTextureParameter(sIllumTexCombinedIllumParam);
875
876        cgGLDisableProfile(RenderState::sCgFragmentProfile);
877
878        FrameBufferObject::Release();
879
880        PrintGLerror("combine");
881}
882
883
[2896]884void DeferredRenderer::CombineSsao(FrameBufferObject *fbo)
[2880]885{
886        GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
[2891]887        //GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
888        GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
[2880]889
890        fbo->Bind();
891
[2895]892        // overwrite old color texture
893        colorBufferIdx = 0;
894        glDrawBuffers(1, mymrt + colorBufferIdx);
[2880]895
896        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
897       
898        cgGLEnableProfile(RenderState::sCgFragmentProfile);
899
900        cgGLBindProgram(sCgCombinedSsaoProgram);
901
902        cgGLSetTextureParameter(sColorsTexCombinedSsaoParam, colorsTex);
903        cgGLEnableTextureParameter(sColorsTexCombinedSsaoParam);
904
905        cgGLSetTextureParameter(sSsaoTexCombinedSsaoParam, ssaoTex);
906        cgGLEnableTextureParameter(sSsaoTexCombinedSsaoParam);
907
908       
909        glColor3f(1.0f, 1.0f, 1.0f);
910
911        const float offs = 0.5f;
912
913        glBegin(GL_QUADS);
914
915        glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
916        glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
917        glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f);
918        glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f);
919
920        glEnd();
921
922        cgGLDisableTextureParameter(sColorsTexCombinedSsaoParam);
923        cgGLDisableTextureParameter(sSsaoTexCombinedSsaoParam);
924
925        cgGLDisableProfile(RenderState::sCgFragmentProfile);
926
927        FrameBufferObject::Release();
928
929        PrintGLerror("combine ssao");
930}
931
932
[2896]933void DeferredRenderer::FirstPassShadow(FrameBufferObject *fbo, ShadowMap *shadowMap)
[2895]934{
935        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
[2880]936
[2895]937        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
938        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
939        GLuint shadowTex = shadowMap->GetShadowTexture();
940
941        Matrix4x4 shadowMatrix;
942        shadowMap->GetTextureMatrix(shadowMatrix);
943
944        fbo->Bind();
945
946        colorBufferIdx = 3;
947        glDrawBuffers(1, mymrt + colorBufferIdx);
948
949       
950        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
951
952        cgGLBindProgram(sCgDeferredShadowProgram);
953
954        cgGLSetTextureParameter(sColorsTexShadowParam, colorsTex);
955        cgGLEnableTextureParameter(sColorsTexShadowParam);
956
957        cgGLSetTextureParameter(sPositionsTexShadowParam, positionsTex);
958        cgGLEnableTextureParameter(sPositionsTexShadowParam);
959
960        cgGLSetTextureParameter(sNormalsTexShadowParam, normalsTex);
961        cgGLEnableTextureParameter(sNormalsTexShadowParam);
962       
963        cgGLSetTextureParameter(sShadowMapParam, shadowTex);
964        cgGLEnableTextureParameter(sShadowMapParam);
965
966        cgGLSetParameter1f(sMaxDepthShadowParam, mScaleFactor);
967        cgGLSetParameter1f(sSampleWidthParam, 1.0f / shadowMap->GetSize());
968         
969        cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x);
970
971        glColor3f(1.0f, 1.0f, 1.0f);
972       
973        glBegin(GL_QUADS);
974
975        float offs2 = 0.5f;
976
977        glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
978        glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
979        glTexCoord2f(1, 1); glVertex3f( offs2,  offs2, -0.5f);
980        glTexCoord2f(0, 1); glVertex3f(-offs2,  offs2, -0.5f);
981
982        glEnd();
983
984        cgGLDisableTextureParameter(sColorsTexShadowParam);
985        cgGLDisableTextureParameter(sPositionsTexShadowParam);
986        cgGLDisableTextureParameter(sNormalsTexShadowParam);
987        cgGLDisableTextureParameter(sShadowMapParam);
988
989        FrameBufferObject::Release();
990
991        PrintGLerror("deferred shading + shadows");
992}
993
994
[2896]995void DeferredRenderer::SetSamplingMethod(SAMPLING_METHOD s)
[2895]996{
997        if (s != mSamplingMethod)
998        {
999                mSamplingMethod = s;
1000                mRegenerateSamples = true;
1001        }
1002}
1003
[2897]1004
1005void DeferredRenderer::SetShadingMethod(SHADING_METHOD s)
1006{
1007        if (s != mShadingMethod)
1008        {
1009                mShadingMethod = s;
1010                mRegenerateSamples = true;
1011        }
1012}
1013
[2858]1014} // namespace
Note: See TracBrowser for help on using the repository browser.