shader renamed
[dd2d.git] / data / shaders / srlight_blur.frag
blob2296e84d51592d7b8b46be6d17826c0083fe8ae5
1 /* DooM2D: Midnight on the Firing Line
2  * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3  * Understanding is not required. Only obedience.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 #version 130
20 //#define DEBUG_RECT
21 //#define DEBUG_RECT1
23 #define PI 3.1415926
25 uniform sampler2D texDist; // 1D distance
26 uniform sampler2D texBg; // background
27 uniform sampler2D texOcc; // occluders
28 uniform sampler2D texOccSmall; // occluders, small map
29 uniform vec2 lightTexSize; // x: size of distmap; y: size of this texture
30 uniform vec2 mapPixSize;
31 uniform vec4 lightColor;
32 uniform vec2 lightPos; // lefttop
33 #define SHADOW_DO_BLUR
35 #define SOFT_SHADOWS  (1.0f)
38 // sample from the 1D distance map
39 #define sample(cc_)  step(r, texture2D(texDist, vec2(tc.x+(cc_)*blur, /*tc.y*/0.0)).r)
41 //gl_FragCoord.x: [0..lightTexSize.x)
42 //gl_FragCoord.y: somehow it is constant; wtf?!
44 void main (void) {
45   vec2 vTexCoord0 = gl_TexCoord[0].xy;
46   // don't even ask me!
47   vec2 btxy = vec2(lightPos.x+gl_FragCoord.x, lightPos.y+gl_TexCoord[0].y*lightTexSize.x);
48   btxy.y = mapPixSize.y-btxy.y;
49   btxy /= mapPixSize;
50   vec2 ocxy = vec2(btxy.x, 1.0-btxy.y);
51   vec4 color;
53   if (texture2D(texOcc, ocxy).a > 0.75) {
54     // occluder hit, this will take care of lighted walls (we'll fix wall in next pass)
55     color = vec4(0.0, 0.0, 0.0, 0.0);
56   } else {
57     float ltsxi = 1.0/lightTexSize.x;
59     // rectangular to polar
60     vec2 norm = vTexCoord0.st*2.0-1.0;
61     float theta = atan(norm.y, norm.x);
62     float r = length(norm);
63     float coord = (theta+PI)/(2.0*PI);
65     // the texDist coord to sample our 1D lookup texture
66     // always 0.0 on y axis
67     vec2 tc = vec2(coord, 0.0);
69     // the center texDist coord, which gives us hard shadows
70     float center = step(r, texture2D(texDist, tc.xy).r); //sample(vec2(tc.x, tc.y), r);
72     // now we use a simple gaussian blur
73     #ifdef SHADOW_DO_BLUR
74       // we multiply the blur amount by our distance from center
75       // this leads to more blurriness as the shadow "fades away"
76       float blur = ltsxi*smoothstep(0.0, 1.0, r);
78       float sum  = sample(-4.0)*0.05;
79             sum += sample(-3.0)*0.09;
80             sum += sample(-2.0)*0.12;
81             sum += sample(-1.0)*0.15;
82             sum += center*0.16;
83             sum += sample( 1.0)*0.15;
84             sum += sample( 2.0)*0.12;
85             sum += sample( 3.0)*0.09;
86             sum += sample( 4.0)*0.05;
87       // sum of 1.0 -> in light, 0.0 -> in shadow
88       float lit = mix(center, sum, SOFT_SHADOWS);
89     #else
90       float lit = center;
91     #endif
93     // multiply the summed amount by our distance, which gives us a radial falloff
94     lit = lit*smoothstep(1.0, 0.0, r);
96     if (lit == 0.0) {
97 #ifdef DEBUG_RECT
98       color = texture2D(texBg, btxy);
99       color.b = 1.0;
100       color.a = 0.4;
101 #else
102       color = vec4(0.0, 0.0, 0.0, 0.0);
103 #endif
104     } else {
105       // has some light here
106       vec4 bcolor = texture2D(texBg, btxy);
107 #ifdef DEBUG_RECT
108       color = bcolor;
109       color.b = 1.0;
110       color.a = 0.4;
111 #else
112       if (lightColor.r+lightColor.g+lightColor.b == 0.0) {
113         color = bcolor*vec4(vec3(lightColor.a), lit);
114       } else {
115         color = lightColor*lit;
116         color += bcolor*lit;
117         color.a = lit/2.0;
118       }
119 #endif
120     }
121   }
123   /*
124   //color.r = (1.0/lightTexSize.x)*gl_FragCoord.y;
125   color.r = gl_TexCoord[0].y;
126   color.g = 0;
127   color.b = 0;
128   color.a = 1;
129   */
130   gl_FragColor = color;