Ignore:
Timestamp:
08/27/08 15:09:04 (16 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.