Changeset 3150


Ignore:
Timestamp:
11/20/08 13:44:57 (16 years ago)
Author:
mattausch
Message:

cleaned up ssao code, removed fixed kernel + jitter and rotation stuff because not working properly

Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp

    r3148 r3150  
    325325 
    326326        // create noise texture for ssao 
    327         CreateNoiseTex2D(mIllumFbo->GetWidth(), mIllumFbo->GetHeight()); 
    328         //CreateNoiseTex2D(mIllumFbo->GetWidth() / 4, mIllumFbo->GetWidth() / 4); 
    329         CreateNoiseTex1D(mWidth / 8); 
     327        // for performance reasons we use a smaller texture and repeat it over the screen 
     328        CreateNoiseTex2D(mIllumFbo->GetWidth() / 4, mIllumFbo->GetWidth() / 4); 
     329        //CreateNoiseTex1D(mIllumFbo->GetWidth() / 4); 
    330330 
    331331        mProjViewMatrix = IdentityMatrix(); 
     
    382382                 "samples", "bl", "br", "tl", "tr",  
    383383                 "modelViewProj", "oldModelViewProj", "oldEyePos", "oldbl", "oldbr",  
    384                  "oldtl", "oldtr", "attribsTex", "noiseTex1D"}; 
    385         sCgSsaoProgram->AddParameters(ssaoParams, 0, 19); 
     384                 "oldtl", "oldtr", "attribsTex"}; 
     385        sCgSsaoProgram->AddParameters(ssaoParams, 0, 18); 
    386386         
    387387        string giParams[] =  
     
    632632        sCgSsaoProgram->SetTexture(i ++, attribsTex); 
    633633 
    634         sCgSsaoProgram->SetTexture(i ++, noiseTex1D); 
    635634 
    636635        DrawQuad(sCgSsaoProgram); 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg

    r3144 r3150  
    246246/** The ssao shader returning the an intensity value between 0 and 1 
    247247*/ 
     248float2 ssao2(fragment IN, 
     249                         sampler2D colors, 
     250                         sampler2D noiseTex, 
     251                         float2 samples[NUM_SAMPLES], 
     252                         float3 normal, 
     253                         float3 centerPosition, 
     254                         float scaleFactor, 
     255                         float3 bl, 
     256                         float3 br, 
     257                         float3 tl, 
     258                         float3 tr,  
     259                         float3 viewDir, 
     260                         sampler2D normalTex 
     261                         ) 
     262{ 
     263        // Check in a circular area around the current position. 
     264        // Shoot vectors to the positions there, and check the angle to these positions. 
     265        // Summing up these angles gives an estimation of the occlusion at the current position. 
     266 
     267        float total_ao = .0f; 
     268        float numSamples = .0f; 
     269 
     270        for (int i = 0; i < NUM_SAMPLES; ++ i)  
     271        { 
     272                const float2 offset = samples[i]; 
     273 
     274#if 1 
     275                //////////////////// 
     276                //-- add random noise: reflect around random normal vector (rather slow!) 
     277 
     278                const float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f, 0, 0)).xy; 
     279                const float2 offsetTransformed = myreflect(offset, mynoise); 
     280#else 
     281                const float2 offsetTransformed = offset; 
     282#endif 
     283                // weight with projected coordinate to reach similar kernel size for near and far 
     284                //const float2 texcoord = IN.texCoord.xy + offsetTransformed * scaleFactor + jitter; 
     285                const float2 texcoord = IN.texCoord.xy + offsetTransformed * scaleFactor; 
     286 
     287                //if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f)) ++ numSamples; 
     288                const float3 samplePos = ReconstructSamplePos(colors, texcoord, bl, br, tl, tr); 
     289                const float3 sampleNormal = normalize(tex2Dlod(normalTex, float4(IN.texCoord * 4.0f, 0, 0)).xyz); 
     290 
     291 
     292                //////////////// 
     293                //-- compute contribution of sample using the direction and angle 
     294 
     295                float3 dirSample = samplePos - centerPosition; 
     296                const float lengthToSample = max(length(dirSample), 1e-6f); 
     297 
     298                dirSample /= lengthToSample; // normalize 
     299 
     300                // angle between current normal and direction to sample controls AO intensity. 
     301                float cosAngle = max(dot(sampleNormal, normal), .0f); 
     302         
     303                // the distance_scale offset is used to avoid singularity that occurs at global illumination when  
     304                // the distance to a sample approaches zero 
     305                const float aoContrib = SAMPLE_INTENSITY / (DISTANCE_SCALE + lengthToSample * lengthToSample); 
     306                //const float aoContrib = (1.0f > lengthToSample) ? occlusionPower(9e-2f, DISTANCE_SCALE + lengthToSample): .0f; 
     307 
     308#if 1 
     309                // if surface normal perpenticular to view dir, approx. half of the samples will not count 
     310                // => compensate for this (on the other hand, projected sampling area could be larger!) 
     311 
     312                const float viewCorrection = 1.0f + VIEW_CORRECTION_SCALE * max(dot(viewDir, normal), 0.0f); 
     313                total_ao += cosAngle * aoContrib * viewCorrection; 
     314#else 
     315                total_ao += cosAngle * aoContrib; 
     316#endif 
     317        } 
     318 
     319        return float2(max(0.0f, 1.0f - total_ao), numSamples); 
     320} 
     321 
     322 
     323/** The ssao shader returning the an intensity value between 0 and 1 
     324*/ 
    248325float2 ssao(fragment IN, 
    249326                        sampler2D colors, 
     
    258335                        float3 tr,  
    259336                        float3 viewDir 
    260                         , float2 noiseOffs 
    261                         , sampler2D noiseTex1D 
    262337                        ) 
    263338{ 
     
    269344        float numSamples = .0f; 
    270345 
    271  
    272         //float2 jitter = tex2Dlod(noiseTex1D, float4(IN.texCoord.x * 4.0f + noiseOffs.x, 0.5f, 0, 0)).xy; 
    273         //float2 jitter = tex2Dlod(noiseTex1D, float4(noiseOffs.x, 0.5f, 0, 0)).xy; 
    274  
    275346        for (int i = 0; i < NUM_SAMPLES; ++ i)  
    276347        { 
     
    281352                //-- add random noise: reflect around random normal vector (rather slow!) 
    282353 
    283                 //float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f + noiseOffs, 0, 0)).xy; 
    284                 float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord, .0f, .0f)).xy; 
    285                 //float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f, 0, 0)).xy; 
     354                float2 mynoise = tex2Dlod(noiseTex, float4(IN.texCoord * 4.0f, 0, 0)).xy; 
    286355                const float2 offsetTransformed = myreflect(offset, mynoise); 
    287356#else 
     
    294363                //if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f)) ++ numSamples; 
    295364                const float3 samplePos = ReconstructSamplePos(colors, texcoord, bl, br, tl, tr); 
    296  
     365                 
    297366 
    298367                //////////////// 
     
    325394        return float2(max(0.0f, 1.0f - total_ao), numSamples); 
    326395} 
     396 
    327397 
    328398 
     
    347417                   uniform float3 oldtl, 
    348418                   uniform float3 oldtr, 
    349                    uniform sampler2D attribsTex, 
    350                    uniform sampler2D noiseTex1D 
     419                   uniform sampler2D attribsTex 
    351420                   ) 
    352421{ 
     
    360429        const float4 eyeSpacePos = float4(-viewDir * eyeSpaceDepth, 1.0f); 
    361430 
    362         /*const float xoffs = 2.0f / 1024.0f; 
    363         const float yoffs = 2.0f / 768.0f; 
    364  
    365         //float3 id = tex2Dlod(attribsTex, float4(IN.texCoord, 0, 0)).xyz; 
    366         float3 x1 = tex2Dlod(attribsTex, float4(IN.texCoord, 0, 0)).xyz; 
    367         float3 x2 = tex2Dlod(attribsTex, float4(IN.texCoord + float2(xoffs, 0), 0, 0)).xyz; 
    368         float3 x3 = tex2Dlod(attribsTex, float4(IN.texCoord + float2(0, yoffs), 0, 0)).xyz; 
    369         float3 x4 = tex2Dlod(attribsTex, float4(IN.texCoord + float2(-xoffs, 0), 0, 0)).xyz; 
    370         float3 x5 = tex2Dlod(attribsTex, float4(IN.texCoord + float2(0, -yoffs), 0, 0)).xyz; 
    371  
    372         float3 diffVec = (x1+x2+x3+x4+x5) * .25f; 
    373 */ 
    374431        float3 diffVec = tex2Dlod(attribsTex, float4(IN.texCoord, 0, 0)).xyz; 
    375432         
     
    401458 
    402459        const float oldSsao = temporalVals.x; 
    403         //const float newWeight = clamp(temporalVals.y, 1.0f, temporalCoherence); 
    404460        const float newWeight = temporalVals.y; 
    405  
    406         //float2 noiseOffs = float2((temporalVals.y - 1)/ 139.0f, .0f); 
    407         float2 noiseOffs = float2(.0f); 
    408461 
    409462        float2 ao; 
     
    413466        { 
    414467                ao = ssao(IN, colors, noiseTex, samples, normal,  
    415                           eyeSpacePos.xyz, scaleFactor, bl, br, tl, tr, normalize(viewDir), noiseOffs, noiseTex1D); 
     468                          eyeSpacePos.xyz, scaleFactor, bl, br, tl, tr, normalize(viewDir)); 
    416469        } 
    417470        else 
Note: See TracChangeset for help on using the changeset viewer.