]> git.tdb.fi Git - libs/gl.git/blob - source/ambientocclusion.cpp
Use RenderTarget objects to manage FBOs in postprocessors
[libs/gl.git] / source / ambientocclusion.cpp
1 #define _USE_MATH_DEFINES
2 #include <cmath>
3 #include "ambientocclusion.h"
4 #include "blend.h"
5 #include "renderer.h"
6 #include "shader.h"
7 #include "tests.h"
8
9 namespace Msp {
10 namespace GL {
11
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())
17 {
18         combine_texturing.attach(2, occlude_target.get_target_texture(RENDER_COLOR));
19
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)
25         {
26                 float a = ((i*541)%16)*M_PI/32;
27                 float c = cos(a);
28                 float s = sin(a);
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);
33         }
34         rotate_lookup.image(0, RGBA, UNSIGNED_BYTE, data);
35
36         occlude_texturing.attach(1, rotate_lookup);
37
38         occlude_shdata.uniform("depth", 0);
39         occlude_shdata.uniform("rotate", 1);
40         occlude_shdata.uniform("screen_size", static_cast<float>(w), static_cast<float>(h));
41
42         combine_shdata.uniform("source", 1);
43         combine_shdata.uniform("depth", 0);
44         combine_shdata.uniform("occlusion", 2);
45         combine_shdata.uniform("screen_size", static_cast<float>(w), static_cast<float>(h));
46
47         set_depth_ratio(depth_ratio);
48         set_darkness(1.5);
49 }
50
51 void AmbientOcclusion::set_depth_ratio(float depth_ratio)
52 {
53         depth_ratio = 1/depth_ratio;
54
55         occlude_shdata.uniform("depth_ratio", depth_ratio, 1+depth_ratio);
56         combine_shdata.uniform("depth_ratio", depth_ratio, 1+depth_ratio);
57 }
58
59 void AmbientOcclusion::set_darkness(float darkness)
60 {
61         occlude_shdata.uniform("darkness", darkness);
62 }
63
64 void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const Texture2D &depth)
65 {
66         occlude_texturing.attach(0, depth);
67         combine_texturing.attach(0, depth);
68         combine_texturing.attach(1, color);
69
70         {
71                 Renderer::Push push(renderer);
72                 BindRestore bind_fbo(occlude_target.get_framebuffer());
73                 renderer.set_texturing(&occlude_texturing);
74                 renderer.set_shader_program(&occlude_shader, &occlude_shdata);
75                 quad.draw(renderer);
76         }
77
78         Renderer::Push push(renderer);
79         renderer.set_texturing(&combine_texturing);
80         renderer.set_shader_program(&combine_shader, &combine_shdata);
81         quad.draw(renderer);
82 }
83
84 } // namespace GL
85 } // namespace Msp