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