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

Revision 2874, 20.4 KB checked in by mattausch, 16 years ago (diff)

implemented glob-illum solution

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};
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;
25static CGprogram sCgCombineProgram = NULL;
26
27static CGparameter sColorsTexCombineParam;
28static CGparameter sSsaoTexCombineParam;
29
30static CGparameter sColorsTexDeferredParam;
31static CGparameter sPositionsTexDeferredParam;
32static CGparameter sNormalsTexDeferredParam;
33
34
35///////////////////////////////////////7
36
37
38static CGparameter sColorsTexParam;
39static CGparameter sPositionsTexParam;
40static CGparameter sNormalsTexParam;
41
42static CGparameter sOldModelViewProjMatrixParam;
43static CGparameter sMaxDepthParam;
44static CGparameter sSamplesParam;
45static CGparameter sOldTexParam;
46static CGparameter sNoiseTexParam;
47static CGparameter sNoiseMultiplierParam;
48static CGparameter sExpFactorParam;
49
50
51///////////////////////////////////////
52
53
54static CGparameter sColorsTexGiParam;
55static CGparameter sPositionsTexGiParam;
56static CGparameter sNormalsTexGiParam;
57
58
59static CGparameter sOldModelViewProjMatrixGiParam;
60static CGparameter sMaxDepthGiParam;
61static CGparameter sSamplesGiParam;
62static CGparameter sOldSsaoTexGiParam;
63static CGparameter sOldIllumTexGiParam;
64static CGparameter sNoiseTexGiParam;
65static CGparameter sNoiseMultiplierGiParam;
66static CGparameter sExpFactorGiParam;
67
68
69////////////
70
71static CGparameter sColorsTexAntiAliasingParam;
72static CGparameter sNormalsTexAntiAliasingParam;
73
74
75static GLuint noiseTex;
76
77// ssao random spherical samples
78static Sample2 samples[NUM_SAMPLES];
79
80
81static void PrintGLerror(char *msg)
82{
83        GLenum errCode;
84        const GLubyte *errStr;
85       
86        if ((errCode = glGetError()) != GL_NO_ERROR)
87        {
88                errStr = gluErrorString(errCode);
89                fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
90        }
91}
92
93
94/** Generate poisson disc distributed sample points on the unit disc
95*/
96static void GenerateSamples()
97{
98        static PoissonDiscSampleGenerator poisson(NUM_SAMPLES, 1.0f);
99        poisson.Generate((Sample2 *)samples);
100}
101
102
103SsaoShader::SsaoShader(int w, int h, Camera *cam, float scaleFactor):
104mWidth(w), mHeight(h),
105mCamera(cam),
106mScaleFactor(scaleFactor)
107{
108        ///////////
109        //-- the flip-flop fbos
110
111        mNewFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
112        // the diffuse color buffer
113        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
114        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
115        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
116       
117        mOldFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
118        // the diffuse color buffer
119        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
120        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
121        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
122       
123        mFbo3 = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
124        // the diffuse color buffer
125        mFbo3->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
126               
127
128        // create noise texture for ssao
129        CreateNoiseTex2D();
130}
131
132
133SsaoShader::~SsaoShader()
134{
135        if (sCgSsaoProgram)
136                cgDestroyProgram(sCgSsaoProgram);
137
138        DEL_PTR(mNewFbo);
139        DEL_PTR(mOldFbo);
140
141        glDeleteTextures(1, &noiseTex);
142}
143
144
145void SsaoShader::Init(CGcontext context)
146{       
147        sCgDeferredProgram =
148                cgCreateProgramFromFile(context,
149                                                                CG_SOURCE,
150                                                                "src/shaders/deferred.cg",
151                                                                RenderState::sCgFragmentProfile,
152                                                                "main",
153                                                                NULL);
154
155        if (sCgDeferredProgram != NULL)
156        {
157                cgGLLoadProgram(sCgDeferredProgram);
158
159                // we need size of texture for scaling
160                sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "positions"); 
161                sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "colors"); 
162                sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals");
163        }
164        else
165                cerr << "deferred program failed to load" << endl;
166
167
168        ///////////////
169
170        sCgSsaoProgram =
171                cgCreateProgramFromFile(context,
172                                                                CG_SOURCE,
173                                                                "src/shaders/ssao.cg",
174                                                                RenderState::sCgFragmentProfile,
175                                                                "main",
176                                                                NULL);
177
178        if (sCgSsaoProgram != NULL)
179        {
180                cgGLLoadProgram(sCgSsaoProgram);
181
182                // we need size of texture for scaling
183                sPositionsTexParam = cgGetNamedParameter(sCgSsaoProgram, "positions"); 
184                sColorsTexParam = cgGetNamedParameter(sCgSsaoProgram, "colors"); 
185                sNormalsTexParam = cgGetNamedParameter(sCgSsaoProgram, "normals"); 
186                sNoiseTexParam = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture");
187                sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier");
188               
189                sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj");
190                sMaxDepthParam = cgGetNamedParameter(sCgSsaoProgram, "maxDepth");
191                sExpFactorParam = cgGetNamedParameter(sCgSsaoProgram, "expFactor");
192
193                sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples");
194                sOldTexParam = cgGetNamedParameter(sCgSsaoProgram, "oldTex"); 
195               
196
197                // generate samples for ssao kernel
198                GenerateSamples();
199                cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);
200        }
201        else
202                cerr << "ssao program failed to load" << endl;
203
204        sCgGiProgram =
205                cgCreateProgramFromFile(context,
206                                                                CG_SOURCE,
207                                                                "src/shaders/globillum.cg",
208                                                                RenderState::sCgFragmentProfile,
209                                                                "main",
210                                                                NULL);
211
212        if (sCgGiProgram != NULL)
213        {
214                cgGLLoadProgram(sCgGiProgram);
215
216                // we need size of texture for scaling
217                sPositionsTexGiParam = cgGetNamedParameter(sCgGiProgram, "positions"); 
218                sColorsTexGiParam = cgGetNamedParameter(sCgGiProgram, "colors"); 
219                sNormalsTexGiParam = cgGetNamedParameter(sCgGiProgram, "normals"); 
220                sNoiseTexGiParam = cgGetNamedParameter(sCgGiProgram, "noiseTexture");
221                sNoiseMultiplierGiParam = cgGetNamedParameter(sCgGiProgram, "noiseMultiplier");
222               
223                sOldModelViewProjMatrixGiParam = cgGetNamedParameter(sCgGiProgram, "oldModelViewProj");
224                sMaxDepthGiParam = cgGetNamedParameter(sCgGiProgram, "maxDepth");
225                sExpFactorGiParam = cgGetNamedParameter(sCgGiProgram, "expFactor");
226
227                sSamplesGiParam = cgGetNamedParameter(sCgGiProgram, "samples");
228               
229                sOldSsaoTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldSsaoTex"); 
230                sOldIllumTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldIllumTex"); 
231
232                // generate samples for ssao kernel
233                GenerateSamples();
234                cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples);
235        }
236        else
237                cerr << "globillum program failed to load" << endl;
238
239        sCgAntiAliasingProgram =
240                cgCreateProgramFromFile(context,
241                                                                CG_SOURCE,
242                                                                "src/shaders/antialiasing.cg",
243                                                                RenderState::sCgFragmentProfile,
244                                                                "main",
245                                                                NULL);
246
247        if (sCgAntiAliasingProgram != NULL)
248        {
249                cgGLLoadProgram(sCgAntiAliasingProgram);
250
251                sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors"); 
252                sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals");
253        }
254        else
255                cerr << "antialiasing program failed to load" << endl;
256
257
258        PrintGLerror("init");
259}
260
261
262void SsaoShader::Render(FrameBufferObject *fbo,
263                                                const Matrix4x4 &oldProjViewMatrix,
264                                                float expFactor)
265{
266       
267        // switch roles of old and new fbo
268        // the algorihm uses two input fbos, where the one
269        // contais the color buffer from the last frame,
270        // the other one will be written
271        swap(mNewFbo, mOldFbo);
272
273
274        glPushAttrib(GL_VIEWPORT_BIT);
275        glViewport(0, 0, mWidth, mHeight);
276
277        FrameBufferObject::Release();
278
279        glDrawBuffers(1, mymrt);
280
281        cgGLEnableProfile(RenderState::sCgFragmentProfile);
282
283        glDisable(GL_ALPHA_TEST);
284        glDisable(GL_TEXTURE_2D);
285        glDisable(GL_LIGHTING);
286
287        glMatrixMode(GL_PROJECTION);
288        glPushMatrix();
289        glLoadIdentity();
290
291        glMatrixMode(GL_MODELVIEW);
292        glPushMatrix();
293        glLoadIdentity();
294
295        const float offs = 0.5f;
296        glOrtho(-offs, offs, -offs, offs, 0, 1);
297
298        FirstPass(fbo);
299       
300        //ComputeSsao(fbo, expFactor, oldProjViewMatrix);
301        ComputeGlobIllum(fbo, expFactor, oldProjViewMatrix);
302
303        //Combine(fbo);
304        AntiAliasing(fbo);
305
306        glEnable(GL_LIGHTING);
307        glDisable(GL_TEXTURE_2D);
308
309        glMatrixMode(GL_PROJECTION);
310        glPopMatrix();
311
312        glMatrixMode(GL_MODELVIEW);
313        glPopMatrix();
314
315        glPopAttrib();
316
317        cgGLDisableProfile(RenderState::sCgFragmentProfile);
318}
319
320
321void SsaoShader::ComputeSsao(FrameBufferObject *fbo,
322                                                         float expFactor,
323                                                         const Matrix4x4 &oldProjViewMatrix
324                                                         )
325{
326        cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x);
327
328        GLuint colorsTex = mFbo3->GetColorBuffer(0)->GetTexture();
329        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
330        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
331
332        if (1)
333        {
334                // generate mip map levels for position texture
335                glBindTexture(GL_TEXTURE_2D, positionsTex);
336                glGenerateMipmapEXT(GL_TEXTURE_2D);
337        }
338
339
340        // read the second buffer, write to the first buffer
341        mNewFbo->Bind();
342        glDrawBuffers(2, mymrt);
343
344        GLuint oldTex = mOldFbo->GetColorBuffer(0)->GetTexture();
345
346        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
347
348        cgGLEnableProfile(RenderState::sCgFragmentProfile);
349        cgGLBindProgram(sCgSsaoProgram);
350
351        cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
352        cgGLEnableTextureParameter(sPositionsTexParam);
353
354        cgGLSetTextureParameter(sColorsTexParam, colorsTex);
355        cgGLEnableTextureParameter(sColorsTexParam);
356
357        cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
358        cgGLEnableTextureParameter(sNormalsTexParam);
359
360        cgGLSetTextureParameter(sNoiseTexParam, noiseTex);
361        cgGLEnableTextureParameter(sNoiseTexParam);
362
363        cgGLSetTextureParameter(sOldTexParam, oldTex);
364        cgGLEnableTextureParameter(sOldTexParam);
365
366        cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));
367       
368        cgGLSetParameter1f(sMaxDepthParam, mScaleFactor);
369        cgGLSetParameter1f(sExpFactorParam, expFactor);
370
371
372        // q: should we generate new samples or only rotate the old ones?
373        // in the first case, the sample patterns look nicer, but the kernel
374        // needs longer to converge
375        GenerateSamples();
376        cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);
377
378        Vector3 tl, tr, bl, br;
379        ComputeViewVectors(tl, tr, bl, br);
380
381        glColor3f(1.0f, 1.0f, 1.0f);
382
383        glBegin(GL_QUADS);
384
385        // note: slightly larger texture hides ambient occlusion error on border but costs resolution
386        //const float new_offs = 0.55f;
387        const float new_offs = 0.5f;
388       
389        glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-new_offs, -new_offs, -0.5f);
390        glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( new_offs, -new_offs, -0.5f);
391        glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( new_offs,  new_offs, -0.5f);
392        glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-new_offs,  new_offs, -0.5f);
393
394        glEnd();
395
396        cgGLDisableTextureParameter(sColorsTexParam);
397        cgGLDisableTextureParameter(sPositionsTexParam);
398        cgGLDisableTextureParameter(sNormalsTexParam);
399        cgGLDisableTextureParameter(sNoiseTexParam);
400        cgGLDisableTextureParameter(sOldTexParam);
401
402        FrameBufferObject::Release();
403
404        PrintGLerror("ssao first pass");
405}
406
407
408void SsaoShader::ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br)
409{
410        Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr;
411
412        mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
413
414#if 1 // matT: debug this!!
415       
416        bl = -Normalize(nbl - fbl);
417        br = -Normalize(nbr - fbr);
418        tl = -Normalize(ntl - ftl);
419        tr = -Normalize(ntr - ftr);
420
421#else // just take camera direction
422       
423        bl = -Normalize(mCamera->GetDirection());
424        br = -Normalize(mCamera->GetDirection());
425        tl = -Normalize(mCamera->GetDirection());
426        tr = -Normalize(mCamera->GetDirection());
427
428#endif
429
430        // normalize to 0 .. 1
431        bl = bl * 0.5f + 0.5f;
432        br = br * 0.5f + 0.5f;
433        tl = tl * 0.5f + 0.5f;
434        tr = tr * 0.5f + 0.5f;
435}
436
437
438void SsaoShader::CreateNoiseTex2D()
439{
440        //GLubyte *randomNormals = new GLubyte[mWidth * mHeight * 3];
441        float *randomNormals = new float[mWidth * mHeight * 3];
442
443        for (int i = 0; i < mWidth * mHeight * 3; i += 3)
444        {
445                // create random samples on a circle
446                const float rx = RandomValue(0, 1);
447                const float theta = 2.0f * acos(sqrt(1.0f - rx));
448
449                //randomNormals[i + 0] = (GLubyte)((cos(theta) * 0.5f + 0.5f) * 255.0f);
450                //randomNormals[i + 1] = (GLubyte)((sin(theta) * 0.5f + 0.5f) * 255.0f);
451                randomNormals[i + 0] = cos(theta);
452                randomNormals[i + 1] = sin(theta);
453                randomNormals[i + 2] = 0;
454        }
455
456        glEnable(GL_TEXTURE_2D);
457        glGenTextures(1, &noiseTex);
458        glBindTexture(GL_TEXTURE_2D, noiseTex);
459               
460        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
461        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
462        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
463        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
464
465        //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, mWidth, mHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);
466        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, mWidth, mHeight, 0, GL_RGB, GL_FLOAT, randomNormals);
467
468        glBindTexture(GL_TEXTURE_2D, 0);
469        glDisable(GL_TEXTURE_2D);
470
471        delete [] randomNormals;
472
473        cout << "created noise texture" << endl;
474
475        PrintGLerror("noisetexture");
476}
477
478
479
480static void SetVertex(float x, float y, float x_offs, float y_offs)
481{
482        glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
483        glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
484        glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
485        glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
486        glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
487
488        glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
489        glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
490
491        glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
492}
493
494
495void SsaoShader::AntiAliasing(FrameBufferObject *fbo)
496{
497        //GLuint colorsTex = mFbo4->GetColorBuffer(0)->GetTexture();
498        GLuint colorsTex = mNewFbo->GetColorBuffer(1)->GetTexture();
499        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
500       
501        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
502
503        cgGLEnableProfile(RenderState::sCgFragmentProfile);
504        cgGLBindProgram(sCgAntiAliasingProgram);
505       
506        cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
507        cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
508
509        cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
510        cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
511
512        glColor3f(1.0f, 1.0f, 1.0f);
513
514        float offs2 = 0.5f;
515
516        glBegin(GL_QUADS);
517
518        // the neighbouring texels
519        float x_offs = 1.0f / mWidth;
520        float y_offs = 1.0f / mHeight;
521
522        SetVertex(0, 0, x_offs, y_offs);
523        SetVertex(1, 0, x_offs, y_offs);
524        SetVertex(1, 1, x_offs, y_offs);
525        SetVertex(0, 1, x_offs, y_offs);
526
527        glEnd();
528
529        cgGLDisableTextureParameter(sColorsTexAntiAliasingParam);
530        cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam);
531
532        PrintGLerror("antialiasing");
533}
534
535
536void SsaoShader::FirstPass(FrameBufferObject *fbo)
537{
538        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
539        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
540        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
541
542        mFbo3->Bind();
543        //mNewFbo->Bind();
544
545        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
546       
547        glDrawBuffers(1, mymrt);
548
549        cgGLEnableProfile(RenderState::sCgFragmentProfile);
550
551        cgGLBindProgram(sCgDeferredProgram);
552
553        cgGLSetTextureParameter(sColorsTexDeferredParam, colorsTex);
554        cgGLEnableTextureParameter(sColorsTexDeferredParam);
555
556        cgGLSetTextureParameter(sPositionsTexDeferredParam, positionsTex);
557        cgGLEnableTextureParameter(sPositionsTexDeferredParam);
558
559        cgGLSetTextureParameter(sNormalsTexDeferredParam, normalsTex);
560        cgGLEnableTextureParameter(sNormalsTexDeferredParam);
561       
562        glColor3f(1.0f, 1.0f, 1.0f);
563
564        const float offs = 0.5f;
565
566        glBegin(GL_QUADS);
567
568        glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
569        glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
570        glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f);
571        glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f);
572
573        glEnd();
574
575        cgGLDisableTextureParameter(sColorsTexDeferredParam);
576        cgGLDisableTextureParameter(sPositionsTexDeferredParam);
577        cgGLDisableTextureParameter(sNormalsTexDeferredParam);
578
579        cgGLDisableProfile(RenderState::sCgFragmentProfile);
580
581        FrameBufferObject::Release();
582
583        PrintGLerror("deferred shading");
584}
585
586
587void SsaoShader::ComputeGlobIllum(FrameBufferObject *fbo,
588                                                                  float expFactor,
589                                                                  const Matrix4x4 &oldProjViewMatrix
590                                                                  )
591{
592        cgGLSetMatrixParameterfc(sOldModelViewProjMatrixGiParam, (const float *)oldProjViewMatrix.x);
593
594        GLuint colorsTex = mFbo3->GetColorBuffer(0)->GetTexture();
595        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
596        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
597
598        if (1)
599        {
600                // generate mip map levels for position texture
601                glBindTexture(GL_TEXTURE_2D, positionsTex);
602                glGenerateMipmapEXT(GL_TEXTURE_2D);
603
604                // generate mip map levels for position texture
605                glBindTexture(GL_TEXTURE_2D, colorsTex);
606                glGenerateMipmapEXT(GL_TEXTURE_2D);
607        }
608
609
610        // read the second buffer, write to the first buffer
611        mNewFbo->Bind();
612        glDrawBuffers(3, mymrt);
613
614        GLuint oldSsaoTex = mOldFbo->GetColorBuffer(0)->GetTexture();
615        GLuint oldIllumTex = mOldFbo->GetColorBuffer(2)->GetTexture();
616
617        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
618
619        cgGLEnableProfile(RenderState::sCgFragmentProfile);
620        cgGLBindProgram(sCgGiProgram);
621
622        cgGLSetTextureParameter(sPositionsTexGiParam, positionsTex);
623        cgGLEnableTextureParameter(sPositionsTexGiParam);
624
625        cgGLSetTextureParameter(sColorsTexGiParam, colorsTex);
626        cgGLEnableTextureParameter(sColorsTexGiParam);
627
628        cgGLSetTextureParameter(sNormalsTexGiParam, normalsTex);
629        cgGLEnableTextureParameter(sNormalsTexGiParam);
630
631        cgGLSetTextureParameter(sNoiseTexGiParam, noiseTex);
632        cgGLEnableTextureParameter(sNoiseTexGiParam);
633
634        cgGLSetTextureParameter(sOldSsaoTexGiParam, oldSsaoTex);
635        cgGLEnableTextureParameter(sOldSsaoTexGiParam);
636
637        cgGLSetTextureParameter(sOldIllumTexGiParam, oldIllumTex);
638        cgGLEnableTextureParameter(sOldIllumTexGiParam);
639
640        cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f));
641       
642        cgGLSetParameter1f(sMaxDepthGiParam, mScaleFactor);
643        cgGLSetParameter1f(sExpFactorGiParam, expFactor);
644
645
646        // q: should we generate new samples or only rotate the old ones?
647        // in the first case, the sample patterns look nicer, but the kernel
648        // needs longer to converge
649        GenerateSamples();
650        cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples);
651
652        Vector3 tl, tr, bl, br;
653        ComputeViewVectors(tl, tr, bl, br);
654
655        glColor3f(1.0f, 1.0f, 1.0f);
656
657        glBegin(GL_QUADS);
658
659        // note: slightly larger texture hides ambient occlusion error on border but costs resolution
660        //const float new_offs = 0.55f;
661        const float new_offs = 0.5f;
662       
663        glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-new_offs, -new_offs, -0.5f);
664        glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( new_offs, -new_offs, -0.5f);
665        glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( new_offs,  new_offs, -0.5f);
666        glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-new_offs,  new_offs, -0.5f);
667
668        glEnd();
669
670        cgGLDisableTextureParameter(sColorsTexGiParam);
671        cgGLDisableTextureParameter(sPositionsTexGiParam);
672        cgGLDisableTextureParameter(sNormalsTexGiParam);
673        cgGLDisableTextureParameter(sNoiseTexGiParam);
674        cgGLDisableTextureParameter(sOldSsaoTexGiParam);
675        cgGLDisableTextureParameter(sOldIllumTexGiParam);
676
677        FrameBufferObject::Release();
678
679        PrintGLerror("globillum first pass");
680}
681
682
683} // namespace
Note: See TracBrowser for help on using the repository browser.