gl/
clear.rs

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use crate::c::GLenum;

/// Specify clear values for the color buffers
///
/// **NOTE:** Calls the [`glClearColor`]` function.
///
/// # Parameters
///
/// * `red`   - the red color (clamped between 0.0 and 1.0)
/// * `green` - the green color (clamped between 0.0 and 1.0)
/// * `blue`  - the blue color (clamped between 0.0 and 1.0)
/// * `alpha` -  the alpha value (clamped between 0.0 and 1.0)
///
/// [`glClearColor`]: https://registry.khronos.org/OpenGL-Refpages/gl4/html/glClearColor.xhtml
pub fn clear_color(red: f32, green: f32, blue: f32, alpha: f32) {
  unsafe { crate::c::glClearColor(red, green, blue, alpha) }
}

/// Specify the clear value for the depth buffer
///
/// **Note:** Calls the [`glClearDepth`] function
///
/// # Parameters
///
/// * `depth` - the depth value (clamped between 0.0 and 1.0)
///
/// [`glClearDepth`]: https://registry.khronos.org/OpenGL-Refpages/gl4/html/glClearDepth.xhtml
pub fn clear_depth(depth: f64) {
  unsafe { crate::c::glClearDepth(depth) }
}

/// Specify the clear value for the stencil buffer
///
/// **Note:** Calls the [`glClearStencil`] function
///
/// # Parameters
///
/// * `s` - the stencil value
///
/// [`glClearStencil`]: https://registry.khronos.org/OpenGL-Refpages/gl4/html/glClearStencil.xhtml
pub fn clear_stencil(s: i32) {
  unsafe { crate::c::glClearStencil(s) }
}

/// Specify the clear value for the color, depth, and stencil buffers
/// used in [`clear`].
#[derive(Copy, Clone)]
pub struct ClearBits(GLenum);

impl ClearBits {
  /// Clear the color buffer
  pub const COLOR: Self = Self(crate::c::GL_COLOR_BUFFER_BIT);

  /// Clear the depth buffer
  pub const DEPTH: Self = Self(crate::c::GL_DEPTH_BUFFER_BIT);

  /// Clear the stencil buffer
  pub const STENCIL: Self = Self(crate::c::GL_STENCIL_BUFFER_BIT);

  /// Constructs a new `ClearBits` from the given bits.
  ///
  /// # Parameters
  ///
  /// * `bits` - the bits to construct the `ClearBits` from
  #[inline]
  pub const fn from_bits(bits: GLenum) -> Self {
    Self(bits)
  }

  /// Returns the bits of the `ClearBits`.
  #[inline]
  pub const fn bits(&self) -> GLenum {
    self.0
  }

  /// Returns whether the `ClearBits` contains the given bits.
  ///
  /// # Parameters
  ///
  /// * `other` - the bits to check for
  #[inline]
  pub const fn contains(&self, other: Self) -> bool {
    self.0 & other.0 == other.0
  }

  /// Returns whether the `ClearBits` is empty.
  #[inline]
  pub const fn is_empty(&self) -> bool {
    self.0 == 0
  }

  /// Returns the union of the `ClearBits` and the given bits.
  #[inline]
  pub const fn all() -> Self {
    Self(
      crate::c::GL_COLOR_BUFFER_BIT
        | crate::c::GL_DEPTH_BUFFER_BIT
        | crate::c::GL_STENCIL_BUFFER_BIT,
    )
  }

  /// Returns an empty `ClearBits`.
  #[inline]
  pub const fn none() -> Self {
    Self(0)
  }

  /// Returns the union of the `ClearBits` and the given bits.
  #[inline]
  pub fn color(mut self) -> Self {
    self.0 |= Self::COLOR.0;
    self
  }

  /// Returns the union of the `ClearBits` and the given bits.
  #[inline]
  pub fn depth(mut self) -> Self {
    self.0 |= Self::DEPTH.0;
    self
  }

  /// Returns the union of the `ClearBits` and the given bits.
  #[inline]
  pub fn stencil(mut self) -> Self {
    self.0 |= Self::STENCIL.0;
    self
  }

  /// Returns whether the `ClearBits` contains the color bits.
  #[inline]
  pub const fn has_color(&self) -> bool {
    self.contains(Self::COLOR)
  }

  /// Returns whether the `ClearBits` contains the depth bits.
  #[inline]
  pub const fn has_depth(&self) -> bool {
    self.contains(Self::DEPTH)
  }

  /// Returns whether the `ClearBits` contains the stencil bits.
  #[inline]
  pub const fn has_stencil(&self) -> bool {
    self.contains(Self::STENCIL)
  }

  /// Clears the buffers to preset values.
  #[inline]
  pub fn clear(self) {
    clear(self)
  }
}

/// Clear buffers to preset values
///
/// **Note:** Calls the [`glClear`] function
///
/// # Parameters
///
/// * `bits` - the buffers to clear
///
/// [`glClear`]: https://registry.khronos.org/OpenGL-Refpages/gl4/html/glClear.xhtml
#[inline]
pub fn clear(bits: ClearBits) {
  unsafe { crate::c::glClear(bits.0) }
}