light system seems to work again
[dd2d.git] / data / shaders / srlight_blur.frag
blob9dc5643ad22d7fc29fdf82a7a7e924566cc187ec
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 vec2 lightTexSize; // x: size of distmap; y: size of this texture
29 uniform vec2 mapPixSize;
30 uniform vec4 lightColor;
31 uniform vec2 lightPos; // lefttop
32 #define SHADOW_DO_BLUR
34 #define SOFT_SHADOWS  (1.0f)
37 // sample from the 1D distance map
38 #define sample(cc_)  step(r, texture2D(texDist, vec2(tc.x+(cc_)*blur, /*tc.y*/0.0)).r)
40 //gl_FragCoord.x: [0..lightTexSize.x)
41 //gl_FragCoord.y: somehow it is constant; wtf?!
43 void main (void) {
44   vec2 vTexCoord0 = gl_TexCoord[0].xy;
45   // don't even ask me!
46   vec2 btxy = vec2(lightPos.x+gl_FragCoord.x, lightPos.y+gl_TexCoord[0].y*lightTexSize.x);
47   btxy.y = mapPixSize.y-btxy.y;
48   btxy /= mapPixSize;
49   vec2 ocxy = vec2(btxy.x, 1.0-btxy.y);
50   vec4 color;
52   if (texture2D(texOcc, ocxy).a > 0.75) {
53     // occluder hit, this will take care of lighted walls (we'll fix wall in next pass)
54     color = vec4(0.0, 0.0, 0.0, 0.0);
55   } else {
56     float ltsxi = 1.0/lightTexSize.x;
58     // rectangular to polar
59     vec2 norm = vTexCoord0.st*2.0-1.0;
60     float theta = atan(norm.y, norm.x);
61     float r = length(norm);
62     float coord = (theta+PI)/(2.0*PI);
64     // the texDist coord to sample our 1D lookup texture
65     // always 0.0 on y axis
66     vec2 tc = vec2(coord, 0.0);
68     // the center texDist coord, which gives us hard shadows
69     float center = step(r, texture2D(texDist, tc.xy).r); //sample(vec2(tc.x, tc.y), r);
71     // now we use a simple gaussian blur
72     #ifdef SHADOW_DO_BLUR
73       // we multiply the blur amount by our distance from center
74       // this leads to more blurriness as the shadow "fades away"
75       float blur = ltsxi*smoothstep(0.0, 1.0, r);
77       float sum  = sample(-4.0)*0.05;
78             sum += sample(-3.0)*0.09;
79             sum += sample(-2.0)*0.12;
80             sum += sample(-1.0)*0.15;
81             sum += center*0.16;
82             sum += sample( 1.0)*0.15;
83             sum += sample( 2.0)*0.12;
84             sum += sample( 3.0)*0.09;
85             sum += sample( 4.0)*0.05;
86       // sum of 1.0 -> in light, 0.0 -> in shadow
87       float lit = mix(center, sum, SOFT_SHADOWS);
88     #else
89       float lit = center;
90     #endif
92     // multiply the summed amount by our distance, which gives us a radial falloff
93     lit = lit*smoothstep(1.0, 0.0, r);
95     if (lit == 0.0) {
96 #ifdef DEBUG_RECT
97       color = texture2D(texBg, btxy);
98       color.b = 1.0;
99       color.a = 0.4;
100 #else
101       color = vec4(0.0, 0.0, 0.0, 0.0);
102 #endif
103     } else {
104       // has some light here
105       vec4 bcolor = texture2D(texBg, btxy);
106 #ifdef DEBUG_RECT
107       color = bcolor;
108       color.b = 1.0;
109       color.a = 0.4;
110 #else
111       if (lightColor.r+lightColor.g+lightColor.b == 0.0) {
112         color = bcolor*vec4(vec3(lightColor.a), lit);
113       } else {
114         color = lightColor*lit;
115         color += bcolor*lit;
116         color.a = lit/2.0;
117       }
118 #endif
119     }
120   }
122   /*
123   //color.r = (1.0/lightTexSize.x)*gl_FragCoord.y;
124   color.r = gl_TexCoord[0].y;
125   color.g = 0;
126   color.b = 0;
127   color.a = 1;
128   */
129   gl_FragColor = color;