shader renamed
[dd2d.git] / data / shaders / srlight_trace.frag
blobe8035e1593ea654facfacec8852372f2433ef493
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 HUMANIOD_THETA
22 #define INTO_DEPTH  (4.0)
24 #define PI  (3.14159265)
26 //uniform sampler2D texOcc;
27 uniform vec2 lightTexSize;
29 uniform sampler2D texOccFull; // occluders map, full
30 uniform vec2 lightPos; // light coords in occluders map
31 uniform vec2 mapPixSize; // size of occluders map
33 // alpha threshold for our occlusion map
34 #define THRESHOLD  (0.75)
37 void main (void) {
38   //float dmposx = gl_TexCoord[0].x;
39   vec2 ltPos = lightPos/mapPixSize;
40   /*if (texture2D(texOccFull, ltPos).a > THRESHOLD) {
41     gl_FragColor = vec4(vec3(1.0), 1.0);
42   } else*/
43   {
44     float dmposx = gl_FragCoord.x/lightTexSize.x;
45     // theta: 90 and greater
46 #ifdef HUMANIOD_THETA
47     float theta = 360.0*dmposx;
48     theta += 90; // turn it
49     theta = PI*theta/180.0; // to radians
50 #else
51     float theta = PI*1.5+(dmposx*2.0-1.0)*PI;
52 #endif
53     float nsint = -sin(theta);
54     float ncost = cos(theta);
55     float dst;
56     for (dst = 0.0; dst < lightTexSize.x/2.0; dst += 1.0) {
57       // sample the occlusion map
58       ltPos = (lightPos+vec2(nsint*dst, ncost*dst))/mapPixSize;
59       float dataa = texture2D(texOccFull, ltPos).a;
60       // if we've hit an opaque fragment (occluder), then get new distance
61       // if the new distance is below the current, then we'll use that for our ray
62       if (dataa > THRESHOLD) {
63 #ifdef INTO_DEPTH
64         // if we hit occluders, trace through it for some more steps, so blur will look better on hitpoints
65         float orgDst = dst;
66         for (float count = 0.0; count <= INTO_DEPTH; count += 1.0) {
67           dst += 1.0;
68           ltPos = (lightPos+vec2(nsint*dst, ncost*dst))/mapPixSize;
69           dataa = texture2D(texOccFull, ltPos).a;
70           if (dataa <= THRESHOLD) {
71             dst -= 1.0;
72             break;
73           }
74         }
75 #else
76         //dst = max(0.0, dst-ltsyStep);
77         //dst = max(0.0, dst-1.0);
78 #endif
79         break; // the current distance is how far from the top we've come
80       }
81     }
82     dst /= lightTexSize.x;
83     dst *= 2.0;
84     dst = min(dst, 1.0);
85     gl_FragColor = vec4(vec3(dst), 1.0);
86   }