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

RevLine 
[2876]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
[2944]15#define NUM_SAMPLES 16
[2876]16
17
[2944]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
[2876]29/** function for standard deferred shading
30*/
31float4 shade(fragment IN,
32                         uniform float4 color,
33                         uniform float4 position,
34                         uniform float3 normal,
[2952]35                         uniform float amb,
36                         float3 lightDir)
[2876]37{
[2945]38        /*
[2876]39        float4 lightDir2 = float4(-0.5f, 0.5f, 0.4f, 0.0f);
[2945]40        float3 light2 = normalize(lightDir2.xyz);
41        float diffuseLight2 = saturate(dot(normal, light2));
42*/
[2954]43        // diffuse intensity
44        const float angle = saturate(dot(normal, lightDir));
45       
46        float4 lightDiffuse = glstate.light[0].diffuse;
47        float4 diffuse = angle * lightDiffuse;
[2876]48
49        // global ambient
[2954]50        const float4 ambient = glstate.light[0].ambient;
51       
[2876]52        return (ambient + diffuse) * color * (1.0f - amb) + amb * color;
53}
54
55
56
57/** The mrt shader for standard rendering
58*/
[2874]59pixel main(fragment IN,
60                   uniform sampler2D colors,
61                   uniform sampler2D positions,
[2952]62                   uniform sampler2D normals,
63                   uniform float3 lightDir
[2874]64                   )
65{
66        pixel OUT;
67
68        float4 norm = tex2D(normals, IN.texCoord.xy);
[2884]69        float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
[2874]70        float4 position = tex2D(positions, IN.texCoord.xy);
71
72        // an ambient color term
73        float amb = norm.w;
74
[2945]75        float3 normal = normalize(norm.xyz);
[2874]76
[2952]77        float4 col = shade(IN, color, position, normal, amb, lightDir);
[2874]78       
79        OUT.color = col;
80        OUT.color.w = color.w;
81
82        return OUT;
[2876]83}
[2892]84
[2944]85
86float CalcShadowTerm(fragment IN,
87                                         uniform sampler2D shadowMap,
88                                         uniform float w,
89                                         uniform float2 lightSpacePos,
[2952]90                                         uniform float depth,
[2944]91                                         uniform float2 samples[NUM_SAMPLES],
92                                         uniform sampler2D noiseTexture
93                                         )
94{
[2954]95        //float shadowDepth = tex2D(shadowMap, lightSpacePos).x;
96        //return step(depth, shadowDepth);
[2944]97
[2954]98        float total_d = 0.0;
99       
[2944]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
[2945]118                total_d += step(depth, shadowDepth);
[2944]119        }
120
121        total_d /= (float)NUM_SAMPLES;
122
123        return total_d;
124}
125
126
[2892]127pixel main_shadow(fragment IN,
128                                  uniform sampler2D colors,
129                                  uniform sampler2D positions,
130                                  uniform sampler2D normals,               
131                                  uniform sampler2D shadowMap,
[2893]132                                  uniform float4x4 shadowMatrix,
[2895]133                                  uniform float maxDepth,
[2952]134                                  uniform float sampleWidth,
[2944]135                                  uniform sampler2D noiseTexture,
[2952]136                                  uniform float2 samples[NUM_SAMPLES],
137                                  uniform float3 lightDir
[2944]138                                  )
[2928]139{
140        pixel OUT;
[2944]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);
[2945]145       
146        const float3 normal = normalize(norm.xyz);
[2952]147       
[2945]148        // hack: an emmisive color term
149        float emmisive = norm.w;
[2944]150
[2945]151        // diffuse intensity
152        const float angle = saturate(dot(normal, lightDir));
[2954]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);
[2944]156
[2954]157        float4 diffuse = lightDiffuse * angle;
158
[2945]159        // calc diffuse illumination + shadow term
160        if ((emmisive < 0.95f) // hack: prevent shadowing the sky       
[2952]161                && (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
[2945]162                )
163        {
164                position *= maxDepth;
165                position.w = 1.0f;
[2944]166
[2945]167                float4 lightSpacePos = mul(shadowMatrix, position);
168                lightSpacePos /= lightSpacePos.w;
[2944]169
[2945]170                float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noiseTexture);
[2944]171
[2945]172                diffuse *= shadowTerm;
[2944]173        }
[2945]174
175        // global ambient
[2954]176        //const float4 ambient = 0.25f;
177        const float4 ambient = glstate.light[0].ambient;
[2944]178       
[2945]179        // base lighting
180        OUT.color = (ambient + diffuse) * color * (1.0f - emmisive) + emmisive * color;
181
[2944]182        // also write out depth component
183        OUT.color.w = color.w;
184
[2928]185        return OUT;
186}
Note: See TracBrowser for help on using the repository browser.