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