]> git.tdb.fi Git - libs/gl.git/blob - source/ambientocclusion.cpp
Drop Id tags and copyright notices from files
[libs/gl.git] / source / ambientocclusion.cpp
1 #include <cmath>
2 #include "ambientocclusion.h"
3 #include "blend.h"
4 #include "meshbuilder.h"
5 #include "tests.h"
6
7 namespace {
8
9 const char vertex_shader[] =
10         "varying vec2 texcoord;\n"
11         "void main()\n"
12         "{\n"
13         "       gl_Position = gl_Vertex;\n"
14         "       texcoord = gl_Vertex.xy*0.5+0.5;\n"
15         "}\n";
16
17 const char occlude_fs[] =
18         "uniform sampler2D depth;\n"
19         "uniform sampler2D rotate;\n"
20         "uniform vec2 screen_size;\n"
21         "uniform vec2 depth_ratio;\n"
22         "uniform float darkness;\n"
23         "varying vec2 texcoord;\n"
24         "void main()\n"
25         "{\n"
26         "       mat2 transform = mat2(texture2D(rotate, texcoord*screen_size/4.0)*2.0-1.0)\n"
27         "               *mat2(screen_size.y/screen_size.x, 0.0, 0.0, 1.0)*3.0/screen_size.y;\n"
28         "       float sample = depth_ratio.x/(texture2D(depth, texcoord).r-depth_ratio.y);\n"
29         "       float sum = 0.0;\n"
30         "       for(int i=0; i<=3; ++i)\n"
31         "               for(int j=0; j<=3; ++j)\n"
32         "               {\n"
33         "                       vec2 offs = transform*vec2(float(i)-1.5, float(j)-1.5);\n"
34         "                       float dxy = length(offs)*-sample;\n"
35         "                       float dz = depth_ratio.x/(texture2D(depth, texcoord+offs).r-depth_ratio.y)-sample;\n"
36         "                       if(abs(dz)<3*dxy)\n"
37         "                               sum += atan(dz/dxy)/1.570796;\n"
38         "                       else if(dz<0)\n"
39         "                               sum -= 0.8;\n"
40         "               }\n"
41         "       gl_FragColor = vec4(min(1.0-sum*darkness/16.0, 1.0), 0.0, 0.0, 1.0);\n"
42         "}\n";
43
44 const char combine_fs[] =
45         "uniform sampler2D color;\n"
46         "uniform sampler2D depth;\n"
47         "uniform sampler2D occlusion;\n"
48         "uniform vec2 screen_size;\n"
49         "uniform vec2 depth_ratio;\n"
50         "varying vec2 texcoord;\n"
51         "void main()\n"
52         "{\n"
53         "       float sample = depth_ratio.x/(texture2D(depth, texcoord).r-depth_ratio.y);\n"
54         "       float sum = 1.0;\n"
55         "       float count = 1.0;\n"
56         "       for(int i=0; i<=3; ++i)\n"
57         "               for(int j=0; j<=3; ++j)\n"
58         "               {\n"
59         "                       vec2 offs = vec2(float(i)-1.5, float(j)-1.5)/screen_size;\n"
60         "                       float dxy = length(offs)*-sample;\n"
61         "                       float dz = depth_ratio.x/(texture2D(depth, texcoord+offs).r-depth_ratio.y)-sample;\n"
62         "                       if(abs(dz)<3*dxy)\n"
63         "                       {\n"
64         "                               sum += texture2D(occlusion, texcoord+offs).r;\n"
65         "                               count += 1.0;\n"
66         "                       }\n"
67         "               }\n"
68         "       gl_FragColor = texture2D(color, texcoord)*sum/count;\n"
69         "}\n";
70
71 }
72
73 namespace Msp {
74 namespace GL {
75
76 AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float depth_ratio):
77         occlude_shader(vertex_shader, occlude_fs),
78         occlude_shdata(occlude_shader),
79         combine_shader(vertex_shader, combine_fs),
80         combine_shdata(combine_shader),
81         quad(VERTEX2)
82 {
83         occlusion.storage(GL::RGB, w, h);
84         occlusion.set_min_filter(GL::NEAREST);
85         occlusion.set_mag_filter(GL::NEAREST);
86         fbo.attach(COLOR_ATTACHMENT0, occlusion, 0);
87
88         combine_texturing.attach(2, occlusion);
89
90         rotate_lookup.storage(GL::RGBA, 4, 4);
91         rotate_lookup.set_min_filter(GL::NEAREST);
92         rotate_lookup.set_mag_filter(GL::NEAREST);
93         unsigned char data[64];
94         for(unsigned i=0; i<16; ++i)
95         {
96                 float a = ((i*541)%16)*M_PI/32;
97                 float c = cos(a);
98                 float s = sin(a);
99                 data[i*3  ] = static_cast<unsigned char>(127+c*127);
100                 data[i*3+1] = static_cast<unsigned char>(127+s*127);
101                 data[i*3+2] = static_cast<unsigned char>(127-s*127);
102                 data[i*3+4] = static_cast<unsigned char>(127+c*127);
103         }
104         rotate_lookup.image(0, GL::RGBA, GL::UNSIGNED_BYTE, data);
105
106         occlude_texturing.attach(1, rotate_lookup);
107
108         occlude_shdata.uniform("depth", 0);
109         occlude_shdata.uniform("rotate", 1);
110         occlude_shdata.uniform("screen_size", w, h);
111
112         combine_shdata.uniform("color", 1);
113         combine_shdata.uniform("depth", 0);
114         combine_shdata.uniform("occlusion", 2);
115         combine_shdata.uniform("screen_size", w, h);
116
117         set_depth_ratio(depth_ratio);
118         set_darkness(1.5);
119
120         MeshBuilder bld(quad);
121         bld.begin(TRIANGLE_STRIP);
122         bld.vertex(-1, 1);
123         bld.vertex(-1, -1);
124         bld.vertex(1, 1);
125         bld.vertex(1, -1);
126         bld.end();
127 }
128
129 void AmbientOcclusion::set_depth_ratio(float depth_ratio)
130 {
131         depth_ratio = 1/depth_ratio;
132
133         occlude_shdata.uniform("depth_ratio", depth_ratio, 1+depth_ratio);
134         combine_shdata.uniform("depth_ratio", depth_ratio, 1+depth_ratio);
135 }
136
137 void AmbientOcclusion::set_darkness(float darkness)
138 {
139         occlude_shdata.uniform("darkness", darkness);
140 }
141
142 void AmbientOcclusion::render(const Texture2D &color, const Texture2D &depth)
143 {
144         occlude_texturing.attach(0, depth);
145         combine_texturing.attach(0, depth);
146         combine_texturing.attach(1, color);
147
148         Bind unbind_dtest(static_cast<DepthTest *>(0), true);
149         Bind unbind_blend(static_cast<Blend *>(0), true);
150
151         {
152                 GL::Bind bind_fbo(fbo, true);
153                 GL::Bind bind_tex(occlude_texturing);
154                 GL::Bind bind_shader(occlude_shader);
155                 occlude_shdata.apply();
156                 quad.draw();
157         }
158
159         GL::Bind bind_tex(combine_texturing);
160         GL::Bind bind_shader(combine_shader);
161         combine_shdata.apply();
162         quad.draw();
163 }
164
165 } // namespace GL
166 } // namespace Msp