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

Revision 2975, 5.0 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 (color.w > 1e19f) outColor = color;
51        if (emmisive > 1.5f) outColor = color;
52        else outColor = (ambient + diffuse) * color;
53
54        return outColor;
55        //return saturate((((ambient + diffuse)) * (1.0f - emmisive) + emmisive) * color);
56}
57
58
59
60/** The mrt shader for standard rendering
61*/
62pixel main(fragment IN,
63                   uniform sampler2D colors,
64                   uniform sampler2D positions,
65                   uniform sampler2D normals,
66                   uniform float3 lightDir
67                   )
68{
69        pixel OUT;
70
71        float4 norm = tex2D(normals, IN.texCoord.xy);
72        float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
73        float4 position = tex2D(positions, IN.texCoord.xy);
74
75        // an ambient color term
76        float amb = color.w;
77
78        float3 normal = normalize(norm.xyz);
79
80        float4 col = shade(IN, color, position, normal, amb, lightDir);
81       
82        OUT.color = col;
83       
84
85        ////////////
86        //-- write out logaritmic luminance for tone mapping
87
88        const float3 w = float3(0.299f, 0.587f, 0.114f);
89
90        float lum = dot(OUT.color.rgb, w);
91        float logLum = log(1e-5f + lum);
92
93        float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
94        float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
95
96        OUT.color.w = logLumScaled;
97
98        return OUT;
99}
100
101
102float CalcShadowTerm(fragment IN,
103                                         uniform sampler2D shadowMap,
104                                         uniform float w,
105                                         uniform float2 lightSpacePos,
106                                         uniform float depth,
107                                         uniform float2 samples[NUM_PCF_TABS],
108                                         uniform sampler2D noiseTexture
109                                         )
110{
111        //float shadowDepth = tex2D(shadowMap, lightSpacePos).x;
112        //return step(depth, shadowDepth);
113
114        float total_d = 0.0;
115       
116        for (int i = 0; i < NUM_PCF_TABS; ++ i)
117        {
118                const float2 offset = samples[i];
119
120#if 1
121                ////////////////////
122                //-- add random noise: reflect around random normal vector (warning: slow!)
123
124                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy).xy;
125                const float2 offsetTransformed = myreflect(offset, mynoise);
126#else
127                const float2 offsetTransformed = offset;
128#endif
129                // weight with projected coordinate to reach similar kernel size for near and far
130                float2 texcoord = lightSpacePos + offsetTransformed * w;
131
132                float shadowDepth = tex2D(shadowMap, texcoord).x;
133
134                total_d += step(depth, shadowDepth);
135        }
136
137        total_d /= (float)NUM_PCF_TABS;
138
139        return total_d;
140}
141
142
143pixel main_shadow(fragment IN,
144                                  uniform sampler2D colors,
145                                  uniform sampler2D positions,
146                                  uniform sampler2D normals,               
147                                  uniform sampler2D shadowMap,
148                                  uniform float4x4 shadowMatrix,
149                                  uniform float maxDepth,
150                                  uniform float sampleWidth,
151                                  uniform sampler2D noiseTexture,
152                                  uniform float2 samples[NUM_PCF_TABS],
153                                  uniform float3 lightDir
154                                  )
155{
156        pixel OUT;
157
158        float4 norm = tex2D(normals, IN.texCoord.xy);
159        float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
160        float4 position = tex2D(positions, IN.texCoord.xy);
161       
162        const float3 normal = normalize(norm.xyz);
163       
164        // hack: an emmisive color term
165        float emmisive = color.w;
166
167        // diffuse intensity
168        const float angle = saturate(dot(normal, lightDir));
169        //float4 lightDiffuse = float4(1.0f, 1.0f, 0.9f, 1.0f);
170        const float4 lightDiffuse = glstate.light[0].diffuse;
171        //float4(1.0f, 1.0f, 0.9f, 1.0f);
172
173        float4 diffuse = lightDiffuse * angle;
174
175        // calc diffuse illumination + shadow term
176        if ((emmisive < 1.5f) // hack: prevent shadowing the sky       
177                && (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
178                )
179        {
180                position *= maxDepth;
181                position.w = 1.0f;
182
183                float4 lightSpacePos = mul(shadowMatrix, position);
184                lightSpacePos /= lightSpacePos.w;
185
186                float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noiseTexture);
187
188                diffuse *= shadowTerm;
189        }
190
191        // light ambient term
192        const float4 ambient = glstate.light[0].ambient;
193       
194        // base lighting
195        OUT.color = (emmisive > 1.5f) ? color: (ambient + diffuse) * color;
196
197        ////////////
198        //-- write out logaritmic luminance for tone mapping
199
200        const float3 w = float3(0.299f, 0.587f, 0.114f);
201
202        float lum = dot(OUT.color.rgb, w);
203        float logLum = log(1e-5f + lum);
204
205        float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
206        float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
207
208        OUT.color.w = logLumScaled;
209       
210        return OUT;
211}
212
Note: See TracBrowser for help on using the repository browser.