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
use prelude::*;
use core::texture::Texture;

/// A uniform value.
///
/// Uniforms are values that can be passed to [`Programs`](struct.Program.html).
/// Various types also implement the [`AsUniform`](trait.AsUniform.html) trait
/// and can be directly used with [`Program::set_uniform()`](struct.Program.html#method.set_uniform).
#[derive(Clone)]
pub enum Uniform {
    Bool(bool),
    SignedInt(i32),
    UnsignedInt(u32),
    Float(f32),
    Mat4([[f32; 4]; 4]),
    Vec2([f32; 2]),
    Vec3([f32; 3]),
    Vec4([f32; 4]),
    Double(f64),
    DoubleMat4([[f64; 4]; 4]),
    DoubleVec2([f64; 2]),
    DoubleVec3([f64; 3]),
    DoubleVec4([f64; 4]),
    Texture(Texture),
}

/// A value usable as a uniform.
pub trait AsUniform {
    fn as_uniform(self: &Self) -> Uniform;
}

/// Multiple uniforms held by a program.
#[derive(Clone)]
pub struct UniformList (pub (crate) HashMap<String, Uniform>);

impl UniformList {
    /// Creates a new uniform list.
    pub fn new() -> Self {
        UniformList(HashMap::new())
    }
    /// Inserts a uniform into the list.
    pub fn insert(self: &mut Self, name: &str, uniform: Uniform) {
        self.0.insert(name.to_string(), uniform);
    }
    /// Removes a uniform from the list and returns whether it existed.
    pub fn remove(self: &mut Self, name: &str) -> bool {
        self.0.remove(name).is_some()
    }
}

impl AsUniform for bool {
    fn as_uniform(self: &Self) -> Uniform {
        Uniform::Bool(*self)
    }
}

impl AsUniform for i32 {
    fn as_uniform(self: &Self) -> Uniform {
        Uniform::SignedInt(*self)
    }
}

impl AsUniform for u32 {
    fn as_uniform(self: &Self) -> Uniform {
        Uniform::UnsignedInt(*self)
    }
}

impl AsUniform for f32 {
    fn as_uniform(self: &Self) -> Uniform {
        Uniform::Float(*self)
    }
}

impl AsUniform for f64 {
    fn as_uniform(self: &Self) -> Uniform {
        Uniform::Double(*self)
    }
}