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

Revision 2968, 4.5 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        float4 texCoord: TEXCOORD0;
9        float3 view: COLOR0;
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 float4 position,
35                         uniform float3 normal,
36                         uniform float emmisive,
37                         float3 lightDir)
38{
39        // diffuse intensity
40        const float angle = saturate(dot(normal, lightDir));
41       
42        float4 lightDiffuse = glstate.light[0].diffuse;
43        float4 diffuse = angle * lightDiffuse;
44
45        // global ambient
46        const float4 ambient = glstate.light[0].ambient;
47       
48        float4 outColor;
49
50        if (emmisive > 1.5) outColor = color;
51        //else if (emmisive > 0.95)     outColor = color * lightDiffuse;
52        else
53                outColor = (ambient + diffuse) * color;
54
55        return outColor;
56        //return saturate((((ambient + diffuse)) * (1.0f - emmisive) + emmisive) * color);
57}
58
59
60
61/** The mrt shader for standard rendering
62*/
63pixel main(fragment IN,
64                   uniform sampler2D colors,
65                   uniform sampler2D positions,
66                   uniform sampler2D normals,
67                   uniform float3 lightDir
68                   )
69{
70        pixel OUT;
71
72        float4 norm = tex2D(normals, IN.texCoord.xy);
73        float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
74        float4 position = tex2D(positions, IN.texCoord.xy);
75
76        // an ambient color term
77        float amb = norm.w;
78
79        float3 normal = normalize(norm.xyz);
80
81        float4 col = shade(IN, color, position, normal, amb, lightDir);
82       
83        OUT.color = col;
84        //OUT.color = float4(100.0, 1, 1, 0);
85        OUT.color.w = color.w;
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).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
131
132pixel main_shadow(fragment IN,
133                                  uniform sampler2D colors,
134                                  uniform sampler2D positions,
135                                  uniform sampler2D normals,               
136                                  uniform sampler2D shadowMap,
137                                  uniform float4x4 shadowMatrix,
138                                  uniform float maxDepth,
139                                  uniform float sampleWidth,
140                                  uniform sampler2D noiseTexture,
141                                  uniform float2 samples[NUM_PCF_TABS],
142                                  uniform float3 lightDir
143                                  )
144{
145        pixel OUT;
146
147        float4 norm = tex2D(normals, IN.texCoord.xy);
148        float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
149        float4 position = tex2D(positions, IN.texCoord.xy);
150       
151        const float3 normal = normalize(norm.xyz);
152       
153        // hack: an emmisive color term
154        float emmisive = norm.w;
155
156        // diffuse intensity
157        const float angle = saturate(dot(normal, lightDir));
158        //float4 lightDiffuse = float4(1.0f, 1.0f, 0.9f, 1.0f);
159        const float4 lightDiffuse = glstate.light[0].diffuse;
160        //float4(1.0f, 1.0f, 0.9f, 1.0f);
161
162        float4 diffuse = lightDiffuse * angle;
163
164        // calc diffuse illumination + shadow term
165        if ((emmisive < 0.95f) // hack: prevent shadowing the sky       
166                && (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
167                )
168        {
169                position *= maxDepth;
170                position.w = 1.0f;
171
172                float4 lightSpacePos = mul(shadowMatrix, position);
173                lightSpacePos /= lightSpacePos.w;
174
175                float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noiseTexture);
176
177                diffuse *= shadowTerm;
178        }
179
180        // global ambient
181        //const float4 ambient = 0.25f;
182        const float4 ambient = glstate.light[0].ambient;
183       
184        // base lighting
185        OUT.color = (emmisive > 1.5f) ? color: (ambient + diffuse) * color;
186
187        // also write out depth component
188        OUT.color.w = color.w;
189
190        return OUT;
191}
192
Note: See TracBrowser for help on using the repository browser.