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

Revision 2884, 25.1 KB checked in by mattausch, 16 years ago (diff)

found bug with lod levels
made env file for shaders

Line 
1#include "SsaoShader.h"
2#include "FrameBufferObject.h"
3#include "RenderState.h"
4#include "SampleGenerator.h"
5#include "Vector3.h"
6#include "Camera.h"
7#include "shaderenv.h"
8
9
10using namespace std;
11
12static GLenum mymrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT};
13
14namespace CHCDemoEngine
15{
16
17static CGprogram sCgSsaoProgram = NULL;
18static CGprogram sCgGiProgram = NULL;
19
20static CGprogram sCgDeferredProgram = NULL;
21static CGprogram sCgAntiAliasingProgram = NULL;
22
23static CGparameter sColorsTexCombineParam;
24static CGparameter sSsaoTexCombineParam;
25
26static CGparameter sColorsTexDeferredParam;
27static CGparameter sPositionsTexDeferredParam;
28static CGparameter sNormalsTexDeferredParam;
29
30
31static CGprogram sCgCombinedSsaoProgram = NULL;
32static CGprogram sCgCombinedIllumProgram = NULL;
33
34///////////////////////////////////////7
35
36
37static CGparameter sColorsTexParam;
38static CGparameter sPositionsTexParam;
39static CGparameter sNormalsTexParam;
40
41static CGparameter sOldModelViewProjMatrixParam;
42static CGparameter sMaxDepthParam;
43static CGparameter sSamplesParam;
44static CGparameter sOldTexParam;
45static CGparameter sNoiseTexParam;
46static CGparameter sNoiseMultiplierParam;
47static CGparameter sExpFactorParam;
48
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
69static CGparameter sColorsTexCombinedIllumParam;
70static CGparameter sSsaoTexCombinedIllumParam;
71static CGparameter sIllumTexCombinedIllumParam;
72
73static CGparameter sColorsTexCombinedSsaoParam;
74static CGparameter sSsaoTexCombinedSsaoParam;
75
76
77////////////
78
79static CGparameter sColorsTexAntiAliasingParam;
80static CGparameter sNormalsTexAntiAliasingParam;
81
82static GLuint noiseTex = 0;
83
84// ssao random spherical samples
85static Sample2 samples[NUM_SAMPLES];
86
87
88static void PrintGLerror(char *msg)
89{
90        GLenum errCode;
91        const GLubyte *errStr;
92       
93        if ((errCode = glGetError()) != GL_NO_ERROR)
94        {
95                errStr = gluErrorString(errCode);
96                fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
97        }
98}
99
100
101/** Generate poisson disc distributed sample points on the unit disc
102*/
103static void GenerateSamples()
104{
105        static PoissonDiscSampleGenerator poisson(NUM_SAMPLES, 1.0f);
106        poisson.Generate((Sample2 *)samples);
107}
108
109
110static void CreateNoiseTex2D(int w, int h)
111{
112        //GLubyte *randomNormals = new GLubyte[mWidth * mHeight * 3];
113        float *randomNormals = new float[w * h * 3];
114
115        for (int i = 0; i < w * h * 3; i += 3)
116        {
117                // create random samples on a circle
118                const float rx = RandomValue(0, 1);
119                const float theta = 2.0f * acos(sqrt(1.0f - rx));
120
121                //randomNormals[i + 0] = (GLubyte)((cos(theta) * 0.5f + 0.5f) * 255.0f);
122                //randomNormals[i + 1] = (GLubyte)((sin(theta) * 0.5f + 0.5f) * 255.0f);
123                randomNormals[i + 0] = cos(theta);
124                randomNormals[i + 1] = sin(theta);
125                randomNormals[i + 2] = 0;
126        }
127
128        glEnable(GL_TEXTURE_2D);
129        glGenTextures(1, &noiseTex);
130        glBindTexture(GL_TEXTURE_2D, noiseTex);
131               
132        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
133        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
134        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
135        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
136
137        //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);
138        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, w, h, 0, GL_RGB, GL_FLOAT, randomNormals);
139
140        glBindTexture(GL_TEXTURE_2D, 0);
141        glDisable(GL_TEXTURE_2D);
142
143        delete [] randomNormals;
144
145        cout << "created noise texture" << endl;
146
147        PrintGLerror("noisetexture");
148}
149
150
151SsaoShader::SsaoShader(int w, int h, Camera *cam, float scaleFactor):
152mWidth(w), mHeight(h),
153mCamera(cam),
154mScaleFactor(scaleFactor),
155mUseTemporalCoherence(true),
156mUseGlobIllum(false)
157{
158        // create noise texture for ssao
159        CreateNoiseTex2D(w, h);
160
161        ///////////
162        //-- the flip-flop fbos
163
164        mNewFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
165       
166        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
167        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
168       
169
170        ///////////////////////
171
172        mOldFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
173       
174        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
175        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
176       
177        //mFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
178        //mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);*/
179}
180
181
182SsaoShader::~SsaoShader()
183{
184        if (sCgSsaoProgram)     cgDestroyProgram(sCgSsaoProgram);
185        if (sCgDeferredProgram) cgDestroyProgram(sCgDeferredProgram);
186        if (sCgSsaoProgram)     cgDestroyProgram(sCgSsaoProgram);
187        if (sCgGiProgram) cgDestroyProgram(sCgGiProgram);
188        if (sCgAntiAliasingProgram) cgDestroyProgram(sCgAntiAliasingProgram);
189
190        DEL_PTR(mNewFbo);
191        DEL_PTR(mOldFbo);
192        //DEL_PTR(mFbo);
193
194        glDeleteTextures(1, &noiseTex);
195}
196
197
198void SsaoShader::SetUseGlobIllum(bool useGlobIllum)
199{
200        mUseGlobIllum = useGlobIllum;
201}
202
203
204void SsaoShader::SetUseTemporalCoherence(bool temporal)
205{
206        mUseTemporalCoherence = temporal;
207}
208
209
210void SsaoShader::Init(CGcontext context)
211{       
212        sCgDeferredProgram =
213                cgCreateProgramFromFile(context,
214                                                                CG_SOURCE,
215                                                                "src/shaders/deferred.cg",
216                                                                RenderState::sCgFragmentProfile,
217                                                                "main",
218                                                                NULL);
219
220        if (sCgDeferredProgram != NULL)
221        {
222                cgGLLoadProgram(sCgDeferredProgram);
223
224                // we need size of texture for scaling
225                sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "positions"); 
226                sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "colors"); 
227                sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals");
228        }
229        else
230                cerr << "deferred program failed to load" << endl;
231
232
233        ///////////////
234
235        sCgSsaoProgram =
236                cgCreateProgramFromFile(context,
237                                                                CG_SOURCE,
238                                                                "src/shaders/ssao.cg",
239                                                                RenderState::sCgFragmentProfile,
240                                                                "main",
241                                                                NULL);
242
243        if (sCgSsaoProgram != NULL)
244        {
245                cgGLLoadProgram(sCgSsaoProgram);
246
247                // we need size of texture for scaling
248                sPositionsTexParam = cgGetNamedParameter(sCgSsaoProgram, "positions"); 
249                sColorsTexParam = cgGetNamedParameter(sCgSsaoProgram, "colors"); 
250                sNormalsTexParam = cgGetNamedParameter(sCgSsaoProgram, "normals"); 
251                sNoiseTexParam = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture");
252                sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier");
253               
254                sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj");
255                sMaxDepthParam = cgGetNamedParameter(sCgSsaoProgram, "maxDepth");
256                sExpFactorParam = cgGetNamedParameter(sCgSsaoProgram, "expFactor");
257
258                sOldTexParam = cgGetNamedParameter(sCgSsaoProgram, "oldTex"); 
259               
260                cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));
261
262                // generate samples for ssao kernel
263                GenerateSamples();
264
265                sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples");
266                //cgSetArraySize(sSamplesParam, NUM_SAMPLES);
267                //cgCompileProgram(sCgSsaoProgram);
268               
269                cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);
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
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 = 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 = fbo->GetColorBuffer(0)->GetTexture();
580        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
581       
582        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
583
584        cgGLEnableProfile(RenderState::sCgFragmentProfile);
585        cgGLBindProgram(sCgAntiAliasingProgram);
586       
587        cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
588        cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
589
590        cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
591        cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
592
593        glColor3f(1.0f, 1.0f, 1.0f);
594
595        float offs2 = 0.5f;
596
597        glBegin(GL_QUADS);
598
599        // the neighbouring texels
600        float x_offs = 1.0f / mWidth;
601        float y_offs = 1.0f / mHeight;
602
603        SetVertex(0, 0, x_offs, y_offs);
604        SetVertex(1, 0, x_offs, y_offs);
605        SetVertex(1, 1, x_offs, y_offs);
606        SetVertex(0, 1, x_offs, y_offs);
607
608        glEnd();
609
610        cgGLDisableTextureParameter(sColorsTexAntiAliasingParam);
611        cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam);
612
613        PrintGLerror("antialiasing");
614}
615
616
617void SsaoShader::FirstPass(FrameBufferObject *fbo)
618{
619        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
620        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
621        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
622
623        fbo->Bind();
624
625        glDrawBuffers(1, mymrt + 3);
626
627        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
628       
629        cgGLEnableProfile(RenderState::sCgFragmentProfile);
630
631        cgGLBindProgram(sCgDeferredProgram);
632
633        cgGLSetTextureParameter(sColorsTexDeferredParam, colorsTex);
634        cgGLEnableTextureParameter(sColorsTexDeferredParam);
635
636        cgGLSetTextureParameter(sPositionsTexDeferredParam, positionsTex);
637        cgGLEnableTextureParameter(sPositionsTexDeferredParam);
638
639        cgGLSetTextureParameter(sNormalsTexDeferredParam, normalsTex);
640        cgGLEnableTextureParameter(sNormalsTexDeferredParam);
641       
642        glColor3f(1.0f, 1.0f, 1.0f);
643
644        const float offs = 0.5f;
645
646        glBegin(GL_QUADS);
647
648        glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
649        glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
650        glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f);
651        glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f);
652
653        glEnd();
654
655        cgGLDisableTextureParameter(sColorsTexDeferredParam);
656        cgGLDisableTextureParameter(sPositionsTexDeferredParam);
657        cgGLDisableTextureParameter(sNormalsTexDeferredParam);
658
659        cgGLDisableProfile(RenderState::sCgFragmentProfile);
660
661        FrameBufferObject::Release();
662
663        PrintGLerror("deferred shading");
664}
665
666
667void SsaoShader::ComputeGlobIllum(FrameBufferObject *fbo,
668                                                                  float expFactor,
669                                                                  const Matrix4x4 &oldProjViewMatrix
670                                                                  )
671{
672        cgGLSetMatrixParameterfc(sOldModelViewProjMatrixGiParam, (const float *)oldProjViewMatrix.x);
673
674        //GLuint colorsTex = mFbo->GetColorBuffer(0)->GetTexture();
675        GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
676        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
677        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
678
679        if (1)
680        {
681                // generate mip map levels for position texture
682                glBindTexture(GL_TEXTURE_2D, positionsTex);
683                glGenerateMipmapEXT(GL_TEXTURE_2D);
684
685                // generate mip map levels for position texture
686                glBindTexture(GL_TEXTURE_2D, colorsTex);
687                glGenerateMipmapEXT(GL_TEXTURE_2D);
688        }
689
690
691        // read the second buffer, write to the first buffer
692        mNewFbo->Bind();
693        glDrawBuffers(2, mymrt);
694
695        GLuint oldSsaoTex = mOldFbo->GetColorBuffer(0)->GetTexture();
696        GLuint oldIllumTex = mOldFbo->GetColorBuffer(1)->GetTexture();
697
698        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
699
700        cgGLEnableProfile(RenderState::sCgFragmentProfile);
701        cgGLBindProgram(sCgGiProgram);
702
703        cgGLSetTextureParameter(sPositionsTexGiParam, positionsTex);
704        cgGLEnableTextureParameter(sPositionsTexGiParam);
705
706        cgGLSetTextureParameter(sColorsTexGiParam, colorsTex);
707        cgGLEnableTextureParameter(sColorsTexGiParam);
708
709        cgGLSetTextureParameter(sNormalsTexGiParam, normalsTex);
710        cgGLEnableTextureParameter(sNormalsTexGiParam);
711
712        cgGLSetTextureParameter(sNoiseTexGiParam, noiseTex);
713        cgGLEnableTextureParameter(sNoiseTexGiParam);
714
715        cgGLSetTextureParameter(sOldSsaoTexGiParam, oldSsaoTex);
716        cgGLEnableTextureParameter(sOldSsaoTexGiParam);
717
718        cgGLSetTextureParameter(sOldIllumTexGiParam, oldIllumTex);
719        cgGLEnableTextureParameter(sOldIllumTexGiParam);
720
721        cgGLSetParameter1f(sMaxDepthGiParam, mScaleFactor);
722
723
724        if (mUseTemporalCoherence)
725        {
726                cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f));
727                cgGLSetParameter1f(sExpFactorGiParam, expFactor);
728
729                // q: should we generate new samples or only rotate the old ones?
730                // in the first case, the sample patterns look nicer, but the kernel
731                // needs longer to converge
732                GenerateSamples();
733                cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples);
734        }
735        else
736        {
737                        cgGLSetParameter1f(sExpFactorGiParam, 1.0f);
738        }
739
740        Vector3 tl, tr, bl, br;
741        ComputeViewVectors(tl, tr, bl, br);
742
743        glColor3f(1.0f, 1.0f, 1.0f);
744
745        glBegin(GL_QUADS);
746
747        // note: slightly larger texture hides ambient occlusion error on border but costs resolution
748        //const float new_offs = 0.55f;
749        const float new_offs = 0.5f;
750       
751        glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-new_offs, -new_offs, -0.5f);
752        glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( new_offs, -new_offs, -0.5f);
753        glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( new_offs,  new_offs, -0.5f);
754        glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-new_offs,  new_offs, -0.5f);
755
756        glEnd();
757
758        cgGLDisableTextureParameter(sColorsTexGiParam);
759        cgGLDisableTextureParameter(sPositionsTexGiParam);
760        cgGLDisableTextureParameter(sNormalsTexGiParam);
761        cgGLDisableTextureParameter(sNoiseTexGiParam);
762        cgGLDisableTextureParameter(sOldSsaoTexGiParam);
763        cgGLDisableTextureParameter(sOldIllumTexGiParam);
764
765        FrameBufferObject::Release();
766
767        PrintGLerror("globillum first pass");
768}
769
770
771void SsaoShader::CombineIllum(FrameBufferObject *fbo)
772{
773        GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
774
775        GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
776        GLuint illumTex = mNewFbo->GetColorBuffer(1)->GetTexture();
777
778        fbo->Bind();
779
780        // overwrite old color texture
781        glDrawBuffers(1, mymrt);
782
783        cgGLEnableProfile(RenderState::sCgFragmentProfile);
784
785        cgGLBindProgram(sCgCombinedIllumProgram);
786
787        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
788       
789
790        cgGLSetTextureParameter(sColorsTexCombinedIllumParam, colorsTex);
791        cgGLEnableTextureParameter(sColorsTexCombinedIllumParam);
792
793        cgGLSetTextureParameter(sSsaoTexCombinedIllumParam, ssaoTex);
794        cgGLEnableTextureParameter(sSsaoTexCombinedIllumParam);
795
796        cgGLSetTextureParameter(sIllumTexCombinedIllumParam, illumTex);
797        cgGLEnableTextureParameter(sIllumTexCombinedIllumParam);
798       
799        glColor3f(1.0f, 1.0f, 1.0f);
800
801        const float offs = 0.5f;
802
803        glBegin(GL_QUADS);
804
805        glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
806        glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
807        glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f);
808        glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f);
809
810        glEnd();
811
812        cgGLDisableTextureParameter(sColorsTexCombinedIllumParam);
813        cgGLDisableTextureParameter(sSsaoTexCombinedIllumParam);
814        cgGLDisableTextureParameter(sIllumTexCombinedIllumParam);
815
816        cgGLDisableProfile(RenderState::sCgFragmentProfile);
817
818        FrameBufferObject::Release();
819
820        PrintGLerror("combine");
821}
822
823
824void SsaoShader::CombineSsao(FrameBufferObject *fbo)
825{
826        GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
827        GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
828
829        fbo->Bind();
830
831        // write into old color texture (not needed anymore)
832        glDrawBuffers(1, mymrt);
833
834        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
835       
836        cgGLEnableProfile(RenderState::sCgFragmentProfile);
837
838        cgGLBindProgram(sCgCombinedSsaoProgram);
839
840        cgGLSetTextureParameter(sColorsTexCombinedSsaoParam, colorsTex);
841        cgGLEnableTextureParameter(sColorsTexCombinedSsaoParam);
842
843        cgGLSetTextureParameter(sSsaoTexCombinedSsaoParam, ssaoTex);
844        cgGLEnableTextureParameter(sSsaoTexCombinedSsaoParam);
845
846       
847        glColor3f(1.0f, 1.0f, 1.0f);
848
849        const float offs = 0.5f;
850
851        glBegin(GL_QUADS);
852
853        glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
854        glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
855        glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f);
856        glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f);
857
858        glEnd();
859
860        cgGLDisableTextureParameter(sColorsTexCombinedSsaoParam);
861        cgGLDisableTextureParameter(sSsaoTexCombinedSsaoParam);
862
863        cgGLDisableProfile(RenderState::sCgFragmentProfile);
864
865        FrameBufferObject::Release();
866
867        PrintGLerror("combine ssao");
868}
869
870
871
872} // namespace
Note: See TracBrowser for help on using the repository browser.