Skip to main content

enigma_3d/postprocessing/
mod.rs

1use glium::{Display, IndexBuffer, Texture2d, VertexBuffer};
2use glium::framebuffer::SimpleFrameBuffer;
3use glium::glutin::surface::WindowSurface;
4use glium::texture::DepthTexture2d;
5use crate::AppState;
6use crate::geometry::Vertex;
7use crate::logging::EnigmaWarning;
8
9pub mod grayscale;
10pub mod bloom;
11pub mod edge;
12pub mod depth_fog;
13pub mod vignette;
14pub mod lens_dirt;
15
16pub trait PostProcessingEffect {
17    fn render(&self, _app_state: &AppState, _vertex_buffer: &VertexBuffer<Vertex>, _index_buffer: &IndexBuffer<u32>, _target: &mut SimpleFrameBuffer, _source: &Texture2d, _depth_source: &DepthTexture2d, _buffer_textures: &Vec<Texture2d>) {
18        EnigmaWarning::new(Some("PostProcessingEffect::render() not implemented. Please implement this trait in your postprocessing struct."), true).log();
19    }
20}
21
22pub fn get_screen_vert_rect(display: &Display<WindowSurface>) -> glium::VertexBuffer<Vertex> {
23    let vertices = vec![
24        Vertex { position: [-1.0, -1.0, 0.0], texcoord: [0.0, 0.0], color: [1.0, 1.0, 1.0], normal: [0.0, 0.0, 1.0], bone_indices: [0, 0, 0, 0], bone_weights: [0.0, 0.0, 0.0, 0.0] },
25        Vertex { position: [-1.0, 1.0, 0.0], texcoord: [0.0, 1.0], color: [1.0, 1.0, 1.0], normal: [0.0, 0.0, 1.0], bone_indices: [0, 0, 0, 0], bone_weights: [0.0, 0.0, 0.0, 0.0] },
26        Vertex { position: [1.0, 1.0, 0.0], texcoord: [1.0, 1.0], color: [1.0, 1.0, 1.0], normal: [0.0, 0.0, 1.0], bone_indices: [0, 0, 0, 0], bone_weights: [0.0, 0.0, 0.0, 0.0] },
27        Vertex { position: [1.0, -1.0, 0.0], texcoord: [1.0, 0.0], color: [1.0, 1.0, 1.0], normal: [0.0, 0.0, 1.0], bone_indices: [0, 0, 0, 0], bone_weights: [0.0, 0.0, 0.0, 0.0] },
28    ];
29    glium::VertexBuffer::new(display, &vertices).unwrap()
30}
31
32pub fn get_screen_indices_rect(display: &Display<WindowSurface>) -> glium::IndexBuffer<u32> {
33    let indices: Vec<u32> = vec![0, 1, 2, 0, 2, 3];
34    glium::IndexBuffer::new(display, glium::index::PrimitiveType::TrianglesList, &indices).unwrap()
35}
36
37pub fn get_screen_program(display: &Display<WindowSurface>) -> glium::Program {
38    let vertex_shader_src = r#"
39        #version 140
40
41        in vec3 position;
42        in vec2 texcoord;
43        in vec3 color;
44        in vec3 normal;
45
46        out vec2 TEXCOORD;
47
48
49        void main() {
50            TEXCOORD = texcoord;
51            gl_Position = vec4(position, 1.0);
52        }
53    "#;
54
55    let fragment_shader_src = r#"
56        #version 140
57
58        in vec2 TEXCOORD;
59
60        out vec4 color;
61
62        uniform sampler2D scene;
63
64        void main() {
65            color = texture(scene, TEXCOORD);
66        }
67    "#;
68
69    glium::Program::from_source(display, vertex_shader_src, fragment_shader_src, None).expect("Failed to compile shader program")
70}