]> git.tdb.fi Git - libs/gl.git/blob - source/ambientocclusion.cpp
Use Renderer for rendering 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_shader("ambientocclusion_occlude.glsl"),
14         combine_shader("ambientocclusion_combine.glsl"),
15         quad(get_fullscreen_quad())
16 {
17         occlusion.storage(RGB, w, h);
18         occlusion.set_min_filter(NEAREST);
19         occlusion.set_mag_filter(NEAREST);
20         occlusion.set_wrap(CLAMP_TO_EDGE);
21         fbo.attach(COLOR_ATTACHMENT0, occlusion, 0);
22         fbo.require_complete();
23
24         combine_texturing.attach(2, occlusion);
25
26         rotate_lookup.storage(RGBA, 4, 4);
27         rotate_lookup.set_min_filter(NEAREST);
28         rotate_lookup.set_mag_filter(NEAREST);
29         unsigned char data[64];
30         for(unsigned i=0; i<16; ++i)
31         {
32                 float a = ((i*541)%16)*M_PI/32;
33                 float c = cos(a);
34                 float s = sin(a);
35                 data[i*3  ] = static_cast<unsigned char>(127+c*127);
36                 data[i*3+1] = static_cast<unsigned char>(127+s*127);
37                 data[i*3+2] = static_cast<unsigned char>(127-s*127);
38                 data[i*3+4] = static_cast<unsigned char>(127+c*127);
39         }
40         rotate_lookup.image(0, RGBA, UNSIGNED_BYTE, data);
41
42         occlude_texturing.attach(1, rotate_lookup);
43
44         occlude_shdata.uniform("depth", 0);
45         occlude_shdata.uniform("rotate", 1);
46         occlude_shdata.uniform("screen_size", static_cast<float>(w), static_cast<float>(h));
47
48         combine_shdata.uniform("source", 1);
49         combine_shdata.uniform("depth", 0);
50         combine_shdata.uniform("occlusion", 2);
51         combine_shdata.uniform("screen_size", static_cast<float>(w), static_cast<float>(h));
52
53         set_depth_ratio(depth_ratio);
54         set_darkness(1.5);
55 }
56
57 void AmbientOcclusion::set_depth_ratio(float depth_ratio)
58 {
59         depth_ratio = 1/depth_ratio;
60
61         occlude_shdata.uniform("depth_ratio", depth_ratio, 1+depth_ratio);
62         combine_shdata.uniform("depth_ratio", depth_ratio, 1+depth_ratio);
63 }
64
65 void AmbientOcclusion::set_darkness(float darkness)
66 {
67         occlude_shdata.uniform("darkness", darkness);
68 }
69
70 void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const Texture2D &depth)
71 {
72         occlude_texturing.attach(0, depth);
73         combine_texturing.attach(0, depth);
74         combine_texturing.attach(1, color);
75
76
77         {
78                 Renderer::Push push(renderer);
79                 BindRestore bind_fbo(fbo);
80                 renderer.set_texturing(&occlude_texturing);
81                 renderer.set_shader_program(&occlude_shader, &occlude_shdata);
82                 quad.draw(renderer);
83         }
84
85         Renderer::Push push(renderer);
86         renderer.set_texturing(&combine_texturing);
87         renderer.set_shader_program(&combine_shader, &combine_shdata);
88         quad.draw(renderer);
89 }
90
91 } // namespace GL
92 } // namespace Msp