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

Revision 2887, 5.7 KB checked in by mattausch, 16 years ago (diff)

made changes to view transformation but still not working

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                //total_color.w -= cos_angle * distance_intensity * view_correction;
100                //total_color.xyz += cos_angle * distance_intensity * view_correction * sample_color * ILLUM_INTENSITY;
101
102                total_color.w -= cos_angle * distance_intensity;
103                total_color.xyz += cos_angle * distance_intensity * sample_color * ILLUM_INTENSITY;
104        }
105
106        return saturate(total_color);
107}
108
109
110/** The mrt shader for screen space ambient occlusion + indirect illumination
111*/
112pixel2 main(fragment IN,
113                   uniform sampler2D colors,
114                   uniform sampler2D positions,
115                   uniform sampler2D normals,
116                   uniform sampler2D noiseTexture,
117                   uniform float2 samples[NUM_SAMPLES],
118                   uniform float noiseMultiplier,
119                   uniform sampler2D oldSsaoTex,
120                   uniform sampler2D oldIllumTex,
121                   const uniform float4x4 oldModelViewProj,
122                   uniform float maxDepth,
123                   uniform float expFactor
124                   )
125{
126        pixel2 OUT;
127
128        float4 norm = tex2D(normals, IN.texCoord.xy);
129       
130        // the ambient term
131        const float amb = norm.w;
132
133        // expand normal
134        float3 normal = normalize(norm.xyz);
135        /// the current view direction
136        float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f));
137
138        // the current world position
139        const float4 centerPosition = tex2D(positions, IN.texCoord.xy);
140       
141        // the current color
142        const float4 currentCol = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
143        // the current depth is stored in the w component
144        const float currentDepth = currentCol.w;
145
146        float4 new_color = globIllum(IN, colors, positions, noiseTexture,
147                                 samples, normal, viewDir, noiseMultiplier, centerPosition);
148       
149
150        /////////////////
151        //-- compute temporally smoothing
152
153        float4 realPos = centerPosition * maxDepth;
154        realPos.w = 1.0f;
155
156        float4 oldPos = mul(oldModelViewProj, realPos);
157
158        const float newDepth = oldPos.z / oldPos.w;
159
160        float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
161
162        float4 oldSsao = tex2D(oldSsaoTex, tex);
163        float4 oldIllum = tex2D(oldIllumTex, tex);
164
165        const float oldDepth = oldSsao.w;
166        const float depthDif = 1.0f - newDepth / oldDepth;
167
168
169        if ((tex.x >= 0.0f) && (tex.x < 1.0f) &&
170                (tex.y >= 0.0f) && (tex.y < 1.0f) &&
171                (abs(depthDif)  < 1e-3f))
172        {
173                OUT.ssao_col.x = new_color.w * expFactor + oldSsao.x * (1.0f - expFactor);
174                OUT.illum_col = new_color * expFactor + oldIllum * (1.0f - expFactor);
175        }
176        else
177        {
178                OUT.ssao_col.x = new_color.w;
179                OUT.illum_col = new_color;
180        }
181
182        OUT.ssao_col.w = currentDepth;
183
184        return OUT;
185}
186
187
188pixel combine(fragment IN,
189                          uniform sampler2D colors,
190                          uniform sampler2D ssaoTex,
191                          uniform sampler2D illumTex
192                   )
193{
194        pixel OUT;
195
196        float4 col = tex2D(colors, IN.texCoord.xy);
197        float ao = tex2D(ssaoTex, IN.texCoord.xy).x;
198        float4 illum = tex2D(illumTex, IN.texCoord.xy);
199       
200        OUT.illum_col = (col + illum) * ao;
201        OUT.illum_col.w = col.w;
202
203        return OUT;
204}
Note: See TracBrowser for help on using the repository browser.