shader renamed
[dd2d.git] / data / shaders / srlight_blur_occ.frag
blob66205be42cfa42417d1688a80d65d563b2fb2075
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
22 #define PI 3.1415926
24 #define SEARCH_RADIUS  (4)
25 #define MIN_ALPHA  (0.05)
27 uniform sampler2D texLMap; // light texture of lightTexSize
28 uniform sampler2D texBg; // background
29 uniform sampler2D texOcc; // occluders
30 uniform sampler2D texOccSmall; // occluders, small map
31 uniform vec2 lightTexSize; // x: lightSize; y: size of this texture
32 uniform vec2 mapPixSize;
33 uniform vec4 lightColor;
34 uniform vec2 lightPos;
37 bool hasLight (in vec2 txo, in float pixelStep) {
38   /*
39   vec2 txc = txo;
40   for (int dy = SEARCH_RADIUS*2; dy > 0; --dy) {
41     for (int dx = SEARCH_RADIUS*2; dx > 0; --dx) {
42       if (texture2D(texLMap, txc).a > MIN_ALPHA) return true;
43       txc.x += pixelStep;
44     }
45     txc.x = txo.x;
46     txc.y += pixelStep;
47   }
48   return false;
49   */
50   float pxs = pixelStep*(SEARCH_RADIUS*2);
51   vec2 txclu = txo-pxs;
52   vec2 txcld = vec2(txo.x-pxs, txo.y+pxs);
53   vec2 txcru = vec2(txo.x+pxs, txo.y-pxs);
54   vec2 txcrd = txo+pxs;
55   vec2 txcu = vec2(txo.x, txo.y-pxs);
56   vec2 txcd = vec2(txo.x, txo.y+pxs);
57   vec2 txcl = vec2(txo.x-pxs, txo.y);
58   vec2 txcr = vec2(txo.x+pxs, txo.y);
59   vec2 txcrStep = vec2(pixelStep, -pixelStep);
60   float pst2 = pixelStep*2;
61   for (int d = SEARCH_RADIUS; d > 0; --d) {
62     if (texture2D(texLMap, txclu).a > MIN_ALPHA) return true;
63     txclu += pst2;
64     if (texture2D(texLMap, txcld).a > MIN_ALPHA) return true;
65     txcld.x += pst2;
66     txcld.y -= pst2;
68     if (texture2D(texLMap, txcru).a > MIN_ALPHA) return true;
69     txcru.x -= pst2;
70     txcru.y += pst2;
71     if (texture2D(texLMap, txcrd).a > MIN_ALPHA) return true;
72     txcrd -= pst2;
74     if (texture2D(texLMap, txcu).a > MIN_ALPHA) return true;
75     txcu.y += pst2;
76     if (texture2D(texLMap, txcd).a > MIN_ALPHA) return true;
77     txcd.y -= pst2;
79     if (texture2D(texLMap, txcl).a > MIN_ALPHA) return true;
80     txcl.x += pst2;
81     if (texture2D(texLMap, txcr).a > MIN_ALPHA) return true;
82     txcr.x -= pst2;
83   }
84   return false;
88 //gl_FragCoord: in background
90 void main (void) {
91   vec2 vTexCoord0 = gl_TexCoord[0].xy; // in light texture (texLMap)
92   //vec2 btxy = gl_FragCoord.xy/mapPixSize;
93   // don't even ask me!
94   vec2 btxy = vec2(/*lightPos.x+*/gl_FragCoord.x, /*lightPos.y+gl_TexCoord[0].y*lightTexSize.x*/gl_FragCoord.y);
95   //btxy.y = mapPixSize.y-btxy.y;
96   btxy /= mapPixSize;
97   vec2 ocxy = vec2(btxy.x, 1.0-btxy.y);
98   vec4 color;
100 #ifdef DEBUG_RECT
101   vec4 bcolor = texture2D(texBg, btxy);
102   color = bcolor;
103   color.b = 1.0;
104   color.a = 0.4;
105   //if (texture2D(texOcc, ocxy).a <= 0.75) color.a = 0.1;
107   vec2 ourpos = gl_FragCoord.xy;
108   ourpos.y = mapPixSize.y-ourpos.y;
109   // distance from this point to light origin
110   float lit = clamp(1.0-(distance(ourpos, lightPos)/(lightTexSize.x/2.0)), 0.0, 1.0);
111   lit = smoothstep(0.1, 1.0, lit);
112   color.a = lit;
114   //if (texture2D(texOcc, ocxy).a <= 0.75) discard; // empty space
115   //float pixelStep = 1.0/lightTexSize.x;
116   //if (!hasLight(vTexCoord0, pixelStep)) discard;
117   /*
118   color.r = vTexCoord0.x;
119   color.g = 0;
120   color.b = 0;
121   color.a = 1.0;
122   */
123 #else
124   if (texture2D(texOcc, ocxy).a <= 0.75) discard; // empty space
126   // occluder hit; look for light in 8px radius
127   float pixelStep = 1.0/lightTexSize.y;
128   if (!hasLight(vTexCoord0, pixelStep)) discard;
129   // background
130   vec4 bcolor = texture2D(texBg, btxy);
131   // light is y-mirrored
132   vec2 ourpos = gl_FragCoord.xy;
133   ourpos.y = mapPixSize.y-ourpos.y;
134   // distance from this point to light origin
135   float lit = clamp(1.0-(distance(ourpos, lightPos)/(lightTexSize.x/2.0)), 0.0, 1.0);
136   lit = smoothstep(0.1, 1.0, lit);
137   if (lightColor.a == 0.0) {
138     color = bcolor*vec4(vec3(1.0), lit);
139   } else {
140     color = lightColor*lit;
141     color += bcolor*lit;
142     color.a = lit/2.0;
143   }
144 #endif
145   gl_FragColor = color;