]> git.tdb.fi Git - libs/gl.git/blob - source/ambientocclusion.cpp
Hide windows.h from public headers
[libs/gl.git] / source / ambientocclusion.cpp
1 #include <algorithm>
2 #include "ambientocclusion.h"
3 #include "blend.h"
4 #include "camera.h"
5 #include "renderer.h"
6 #include "shader.h"
7 #include "tests.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float):
15         occlude_target(w, h, (RENDER_COLOR,RED)),
16         occlude_shader("ambientocclusion_occlude.glsl"),
17         combine_shader("ambientocclusion_combine.glsl"),
18         quad(get_fullscreen_quad())
19 {
20         occlude_target.set_texture_filter(LINEAR);
21         texturing.attach(2, occlude_target.get_target_texture(RENDER_COLOR));
22
23         unsigned seed = 1;
24         rotate_lookup.storage(RGBA, 4, 4, 1);
25         rotate_lookup.set_filter(NEAREST);
26         unsigned char data[64];
27         for(unsigned i=0; i<16; ++i)
28         {
29                 Geometry::Angle<float> a = Geometry::Angle<float>::from_turns(random(seed));
30                 unsigned char c = (cos(a)*0.5f+0.5f)*255;
31                 unsigned char s = (sin(a)*0.5f+0.5f)*255;
32                 data[i*4  ] = c;
33                 data[i*4+1] = s;
34                 data[i*4+2] = 255-s;
35                 data[i*4+3] = ((i+i/4)%2)*255;
36         }
37         rotate_lookup.image(0, RGBA, UNSIGNED_BYTE, data);
38
39         texturing.attach(3, rotate_lookup);
40
41         shdata.uniform("source", 0);
42         shdata.uniform("depth", 1);
43         shdata.uniform("occlusion", 2);
44         shdata.uniform("rotate", 3);
45         shdata.uniform("inverse_projection", Matrix());
46
47         set_n_samples(16);
48         set_occlusion_radius(0.5f);
49         set_darkness(1.0f);
50         set_edge_depth_threshold(0.1f);
51 }
52
53 float AmbientOcclusion::random(unsigned &seed)
54 {
55         static const unsigned modulus = (1U<<31)-1;
56         seed = (static_cast<UInt64>(seed)*48271)%modulus;  // minstd
57         return static_cast<float>(seed)/(modulus-1);
58 }
59
60 void AmbientOcclusion::set_n_samples(unsigned n)
61 {
62         if(n<1 || n>32)
63                 throw out_of_range("AmbientOcclusion::set_n_samples");
64
65         unsigned seed = 1;
66         float radius_divisor = (n-1)*(n-1);
67         Vector3 sample_points[32];
68         for(unsigned i=0; i<n; ++i)
69         {
70                 Vector3 v(random(seed)-0.5f, random(seed)-0.5f, random(seed)-0.5f);
71                 sample_points[i] = normalize(v)*(0.1f+0.9f*i*i/radius_divisor);
72         }
73         shdata.uniform3_array("sample_points", n, &sample_points[0].x);
74         shdata.uniform("n_samples", static_cast<int>(n));
75 }
76
77 void AmbientOcclusion::set_occlusion_radius(float r)
78 {
79         shdata.uniform("occlusion_radius", r);
80 }
81
82 void AmbientOcclusion::set_depth_ratio(float)
83 {
84 }
85
86 void AmbientOcclusion::set_darkness(float darkness)
87 {
88         shdata.uniform("darkness", darkness);
89 }
90
91 void AmbientOcclusion::set_edge_depth_threshold(float edt)
92 {
93         shdata.uniform("edge_depth_threshold", edt);
94 }
95
96 void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const Texture2D &depth)
97 {
98         texturing.attach(0, color);
99         texturing.attach(1, depth);
100
101         if(renderer.get_camera())
102                 shdata.uniform("inverse_projection", invert(renderer.get_camera()->get_projection_matrix()));
103
104         Renderer::Push push(renderer);
105         renderer.set_texturing(&texturing);
106         renderer.set_shader_program(&occlude_shader, &shdata);
107
108         {
109                 BindRestore bind_fbo(occlude_target.get_framebuffer());
110                 quad.draw(renderer);
111         }
112
113         renderer.set_shader_program(&combine_shader);
114         quad.draw(renderer);
115 }
116
117
118 AmbientOcclusion::Template::Template():
119         n_samples(16),
120         occlusion_radius(0.5f),
121         darkness(1.0f),
122         edge_depth_threshold(0.1f)
123 { }
124
125 AmbientOcclusion *AmbientOcclusion::Template::create(unsigned width, unsigned height) const
126 {
127         RefPtr<AmbientOcclusion> ao = new AmbientOcclusion(width/size_divisor, height/size_divisor);
128         ao->set_n_samples(n_samples);
129         ao->set_occlusion_radius(occlusion_radius);
130         ao->set_darkness(darkness);
131         ao->set_edge_depth_threshold(edge_depth_threshold);
132         return ao.release();
133 }
134
135
136 AmbientOcclusion::Template::Loader::Loader(Template &t):
137         DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
138 {
139         add("darkness", &Template::darkness);
140         add("edge_depth_threshold", &Template::edge_depth_threshold);
141         add("occlusion_radius", &Template::occlusion_radius);
142         add("samples", &Template::n_samples);
143 }
144
145 } // namespace GL
146 } // namespace Msp