2 #include "ambientocclusion.h"
4 #include "meshbuilder.h"
9 const char vertex_shader[] =
10 "varying vec2 texcoord;\n"
13 " gl_Position = gl_Vertex;\n"
14 " texcoord = gl_Vertex.xy*0.5+0.5;\n"
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"
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"
30 " for(int i=0; i<=3; ++i)\n"
31 " for(int j=0; j<=3; ++j)\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"
41 " gl_FragColor = vec4(min(1.0-sum*darkness/16.0, 1.0), 0.0, 0.0, 1.0);\n"
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"
53 " float sample = depth_ratio.x/(texture2D(depth, texcoord).r-depth_ratio.y);\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"
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"
64 " sum += texture2D(occlusion, texcoord+offs).r;\n"
68 " gl_FragColor = texture2D(color, texcoord)*sum/count;\n"
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),
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);
88 combine_texturing.attach(2, occlusion);
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)
96 float a = ((i*541)%16)*M_PI/32;
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);
104 rotate_lookup.image(0, GL::RGBA, GL::UNSIGNED_BYTE, data);
106 occlude_texturing.attach(1, rotate_lookup);
108 occlude_shdata.uniform("depth", 0);
109 occlude_shdata.uniform("rotate", 1);
110 occlude_shdata.uniform("screen_size", w, h);
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);
117 set_depth_ratio(depth_ratio);
120 MeshBuilder bld(quad);
121 bld.begin(TRIANGLE_STRIP);
129 void AmbientOcclusion::set_depth_ratio(float depth_ratio)
131 depth_ratio = 1/depth_ratio;
133 occlude_shdata.uniform("depth_ratio", depth_ratio, 1+depth_ratio);
134 combine_shdata.uniform("depth_ratio", depth_ratio, 1+depth_ratio);
137 void AmbientOcclusion::set_darkness(float darkness)
139 occlude_shdata.uniform("darkness", darkness);
142 void AmbientOcclusion::render(const Texture2D &color, const Texture2D &depth)
144 occlude_texturing.attach(0, depth);
145 combine_texturing.attach(0, depth);
146 combine_texturing.attach(1, color);
148 Bind unbind_dtest(static_cast<DepthTest *>(0), true);
149 Bind unbind_blend(static_cast<Blend *>(0), true);
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();
159 GL::Bind bind_tex(combine_texturing);
160 GL::Bind bind_shader(combine_shader);
161 combine_shdata.apply();