source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp @ 2888

Revision 2888, 26.1 KB checked in by mattausch, 16 years ago (diff)

corrected views

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