]> git.tdb.fi Git - libs/gl.git/blob - source/ambientocclusion.cpp
Give both color and depth buffers to postprocessors
[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)*0.02;\n"
35         "       float depth_avg = 0.0;\n"
36         "       for(int i=0; i<=3; ++i)\n"
37         "               for(int j=0; j<=3; ++j)\n"
38         "               {\n"
39         "                       vec2 offs = transform*vec2(float(i)-1.5, float(j)-1.5);\n"
40         "                       depth_avg += depth_ratio.x/(texture2D(depth, texcoord+offs).r-depth_ratio.y);\n"
41         "               }\n"
42         "       depth_avg /= 16;\n"
43         "       float sample = depth_ratio.x/(texture2D(depth, texcoord).r-depth_ratio.y);\n"
44         "       float diff = sample-depth_avg;\n"
45         "       float shade = min(diff*darkness, 0.0)+1.0;\n"
46         "       gl_FragColor = vec4(shade, 0.0, 0.0, 1.0);\n"
47         "}\n";
48
49 const char combine_fs[] =
50         "uniform sampler2D color;\n"
51         "uniform sampler2D depth;\n"
52         "uniform sampler2D occlusion;\n"
53         "uniform vec2 screen_size;\n"
54         "uniform vec2 depth_ratio;\n"
55         "uniform float edge_threshold;\n"
56         "varying vec2 texcoord;\n"
57         "void main()\n"
58         "{\n"
59         "       float sample = depth_ratio.x/(texture2D(depth, texcoord).r-depth_ratio.y);\n"
60         "       float sum = 1.0;\n"
61         "       float count = 1.0;\n"
62         "       for(int i=0; i<=3; ++i)\n"
63         "               for(int j=0; j<=3; ++j)\n"
64         "               {\n"
65         "                       vec2 offs = vec2(float(i)-1.5, float(j)-1.5)/screen_size;\n"
66         "                       float depth = depth_ratio.x/(texture2D(depth, texcoord+offs).r-depth_ratio.y);\n"
67         "                       if(abs(depth-sample)<edge_threshold)\n"
68         "                       {\n"
69         "                               sum += texture2D(occlusion, texcoord+offs).r;\n"
70         "                               count += 1.0;\n"
71         "                       }\n"
72         "               }\n"
73         "       gl_FragColor = texture2D(color, texcoord)*sum/count;\n"
74         "}\n";
75
76 }
77
78 namespace Msp {
79 namespace GL {
80
81 AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float depth_ratio):
82         occlude_shader(vertex_shader, occlude_fs),
83         combine_shader(vertex_shader, combine_fs),
84         quad(VERTEX2)
85 {
86         occlusion.storage(GL::RGB, w, h);
87         occlusion.set_min_filter(GL::NEAREST);
88         occlusion.set_mag_filter(GL::NEAREST);
89         fbo.attach(COLOR_ATTACHMENT0, occlusion, 0);
90
91         combine_texturing.attach(2, occlusion);
92
93         rotate_lookup.storage(GL::RGBA, 4, 4);
94         rotate_lookup.set_min_filter(GL::NEAREST);
95         rotate_lookup.set_mag_filter(GL::NEAREST);
96         unsigned char data[64];
97         for(unsigned i=0; i<16; ++i)
98         {
99                 float a = ((i*541)%16)*M_PI/32;
100                 float c = cos(a);
101                 float s = sin(a);
102                 data[i*3  ] = static_cast<unsigned char>(127+c*127);
103                 data[i*3+1] = static_cast<unsigned char>(127+s*127);
104                 data[i*3+2] = static_cast<unsigned char>(127-s*127);
105                 data[i*3+4] = static_cast<unsigned char>(127+c*127);
106         }
107         rotate_lookup.image(0, GL::RGBA, GL::UNSIGNED_BYTE, data);
108
109         occlude_texturing.attach(1, rotate_lookup);
110
111         depth_ratio = 1/depth_ratio;
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         occlude_shdata.uniform(occlude_shader.get_uniform_location("depth_ratio"), depth_ratio, 1+depth_ratio);
117
118         combine_shdata.uniform(combine_shader.get_uniform_location("color"), 1);
119         combine_shdata.uniform(combine_shader.get_uniform_location("depth"), 0);
120         combine_shdata.uniform(combine_shader.get_uniform_location("occlusion"), 2);
121         combine_shdata.uniform(combine_shader.get_uniform_location("screen_size"), w, h);
122         combine_shdata.uniform(combine_shader.get_uniform_location("depth_ratio"), depth_ratio, 1+depth_ratio);
123         combine_shdata.uniform(combine_shader.get_uniform_location("edge_threshold"), 0.05f);
124
125         set_darkness(15);
126
127         MeshBuilder bld(quad);
128         bld.begin(TRIANGLE_STRIP);
129         bld.vertex(-1, -1);
130         bld.vertex(-1, 1);
131         bld.vertex(1, -1);
132         bld.vertex(1, 1);
133         bld.end();
134 }
135
136 void AmbientOcclusion::set_darkness(float d)
137 {
138         darkness = d;
139         occlude_shdata.uniform(occlude_shader.get_uniform_location("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