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.
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.
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.
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/>.
18 // this shader will light level geometry
25 #define SEARCH_RADIUS (4)
26 #define MIN_ALPHA (0.05)
28 uniform sampler2D texLMap; // light texture of lightTexSize
29 uniform sampler2D texBg; // background
30 uniform sampler2D texOccFull; // occluders
31 uniform sampler2D texOccSmall; // occluders, small map
32 uniform vec2 lightTexSize; // x: lightSize; y: size of this texture
33 uniform vec2 mapPixSize;
34 uniform vec4 lightColor;
35 uniform vec2 lightPos;
38 bool hasLight (in vec2 txo, in float pixelStep) {
41 for (int dy = SEARCH_RADIUS*2; dy > 0; --dy) {
42 for (int dx = SEARCH_RADIUS*2; dx > 0; --dx) {
43 if (texture2D(texLMap, txc).a > MIN_ALPHA) return true;
51 float pxs = pixelStep*(SEARCH_RADIUS*2);
53 vec2 txcld = vec2(txo.x-pxs, txo.y+pxs);
54 vec2 txcru = vec2(txo.x+pxs, txo.y-pxs);
56 vec2 txcu = vec2(txo.x, txo.y-pxs);
57 vec2 txcd = vec2(txo.x, txo.y+pxs);
58 vec2 txcl = vec2(txo.x-pxs, txo.y);
59 vec2 txcr = vec2(txo.x+pxs, txo.y);
60 vec2 txcrStep = vec2(pixelStep, -pixelStep);
61 float pst2 = pixelStep*2;
62 for (int d = SEARCH_RADIUS; d > 0; --d) {
63 if (texture2D(texLMap, txclu).a > MIN_ALPHA) return true;
65 if (texture2D(texLMap, txcld).a > MIN_ALPHA) return true;
69 if (texture2D(texLMap, txcru).a > MIN_ALPHA) return true;
72 if (texture2D(texLMap, txcrd).a > MIN_ALPHA) return true;
75 if (texture2D(texLMap, txcu).a > MIN_ALPHA) return true;
77 if (texture2D(texLMap, txcd).a > MIN_ALPHA) return true;
80 if (texture2D(texLMap, txcl).a > MIN_ALPHA) return true;
82 if (texture2D(texLMap, txcr).a > MIN_ALPHA) return true;
89 //gl_FragCoord: in background
92 vec2 vTexCoord0 = gl_TexCoord[0].xy; // in light texture (texLMap)
93 //vec2 btxy = gl_FragCoord.xy/mapPixSize;
95 vec2 btxy = vec2(/*lightPos.x+*/gl_FragCoord.x, /*lightPos.y+gl_TexCoord[0].y*lightTexSize.x*/gl_FragCoord.y);
96 //btxy.y = mapPixSize.y-btxy.y;
98 vec2 ocxy = vec2(btxy.x, 1.0-btxy.y);
102 vec4 bcolor = texture2D(texBg, btxy);
106 //if (texture2D(texOccFull, ocxy).a <= 0.75) color.a = 0.1;
108 vec2 ourpos = gl_FragCoord.xy;
109 ourpos.y = mapPixSize.y-ourpos.y;
110 // distance from this point to light origin
111 float lit = clamp(1.0-(distance(ourpos, lightPos)/(lightTexSize.x/2.0)), 0.0, 1.0);
112 lit = smoothstep(0.1, 1.0, lit);
115 //if (texture2D(texOccFull, ocxy).a <= 0.75) discard; // empty space
116 //float pixelStep = 1.0/lightTexSize.x;
117 //if (!hasLight(vTexCoord0, pixelStep)) discard;
119 color.r = vTexCoord0.x;
125 if (texture2D(texOccFull, ocxy).a <= 0.75) discard; // empty space
127 // occluder hit; look for light in 8px radius
128 float pixelStep = 1.0/lightTexSize.y;
129 if (!hasLight(vTexCoord0, pixelStep)) discard;
131 vec4 bcolor = texture2D(texBg, btxy);
132 // light is y-mirrored
133 vec2 ourpos = gl_FragCoord.xy;
134 ourpos.y = mapPixSize.y-ourpos.y;
135 // distance from this point to light origin
136 float lit = clamp(1.0-(distance(ourpos, lightPos)/(lightTexSize.x/2.0)), 0.0, 1.0);
137 lit = smoothstep(0.1, 1.0, lit);
138 if (lightColor.a == 0.0) {
139 color = bcolor*vec4(vec3(1.0), lit);
141 color = lightColor*lit;
146 gl_FragColor = color;