Changeset 2873


Ignore:
Timestamp:
08/27/08 15:09:04 (16 years ago)
Author:
mattausch
Message:
 
Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj

    r2869 r2873  
    792792                        </File> 
    793793                        <File 
     794                                RelativePath=".\src\shaders\globillum.cg" 
     795                                > 
     796                        </File> 
     797                        <File 
    794798                                RelativePath=".\src\shaders\mrt.cg" 
    795799                                > 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SampleGenerator.cpp

    r2865 r2873  
    1919void PoissonDiscSampleGenerator::Generate(Sample2 *samples) const 
    2020{ 
     21        // this is a hacky poisson sampling generator which does random dart-throwing 
     22        // until it is not able to place any dart for a number of tries 
     23        // in this case, the required min distance is reduced 
     24        // the solution is a possion sampling with respect to the adjusted min distance 
     25        // better solutions have been proposed, i.e., using hierarchical sampling 
     26 
     27        const float maxTries = 1000; 
     28        const float f_reduction = 0.9f; 
     29 
    2130        static HaltonSequence halton; 
    2231        float r[2]; 
    2332 
    2433        // generates poisson distribution on disc 
    25         float minDist = 4.0f / sqrt((float)mNumSamples); 
     34        float minDist = 2.0f / sqrt((float)mNumSamples); 
    2635 
    2736        //cout << "minDist before= " << minDist << endl; 
     
    6675                        } 
    6776 
    68                         if (tries > 2000) 
     77                        if (tries > maxTries) 
    6978                        { 
    70                                 minDist *= 0.9f; 
     79                                minDist *= f_reduction; 
    7180                                tries = 0; 
    7281                        } 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp

    r2871 r2873  
    1515 
    1616// number of ssao samples 
    17 #define NUM_SAMPLES 16 
     17#define NUM_SAMPLES 10 
    1818 
    1919 
    2020static CGprogram sCgSsaoProgram = NULL; 
    21 static CGprogram sCgDeferredProgram2 = NULL; 
     21static CGprogram sCgGiProgram = NULL; 
     22 
     23static CGprogram sCgDeferredProgram = NULL; 
    2224static CGprogram sCgAntiAliasingProgram = NULL; 
    2325static CGprogram sCgCombineProgram = NULL; 
     
    3032static CGparameter sNormalsTexDeferredParam; 
    3133 
     34 
     35///////////////////////////////////////7 
     36 
     37 
    3238static CGparameter sColorsTexParam; 
    3339static CGparameter sPositionsTexParam; 
    3440static CGparameter sNormalsTexParam; 
    35  
    3641 
    3742static CGparameter sOldModelViewProjMatrixParam; 
     
    4348static CGparameter sExpFactorParam; 
    4449 
     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 
    4571static CGparameter sColorsTexAntiAliasingParam; 
    4672static CGparameter sNormalsTexAntiAliasingParam; 
     
    88114        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    89115        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
     116        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    90117         
    91118        mOldFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 
     
    93120        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    94121        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
     122        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    95123         
    96124        mFbo3 = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 
     
    98126        mFbo3->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    99127                 
    100         mFbo4 = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 
    101         // the diffuse color buffer 
    102         mFbo4->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    103128 
    104129        // create noise texture for ssao 
     
    121146void SsaoShader::Init(CGcontext context) 
    122147{        
    123         sCgDeferredProgram2 =  
     148        sCgDeferredProgram =  
    124149                cgCreateProgramFromFile(context,  
    125150                                                                CG_SOURCE, 
    126151                                                                "src/shaders/deferred.cg",  
    127152                                                                RenderState::sCgFragmentProfile, 
    128                                                                 "main2", 
     153                                                                "main", 
    129154                                                                NULL); 
    130155 
    131         if (sCgDeferredProgram2 != NULL) 
    132         { 
    133                 cgGLLoadProgram(sCgDeferredProgram2); 
     156        if (sCgDeferredProgram != NULL) 
     157        { 
     158                cgGLLoadProgram(sCgDeferredProgram); 
    134159 
    135160                // we need size of texture for scaling 
    136                 sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram2, "positions");   
    137                 sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram2, "colors");   
    138                 sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram2, "normals");  
     161                sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "positions");   
     162                sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "colors");   
     163                sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals");  
    139164        } 
    140165        else 
    141166                cerr << "deferred program failed to load" << endl; 
    142167 
    143  
    144         /*sCgCombineProgram =  
    145                 cgCreateProgramFromFile(context,  
    146                                                                 CG_SOURCE, 
    147                                                                 "src/shaders/ssao.cg",  
    148                                                                 RenderState::sCgFragmentProfile, 
    149                                                                 "combined", 
    150                                                                 NULL); 
    151  
    152         if (sCgCombineProgram != NULL) 
    153         { 
    154                 cgGLLoadProgram(sCgCombineProgram); 
    155  
    156                 // we need size of texture for scaling 
    157                 sColorsTexCombineParam = cgGetNamedParameter(sCgCombineProgram, "colors");   
    158                 sSsaoTexCombineParam = cgGetNamedParameter(sCgCombineProgram, "ssaoTex");  
    159         } 
    160         else 
    161                 cerr << "combined program failed to load" << endl; 
    162 */ 
    163168 
    164169        /////////////// 
     
    189194                sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples"); 
    190195                sOldTexParam = cgGetNamedParameter(sCgSsaoProgram, "oldTex");   
     196                 
    191197 
    192198                // generate samples for ssao kernel 
     
    196202        else 
    197203                cerr << "ssao program failed to load" << endl; 
     204 
     205        sCgGiProgram =  
     206                cgCreateProgramFromFile(context,  
     207                                                                CG_SOURCE, 
     208                                                                "src/shaders/globillum.cg",  
     209                                                                RenderState::sCgFragmentProfile, 
     210                                                                "main", 
     211                                                                NULL); 
     212 
     213        if (sCgGiProgram != NULL) 
     214        { 
     215                cgGLLoadProgram(sCgGiProgram); 
     216 
     217                // we need size of texture for scaling 
     218                sPositionsTexGiParam = cgGetNamedParameter(sCgGiProgram, "positions");   
     219                sColorsTexGiParam = cgGetNamedParameter(sCgGiProgram, "colors");   
     220                sNormalsTexGiParam = cgGetNamedParameter(sCgGiProgram, "normals");   
     221                sNoiseTexGiParam = cgGetNamedParameter(sCgGiProgram, "noiseTexture"); 
     222                sNoiseMultiplierGiParam = cgGetNamedParameter(sCgGiProgram, "noiseMultiplier"); 
     223                 
     224                sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgGiProgram, "oldModelViewProj"); 
     225                sMaxDepthGiParam = cgGetNamedParameter(sCgGiProgram, "maxDepth"); 
     226                sExpFactorGiParam = cgGetNamedParameter(sCgGiProgram, "expFactor"); 
     227 
     228                sSamplesGiParam = cgGetNamedParameter(sCgGiProgram, "samples"); 
     229                 
     230                sOldSsaoTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldSsaoTex");   
     231                sOldIllumTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldIllumTex");   
     232 
     233                // generate samples for ssao kernel 
     234                GenerateSamples();  
     235                cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples); 
     236        } 
     237        else 
     238                cerr << "globillum program failed to load" << endl; 
    198239 
    199240        sCgAntiAliasingProgram =  
     
    258299 
    259300        FirstPass(fbo); 
     301         
    260302        ComputeSsao(fbo, expFactor); 
     303        //ComputeGlobIllum(fbo, expFactor); 
     304 
    261305        //Combine(fbo); 
    262306        AntiAliasing(fbo); 
     
    323367 
    324368 
    325         //GenerateSamples();  
     369        // q: should we generate new samples or only rotate the old ones? 
     370        // in the first case, the sample patterns look nicer, but the kernel 
     371        // needs longer to converge 
     372        GenerateSamples();  
    326373        cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples); 
    327374 
     
    499546        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    500547 
    501         cgGLBindProgram(sCgDeferredProgram2); 
     548        cgGLBindProgram(sCgDeferredProgram); 
    502549 
    503550        cgGLSetTextureParameter(sColorsTexDeferredParam, colorsTex); 
     
    535582 
    536583 
    537 void SsaoShader::Combine(FrameBufferObject *fbo) 
     584void SsaoShader::ComputeGlobIllum(FrameBufferObject *fbo, float expFactor) 
    538585{ 
    539586        GLuint colorsTex = mFbo3->GetColorBuffer(0)->GetTexture(); 
    540         GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture(); 
    541  
    542         mFbo4->Bind(); 
    543         //mNewFbo->Bind(); 
     587        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 
     588        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
     589 
     590        if (1) 
     591        { 
     592                // generate mip map levels for position texture 
     593                glBindTexture(GL_TEXTURE_2D, positionsTex); 
     594                glGenerateMipmapEXT(GL_TEXTURE_2D); 
     595 
     596                // generate mip map levels for position texture 
     597                glBindTexture(GL_TEXTURE_2D, colorsTex); 
     598                glGenerateMipmapEXT(GL_TEXTURE_2D); 
     599        } 
     600 
     601 
     602        // read the second buffer, write to the first buffer 
     603        mNewFbo->Bind(); 
     604        glDrawBuffers(3, mymrt); 
     605 
     606        GLuint oldSsaoTex = mOldFbo->GetColorBuffer(0)->GetTexture(); 
     607        GLuint oldIllumTex = mOldFbo->GetColorBuffer(2)->GetTexture(); 
    544608 
    545609        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    546          
    547         glDrawBuffers(1, mymrt); 
    548610 
    549611        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    550  
    551         cgGLBindProgram(sCgCombineProgram); 
    552  
    553         cgGLSetTextureParameter(sColorsTexCombineParam, colorsTex); 
    554         cgGLEnableTextureParameter(sColorsTexCombineParam); 
    555  
    556         cgGLSetTextureParameter(sSsaoTexCombineParam, ssaoTex); 
    557         cgGLEnableTextureParameter(sSsaoTexCombineParam); 
     612        cgGLBindProgram(sCgGiProgram); 
     613 
     614        cgGLSetTextureParameter(sPositionsTexGiParam, positionsTex); 
     615        cgGLEnableTextureParameter(sPositionsTexGiParam); 
     616 
     617        cgGLSetTextureParameter(sColorsTexGiParam, colorsTex); 
     618        cgGLEnableTextureParameter(sColorsTexGiParam); 
     619 
     620        cgGLSetTextureParameter(sNormalsTexGiParam, normalsTex); 
     621        cgGLEnableTextureParameter(sNormalsTexGiParam); 
     622 
     623        cgGLSetTextureParameter(sNoiseTexGiParam, noiseTex); 
     624        cgGLEnableTextureParameter(sNoiseTexGiParam); 
     625 
     626        cgGLSetTextureParameter(sOldSsaoTexGiParam, oldSsaoTex); 
     627        cgGLEnableTextureParameter(sOldSsaoTexGiParam); 
     628 
     629        cgGLSetTextureParameter(sOldIllumTexGiParam, oldIllumTex); 
     630        cgGLEnableTextureParameter(sOldIllumTexGiParam); 
     631 
     632        cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f)); 
     633         
     634        cgGLSetParameter1f(sMaxDepthGiParam, mScaleFactor); 
     635        cgGLSetParameter1f(sExpFactorGiParam, expFactor); 
     636 
     637 
     638        // q: should we generate new samples or only rotate the old ones? 
     639        // in the first case, the sample patterns look nicer, but the kernel 
     640        // needs longer to converge 
     641        GenerateSamples();  
     642        cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples); 
     643 
     644        Vector3 tl, tr, bl, br; 
     645        ComputeViewVectors(tl, tr, bl, br); 
    558646 
    559647        glColor3f(1.0f, 1.0f, 1.0f); 
    560648 
    561         const float offs = 0.5f; 
    562  
    563649        glBegin(GL_QUADS); 
    564650 
    565         glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f); 
    566         glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f); 
    567         glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f); 
    568         glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f); 
     651        // note: slightly larger texture hides ambient occlusion error on border but costs resolution 
     652        //const float new_offs = 0.55f; 
     653        const float new_offs = 0.5f; 
     654         
     655        glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-new_offs, -new_offs, -0.5f); 
     656        glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( new_offs, -new_offs, -0.5f); 
     657        glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( new_offs,  new_offs, -0.5f); 
     658        glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-new_offs,  new_offs, -0.5f); 
    569659 
    570660        glEnd(); 
    571661 
    572         cgGLDisableTextureParameter(sColorsTexCombineParam); 
    573         cgGLDisableTextureParameter(sSsaoTexCombineParam); 
    574  
    575         cgGLDisableProfile(RenderState::sCgFragmentProfile); 
     662        cgGLDisableTextureParameter(sColorsTexGiParam); 
     663        cgGLDisableTextureParameter(sPositionsTexGiParam); 
     664        cgGLDisableTextureParameter(sNormalsTexGiParam); 
     665        cgGLDisableTextureParameter(sNoiseTexGiParam); 
     666        cgGLDisableTextureParameter(sOldSsaoTexGiParam); 
     667        cgGLDisableTextureParameter(sOldIllumTexGiParam); 
    576668 
    577669        FrameBufferObject::Release(); 
    578670 
    579         PrintGLerror("deferred shading"); 
     671        PrintGLerror("globillum first pass"); 
    580672} 
    581673 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.h

    r2869 r2873  
    5050        void ComputeSsao(FrameBufferObject *fbo, float expFactor); 
    5151 
     52        void ComputeGlobIllum(FrameBufferObject *fbo, float expFactor); 
     53 
    5254        void FirstPass(FrameBufferObject *fbo); 
    5355 
    5456        void AntiAliasing(FrameBufferObject *fbo); 
    55         void Combine(FrameBufferObject *fbo); 
    5657 
    5758        void CreateNoiseTex2D(); 
     
    7273        FrameBufferObject *mNewFbo; 
    7374        FrameBufferObject *mFbo3; 
    74         FrameBufferObject *mFbo4; 
    7575}; 
    7676 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r2868 r2873  
    274274        Vector3 camDir(.0f, 1.0f, .0f); 
    275275 
    276         cout << "=== reading environment file === " << endl; 
     276        cout << "=== reading environment file ===" << endl << endl; 
    277277 
    278278        string envFileName = "default.env"; 
     
    303303                //env.GetIntParam(string("numSssaoSamples"), numSsaoSamples); 
    304304 
    305  
    306                 cout << "*********** parameters ***************" << endl; 
    307305 
    308306                cout << "assumedVisibleFrames: " << assumedVisibleFrames << endl;  
     
    318316                cout << "camPosition: " << camPos << endl; 
    319317                cout << "expFactor: " << ssaoExpFactor << endl; 
     318 
    320319                //cout << "model path: " << model_path << endl; 
     320 
     321                cout << "**** end parameters ****" << endl; 
    321322        } 
    322323 
     
    538539 
    539540        // the diffuse color buffer 
     541        //fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_MIPMAP_LINEAR, true); 
    540542        fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false); 
    541543 
    542544        // the positions buffer 
    543545        fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_MIPMAP_LINEAR, true); 
    544         //fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, true); 
     546        //fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false); 
    545547         
    546548        // the normals buffer 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg

    r2868 r2873  
    4242 
    4343 
    44 /** The mrt shader for standard rendering 
    45 */ 
    46 pixel main2(fragment IN,  
    47                    uniform sampler2D colors, 
    48                    uniform sampler2D positions, 
    49                    uniform sampler2D normals 
    50                    ) 
    51 { 
    52         pixel OUT; 
    53  
    54         float4 norm = tex2D(normals, IN.texCoord.xy); 
    55         float4 color = tex2D(colors, IN.texCoord.xy); 
    56         float4 position = tex2D(positions, IN.texCoord.xy); 
    57  
    58         // an ambient color term 
    59         float amb = norm.w; 
    60  
    61         // expand normal 
    62         float3 normal = normalize(norm.xyz);// * 2.0f - float4(1.0f)); 
    63  
    64         float4 col = shade(IN, color, position, normal, amb); 
    65          
    66         //OUT.color = float4(1.0f); 
    67         OUT.color = col; 
    68         OUT.color.w = color.w; 
    69  
    70         return OUT; 
    71 } 
    7244 
    7345/** The mrt shader for standard rendering 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg

    r2867 r2873  
    101101 
    102102pixel frag(fragin IN,  
    103            uniform float maxDepth,  
    104            uniform float4 ambient,  
    105            uniform float4 diffuse) 
     103                   uniform float maxDepth,  
     104                   uniform float4 ambient,  
     105                   uniform float4 diffuse 
     106                   ) 
    106107{ 
    107108        pixel pix; 
     
    109110        pix.col = diffuse; 
    110111        pix.pos = IN.worldPos * maxDepth; 
     112 
    111113        //pix.norm.xyz = IN.normal * 0.5f + float3(0.5f); 
    112114        pix.norm.xyz = IN.normal; 
     115         
    113116        // hack: squeeze some information about the ambient term into the target 
    114117        pix.norm.w = ambient.x; 
    115118        pix.pos.w = IN.mypos.w; 
     119         
     120        // the projected depth 
    116121        pix.col.w = IN.mypos.z / IN.mypos.w; 
    117122 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg

    r2872 r2873  
    33// based on shader of Alexander Kusternig 
    44 
    5 //#define NUM_SAMPLES 8 
    6 #define NUM_SAMPLES 16 
     5#define NUM_SAMPLES 10 
     6//#define NUM_SAMPLES 16 
    77 
    88// rule of thumb: approx 1 / NUM_SAMPLES 
    9 #define SAMPLE_INTENSITY 0.15 
    10 //#define SAMPLE_INTENSITY 0.125f 
     9//#define SAMPLE_INTENSITY 0.15f 
     10#define SAMPLE_INTENSITY 0.2f 
    1111 
    1212#define AREA_SIZE 7e-1f 
     
    4343 
    4444 
    45 /*float2 rotate(float2 pt, float2 n) 
    46 { 
    47         float2 ptTransformed; 
    48         ptTransformed.x = n.r * pt.x - n.g * pt.y; 
    49         ptTransformed.y = n.g * pt.x + n.r * pt.y; 
    50  
    51         return ptTransformed; 
    52 }*/ 
    53  
    5445 
    5546/** The ssao shader returning the an intensity value between 0 and 1 
     
    7465        float total_ao = 0.0; 
    7566 
    76         const float areaSize = 5e-1f; 
    77  
    7867        for (int i = 0; i < NUM_SAMPLES; i ++)  
    7968        { 
    8069                float2 offset = samples[i]; 
    8170 
    82                 //sample noisetex; r stores costheta, g stores sintheta 
     71                //////////////////// 
     72                // add random noise: r stores costheta, g stores sintheta 
     73 
    8374                //float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f; 
    8475                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy; 
    8576 
    86                 // rotation 
    87                 //float2 offsetTransformed = offset; 
    88                 //float2 offsetTransformed = rotate(offset, mynoise); 
    8977                float2 offsetTransformed = reflect(offset, mynoise); 
    9078 
     
    117105        return max(0.0f, 1.0f - total_ao); 
    118106        //return dot(currentViewDir, currentNormal); 
    119 } 
    120  
    121  
    122 /** Computes ambient occlusion + diffuse reflections 
    123 */ 
    124 float4 globIllum(fragment IN, 
    125                                  uniform sampler2D colors, 
    126                                  uniform sampler2D positions, 
    127                                  uniform sampler2D noiseTexture, 
    128                                  uniform float2 samples[NUM_SAMPLES], 
    129                                  uniform float3 currentNormal, 
    130                                  uniform float3 currentViewDir, 
    131                                  uniform float noiseMultiplier, 
    132                                  uniform float4 centerPosition 
    133                                  ) 
    134 { 
    135         // the w coordinate from the persp. projection 
    136         float w = centerPosition.w; 
    137  
    138         // Check in a circular area around the current position. 
    139         // Shoot vectors to the positions there, and check the angle to these positions. 
    140         // Summing up these angles gives an estimation of the occlusion at the current position. 
    141  
    142         // ao is in stored in the w 
    143         float4 total_color = float4(0, 0, 0, 1); 
    144  
    145         const float areaSize = 5e-1f; 
    146  
    147         for (int i = 0; i < NUM_SAMPLES; i ++)  
    148         { 
    149                 float2 offset = samples[i]; 
    150  
    151                 //sample noisetex; r stores costheta, g stores sintheta 
    152                 float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy; 
    153  
    154                 // rotation 
    155                 float2 offsetTransformed = reflect(offset, mynoise); 
    156  
    157                 // weight with projected coordinate to reach similar kernel size for near and far 
    158                 float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; 
    159  
    160                 float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz; 
    161                 float3 sample_color = tex2Dlod(colors, float4(texcoord, 0, 1)).xyz; 
    162  
    163                 float3 vector_to_sample = sample_position - centerPosition.xyz; 
    164                 float length_to_sample = length(vector_to_sample); 
    165                 float3 direction_to_sample = vector_to_sample / length_to_sample; 
    166  
    167                 // Angle between current normal and direction to sample controls AO intensity. 
    168                 float cos_angle = dot(direction_to_sample, currentNormal); 
    169                 cos_angle = max(cos_angle, 0.0f); 
    170  
    171                 // distance between current position and sample position controls AO intensity. 
    172                 float distance_intensity =  
    173                         (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample); 
    174  
    175                 // if normal perpenticular to view dir, only half of the samples count 
    176                 float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal)); 
    177  
    178                 total_color.w -= cos_angle * distance_intensity * view_correction; 
    179  
    180                 const float scale_factor = 0.3f; 
    181                 total_color.xyz += cos_angle * distance_intensity * view_correction * sample_color * scale_factor; 
    182         } 
    183  
    184         return saturate(total_color); 
    185107} 
    186108 
     
    220142        const float currentDepth = currentCol.w; 
    221143 
    222         //float4 new_col = (float4)ssao(IN, positions, noiseTexture, samples, normal, viewDir, noiseMultiplier, centerPosition); 
    223         float4 new_col = globIllum(IN, colors, positions, noiseTexture, samples, normal, viewDir, noiseMultiplier, centerPosition);  
    224          
     144        const float ao = ssao(IN, positions, noiseTexture, samples, normal, viewDir, noiseMultiplier, centerPosition); 
     145                 
    225146 
    226147        ///////////////// 
    227         //-- compute temporally smoothed value 
     148        //-- compute temporally smoothing 
    228149 
    229150        float4 realPos = centerPosition * maxDepth; 
     
    240161        const float depthDif = 1.0f - newDepth / oldDepth; 
    241162 
     163 
    242164        if ((tex.x >= 0.0f) && (tex.x < 1.0f) &&  
    243165                (tex.y >= 0.0f) && (tex.y < 1.0f) &&  
    244                 (abs(depthDif)  < 1e-4f)) 
     166                (abs(depthDif)  < 1e-3f)) 
    245167        { 
    246                 OUT.illum_col = new_col * expFactor + oldCol * float4(1.0f - expFactor); 
     168                OUT.illum_col.x = ao * expFactor + oldCol.x * (1.0f - expFactor); 
    247169        } 
    248170        else 
    249171        { 
    250                 OUT.illum_col = new_col; 
     172                OUT.illum_col.x = ao; 
    251173        } 
    252174 
    253         //const float ao = OUT.illum_col.x; 
    254         const float ao = OUT.illum_col.w; 
     175        OUT.combined_col = currentCol * OUT.illum_col.x; 
     176        OUT.illum_col.w = currentDepth; 
    255177 
    256         //OUT.combined_col = (currentCol + OUT.illum_col) * ao; 
    257         OUT.combined_col = currentCol * ao; 
    258  
    259         OUT.illum_col.w = currentDepth; 
     178         
    260179 
    261180        return OUT; 
    262181} 
    263  
    264 /* 
    265 pixel combined(fragment IN,  
    266                            uniform sampler2D colors, 
    267                            uniform sampler2D ssaoTex 
    268                    ) 
    269 { 
    270         pixel OUT; 
    271  
    272         float4 col = tex2D(colors, IN.texCoord.xy); 
    273         float4 ao = tex2D(ssaoTex, IN.texCoord.xy); 
    274         //float4 illum = tex2D(ssaoTex, IN.texCoord.xy); 
    275  
    276         OUT.illum_col = col * ao; 
    277  
    278         return OUT; 
    279 }*/ 
Note: See TracChangeset for help on using the changeset viewer.