1 | //----------------------------------------------------------------------------------
|
---|
2 | // File: shadow_single_fragment.glsl
|
---|
3 | // Author: Rouslan Dimitrov
|
---|
4 | // Email: sdkfeedback@nvidia.com
|
---|
5 | // Cascaded shadows maps, single shadow sample
|
---|
6 | // Copyright (c) NVIDIA Corporation. All rights reserved.
|
---|
7 | //----------------------------------------------------------------------------------
|
---|
8 | #version 120
|
---|
9 | #extension GL_EXT_texture_array : enable
|
---|
10 |
|
---|
11 | uniform sampler2D tex;
|
---|
12 | uniform vec4 far_d;
|
---|
13 |
|
---|
14 | varying vec4 vPos;
|
---|
15 |
|
---|
16 | uniform sampler2DArray stex;
|
---|
17 | float shadowCoef()
|
---|
18 | {
|
---|
19 | int index = 3;
|
---|
20 |
|
---|
21 | // find the appropriate depth map to look up in based on the depth of this fragment
|
---|
22 | if(gl_FragCoord.z < far_d.x)
|
---|
23 | index = 0;
|
---|
24 | else if(gl_FragCoord.z < far_d.y)
|
---|
25 | index = 1;
|
---|
26 | else if(gl_FragCoord.z < far_d.z)
|
---|
27 | index = 2;
|
---|
28 |
|
---|
29 | // transform this fragment's position from view space to scaled light clip space
|
---|
30 | // such that the xy coordinates are in [0;1]
|
---|
31 | // note there is no need to divide by w for othogonal light sources
|
---|
32 | vec4 shadow_coord = gl_TextureMatrix[index]*vPos;
|
---|
33 |
|
---|
34 | shadow_coord.w = shadow_coord.z;
|
---|
35 |
|
---|
36 | // tell glsl in which layer to do the look up
|
---|
37 | shadow_coord.z = float(index);
|
---|
38 |
|
---|
39 | // get the stored depth
|
---|
40 | float shadow_d = texture2DArray(stex, shadow_coord.xyz).x;
|
---|
41 |
|
---|
42 | // get the difference of the stored depth and the distance of this fragment to the light
|
---|
43 | float diff = shadow_d - shadow_coord.w;
|
---|
44 |
|
---|
45 | // smoothen the result a bit, to avoid aliasing at shadow contact point
|
---|
46 | return clamp( diff*250.0 + 1.0, 0.0, 1.0);
|
---|
47 | }
|
---|
48 |
|
---|
49 | void main()
|
---|
50 | {
|
---|
51 | const float shadow_ambient = 0.9;
|
---|
52 | vec4 color_tex = texture2D(tex, gl_TexCoord[0].st);
|
---|
53 | float shadow_coef = shadowCoef();
|
---|
54 | float fog = clamp(gl_Fog.scale*(gl_Fog.end + vPos.z), 0.0, 1.0);
|
---|
55 | gl_FragColor = mix(gl_Fog.color, (shadow_ambient * shadow_coef * gl_Color * color_tex + (1.0 - shadow_ambient) * color_tex), fog);
|
---|
56 | } |
---|