1 #define _USE_MATH_DEFINES
3 #include "ambientocclusion.h"
12 AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float depth_ratio):
13 occlude_target(w, h, (RENDER_COLOR,RGB)),
14 occlude_shader("ambientocclusion_occlude.glsl"),
15 combine_shader("ambientocclusion_combine.glsl"),
16 quad(get_fullscreen_quad())
18 texturing.attach(2, occlude_target.get_target_texture(RENDER_COLOR));
20 rotate_lookup.storage(RGBA, 4, 4);
21 rotate_lookup.set_min_filter(NEAREST);
22 rotate_lookup.set_mag_filter(NEAREST);
23 unsigned char data[64];
24 for(unsigned i=0; i<16; ++i)
26 float a = ((i*541)%16)*M_PI/32;
29 data[i*3 ] = static_cast<unsigned char>(127+c*127);
30 data[i*3+1] = static_cast<unsigned char>(127+s*127);
31 data[i*3+2] = static_cast<unsigned char>(127-s*127);
32 data[i*3+4] = static_cast<unsigned char>(127+c*127);
34 rotate_lookup.image(0, RGBA, UNSIGNED_BYTE, data);
36 texturing.attach(3, rotate_lookup);
38 shdata.uniform("source", 0);
39 shdata.uniform("depth", 1);
40 shdata.uniform("occlusion", 2);
41 shdata.uniform("rotate", 3);
42 shdata.uniform("screen_size", static_cast<float>(w), static_cast<float>(h));
44 set_depth_ratio(depth_ratio);
48 void AmbientOcclusion::set_depth_ratio(float depth_ratio)
50 depth_ratio = 1/depth_ratio;
52 shdata.uniform("depth_ratio", depth_ratio, 1+depth_ratio);
55 void AmbientOcclusion::set_darkness(float darkness)
57 shdata.uniform("darkness", darkness);
60 void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const Texture2D &depth)
62 texturing.attach(0, color);
63 texturing.attach(1, depth);
65 Renderer::Push push(renderer);
66 renderer.set_texturing(&texturing);
67 renderer.set_shader_program(&occlude_shader, &shdata);
70 BindRestore bind_fbo(occlude_target.get_framebuffer());
74 renderer.set_shader_program(&combine_shader);