1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*!
 * Postprocessing module including bloom, fxaa and tonemapping
 */
use crate::Bundle;
use rinecs::{SystemThreadLocal, EntitiesThreadLocal, ResourcesThreadLocal};
use crate::renderer::{
    resources::ScreenRenderBuffer,
};
use rin::graphics;
use rin::window::{Window, WindowT};
use rin::gl::{self, Renderer2d};
use rin::util::Result;
use na::{Pnt2, pnt2};
use postprocessing;
#[cfg(feature="gui")]
use crate::gui::Gui;
#[cfg(feature="gui")]
use ringui::ControlGeometry;
pub use postprocessing::Parameters;
use crate::Scene;


pub struct Postprocessing{
    postpro: postprocessing::PostProcessing,
    parameters: Parameters,
}

impl Postprocessing{
    pub fn new(gl: &gl::Renderer, w: u32, h: u32, format: gl::fbo::ColorFormat, parameters: Parameters) -> Result<Postprocessing>{
        postprocessing::PostProcessing::new(gl, w, h, format)
            .map(|postpro| Postprocessing{
                postpro,
                parameters,
            })
    }

    pub fn new_and_parameters(gl: &gl::Renderer, w: u32, h: u32, format: gl::fbo::ColorFormat) -> Result<(Postprocessing, Parameters)>{
        let parameters = Parameters::default();
        let postpo = postprocessing::PostProcessing::new(gl, w, h, format)
            .map(|postpro| Postprocessing{
                postpro,
                parameters: parameters.clone(),
            });
        postpo.map(|p| (p, parameters))
    }
}

impl Bundle for Postprocessing{
    fn setup(self, world: &mut Scene){
        world.add_system_with_name_thread_local(self, "postprocessing");
    }

    fn register(_world: &mut Scene){
    }
}

impl<'a> SystemThreadLocal<'a> for Postprocessing{
    fn run(&mut self, _entities: EntitiesThreadLocal, resources: ResourcesThreadLocal){
        let glin = resources.get::<gl::Renderer<'static>>().unwrap();
        let window = resources.get::<Window>().unwrap();
        #[cfg(feature="gui")]
        let pos = {
            if let Some(gui) = resources.get::<Gui>(){
                let pos = gui.position().to_value();
                let width = gui.width().to_value();
                pnt2(pos.x + width, pos.y)
            }else{
                Pnt2::origin()
            }
        };
        #[cfg(not(feature="gui"))]
        let pos = Pnt2::origin();

        let viewport = window.viewport();
        if let Some(fbo) = resources.get::<ScreenRenderBuffer>(){
            // Postpo and render final texture and gui
            let glin = glin.with_mvp(graphics::Mvp::ortho_top_left(viewport));
            if let glin::fbo::ColorAttachment::Texture(tex) = fbo.color_attachment() {
                let tex = self.postpro.process(&glin, tex, &self.parameters)
                    .unwrap();
                glin.draw_pos(tex, &pos);
            }else{
                panic!("Trying to postprocess on render buffer without color attachment")
            }
        }else{
            panic!("Trying to postprocess on without render buffer.
You probably need to create the renderer bundle using new_with_render_surface")
        }
    }
}