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

Revision 2959, 4.5 KB checked in by mattausch, 16 years ago (diff)

preetham working but big hack

Line 
1struct fragment
2{
3         // normalized screen position
4        float4 pos: WPOS;
5        float4 texCoord: TEXCOORD0;
6        float3 view: COLOR0;
7};
8
9
10struct pixel
11{
12        float4 color: COLOR0;
13};
14
15#define NUM_SAMPLES 16
16
17
18float2 myreflect(float2 pt, float2 n)
19{
20        // distance to plane
21        float d = dot(n, pt);
22        // reflect around plane
23        float2 rpt = pt - d * 2.0f * n;
24
25        return rpt;
26}
27
28
29/** function for standard deferred shading
30*/
31float4 shade(fragment IN,
32                         uniform float4 color,
33                         uniform float4 position,
34                         uniform float3 normal,
35                         uniform float emmisive,
36                         float3 lightDir)
37{
38        /*
39        float4 lightDir2 = float4(-0.5f, 0.5f, 0.4f, 0.0f);
40        float3 light2 = normalize(lightDir2.xyz);
41        float diffuseLight2 = saturate(dot(normal, light2));
42*/
43        // diffuse intensity
44        const float angle = saturate(dot(normal, lightDir));
45       
46        float4 lightDiffuse = glstate.light[0].diffuse;
47        float4 diffuse = angle * lightDiffuse;
48
49        // global ambient
50        const float4 ambient = glstate.light[0].ambient;
51       
52        //return (saturate(((ambient + diffuse))) * (1.0f - emmisive) + emmisive) * color;
53        return saturate((((ambient + diffuse)) * (1.0f - emmisive) + emmisive) * color);
54}
55
56
57
58/** The mrt shader for standard rendering
59*/
60pixel main(fragment IN,
61                   uniform sampler2D colors,
62                   uniform sampler2D positions,
63                   uniform sampler2D normals,
64                   uniform float3 lightDir
65                   )
66{
67        pixel OUT;
68
69        float4 norm = tex2D(normals, IN.texCoord.xy);
70        float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
71        float4 position = tex2D(positions, IN.texCoord.xy);
72
73        // an ambient color term
74        float amb = norm.w;
75
76        float3 normal = normalize(norm.xyz);
77
78        float4 col = shade(IN, color, position, normal, amb, lightDir);
79       
80        OUT.color = col;
81        OUT.color.w = color.w;
82
83        return OUT;
84}
85
86
87float CalcShadowTerm(fragment IN,
88                                         uniform sampler2D shadowMap,
89                                         uniform float w,
90                                         uniform float2 lightSpacePos,
91                                         uniform float depth,
92                                         uniform float2 samples[NUM_SAMPLES],
93                                         uniform sampler2D noiseTexture
94                                         )
95{
96        //float shadowDepth = tex2D(shadowMap, lightSpacePos).x;
97        //return step(depth, shadowDepth);
98
99        float total_d = 0.0;
100       
101        for (int i = 0; i < NUM_SAMPLES; ++ i)
102        {
103                const float2 offset = samples[i];
104
105#if 1
106                ////////////////////
107                //-- add random noise: reflect around random normal vector (warning: slow!)
108
109                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy).xy;
110                const float2 offsetTransformed = myreflect(offset, mynoise);
111#else
112                const float2 offsetTransformed = offset;
113#endif
114                // weight with projected coordinate to reach similar kernel size for near and far
115                float2 texcoord = lightSpacePos + offsetTransformed * w;
116
117                float shadowDepth = tex2D(shadowMap, texcoord).x;
118
119                total_d += step(depth, shadowDepth);
120        }
121
122        total_d /= (float)NUM_SAMPLES;
123
124        return total_d;
125}
126
127
128pixel main_shadow(fragment IN,
129                                  uniform sampler2D colors,
130                                  uniform sampler2D positions,
131                                  uniform sampler2D normals,               
132                                  uniform sampler2D shadowMap,
133                                  uniform float4x4 shadowMatrix,
134                                  uniform float maxDepth,
135                                  uniform float sampleWidth,
136                                  uniform sampler2D noiseTexture,
137                                  uniform float2 samples[NUM_SAMPLES],
138                                  uniform float3 lightDir
139                                  )
140{
141        pixel OUT;
142
143        float4 norm = tex2D(normals, IN.texCoord.xy);
144        float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
145        float4 position = tex2D(positions, IN.texCoord.xy);
146       
147        const float3 normal = normalize(norm.xyz);
148       
149        // hack: an emmisive color term
150        float emmisive = norm.w;
151
152        // diffuse intensity
153        const float angle = saturate(dot(normal, lightDir));
154        //float4 lightDiffuse = float4(1.0f, 1.0f, 0.9f, 1.0f);
155        const float4 lightDiffuse = glstate.light[0].diffuse;
156        //float4(1.0f, 1.0f, 0.9f, 1.0f);
157
158        float4 diffuse = lightDiffuse * angle;
159
160        // calc diffuse illumination + shadow term
161        if ((emmisive < 0.95f) // hack: prevent shadowing the sky       
162                && (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
163                )
164        {
165                position *= maxDepth;
166                position.w = 1.0f;
167
168                float4 lightSpacePos = mul(shadowMatrix, position);
169                lightSpacePos /= lightSpacePos.w;
170
171                float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noiseTexture);
172
173                diffuse *= shadowTerm;
174        }
175
176        // global ambient
177        //const float4 ambient = 0.25f;
178        const float4 ambient = glstate.light[0].ambient;
179       
180        // base lighting
181        OUT.color = (emmisive < 0.95) ? (ambient + diffuse) * color : color;
182
183        // also write out depth component
184        OUT.color.w = color.w;
185
186        return OUT;
187}
Note: See TracBrowser for help on using the repository browser.