source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg @ 3018

Revision 3018, 4.9 KB checked in by mattausch, 16 years ago (diff)
Line 
1#include "../shaderenv.h"
2
3
4struct fragment
5{
6         // normalized screen position
7        float4 pos: WPOS;
8        float2 texCoord: TEXCOORD0;
9        float3 view: TEXCOORD1;
10};
11
12
13struct pixel
14{
15        float4 color: COLOR0;
16};
17
18
19float2 myreflect(float2 pt, float2 n)
20{
21        // distance to plane
22        float d = dot(n, pt);
23        // reflect around plane
24        float2 rpt = pt - d * 2.0f * n;
25
26        return rpt;
27}
28
29
30/** function for standard deferred shading
31*/
32float4 shade(fragment IN,
33                         uniform float4 color,
34                         uniform float3 normal,
35                         uniform float emmisive,
36                         float3 lightDir)
37{
38        // diffuse intensity
39        const float angle = saturate(dot(normal, lightDir));
40       
41        float4 lightDiffuse = glstate.light[0].diffuse;
42        float4 diffuse = angle * lightDiffuse;
43
44        // global ambient
45        const float4 ambient = glstate.light[0].ambient;
46       
47        float4 outColor;
48
49        // hack: prevent shading the sky
50        if (color.w > 1e19f) outColor = color;
51        //if (emmisive > 1.5f) outColor = color;
52        else outColor = (ambient + diffuse) * color;
53
54        return outColor;
55}
56
57
58
59/** The mrt shader for standard rendering
60*/
61pixel main(fragment IN,
62                   uniform sampler2D colors,
63                   uniform sampler2D positions,
64                   uniform sampler2D normals,
65                   uniform float3 lightDir
66                   )
67{
68        pixel OUT;
69
70        float4 norm = tex2D(normals, IN.texCoord);
71        float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
72       
73
74        // an ambient color term
75        float amb = color.w;
76        float3 normal = normalize(norm.xyz);
77        float4 col = shade(IN, color, normal, amb, lightDir);
78       
79        OUT.color = col;
80
81        // store scaled view vector from now on so wie don't have to normalize for e.g., ssao
82        float3 viewDir = IN.view;
83        const float lenView = length(viewDir);
84
85        OUT.color.w = color.w / lenView;
86
87        return OUT;
88}
89
90
91float CalcShadowTerm(fragment IN,
92                                         uniform sampler2D shadowMap,
93                                         uniform float w,
94                                         uniform float2 lightSpacePos,
95                                         uniform float depth,
96                                         uniform float2 samples[NUM_PCF_TABS],
97                                         uniform sampler2D noiseTexture
98                                         )
99{
100        //float shadowDepth = tex2D(shadowMap, lightSpacePos).x;
101        //return step(depth, shadowDepth);
102
103        float total_d = 0.0;
104       
105        for (int i = 0; i < NUM_PCF_TABS; ++ i)
106        {
107                const float2 offset = samples[i];
108
109#if 1
110                ////////////////////
111                //-- add random noise: reflect around random normal vector (warning: slow!)
112
113                float2 mynoise = tex2D(noiseTexture, IN.texCoord).xy;
114                const float2 offsetTransformed = myreflect(offset, mynoise);
115#else
116                const float2 offsetTransformed = offset;
117#endif
118                // weight with projected coordinate to reach similar kernel size for near and far
119                float2 texcoord = lightSpacePos + offsetTransformed * w;
120
121                float shadowDepth = tex2D(shadowMap, texcoord).x;
122
123                total_d += step(depth, shadowDepth);
124        }
125
126        total_d /= (float)NUM_PCF_TABS;
127
128        return total_d;
129}
130
131inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
132{
133        float3 x1 = lerp(bl, tl, w.y);
134        float3 x2 = lerp(br, tr, w.y);
135        float3 v = lerp(x1, x2, w.x);
136
137        return v;
138}
139
140
141pixel main_shadow(fragment IN,
142                                  uniform sampler2D colors,
143                                  uniform sampler2D positions,
144                                  uniform sampler2D normals,               
145                                  uniform sampler2D shadowMap,
146                                  uniform float4x4 shadowMatrix,
147                                  uniform float sampleWidth,
148                                  uniform sampler2D noiseTexture,
149                                  uniform float2 samples[NUM_PCF_TABS],
150                                  uniform float3 lightDir,
151                                  uniform float3 eyePos,
152                                  uniform float3 bl,
153                                  uniform float3 br,
154                                  uniform float3 tl,
155                                  uniform float3 tr
156                                  )
157{
158        pixel OUT;
159
160        float4 norm = tex2D(normals, IN.texCoord.xy);
161        const float3 normal = normalize(norm.xyz);
162
163        float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
164
165        /// reconstruct position from the eye space depth
166        float3 viewDir = IN.view;
167        const float lenView = length(viewDir);
168        viewDir /= lenView;
169
170        const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
171
172        float4 position;
173        position.xyz = eyePos - viewDir * eyeDepth;
174       
175       
176        // diffuse intensity
177        const float angle = saturate(dot(normal, lightDir));
178        const float4 lightDiffuse = glstate.light[0].diffuse;
179       
180        float4 diffuse = lightDiffuse * angle;
181
182        // hack: prevent shadowing the sky     
183        const bool useShading = (color.w < 1e19f);
184
185        // calc diffuse illumination + shadow term
186        if (useShading &&
187                (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
188                )
189        {
190                position.w = 1.0f;
191
192                float4 lightSpacePos = mul(shadowMatrix, position);
193                lightSpacePos /= lightSpacePos.w;
194
195                float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noiseTexture);
196
197                diffuse *= shadowTerm;
198        }
199
200        // light ambient term
201        const float4 ambient = glstate.light[0].ambient;
202        // compute shading
203        OUT.color = useShading ? (ambient + diffuse) * color : color;
204
205        // store scaled view vector from now on so wie don't have to normalize for e.g., ssao
206        OUT.color.w = color.w / lenView;
207
208        return OUT;
209}
210
Note: See TracBrowser for help on using the repository browser.