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
//! Types and methods for setting shader uniforms


// External crates.
use std::ffi::CString;
use gl;
use gl::types::{GLboolean, GLint};
use std::marker::PhantomData;

// Local crate.
use back_end::GlGraphics;

/// Describes a shader uniform of a given type.
#[derive(Clone, Copy)]
pub struct ShaderUniform<T : ?Sized>{
    location : GLint,
    phantom : PhantomData<T>,
}

/// Shader uniform type
///
/// For now a small subset
pub trait UniformType {}

/// Shader uniform float
#[derive(Clone, Copy)]
pub struct SUFloat {}
impl UniformType for SUFloat {}

/// Shader uniform integer
#[derive(Clone, Copy)]
pub struct SUInt {}
impl UniformType for SUInt {}

/// Shader uniform vector of size 2
/// Vector elements are floats
#[derive(Clone, Copy)]
pub struct SUVec2 {}
impl UniformType for SUVec2 {}

/// Shader uniform vector of size 3
/// Vector elements are floats
#[derive(Clone, Copy)]
pub struct SUVec3 {}
impl UniformType for SUVec3 {}

/// Shader uniform vector of size 4
/// Vector elements are floats
#[derive(Clone, Copy)]
pub struct SUVec4 {}
impl UniformType for SUVec4 {}

/// Shader uniform 2x2 matrix
/// Matrix elements are floats
#[derive(Clone, Copy)]
pub struct SUMat2x2 {}
impl UniformType for SUMat2x2 {}

/// Shader uniform 3x3 matrix
/// Matrix elements are floats
#[derive(Clone, Copy)]
pub struct SUMat3x3 {}
impl UniformType for SUMat3x3 {}

/// Shader uniform 4x4 matrix
/// Matrix elements are floats
#[derive(Clone, Copy)]
pub struct SUMat4x4 {}
impl UniformType for SUMat4x4 {}

impl GlGraphics {
    /// Try to get uniform from the current shader of a given name.
    pub fn get_uniform<T : UniformType + ?Sized>(&self, name : &str) -> Option<ShaderUniform<T>> {
        self.get_current_program().and_then( |p| {
            unsafe {
                let c_source = CString::new(name).ok();
                c_source.and_then(|name| {
                    let uniform = match gl::GetUniformLocation(p, name.as_ptr()) {
                        -1 => None,
                        location => {
                            Some(ShaderUniform{
                                location : location,
                                phantom : PhantomData,
                            })
                        },
                    };
                    drop(name);
                    uniform
                })
            }
        })
    }
}

impl ShaderUniform<SUFloat> {
    /// Set the value of the float uniform.
    pub fn set(&self, gl : &GlGraphics, value : f32) {
        gl.get_current_program().map(|p| {
            unsafe {gl::ProgramUniform1f(p, self.location, value)}
        });
    }
}

impl ShaderUniform<SUInt> {
    /// Set the value of the integer uniform.
    pub fn set(&self, gl : &GlGraphics, value : i32) {
        gl.get_current_program().map(|p| {
            unsafe {gl::ProgramUniform1i(p, self.location, value)}
        });
    }
}

impl ShaderUniform<SUVec2> {
    /// Set the value of the vector 2 uniform.
    pub fn set(&self, gl : &GlGraphics, value : &[f32; 2]) {
        gl.get_current_program().map(|p| {
            unsafe {gl::ProgramUniform2f(p, self.location, value[0], value[1])}
        });
    }
}

impl ShaderUniform<SUVec3> {
    /// Set the value of the vector 3 uniform.
    pub fn set(&self, gl : &GlGraphics, value : &[f32; 3]) {
        gl.get_current_program().map(|p| {
            unsafe {gl::ProgramUniform3f(p, self.location, value[0], value[1], value[2])}
        });
    }
}

impl ShaderUniform<SUVec4> {
    /// Set the value of the vector 4 uniform.
    pub fn set(&self, gl : &GlGraphics, value : &[f32; 4]) {
        gl.get_current_program().map(|p| {
            unsafe {gl::ProgramUniform4f(p, self.location, value[0], value[1], value[2], value[3])}
        });
    }
}

impl ShaderUniform<SUMat2x2> {
    /// Set the value of the 2x2 matrix uniform.
    pub fn set(&self, gl : &GlGraphics, values : &[f32; 4]) {
        gl.get_current_program().map(|p| {
            unsafe {gl::ProgramUniformMatrix2fv(p, self.location, 1 as GLint, false as GLboolean, values.as_ptr())}
        });
    }
}

impl ShaderUniform<SUMat3x3> {
    /// Set the value of the 3x3 matrix uniform.
    pub fn set(&self, gl : &GlGraphics, values : &[f32; 9]) {
        gl.get_current_program().map(|p| {
            unsafe {gl::ProgramUniformMatrix3fv(p, self.location, 1 as GLint, false as GLboolean, values.as_ptr())}
        });
    }
}

impl ShaderUniform<SUMat4x4> {
    /// Set the value of the 4x4 matrix uniform.
    pub fn set(&self, gl : &GlGraphics, values : &[f32; 16]) {
        gl.get_current_program().map(|p| {
            unsafe {gl::ProgramUniformMatrix4fv(p, self.location, 1 as GLint, false as GLboolean, values.as_ptr())}
        });
    }
}