Ignore:
Timestamp:
11/06/08 10:41:02 (16 years ago)
Author:
mattausch
Message:

still some error with ssao on edges
bilateral filter slow

File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg

    r3101 r3103  
    44// Screen Spaced Ambient Occlusion shader 
    55// based on shader of Alexander Kusternig 
    6  
    7  
    8 #define USE_EYE_SPACE_DEPTH 1 
    9  
    106 
    117struct fragment 
     
    8278                                                           ) 
    8379{ 
    84         const float2 mynoise = tex2D(noiseTex, texcoord0).xy; 
     80        const float2 mynoise = tex2Dlod(noiseTex, texcoord0).xy; 
    8581 
    8682        const float2 offsetTransformed = myreflect(offset, mynoise); 
     
    151147        //else trafo = inverseModelTrafo * oldModelViewProj; 
    152148 
    153         float3 dummyPt = worldPos.xyz - oldEyePos; 
     149        float3 translatedPt = worldPos.xyz - oldEyePos; 
    154150 
    155151        // reproject into old frame and calculate texture position of sample in old frame 
    156         float4 backProjPos = mul(oldModelViewProj, float4(dummyPt, 1.0f)); 
     152        float4 backProjPos = mul(oldModelViewProj, float4(translatedPt, 1.0f)); 
    157153        backProjPos /= backProjPos.w; 
    158154        // fit from unit cube into 0 .. 1 
     
    169165         
    170166        const float invlen = 1.0f / length(viewVec); 
    171         const float projectedEyeSpaceDepth = length(dummyPt) * invlen; 
     167        const float projectedEyeSpaceDepth = length(translatedPt) * invlen; 
    172168         
    173169        const float depthDif = abs(oldEyeSpaceDepth - projectedEyeSpaceDepth); 
     170        //const float depthDif = abs(oldEyeSpaceDepth - projectedEyeSpaceDepth) / projectedEyeSpaceDepth; 
    174171         
    175172        float notValid = 0.5f; 
    176173 
     174        /* 
    177175        for (int i = 0; i < NUM_SAMPLES; ++ i)  
    178176        { 
     
    191189                if (sampleDif >= 5e-2f) ++ notValid; 
    192190        } 
     191        */ 
    193192 
    194193        // the number of valid samples in this frame 
     
    202201                && (oldTexCoords.x >= 0.0f) && (oldTexCoords.x < 1.0f) 
    203202                && (oldTexCoords.y >= 0.0f) && (oldTexCoords.y < 1.0f) 
    204                 && (abs(depthDif) <= MIN_DEPTH_DIFF)  
     203                && (depthDif <= MIN_DEPTH_DIFF)  
    205204                // if visibility changed in the surrounding area we have to recompute 
    206205                //&& (oldNumSamples > 0.8f * newNumSamples) 
     
    259258                //-- add random noise: reflect around random normal vector (rather slow!) 
    260259 
    261                 float2 mynoise = tex2D(noiseTex, IN.texCoord).xy; 
     260                float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f, 0, 0)).xy; 
    262261                const float2 offsetTransformed = myreflect(offset, mynoise); 
    263262#else 
     
    275274 
    276275                float3 dirSample = samplePos - centerPosition; 
    277                 const float lengthToSample = length(dirSample); 
     276                const float lengthToSample = max(length(dirSample), 1e-6f); 
    278277 
    279278                dirSample /= lengthToSample; // normalize 
    280279 
    281280                // angle between current normal and direction to sample controls AO intensity. 
    282                 const float cosAngle = max(dot(dirSample, normal), .0f); 
    283  
     281                float cosAngle = max(dot(dirSample, normal), .0f); 
     282         
    284283                // the distance_scale offset is used to avoid singularity that occurs at global illumination when  
    285284                // the distance to a sample approaches zero 
     
    291290                // => compensate for this (on the other hand, projected sampling area could be larger!) 
    292291 
    293                 const float viewCorrection = 1.0f + VIEW_CORRECTION_SCALE * dot(viewDir, normal); 
     292                const float viewCorrection = 1.0f + VIEW_CORRECTION_SCALE * max(dot(viewDir, normal), 0.0f); 
    294293                total_ao += cosAngle * aoContrib * viewCorrection; 
    295294#else 
     
    352351                                                                          oldbl, oldbr, oldtl, oldtr); 
    353352 
     353        //OUT.illum_col.xyz = normal * 0.5f + 0.5f; 
    354354        return OUT; 
    355355} 
    356  
    357  
    358 float Filter(float2 texCoord,  
    359                          uniform sampler2D ssaoTex, 
    360                          uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES], 
    361                          uniform float filterWeights[NUM_SSAO_FILTERSAMPLES]) 
    362 { 
    363         float average = .0f; 
    364         float w = .0f; 
    365  
    366         for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i) 
    367         { 
    368                 average += filterWeights[i] * tex2Dlod(ssaoTex, float4(texCoord + filterOffs[i], 0, 0)).x; 
    369                 w += filterWeights[i]; 
    370         } 
    371  
    372         average *= 1.0f / (float)w; 
    373  
    374         return average; 
    375 } 
    376  
    377  
    378 pixel combine(fragment IN,  
    379                           uniform sampler2D colors, 
    380                           uniform sampler2D ssaoTex, 
    381                           uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES], 
    382                           uniform float filterWeights[NUM_SSAO_FILTERSAMPLES] 
    383                           ) 
    384 { 
    385         pixel OUT; 
    386  
    387         float4 col = tex2Dlod(colors, float4(IN.texCoord, 0, 0)); 
    388         float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0)); 
    389  
    390         if (ao.y < 10.0f) ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights); 
    391  
    392         OUT.illum_col = col * ao.x; 
    393         //OUT.illum_col = float4(ao.y, ao.y, ao.y, col.w); 
    394         //OUT.illum_col.xyz = float3(1.0f - ao.x, 1.0f - ao.y * 1e-2f, 1); 
    395         //OUT.illum_col.xyz = float3(1.0f - ao.x, ao.y, 0); 
    396         OUT.illum_col.w = col.w; 
    397  
    398         return OUT; 
    399 } 
Note: See TracChangeset for help on using the changeset viewer.