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/>.
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?!
44 vec2 vTexCoord0 = gl_TexCoord[0].xy;
46 vec2 btxy = vec2(lightPos.x+gl_FragCoord.x, lightPos.y+gl_TexCoord[0].y*lightTexSize.x);
47 btxy.y = mapPixSize.y-btxy.y;
51 float ltsxi = 1.0/lightTexSize.x;
53 // rectangular to polar
54 vec2 norm = vTexCoord0.st*2.0-1.0;
55 float theta = atan(norm.y, norm.x);
56 float r = length(norm);
57 float coord = (theta+PI)/(2.0*PI);
59 // the texDist coord to sample our 1D lookup texture
60 // always 0.0 on y axis
61 vec2 tc = vec2(coord, 0.0);
63 // the center texDist coord, which gives us hard shadows
64 float center = step(r, texture2D(texDist, tc.xy).r); //sample(vec2(tc.x, tc.y), r);
66 // now we use a simple gaussian blur
68 // we multiply the blur amount by our distance from center
69 // this leads to more blurriness as the shadow "fades away"
70 float blur = ltsxi*smoothstep(0.0, 1.0, r);
72 float sum = sample(-4.0)*0.05;
73 sum += sample(-3.0)*0.09;
74 sum += sample(-2.0)*0.12;
75 sum += sample(-1.0)*0.15;
77 sum += sample( 1.0)*0.15;
78 sum += sample( 2.0)*0.12;
79 sum += sample( 3.0)*0.09;
80 sum += sample( 4.0)*0.05;
81 // sum of 1.0 -> in light, 0.0 -> in shadow
82 float lit = mix(center, sum, SOFT_SHADOWS);
87 // multiply the summed amount by our distance, which gives us a radial falloff
88 lit = lit*smoothstep(1.0, 0.0, r);
92 color = texture2D(texBg, btxy/mapPixSize);
96 color = vec4(0.0, 0.0, 0.0, 0.0);
99 // has some light here
100 vec4 bcolor = texture2D(texBg, btxy/mapPixSize);
106 if (lightColor.r+lightColor.g+lightColor.b == 0.0) {
107 color = bcolor*vec4(vec3(lightColor.a), lit);
109 color = lightColor*lit;
118 //color.r = (1.0/lightTexSize.x)*gl_FragCoord.y;
119 color.r = gl_TexCoord[0].y;
124 gl_FragColor = color;