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

RevLine 
[2859]1#include "SsaoShader.h"
2#include "FrameBufferObject.h"
3#include "RenderState.h"
4#include "SampleGenerator.h"
[2860]5#include "Vector3.h"
6#include "Camera.h"
[2884]7#include "shaderenv.h"
[2858]8
[2859]9
[2858]10using namespace std;
11
[2879]12static GLenum mymrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT};
[2858]13
14namespace CHCDemoEngine
15{
16
[2859]17static CGprogram sCgSsaoProgram = NULL;
[2873]18static CGprogram sCgGiProgram = NULL;
19
20static CGprogram sCgDeferredProgram = NULL;
[2868]21static CGprogram sCgAntiAliasingProgram = NULL;
[2859]22
[2869]23static CGparameter sColorsTexCombineParam;
24static CGparameter sSsaoTexCombineParam;
25
[2868]26static CGparameter sColorsTexDeferredParam;
27static CGparameter sPositionsTexDeferredParam;
28static CGparameter sNormalsTexDeferredParam;
29
[2873]30
[2880]31static CGprogram sCgCombinedSsaoProgram = NULL;
32static CGprogram sCgCombinedIllumProgram = NULL;
33
[2873]34///////////////////////////////////////7
35
36
[2859]37static CGparameter sColorsTexParam;
38static CGparameter sPositionsTexParam;
39static CGparameter sNormalsTexParam;
[2868]40
[2859]41static CGparameter sOldModelViewProjMatrixParam;
42static CGparameter sMaxDepthParam;
43static CGparameter sSamplesParam;
44static CGparameter sOldTexParam;
45static CGparameter sNoiseTexParam;
46static CGparameter sNoiseMultiplierParam;
47static CGparameter sExpFactorParam;
48
[2873]49
[2884]50
[2873]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
[2880]69static CGparameter sColorsTexCombinedIllumParam;
70static CGparameter sSsaoTexCombinedIllumParam;
71static CGparameter sIllumTexCombinedIllumParam;
72
73static CGparameter sColorsTexCombinedSsaoParam;
74static CGparameter sSsaoTexCombinedSsaoParam;
75
76
[2873]77////////////
78
[2865]79static CGparameter sColorsTexAntiAliasingParam;
80static CGparameter sNormalsTexAntiAliasingParam;
81
[2879]82static GLuint noiseTex = 0;
[2865]83
[2859]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
[2879]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
[2884]137        //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);
[2879]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
[2867]151SsaoShader::SsaoShader(int w, int h, Camera *cam, float scaleFactor):
[2860]152mWidth(w), mHeight(h),
153mCamera(cam),
[2875]154mScaleFactor(scaleFactor),
155mUseTemporalCoherence(true),
156mUseGlobIllum(false)
[2861]157{
[2879]158        // create noise texture for ssao
159        CreateNoiseTex2D(w, h);
160
[2861]161        ///////////
162        //-- the flip-flop fbos
[2859]163
[2861]164        mNewFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
[2880]165       
[2867]166        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
[2871]167        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
[2861]168       
[2881]169
170        ///////////////////////
171
[2861]172        mOldFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
[2880]173       
[2867]174        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
[2871]175        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
[2861]176       
[2879]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);*/
[2861]179}
180
181
182SsaoShader::~SsaoShader()
183{
[2879]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);
[2861]189
190        DEL_PTR(mNewFbo);
191        DEL_PTR(mOldFbo);
[2879]192        //DEL_PTR(mFbo);
[2861]193
194        glDeleteTextures(1, &noiseTex);
195}
196
197
[2875]198void SsaoShader::SetUseGlobIllum(bool useGlobIllum)
199{
200        mUseGlobIllum = useGlobIllum;
201}
202
203
204void SsaoShader::SetUseTemporalCoherence(bool temporal)
205{
206        mUseTemporalCoherence = temporal;
207}
208
209
[2859]210void SsaoShader::Init(CGcontext context)
[2861]211{       
[2873]212        sCgDeferredProgram =
[2868]213                cgCreateProgramFromFile(context,
214                                                                CG_SOURCE,
215                                                                "src/shaders/deferred.cg",
216                                                                RenderState::sCgFragmentProfile,
[2873]217                                                                "main",
[2868]218                                                                NULL);
219
[2873]220        if (sCgDeferredProgram != NULL)
[2868]221        {
[2873]222                cgGLLoadProgram(sCgDeferredProgram);
[2868]223
224                // we need size of texture for scaling
[2873]225                sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "positions"); 
226                sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "colors"); 
227                sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals");
[2868]228        }
229        else
230                cerr << "deferred program failed to load" << endl;
231
232
[2859]233        ///////////////
234
235        sCgSsaoProgram =
236                cgCreateProgramFromFile(context,
237                                                                CG_SOURCE,
[2868]238                                                                "src/shaders/ssao.cg",
[2859]239                                                                RenderState::sCgFragmentProfile,
[2868]240                                                                "main",
[2859]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");
[2868]253               
[2859]254                sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj");
255                sMaxDepthParam = cgGetNamedParameter(sCgSsaoProgram, "maxDepth");
256                sExpFactorParam = cgGetNamedParameter(sCgSsaoProgram, "expFactor");
257
258                sOldTexParam = cgGetNamedParameter(sCgSsaoProgram, "oldTex"); 
[2873]259               
[2884]260                cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));
[2859]261
262                // generate samples for ssao kernel
263                GenerateSamples();
[2875]264
[2884]265                sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples");
266                //cgSetArraySize(sSamplesParam, NUM_SAMPLES);
267                //cgCompileProgram(sCgSsaoProgram);
268               
269                cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);
[2859]270        }
271        else
272                cerr << "ssao program failed to load" << endl;
273
[2873]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               
[2874]293                sOldModelViewProjMatrixGiParam = cgGetNamedParameter(sCgGiProgram, "oldModelViewProj");
[2873]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);
[2875]305
306                cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f));
[2873]307        }
308        else
309                cerr << "globillum program failed to load" << endl;
310
[2880]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       
[2865]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
[2859]368        PrintGLerror("init");
369}
370
371
[2861]372void SsaoShader::Render(FrameBufferObject *fbo,
373                                                const Matrix4x4 &oldProjViewMatrix,
374                                                float expFactor)
[2859]375{
[2868]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
[2867]385        cgGLEnableProfile(RenderState::sCgFragmentProfile);
386
387        glDisable(GL_ALPHA_TEST);
388        glDisable(GL_TEXTURE_2D);
389        glDisable(GL_LIGHTING);
390
[2880]391        glPushAttrib(GL_VIEWPORT_BIT);
392        glViewport(0, 0, mWidth, mHeight);
393
[2867]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
[2868]405        FirstPass(fbo);
[2873]406       
[2875]407        if (!mUseGlobIllum)
[2880]408        {
[2875]409                ComputeSsao(fbo, expFactor, oldProjViewMatrix);
[2880]410                CombineSsao(fbo);
411        }
[2875]412        else
[2880]413        {
[2875]414                ComputeGlobIllum(fbo, expFactor, oldProjViewMatrix);
[2880]415                CombineIllum(fbo);
416        }
[2884]417
[2865]418        AntiAliasing(fbo);
[2867]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);
[2859]432}
433
434
[2874]435void SsaoShader::ComputeSsao(FrameBufferObject *fbo,
436                                                         float expFactor,
437                                                         const Matrix4x4 &oldProjViewMatrix
438                                                         )
[2859]439{
[2874]440        cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x);
441
[2879]442        GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
[2861]443        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
444        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
[2859]445
[2867]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
[2861]454        // read the second buffer, write to the first buffer
455        mNewFbo->Bind();
[2880]456        glDrawBuffers(1, mymrt);
[2868]457
[2880]458
[2861]459        GLuint oldTex = mOldFbo->GetColorBuffer(0)->GetTexture();
[2860]460
[2859]461        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
462
[2868]463        cgGLEnableProfile(RenderState::sCgFragmentProfile);
[2859]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);
[2875]482       
483        if (mUseTemporalCoherence)
484        {
485                cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));
486                cgGLSetParameter1f(sExpFactorParam, expFactor);
[2859]487
488
[2875]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        }
[2859]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
[2860]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);
[2859]515
516        glEnd();
517
[2860]518        cgGLDisableTextureParameter(sColorsTexParam);
519        cgGLDisableTextureParameter(sPositionsTexParam);
520        cgGLDisableTextureParameter(sNormalsTexParam);
521        cgGLDisableTextureParameter(sNoiseTexParam);
522        cgGLDisableTextureParameter(sOldTexParam);
[2859]523
[2880]524
[2861]525        FrameBufferObject::Release();
[2859]526
527        PrintGLerror("ssao first pass");
528}
529
530
[2860]531void SsaoShader::ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br)
532{
533        Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr;
[2859]534
[2860]535        mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
536
[2863]537#if 1 // matT: debug this!!
[2860]538       
[2863]539        bl = -Normalize(nbl - fbl);
540        br = -Normalize(nbr - fbr);
541        tl = -Normalize(ntl - ftl);
542        tr = -Normalize(ntr - ftr);
[2860]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
[2865]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{
[2880]579        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
[2865]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);
[2868]586       
[2865]587        cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
588        cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
589
590        cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
591        cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
[2868]592
[2865]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
[2867]613        PrintGLerror("antialiasing");
[2865]614}
615
616
[2868]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
[2879]623        fbo->Bind();
[2868]624
[2879]625        glDrawBuffers(1, mymrt + 3);
626
[2868]627        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
628       
629        cgGLEnableProfile(RenderState::sCgFragmentProfile);
630
[2873]631        cgGLBindProgram(sCgDeferredProgram);
[2868]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
[2869]666
[2874]667void SsaoShader::ComputeGlobIllum(FrameBufferObject *fbo,
668                                                                  float expFactor,
669                                                                  const Matrix4x4 &oldProjViewMatrix
670                                                                  )
[2869]671{
[2874]672        cgGLSetMatrixParameterfc(sOldModelViewProjMatrixGiParam, (const float *)oldProjViewMatrix.x);
673
[2879]674        //GLuint colorsTex = mFbo->GetColorBuffer(0)->GetTexture();
675        GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
[2873]676        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
677        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
[2869]678
[2873]679        if (1)
680        {
681                // generate mip map levels for position texture
682                glBindTexture(GL_TEXTURE_2D, positionsTex);
683                glGenerateMipmapEXT(GL_TEXTURE_2D);
[2869]684
[2873]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();
[2880]693        glDrawBuffers(2, mymrt);
[2873]694
695        GLuint oldSsaoTex = mOldFbo->GetColorBuffer(0)->GetTexture();
[2880]696        GLuint oldIllumTex = mOldFbo->GetColorBuffer(1)->GetTexture();
[2873]697
[2869]698        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
699
700        cgGLEnableProfile(RenderState::sCgFragmentProfile);
[2873]701        cgGLBindProgram(sCgGiProgram);
[2869]702
[2873]703        cgGLSetTextureParameter(sPositionsTexGiParam, positionsTex);
704        cgGLEnableTextureParameter(sPositionsTexGiParam);
[2869]705
[2873]706        cgGLSetTextureParameter(sColorsTexGiParam, colorsTex);
707        cgGLEnableTextureParameter(sColorsTexGiParam);
[2869]708
[2873]709        cgGLSetTextureParameter(sNormalsTexGiParam, normalsTex);
710        cgGLEnableTextureParameter(sNormalsTexGiParam);
[2869]711
[2873]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
[2875]724        if (mUseTemporalCoherence)
725        {
726                cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f));
727                cgGLSetParameter1f(sExpFactorGiParam, expFactor);
[2873]728
[2875]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
[2873]740        Vector3 tl, tr, bl, br;
741        ComputeViewVectors(tl, tr, bl, br);
742
[2869]743        glColor3f(1.0f, 1.0f, 1.0f);
744
745        glBegin(GL_QUADS);
746
[2873]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);
[2869]755
756        glEnd();
757
[2873]758        cgGLDisableTextureParameter(sColorsTexGiParam);
759        cgGLDisableTextureParameter(sPositionsTexGiParam);
760        cgGLDisableTextureParameter(sNormalsTexGiParam);
761        cgGLDisableTextureParameter(sNoiseTexGiParam);
762        cgGLDisableTextureParameter(sOldSsaoTexGiParam);
763        cgGLDisableTextureParameter(sOldIllumTexGiParam);
[2869]764
765        FrameBufferObject::Release();
766
[2873]767        PrintGLerror("globillum first pass");
[2869]768}
769
770
[2880]771void SsaoShader::CombineIllum(FrameBufferObject *fbo)
772{
773        GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
[2884]774
[2880]775        GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
776        GLuint illumTex = mNewFbo->GetColorBuffer(1)->GetTexture();
777
778        fbo->Bind();
779
[2884]780        // overwrite old color texture
[2880]781        glDrawBuffers(1, mymrt);
782
783        cgGLEnableProfile(RenderState::sCgFragmentProfile);
784
785        cgGLBindProgram(sCgCombinedIllumProgram);
786
[2881]787        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
788       
789
[2880]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
[2858]872} // namespace
Note: See TracBrowser for help on using the repository browser.