Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/branches/Shader_HS18/programs/GLSL150/DepthShadowObjectFp.glsl @ 12115

Last change on this file since 12115 was 12115, checked in by wiesep, 5 years ago

Changed folder structure, deletet some unused files and cleaned up code

File size: 4.3 KB
Line 
1#version 150
2/* Copyright Torus Knot Software Ltd 2000-2014
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20THE SOFTWARE.
21*/
22
23in vec3 diffuseUV;
24uniform sampler2D diffuseMap;
25uniform vec3 materialAmbient;
26
27#if !SHADOWCASTER
28in vec3 col;
29#endif
30#if DEPTH_SHADOWCASTER
31in float depth;
32#endif
33
34#if DEPTH_SHADOWRECEIVER
35in vec4 lightSpacePos0;
36in vec4 lightSpacePos1;
37in vec4 lightSpacePos2;
38uniform sampler2D shadowMap0;
39uniform sampler2D shadowMap1;
40uniform sampler2D shadowMap2;
41uniform float inverseShadowmapSize0;
42uniform float inverseShadowmapSize1;
43uniform float inverseShadowmapSize2;
44uniform vec4 pssmSplitPoints;
45#endif
46       
47#if SHADOWCASTER
48uniform vec3 shadowColour;
49#endif
50#if FOG
51uniform vec3 fogColour;
52#endif
53
54out vec4 fragColour;
55
56// Number of samples in one dimension (square for total samples)
57#define NUM_SHADOW_SAMPLES_1D 2.0
58#define SHADOW_FILTER_SCALE 1
59
60#define SHADOW_SAMPLES NUM_SHADOW_SAMPLES_1D*NUM_SHADOW_SAMPLES_1D
61
62vec4 offsetSample(vec4 uv, vec2 offset, float invMapSize)
63{
64        return vec4(uv.xy + offset * invMapSize * uv.w, uv.z, uv.w);
65}
66
67float calcDepthShadow(sampler2D shadowMap, vec4 uv, float invShadowMapSize)
68{
69        // 4-sample PCF
70       
71        float shadow = 0.0;
72        float offset = (NUM_SHADOW_SAMPLES_1D/2 - 0.5) * SHADOW_FILTER_SCALE;
73        for (float y = -offset; y <= offset; y += SHADOW_FILTER_SCALE)
74                for (float x = -offset; x <= offset; x += SHADOW_FILTER_SCALE)
75                {
76                        float depth = textureProj(shadowMap, offsetSample(uv, vec2(x, y), invShadowMapSize)).x;
77                        if (depth >= 1 || depth >= uv.z)
78                                shadow += 1.0;
79                }
80
81        shadow /= SHADOW_SAMPLES;
82
83        return shadow;
84}
85
86float calcPSSMDepthShadow(sampler2D shadowMap0, sampler2D shadowMap1, sampler2D shadowMap2, 
87                                                   vec4 lsPos0, vec4 lsPos1, vec4 lsPos2,
88                                                   float invShadowmapSize0, float invShadowmapSize1, float invShadowmapSize2,
89                                                   vec4 pssmSplitPoints, float camDepth)
90{
91
92        float shadow;
93        vec4 splitColour;
94        // calculate shadow
95        if (camDepth <= pssmSplitPoints.y)
96        {
97                splitColour = vec4(0.3, 0.0, 0, 0);
98                shadow = calcDepthShadow(shadowMap0, lsPos0, invShadowmapSize0);
99        }
100        else if (camDepth <= pssmSplitPoints.z)
101        {
102                splitColour = vec4(0, 0.3, 0, 0);
103                shadow = calcDepthShadow(shadowMap1, lsPos1, invShadowmapSize1);
104        }
105        else
106        {
107                splitColour = vec4(0.0, 0.0, 0.3, 0);
108                shadow = calcDepthShadow(shadowMap2, lsPos2, invShadowmapSize2);
109        }
110
111        return shadow;
112}
113
114void main()
115{
116    // look up the diffuse map layer
117    vec4 texDiffuse = texture(diffuseMap, diffuseUV.xy);
118   
119#if SHADOWCASTER
120#  if DEPTH_SHADOWCASTER
121        // early-out with depth (we still include alpha for those cards that support it)
122        fragColour = vec4(depth, depth, depth, 1);
123#  else
124        fragColour = vec4(shadowColour.xyz, texDiffuse.a);
125#  endif
126
127#else
128    // compute the ambient contribution (pulled from the diffuse map)
129    vec3 vAmbient = texDiffuse.xyz * materialAmbient.xyz;
130    vec3 vColor3 = texDiffuse.rgb * col.rgb;
131
132#  if DEPTH_SHADOWRECEIVER
133        float camDepth = diffuseUV.z;
134        float shadow = calcPSSMDepthShadow(shadowMap0, shadowMap1, shadowMap2, 
135                lightSpacePos0, lightSpacePos1, lightSpacePos2,
136                inverseShadowmapSize0, inverseShadowmapSize1, inverseShadowmapSize2,
137                pssmSplitPoints, camDepth);
138        vColor3 *= shadow;
139#  endif
140
141    fragColour = vec4(vColor3 + vAmbient, texDiffuse.a);
142   
143#  if FOG
144    // if fog is active, interpolate between the unfogged color and the fog color
145    // based on vertex shader fog value
146    fragColour.rgb = mix(vColor.rgb, fogColour, diffuseUV.z).rgb;
147#  endif
148
149#endif
150}
Note: See TracBrowser for help on using the repository browser.