1 #define _USE_MATH_DEFINES
3 #include "ambientocclusion.h"
10 const char occlude_fs[] =
11 "uniform sampler2D depth;\n"
12 "uniform sampler2D rotate;\n"
13 "uniform vec2 screen_size;\n"
14 "uniform vec2 depth_ratio;\n"
15 "uniform float darkness;\n"
16 "varying vec2 texcoord;\n"
19 " mat2 transform = mat2(texture2D(rotate, texcoord*screen_size/4.0)*2.0-1.0)\n"
20 " *mat2(screen_size.y/screen_size.x, 0.0, 0.0, 1.0)*3.0/screen_size.y;\n"
21 " float sample = depth_ratio.x/(texture2D(depth, texcoord).r-depth_ratio.y);\n"
23 " for(int i=0; i<=3; ++i)\n"
24 " for(int j=0; j<=3; ++j)\n"
26 " vec2 offs = transform*vec2(float(i)-1.5, float(j)-1.5);\n"
27 " float dxy = length(offs)*-sample;\n"
28 " float dz = depth_ratio.x/(texture2D(depth, texcoord+offs).r-depth_ratio.y)-sample;\n"
29 " if(abs(dz)<3.0*dxy)\n"
30 " sum += atan(dz/dxy)/1.570796;\n"
34 " gl_FragColor = vec4(min(1.0-sum*darkness/16.0, 1.0), 0.0, 0.0, 1.0);\n"
37 const char combine_fs[] =
38 "uniform sampler2D color;\n"
39 "uniform sampler2D depth;\n"
40 "uniform sampler2D occlusion;\n"
41 "uniform vec2 screen_size;\n"
42 "uniform vec2 depth_ratio;\n"
43 "varying vec2 texcoord;\n"
46 " float sample = depth_ratio.x/(texture2D(depth, texcoord).r-depth_ratio.y);\n"
48 " float count = 1.0;\n"
49 " for(int i=0; i<=3; ++i)\n"
50 " for(int j=0; j<=3; ++j)\n"
52 " vec2 offs = vec2(float(i)-1.5, float(j)-1.5)/screen_size;\n"
53 " float dxy = length(offs)*-sample;\n"
54 " float dz = depth_ratio.x/(texture2D(depth, texcoord+offs).r-depth_ratio.y)-sample;\n"
55 " if(abs(dz)<3.0*dxy)\n"
57 " sum += texture2D(occlusion, texcoord+offs).r;\n"
61 " gl_FragColor = texture2D(color, texcoord)*sum/count;\n"
69 AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float depth_ratio):
70 quad(get_fullscreen_quad())
72 occlude_shader.attach_shader(get_fullscreen_vertex_shader());
73 occlude_shader.attach_shader_owned(new FragmentShader(occlude_fs));
74 occlude_shader.bind_attribute(get_component_type(VERTEX2), "vertex");
75 occlude_shader.link();
77 combine_shader.attach_shader(get_fullscreen_vertex_shader());
78 combine_shader.attach_shader_owned(new FragmentShader(combine_fs));
79 combine_shader.bind_attribute(get_component_type(VERTEX2), "vertex");
80 combine_shader.link();
82 occlusion.storage(RGB, w, h);
83 occlusion.set_min_filter(NEAREST);
84 occlusion.set_mag_filter(NEAREST);
85 occlusion.set_wrap(CLAMP_TO_EDGE);
86 fbo.attach(COLOR_ATTACHMENT0, occlusion, 0);
87 fbo.require_complete();
89 combine_texturing.attach(2, occlusion);
91 rotate_lookup.storage(RGBA, 4, 4);
92 rotate_lookup.set_min_filter(NEAREST);
93 rotate_lookup.set_mag_filter(NEAREST);
94 unsigned char data[64];
95 for(unsigned i=0; i<16; ++i)
97 float a = ((i*541)%16)*M_PI/32;
100 data[i*3 ] = static_cast<unsigned char>(127+c*127);
101 data[i*3+1] = static_cast<unsigned char>(127+s*127);
102 data[i*3+2] = static_cast<unsigned char>(127-s*127);
103 data[i*3+4] = static_cast<unsigned char>(127+c*127);
105 rotate_lookup.image(0, RGBA, UNSIGNED_BYTE, data);
107 occlude_texturing.attach(1, rotate_lookup);
109 occlude_shdata.uniform("depth", 0);
110 occlude_shdata.uniform("rotate", 1);
111 occlude_shdata.uniform("screen_size", static_cast<float>(w), static_cast<float>(h));
113 combine_shdata.uniform("color", 1);
114 combine_shdata.uniform("depth", 0);
115 combine_shdata.uniform("occlusion", 2);
116 combine_shdata.uniform("screen_size", static_cast<float>(w), static_cast<float>(h));
118 set_depth_ratio(depth_ratio);
122 void AmbientOcclusion::set_depth_ratio(float depth_ratio)
124 depth_ratio = 1/depth_ratio;
126 occlude_shdata.uniform("depth_ratio", depth_ratio, 1+depth_ratio);
127 combine_shdata.uniform("depth_ratio", depth_ratio, 1+depth_ratio);
130 void AmbientOcclusion::set_darkness(float darkness)
132 occlude_shdata.uniform("darkness", darkness);
135 void AmbientOcclusion::render(const Texture2D &color, const Texture2D &depth)
137 occlude_texturing.attach(0, depth);
138 combine_texturing.attach(0, depth);
139 combine_texturing.attach(1, color);
141 BindRestore unbind_dtest(static_cast<DepthTest *>(0));
142 BindRestore unbind_blend(static_cast<Blend *>(0));
143 Bind bind_mesh(quad);
146 BindRestore bind_fbo(fbo);
147 Bind bind_tex(occlude_texturing);
148 Bind bind_shader(occlude_shader);
149 occlude_shdata.apply();
153 Bind bind_tex(combine_texturing);
154 Bind bind_shader(combine_shader);
155 combine_shdata.apply();