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

RevLine 
[2966]1#include "../shaderenv.h"
2
3
[2876]4struct fragment
5{
6         // normalized screen position
7        float4 pos: WPOS;
[3009]8        float2 texCoord: TEXCOORD0;
9        float3 view: TEXCOORD1;
[2876]10};
11
12
13struct pixel
14{
15        float4 color: COLOR0;
16};
17
18
[2944]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
[2876]30/** function for standard deferred shading
31*/
32float4 shade(fragment IN,
33                         uniform float4 color,
34                         uniform float3 normal,
[2959]35                         uniform float emmisive,
[2952]36                         float3 lightDir)
[2876]37{
[2954]38        // diffuse intensity
39        const float angle = saturate(dot(normal, lightDir));
40       
41        float4 lightDiffuse = glstate.light[0].diffuse;
42        float4 diffuse = angle * lightDiffuse;
[2876]43
44        // global ambient
[2954]45        const float4 ambient = glstate.light[0].ambient;
46       
[2968]47        float4 outColor;
[2967]48
[3005]49        // hack: prevent shading the sky
[3009]50        if (color.w > 1e19f) outColor = color;
[2984]51        //if (emmisive > 1.5f) outColor = color;
[2974]52        else outColor = (ambient + diffuse) * color;
[2968]53
54        return outColor;
[2876]55}
56
57
58
59/** The mrt shader for standard rendering
60*/
[2874]61pixel main(fragment IN,
62                   uniform sampler2D colors,
63                   uniform sampler2D positions,
[2952]64                   uniform sampler2D normals,
[2991]65                   uniform float3 lightDir
[2874]66                   )
67{
68        pixel OUT;
69
[3009]70        float4 norm = tex2D(normals, IN.texCoord);
71        float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
72       
[2874]73
74        // an ambient color term
[2974]75        float amb = color.w;
[2945]76        float3 normal = normalize(norm.xyz);
[3009]77        float4 col = shade(IN, color, normal, amb, lightDir);
[2874]78       
79        OUT.color = col;
[2985]80
[3018]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
[2874]87        return OUT;
[2876]88}
[2892]89
[2944]90
91float CalcShadowTerm(fragment IN,
92                                         uniform sampler2D shadowMap,
[3025]93                                         uniform float scale,
[2944]94                                         uniform float2 lightSpacePos,
[2952]95                                         uniform float depth,
[2966]96                                         uniform float2 samples[NUM_PCF_TABS],
[3025]97                                         uniform float weights[NUM_PCF_TABS],
[2944]98                                         uniform sampler2D noiseTexture
99                                         )
100{
[2954]101        //float shadowDepth = tex2D(shadowMap, lightSpacePos).x;
102        //return step(depth, shadowDepth);
[2944]103
[3025]104        float total_d = .0f;
105        float total_w = .0f;
106
[2966]107        for (int i = 0; i < NUM_PCF_TABS; ++ i)
[2944]108        {
109                const float2 offset = samples[i];
[3025]110                const float w = weights[i];
[2944]111
112#if 1
113                ////////////////////
114                //-- add random noise: reflect around random normal vector (warning: slow!)
115
[3009]116                float2 mynoise = tex2D(noiseTexture, IN.texCoord).xy;
[2944]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
[3025]122                float2 texcoord = lightSpacePos + offsetTransformed * scale;
[2944]123
124                float shadowDepth = tex2D(shadowMap, texcoord).x;
125
[3025]126                total_d += w * step(depth, shadowDepth);
127                total_w += w;
[2944]128        }
129
[3025]130        total_d /= (float)total_w;
[2944]131
132        return total_d;
133}
134
[3025]135
[3009]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);
[2944]141
[3009]142        return v;
143}
144
145
[2892]146pixel main_shadow(fragment IN,
147                                  uniform sampler2D colors,
148                                  uniform sampler2D positions,
149                                  uniform sampler2D normals,               
150                                  uniform sampler2D shadowMap,
[2893]151                                  uniform float4x4 shadowMatrix,
[2952]152                                  uniform float sampleWidth,
[2944]153                                  uniform sampler2D noiseTexture,
[2966]154                                  uniform float2 samples[NUM_PCF_TABS],
[3024]155                                  uniform float weights[NUM_PCF_TABS],
[3009]156                                  uniform float3 lightDir,
157                                  uniform float3 eyePos,
158                                  uniform float3 bl,
159                                  uniform float3 br,
160                                  uniform float3 tl,
161                                  uniform float3 tr
[2944]162                                  )
[2928]163{
164        pixel OUT;
[2944]165
166        float4 norm = tex2D(normals, IN.texCoord.xy);
[2945]167        const float3 normal = normalize(norm.xyz);
[2944]168
[3009]169        float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
170
171        /// reconstruct position from the eye space depth
172        float3 viewDir = IN.view;
[3018]173        const float lenView = length(viewDir);
174        viewDir /= lenView;
175
[3009]176        const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
177
178        float4 position;
179        position.xyz = eyePos - viewDir * eyeDepth;
180       
181       
[2945]182        // diffuse intensity
183        const float angle = saturate(dot(normal, lightDir));
[2959]184        const float4 lightDiffuse = glstate.light[0].diffuse;
[3017]185       
[2954]186        float4 diffuse = lightDiffuse * angle;
187
[3009]188        // hack: prevent shadowing the sky     
189        const bool useShading = (color.w < 1e19f);
190
[2945]191        // calc diffuse illumination + shadow term
[3009]192        if (useShading &&
193                (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
[2945]194                )
195        {
196                position.w = 1.0f;
[2944]197
[2945]198                float4 lightSpacePos = mul(shadowMatrix, position);
199                lightSpacePos /= lightSpacePos.w;
[2944]200
[3024]201                float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, weights, noiseTexture);
[3025]202                //float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noiseTexture);
[2944]203
[2945]204                diffuse *= shadowTerm;
[2944]205        }
[2945]206
[2974]207        // light ambient term
[2954]208        const float4 ambient = glstate.light[0].ambient;
[3009]209        // compute shading
210        OUT.color = useShading ? (ambient + diffuse) * color : color;
[2945]211
[3018]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;
[2991]214
[2928]215        return OUT;
216}
[2965]217
Note: See TracBrowser for help on using the repository browser.