source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg @ 2884

Revision 2884, 5.5 KB checked in by mattausch, 16 years ago (diff)

found bug with lod levels
made env file for shaders

Line 
1////////////////////
2// SSAO + color bleeding shader
3// based on shader of Alexander Kusternig
4
5#include "../shaderenv.h"
6
7
8struct fragment
9{
10         // normalized screen position
11        float4 pos: WPOS;
12        float4 texCoord: TEXCOORD0;
13        float3 view: COLOR0;
14};
15
16
17
18struct pixel2
19{
20        float4 ssao_col: COLOR0;
21        float4 illum_col: COLOR1;
22};
23
24
25struct pixel
26{
27        float4 illum_col: COLOR0;
28};
29
30
31float2 reflect(float2 pt, float2 n)
32{
33        // distance to plane
34        float d = dot(n, pt);
35        // reflect around plane
36        float2 rpt = pt - d * 2.0f * n;
37        return rpt;
38}
39
40/** Computes  diffuse reflections + ambient occlusion
41*/
42float4 globIllum(fragment IN,
43                                 uniform sampler2D colors,
44                                 uniform sampler2D positions,
45                                 uniform sampler2D noiseTexture,
46                                 uniform float2 samples[NUM_SAMPLES],
47                                 uniform float3 currentNormal,
48                                 uniform float3 currentViewDir,
49                                 uniform float noiseMultiplier,
50                                 uniform float4 centerPosition
51                                 )
52{
53        // the w coordinate from the persp. projection
54        float w = centerPosition.w;
55
56        // Check in a circular area around the current position.
57        // Shoot vectors to the positions there, and check the angle to these positions.
58        // Summing up these angles gives an estimation of the occlusion at the current position.
59
60        // ao is in stored in the w component
61        float4 total_color = float4(0, 0, 0, 1);
62
63
64        ////////////
65        //-- the main sampling loop
66
67        for (int i = 0; i < NUM_SAMPLES; i ++)
68        {
69                float2 offset = samples[i];
70
71                //sample noisetex; r stores costheta, g stores sintheta
72                //float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f;
73                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy;
74
75                // rotation
76                float2 offsetTransformed = reflect(offset, mynoise);
77
78                // weight with projected coordinate to reach similar kernel size for near and far
79                float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
80
81                // use lower lod level to improve cache coherence
82                float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz;
83                float3 sample_color = tex2Dlod(colors, float4(texcoord, 0, 2)).xyz;
84
85                float3 vector_to_sample = sample_position - centerPosition.xyz;
86                const float length_to_sample = length(vector_to_sample);
87
88                float3 direction_to_sample = vector_to_sample / length_to_sample;
89
90                // Angle between current normal and direction to sample controls AO intensity.
91                float cos_angle = max(dot(direction_to_sample, currentNormal), 0);
92
93                // distance between current position and sample position controls AO intensity.
94                const float distance_intensity =
95                        (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);
96
97                // if normal perpenticular to view dir, only half of the samples count
98                const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
99
100                total_color.w -= cos_angle * distance_intensity * view_correction;
101
102                total_color.xyz += cos_angle * distance_intensity * view_correction * sample_color * ILLUM_INTENSITY;
103        }
104
105        return saturate(total_color);
106}
107
108
109/** The mrt shader for screen space ambient occlusion + indirect illumination
110*/
111pixel2 main(fragment IN,
112                   uniform sampler2D colors,
113                   uniform sampler2D positions,
114                   uniform sampler2D normals,
115                   uniform sampler2D noiseTexture,
116                   uniform float2 samples[NUM_SAMPLES],
117                   uniform float noiseMultiplier,
118                   uniform sampler2D oldSsaoTex,
119                   uniform sampler2D oldIllumTex,
120                   const uniform float4x4 oldModelViewProj,
121                   uniform float maxDepth,
122                   uniform float expFactor
123                   )
124{
125        pixel2 OUT;
126
127        float4 norm = tex2D(normals, IN.texCoord.xy);
128       
129        // the ambient term
130        const float amb = norm.w;
131
132        // expand normal
133        float3 normal = normalize(norm.xyz);
134        /// the current view direction
135        float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f));
136
137        // the current world position
138        const float4 centerPosition = tex2D(positions, IN.texCoord.xy);
139       
140        // the current color
141        const float4 currentCol = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
142        // the current depth is stored in the w component
143        const float currentDepth = currentCol.w;
144
145        float4 new_color = globIllum(IN, colors, positions, noiseTexture,
146                                 samples, normal, viewDir, noiseMultiplier, centerPosition);
147       
148
149        /////////////////
150        //-- compute temporally smoothing
151
152        float4 realPos = centerPosition * maxDepth;
153        realPos.w = 1.0f;
154
155        float4 oldPos = mul(oldModelViewProj, realPos);
156
157        const float newDepth = oldPos.z / oldPos.w;
158
159        float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
160
161        float4 oldSsao = tex2D(oldSsaoTex, tex);
162        float4 oldIllum = tex2D(oldIllumTex, tex);
163
164        const float oldDepth = oldSsao.w;
165        const float depthDif = 1.0f - newDepth / oldDepth;
166
167
168        if ((tex.x >= 0.0f) && (tex.x < 1.0f) &&
169                (tex.y >= 0.0f) && (tex.y < 1.0f) &&
170                (abs(depthDif)  < 1e-3f))
171        {
172                OUT.ssao_col.x = new_color.w * expFactor + oldSsao.x * (1.0f - expFactor);
173                OUT.illum_col = new_color * expFactor + oldIllum * (1.0f - expFactor);
174        }
175        else
176        {
177                OUT.ssao_col.x = new_color.w;
178                OUT.illum_col = new_color;
179        }
180
181        OUT.ssao_col.w = currentDepth;
182
183        return OUT;
184}
185
186
187pixel combine(fragment IN,
188                          uniform sampler2D colors,
189                          uniform sampler2D ssaoTex,
190                          uniform sampler2D illumTex
191                   )
192{
193        pixel OUT;
194
195        float4 col = tex2D(colors, IN.texCoord.xy);
196        float ao = tex2D(ssaoTex, IN.texCoord.xy).x;
197        float4 illum = tex2D(illumTex, IN.texCoord.xy);
198       
199        OUT.illum_col = (col + illum) * ao;
200        OUT.illum_col.w = col.w;
201
202        return OUT;
203}
Note: See TracBrowser for help on using the repository browser.