summaryrefslogtreecommitdiff
path: root/opengl-bindings/src/blending.rs
blob: 5e8c1effc33d915b4b00b02e24ede7341f0ca177 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use crate::CurrentContextWithFns;

pub fn configure(
    current_context: &CurrentContextWithFns<'_>,
    configuration: Configuration,
)
{
    unsafe {
        current_context.fns().BlendFunc(
            configuration.source_factor.into_gl(),
            configuration.destination_factor.into_gl(),
        );

        current_context
            .fns()
            .BlendEquation(configuration.equation.into_gl());
    }
}

#[derive(Debug, Clone)]
pub struct Configuration
{
    source_factor: Factor,
    destination_factor: Factor,
    equation: Equation,
}

impl Configuration
{
    pub fn with_source_factor(mut self, source_factor: Factor) -> Self
    {
        self.source_factor = source_factor;
        self
    }

    pub fn with_destination_factor(mut self, destination_factor: Factor) -> Self
    {
        self.destination_factor = destination_factor;
        self
    }

    pub fn with_equation(mut self, equation: Equation) -> Self
    {
        self.equation = equation;
        self
    }
}

impl Default for Configuration
{
    fn default() -> Self
    {
        Self {
            source_factor: Factor::One,
            destination_factor: Factor::Zero,
            equation: Equation::default(),
        }
    }
}

#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum Factor
{
    /// Factor will be the RGBA color `(0,0,0,0)`
    Zero,

    /// Factor will be the RGBA color `(1,1,1,1)`
    One,

    /// Factor will be the source color
    SrcColor,

    /// Factor will be the RGBA color `(1,1,1,1) - source color`
    OneMinusSrcColor,

    /// Factor will be the destination color
    DstColor,

    /// Factor will be the RGBA color `(1,1,1,1) - destination color`
    OneMinusDstColor,

    /// Factor will be the alpha component of the source color.
    SrcAlpha,

    /// Factor will be the RGBA color `(1,1,1,1) - source color alpha`
    OneMinusSrcAlpha,

    /// Factor will be the alpha component of the destination color.
    DstAlpha,

    /// Factor will be the RGBA color `(1,1,1,1) - destination color alpha`
    OneMinusDstAlpha,

    /// Factor will be the constant color
    ConstantColor,

    /// Factor will be the RGBA color `(1,1,1,1) - constant color`
    OneMinusConstantColor,

    /// Factor will be the alpha component of the constant color.
    ConstantAlpha,

    /// Factor will be the RGBA color `(1,1,1,1) - constant color alpha`
    OneMinusConstantAlpha,
    // /// (i,i,i,1)
    // SrcAlphaSaturate,
    //
    // /// (Rs1kR,Gs1kG,Bs1kB,As1kA)
    // Src1Color,
    //
    // /// (1,1,1,1)−(Rs1kR,Gs1kG,Bs1kB,As1kA)
    // OneMinusSrc1Color,
    //
    // /// (As1kA,As1kA,As1kA,As1kA)
    // Src1Alpha,
    //
    // /// (1,1,1,1)−(As1kA,As1kA,As1kA,As1kA)
    // OneMinusSrc1Alpha,
}

impl Factor
{
    fn into_gl(self) -> crate::sys::types::GLenum
    {
        match self {
            Self::Zero => crate::sys::ZERO,
            Self::One => crate::sys::ONE,
            Self::SrcColor => crate::sys::SRC_COLOR,
            Self::OneMinusSrcColor => crate::sys::ONE_MINUS_SRC_COLOR,
            Self::DstColor => crate::sys::DST_COLOR,
            Self::OneMinusDstColor => crate::sys::ONE_MINUS_DST_COLOR,
            Self::SrcAlpha => crate::sys::SRC_ALPHA,
            Self::OneMinusSrcAlpha => crate::sys::ONE_MINUS_SRC_ALPHA,
            Self::DstAlpha => crate::sys::DST_ALPHA,
            Self::OneMinusDstAlpha => crate::sys::ONE_MINUS_DST_ALPHA,
            Self::ConstantColor => crate::sys::CONSTANT_COLOR,
            Self::OneMinusConstantColor => crate::sys::ONE_MINUS_CONSTANT_COLOR,
            Self::ConstantAlpha => crate::sys::CONSTANT_ALPHA,
            Self::OneMinusConstantAlpha => crate::sys::ONE_MINUS_CONSTANT_ALPHA,
        }
    }
}

#[derive(Debug, Default, Clone, Copy)]
pub enum Equation
{
    /// The destination color and source color is added to each other in the blend
    /// function
    #[default]
    Add,

    /// The destination color is subtracted from the source color in the blend function
    Subtract,

    /// The source color is subtracted from the destination color in the blend function
    ReverseSubtract,

    /// The blend function will take the component-wise minimum of the destination color
    /// and the source color
    Min,

    /// The blend function will take the component-wise maximum of the destination color
    /// and the source color
    Max,
}

impl Equation
{
    fn into_gl(self) -> crate::sys::types::GLenum
    {
        match self {
            Self::Add => crate::sys::FUNC_ADD,
            Self::Subtract => crate::sys::FUNC_SUBTRACT,
            Self::ReverseSubtract => crate::sys::FUNC_REVERSE_SUBTRACT,
            Self::Min => crate::sys::MIN,
            Self::Max => crate::sys::MAX,
        }
    }
}