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

Revision 2881, 25.2 KB checked in by mattausch, 16 years ago (diff)

working quite well

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