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

Revision 2880, 25.7 KB checked in by mattausch, 16 years ago (diff)

downsampling not working yet

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        // hack: should be able to recalc noise texture
115        //if (noiseTex > 0) return;
116
117        //GLubyte *randomNormals = new GLubyte[mWidth * mHeight * 3];
118        float *randomNormals = new float[w * h * 3];
119
120        for (int i = 0; i < w * h * 3; i += 3)
121        {
122                // create random samples on a circle
123                const float rx = RandomValue(0, 1);
124                const float theta = 2.0f * acos(sqrt(1.0f - rx));
125
126                //randomNormals[i + 0] = (GLubyte)((cos(theta) * 0.5f + 0.5f) * 255.0f);
127                //randomNormals[i + 1] = (GLubyte)((sin(theta) * 0.5f + 0.5f) * 255.0f);
128                randomNormals[i + 0] = cos(theta);
129                randomNormals[i + 1] = sin(theta);
130                randomNormals[i + 2] = 0;
131        }
132
133        glEnable(GL_TEXTURE_2D);
134        glGenTextures(1, &noiseTex);
135        glBindTexture(GL_TEXTURE_2D, noiseTex);
136               
137        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
138        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
139        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
140        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
141
142        //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, mWidth, mHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);
143        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, w, h, 0, GL_RGB, GL_FLOAT, randomNormals);
144
145        glBindTexture(GL_TEXTURE_2D, 0);
146        glDisable(GL_TEXTURE_2D);
147
148        delete [] randomNormals;
149
150        cout << "created noise texture" << endl;
151
152        PrintGLerror("noisetexture");
153}
154
155
156SsaoShader::SsaoShader(int w, int h, Camera *cam, float scaleFactor):
157mWidth(w), mHeight(h),
158mCamera(cam),
159mScaleFactor(scaleFactor),
160mUseTemporalCoherence(true),
161mUseGlobIllum(false)
162{
163        // create noise texture for ssao
164        CreateNoiseTex2D(w, h);
165
166        ///////////
167        //-- the flip-flop fbos
168
169        w = 256; h = 256;
170        mNewFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
171       
172        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
173        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
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        glPushAttrib(GL_VIEWPORT_BIT);
460        glViewport(0, 0, 256, 256);
461
462        glMatrixMode(GL_PROJECTION);
463        glPushMatrix();
464        glLoadIdentity();
465
466        glMatrixMode(GL_MODELVIEW);
467        glPushMatrix();
468        glLoadIdentity();
469
470        const float offs = 0.5f;
471        glOrtho(-offs, offs, -offs, offs, 0, 1);
472
473
474        GLuint oldTex = mOldFbo->GetColorBuffer(0)->GetTexture();
475
476        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
477
478        cgGLEnableProfile(RenderState::sCgFragmentProfile);
479        cgGLBindProgram(sCgSsaoProgram);
480
481        cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
482        cgGLEnableTextureParameter(sPositionsTexParam);
483
484        cgGLSetTextureParameter(sColorsTexParam, colorsTex);
485        cgGLEnableTextureParameter(sColorsTexParam);
486
487        cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
488        cgGLEnableTextureParameter(sNormalsTexParam);
489
490        cgGLSetTextureParameter(sNoiseTexParam, noiseTex);
491        cgGLEnableTextureParameter(sNoiseTexParam);
492
493        cgGLSetTextureParameter(sOldTexParam, oldTex);
494        cgGLEnableTextureParameter(sOldTexParam);
495       
496        cgGLSetParameter1f(sMaxDepthParam, mScaleFactor);
497       
498        if (mUseTemporalCoherence)
499        {
500                cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));
501                cgGLSetParameter1f(sExpFactorParam, expFactor);
502
503
504                // q: should we generate new samples or only rotate the old ones?
505                // in the first case, the sample patterns look nicer, but the kernel
506                // needs longer to converge
507                GenerateSamples();
508                cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);
509        }
510        else
511        {
512                        cgGLSetParameter1f(sExpFactorParam, 1.0f);
513        }
514
515        Vector3 tl, tr, bl, br;
516        ComputeViewVectors(tl, tr, bl, br);
517
518        glColor3f(1.0f, 1.0f, 1.0f);
519
520        glBegin(GL_QUADS);
521
522        // note: slightly larger texture hides ambient occlusion error on border but costs resolution
523        //const float new_offs = 0.55f;
524        const float new_offs = 0.5f;
525       
526        glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-new_offs, -new_offs, -0.5f);
527        glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( new_offs, -new_offs, -0.5f);
528        glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( new_offs,  new_offs, -0.5f);
529        glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-new_offs,  new_offs, -0.5f);
530
531        glEnd();
532
533        cgGLDisableTextureParameter(sColorsTexParam);
534        cgGLDisableTextureParameter(sPositionsTexParam);
535        cgGLDisableTextureParameter(sNormalsTexParam);
536        cgGLDisableTextureParameter(sNoiseTexParam);
537        cgGLDisableTextureParameter(sOldTexParam);
538
539        glMatrixMode(GL_PROJECTION);
540        glPopMatrix();
541
542        glMatrixMode(GL_MODELVIEW);
543        glPopMatrix();
544
545        glPopAttrib();
546
547        FrameBufferObject::Release();
548
549        PrintGLerror("ssao first pass");
550}
551
552
553void SsaoShader::ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br)
554{
555        Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr;
556
557        mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
558
559#if 1 // matT: debug this!!
560       
561        bl = -Normalize(nbl - fbl);
562        br = -Normalize(nbr - fbr);
563        tl = -Normalize(ntl - ftl);
564        tr = -Normalize(ntr - ftr);
565
566#else // just take camera direction
567       
568        bl = -Normalize(mCamera->GetDirection());
569        br = -Normalize(mCamera->GetDirection());
570        tl = -Normalize(mCamera->GetDirection());
571        tr = -Normalize(mCamera->GetDirection());
572
573#endif
574
575        // normalize to 0 .. 1
576        bl = bl * 0.5f + 0.5f;
577        br = br * 0.5f + 0.5f;
578        tl = tl * 0.5f + 0.5f;
579        tr = tr * 0.5f + 0.5f;
580}
581
582
583
584static void SetVertex(float x, float y, float x_offs, float y_offs)
585{
586        glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
587        glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
588        glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
589        glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
590        glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
591
592        glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
593        glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
594
595        glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
596}
597
598
599void SsaoShader::AntiAliasing(FrameBufferObject *fbo)
600{
601        //GLuint colorsTex = mNewFbo->GetColorBuffer(1)->GetTexture();
602        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
603        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
604       
605        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
606
607        cgGLEnableProfile(RenderState::sCgFragmentProfile);
608        cgGLBindProgram(sCgAntiAliasingProgram);
609       
610        cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
611        cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
612
613        cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
614        cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
615
616        glColor3f(1.0f, 1.0f, 1.0f);
617
618        float offs2 = 0.5f;
619
620        glBegin(GL_QUADS);
621
622        // the neighbouring texels
623        float x_offs = 1.0f / mWidth;
624        float y_offs = 1.0f / mHeight;
625
626        SetVertex(0, 0, x_offs, y_offs);
627        SetVertex(1, 0, x_offs, y_offs);
628        SetVertex(1, 1, x_offs, y_offs);
629        SetVertex(0, 1, x_offs, y_offs);
630
631        glEnd();
632
633        cgGLDisableTextureParameter(sColorsTexAntiAliasingParam);
634        cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam);
635
636        PrintGLerror("antialiasing");
637}
638
639
640void SsaoShader::FirstPass(FrameBufferObject *fbo)
641{
642        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
643        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
644        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
645
646        fbo->Bind();
647
648        glDrawBuffers(1, mymrt + 3);
649
650        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
651       
652        cgGLEnableProfile(RenderState::sCgFragmentProfile);
653
654        cgGLBindProgram(sCgDeferredProgram);
655
656        cgGLSetTextureParameter(sColorsTexDeferredParam, colorsTex);
657        cgGLEnableTextureParameter(sColorsTexDeferredParam);
658
659        cgGLSetTextureParameter(sPositionsTexDeferredParam, positionsTex);
660        cgGLEnableTextureParameter(sPositionsTexDeferredParam);
661
662        cgGLSetTextureParameter(sNormalsTexDeferredParam, normalsTex);
663        cgGLEnableTextureParameter(sNormalsTexDeferredParam);
664       
665        glColor3f(1.0f, 1.0f, 1.0f);
666
667        const float offs = 0.5f;
668
669        glBegin(GL_QUADS);
670
671        glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
672        glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
673        glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f);
674        glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f);
675
676        glEnd();
677
678        cgGLDisableTextureParameter(sColorsTexDeferredParam);
679        cgGLDisableTextureParameter(sPositionsTexDeferredParam);
680        cgGLDisableTextureParameter(sNormalsTexDeferredParam);
681
682        cgGLDisableProfile(RenderState::sCgFragmentProfile);
683
684        FrameBufferObject::Release();
685
686        PrintGLerror("deferred shading");
687}
688
689
690void SsaoShader::ComputeGlobIllum(FrameBufferObject *fbo,
691                                                                  float expFactor,
692                                                                  const Matrix4x4 &oldProjViewMatrix
693                                                                  )
694{
695        cgGLSetMatrixParameterfc(sOldModelViewProjMatrixGiParam, (const float *)oldProjViewMatrix.x);
696
697        //GLuint colorsTex = mFbo->GetColorBuffer(0)->GetTexture();
698        GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
699        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
700        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
701
702        if (1)
703        {
704                // generate mip map levels for position texture
705                glBindTexture(GL_TEXTURE_2D, positionsTex);
706                glGenerateMipmapEXT(GL_TEXTURE_2D);
707
708                // generate mip map levels for position texture
709                glBindTexture(GL_TEXTURE_2D, colorsTex);
710                glGenerateMipmapEXT(GL_TEXTURE_2D);
711        }
712
713
714        // read the second buffer, write to the first buffer
715        mNewFbo->Bind();
716        glDrawBuffers(2, mymrt);
717
718        GLuint oldSsaoTex = mOldFbo->GetColorBuffer(0)->GetTexture();
719        GLuint oldIllumTex = mOldFbo->GetColorBuffer(1)->GetTexture();
720
721        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
722
723        cgGLEnableProfile(RenderState::sCgFragmentProfile);
724        cgGLBindProgram(sCgGiProgram);
725
726        cgGLSetTextureParameter(sPositionsTexGiParam, positionsTex);
727        cgGLEnableTextureParameter(sPositionsTexGiParam);
728
729        cgGLSetTextureParameter(sColorsTexGiParam, colorsTex);
730        cgGLEnableTextureParameter(sColorsTexGiParam);
731
732        cgGLSetTextureParameter(sNormalsTexGiParam, normalsTex);
733        cgGLEnableTextureParameter(sNormalsTexGiParam);
734
735        cgGLSetTextureParameter(sNoiseTexGiParam, noiseTex);
736        cgGLEnableTextureParameter(sNoiseTexGiParam);
737
738        cgGLSetTextureParameter(sOldSsaoTexGiParam, oldSsaoTex);
739        cgGLEnableTextureParameter(sOldSsaoTexGiParam);
740
741        cgGLSetTextureParameter(sOldIllumTexGiParam, oldIllumTex);
742        cgGLEnableTextureParameter(sOldIllumTexGiParam);
743
744        cgGLSetParameter1f(sMaxDepthGiParam, mScaleFactor);
745
746
747        if (mUseTemporalCoherence)
748        {
749                cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f));
750                cgGLSetParameter1f(sExpFactorGiParam, expFactor);
751
752
753                // q: should we generate new samples or only rotate the old ones?
754                // in the first case, the sample patterns look nicer, but the kernel
755                // needs longer to converge
756                GenerateSamples();
757                cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples);
758        }
759        else
760        {
761                        cgGLSetParameter1f(sExpFactorGiParam, 1.0f);
762        }
763
764        Vector3 tl, tr, bl, br;
765        ComputeViewVectors(tl, tr, bl, br);
766
767        glColor3f(1.0f, 1.0f, 1.0f);
768
769        glBegin(GL_QUADS);
770
771        // note: slightly larger texture hides ambient occlusion error on border but costs resolution
772        //const float new_offs = 0.55f;
773        const float new_offs = 0.5f;
774       
775        glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-new_offs, -new_offs, -0.5f);
776        glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( new_offs, -new_offs, -0.5f);
777        glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( new_offs,  new_offs, -0.5f);
778        glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-new_offs,  new_offs, -0.5f);
779
780        glEnd();
781
782        cgGLDisableTextureParameter(sColorsTexGiParam);
783        cgGLDisableTextureParameter(sPositionsTexGiParam);
784        cgGLDisableTextureParameter(sNormalsTexGiParam);
785        cgGLDisableTextureParameter(sNoiseTexGiParam);
786        cgGLDisableTextureParameter(sOldSsaoTexGiParam);
787        cgGLDisableTextureParameter(sOldIllumTexGiParam);
788
789        FrameBufferObject::Release();
790
791        PrintGLerror("globillum first pass");
792}
793
794
795void SsaoShader::CombineIllum(FrameBufferObject *fbo)
796{
797        GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
798        GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
799        GLuint illumTex = mNewFbo->GetColorBuffer(1)->GetTexture();
800
801        //
802        fbo->Bind();
803
804        // write into old color texture (not needed anymore)
805        glDrawBuffers(1, mymrt);
806
807        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
808       
809        cgGLEnableProfile(RenderState::sCgFragmentProfile);
810
811        cgGLBindProgram(sCgCombinedIllumProgram);
812
813        cgGLSetTextureParameter(sColorsTexCombinedIllumParam, colorsTex);
814        cgGLEnableTextureParameter(sColorsTexCombinedIllumParam);
815
816        cgGLSetTextureParameter(sSsaoTexCombinedIllumParam, ssaoTex);
817        cgGLEnableTextureParameter(sSsaoTexCombinedIllumParam);
818
819        cgGLSetTextureParameter(sIllumTexCombinedIllumParam, illumTex);
820        cgGLEnableTextureParameter(sIllumTexCombinedIllumParam);
821       
822        glColor3f(1.0f, 1.0f, 1.0f);
823
824        const float offs = 0.5f;
825
826        glBegin(GL_QUADS);
827
828        glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
829        glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
830        glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f);
831        glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f);
832
833        glEnd();
834
835        cgGLDisableTextureParameter(sColorsTexCombinedIllumParam);
836        cgGLDisableTextureParameter(sSsaoTexCombinedIllumParam);
837        cgGLDisableTextureParameter(sIllumTexCombinedIllumParam);
838
839        cgGLDisableProfile(RenderState::sCgFragmentProfile);
840
841        FrameBufferObject::Release();
842
843        PrintGLerror("combine");
844}
845
846
847void SsaoShader::CombineSsao(FrameBufferObject *fbo)
848{
849        GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
850        GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
851
852        //
853        fbo->Bind();
854
855        // write into old color texture (not needed anymore)
856        glDrawBuffers(1, mymrt);
857
858        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
859       
860        cgGLEnableProfile(RenderState::sCgFragmentProfile);
861
862        cgGLBindProgram(sCgCombinedSsaoProgram);
863
864        cgGLSetTextureParameter(sColorsTexCombinedSsaoParam, colorsTex);
865        cgGLEnableTextureParameter(sColorsTexCombinedSsaoParam);
866
867        cgGLSetTextureParameter(sSsaoTexCombinedSsaoParam, ssaoTex);
868        cgGLEnableTextureParameter(sSsaoTexCombinedSsaoParam);
869
870       
871        glColor3f(1.0f, 1.0f, 1.0f);
872
873        const float offs = 0.5f;
874
875        glBegin(GL_QUADS);
876
877        glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
878        glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
879        glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f);
880        glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f);
881
882        glEnd();
883
884        cgGLDisableTextureParameter(sColorsTexCombinedSsaoParam);
885        cgGLDisableTextureParameter(sSsaoTexCombinedSsaoParam);
886
887        cgGLDisableProfile(RenderState::sCgFragmentProfile);
888
889        FrameBufferObject::Release();
890
891        PrintGLerror("combine ssao");
892}
893
894
895
896} // namespace
Note: See TracBrowser for help on using the repository browser.