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

Revision 3025, 5.2 KB checked in by mattausch, 16 years ago (diff)

worked on cg shader wrapper class

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 scale,
94                                         uniform float2 lightSpacePos,
95                                         uniform float depth,
96                                         uniform float2 samples[NUM_PCF_TABS],
97                                         uniform float weights[NUM_PCF_TABS],
98                                         uniform sampler2D noiseTexture
99                                         )
100{
101        //float shadowDepth = tex2D(shadowMap, lightSpacePos).x;
102        //return step(depth, shadowDepth);
103
104        float total_d = .0f;
105        float total_w = .0f;
106
107        for (int i = 0; i < NUM_PCF_TABS; ++ i)
108        {
109                const float2 offset = samples[i];
110                const float w = weights[i];
111
112#if 1
113                ////////////////////
114                //-- add random noise: reflect around random normal vector (warning: slow!)
115
116                float2 mynoise = tex2D(noiseTexture, IN.texCoord).xy;
117                const float2 offsetTransformed = myreflect(offset, mynoise);
118#else
119                const float2 offsetTransformed = offset;
120#endif
121                // weight with projected coordinate to reach similar kernel size for near and far
122                float2 texcoord = lightSpacePos + offsetTransformed * scale;
123
124                float shadowDepth = tex2D(shadowMap, texcoord).x;
125
126                total_d += w * step(depth, shadowDepth);
127                total_w += w;
128        }
129
130        total_d /= (float)total_w;
131
132        return total_d;
133}
134
135
136inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
137{
138        float3 x1 = lerp(bl, tl, w.y);
139        float3 x2 = lerp(br, tr, w.y);
140        float3 v = lerp(x1, x2, w.x);
141
142        return v;
143}
144
145
146pixel main_shadow(fragment IN,
147                                  uniform sampler2D colors,
148                                  uniform sampler2D positions,
149                                  uniform sampler2D normals,               
150                                  uniform sampler2D shadowMap,
151                                  uniform float4x4 shadowMatrix,
152                                  uniform float sampleWidth,
153                                  uniform sampler2D noiseTexture,
154                                  uniform float2 samples[NUM_PCF_TABS],
155                                  uniform float weights[NUM_PCF_TABS],
156                                  uniform float3 lightDir,
157                                  uniform float3 eyePos,
158                                  uniform float3 bl,
159                                  uniform float3 br,
160                                  uniform float3 tl,
161                                  uniform float3 tr
162                                  )
163{
164        pixel OUT;
165
166        float4 norm = tex2D(normals, IN.texCoord.xy);
167        const float3 normal = normalize(norm.xyz);
168
169        float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
170
171        /// reconstruct position from the eye space depth
172        float3 viewDir = IN.view;
173        const float lenView = length(viewDir);
174        viewDir /= lenView;
175
176        const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
177
178        float4 position;
179        position.xyz = eyePos - viewDir * eyeDepth;
180       
181       
182        // diffuse intensity
183        const float angle = saturate(dot(normal, lightDir));
184        const float4 lightDiffuse = glstate.light[0].diffuse;
185       
186        float4 diffuse = lightDiffuse * angle;
187
188        // hack: prevent shadowing the sky     
189        const bool useShading = (color.w < 1e19f);
190
191        // calc diffuse illumination + shadow term
192        if (useShading &&
193                (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
194                )
195        {
196                position.w = 1.0f;
197
198                float4 lightSpacePos = mul(shadowMatrix, position);
199                lightSpacePos /= lightSpacePos.w;
200
201                float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, weights, noiseTexture);
202                //float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noiseTexture);
203
204                diffuse *= shadowTerm;
205        }
206
207        // light ambient term
208        const float4 ambient = glstate.light[0].ambient;
209        // compute shading
210        OUT.color = useShading ? (ambient + diffuse) * color : color;
211
212        // store scaled view vector from now on so wie don't have to normalize for e.g., ssao
213        OUT.color.w = color.w / lenView;
214
215        return OUT;
216}
217
Note: See TracBrowser for help on using the repository browser.