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

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

using another pass for deferred shading: still working quite well
started shadow mapping

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