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

Revision 2954, 4.4 KB checked in by mattausch, 16 years ago (diff)

implemented sun color

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