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
use crate::Framebuffer;

use crate::Context;
use glow::HasContext;



// ====================
// === ClearProgram ===
// ====================

/// A program that clears colors, depth and stencil of a `framebuffer`.
pub struct ClearProgram<'context> {
    context : &'context Context,
    color : (f32, f32, f32, f32),
    depth: f32,
    stencil: i32
}

impl<'context> ClearProgram<'context> {
    /// Color buffer bit.
    pub const COLOR   : u32 = glow::COLOR_BUFFER_BIT;
    /// Depth buffer bit.
    pub const DEPTH   : u32 = glow::DEPTH_BUFFER_BIT;
    /// Stencil buffer bit.
    pub const STENCIL : u32 = glow::STENCIL_BUFFER_BIT;

    /// Creates a new `ClearProgram`.
    pub fn new(context:&'context Context) -> Self {
        let color = (0.0, 0.0, 0.0, 0.0);
        let depth = 1.0; // is it default?
        let stencil = 0; // is it default?
        Self {context,color,depth,stencil}
    }

    /// Sets the color clear value.
    pub fn set_color(&mut self, color: (f32, f32, f32, f32)) { self.color = color; }
    /// Gets the color clear value.
    pub fn color(&self) -> (f32, f32, f32, f32) { self.color }

    /// Sets the depth clear value.
    pub fn set_depth(&mut self, depth: f32) { self.depth = depth; }
    /// Gets the depth clear value.
    pub fn depth(&self) -> f32 { self.depth }

    /// Sets the stencil clear value.
    pub fn set_stencil(&mut self, stencil: i32) { self.stencil = stencil; }
    /// Gets the stencil clear value.
    pub fn stencil(&self) -> i32 { self.stencil }

    /// Clear the target `Framebuffer` using the buffer bit mask.
    /// Here is an example that clears color, depth and stencil in a single call:
    /// ```rust,ignore
    /// clear(framebuffer, ClearProgram::COLOR | ClearProgram::DEPTH | ClearProgram::STENCIL)
    /// ```
    pub fn clear(&self, framebuffer:&'context mut Framebuffer<'_>, clear_mask: u32) {
        let gl = &self.context.gl;
        unsafe {
            framebuffer.bind();
            gl.clear_color(self.color.0, self.color.1, self.color.2, self.color.3);
            gl.clear_depth_f32(self.depth);
            gl.clear_stencil(self.stencil);
            gl.clear(clear_mask);
        }
    }
}