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