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 texOccFull; // 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?!
45 vec2 vTexCoord0 = gl_TexCoord[0].xy;
47 vec2 btxy = vec2(lightPos.x+gl_FragCoord.x, lightPos.y+gl_TexCoord[0].y*lightTexSize.x);
48 btxy.y = mapPixSize.y-btxy.y;
50 vec2 ocxy = vec2(btxy.x, 1.0-btxy.y);
53 if (texture2D(texOccFull, 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);
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
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;
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);
93 // multiply the summed amount by our distance, which gives us a radial falloff
94 lit = lit*smoothstep(1.0, 0.0, r);
98 color = texture2D(texBg, btxy);
102 color = vec4(0.0, 0.0, 0.0, 0.0);
105 // has some light here
106 vec4 bcolor = texture2D(texBg, btxy);
112 if (lightColor.r+lightColor.g+lightColor.b == 0.0) {
113 color = bcolor*vec4(vec3(lightColor.a), lit);
115 color = lightColor*lit;
124 //color.r = (1.0/lightTexSize.x)*gl_FragCoord.y;
125 color.r = gl_TexCoord[0].y;
130 gl_FragColor = color;