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
use glin;
use ::Material;
use color::{Rgba,ToRgba};
use color::consts::*;
use std::borrow::Borrow;
use glin::RenderSurface;

pub struct BasicMaterial<T = glin::Texture>{
    color: Option<Rgba<f32>>,
    texture: Option<T>,
    properties: Vec<glin::Property>
}

pub struct Builder{
    properties: Vec<glin::Property>,
    color: Option<Rgba<f32>>
}

pub struct BuilderWithTexture<T = glin::Texture>{
    properties: Vec<glin::Property>,
    color: Option<Rgba<f32>>,
    texture: T,
}

impl Builder{
    pub fn new() -> Builder{
        Builder{
            properties: vec![],
            color: None,
        }
    }

    pub fn color<C: ToRgba>(mut self, color: &C) -> Builder{
        self.color = Some(color.to_rgba());
        self
    }

    pub fn texture<T: Borrow<glin::Texture>>(self, texture: T) -> BuilderWithTexture<T>{
        BuilderWithTexture{
            properties: self.properties,
            color: self.color,
            texture: texture,
        }
    }

    pub fn properties(mut self, properties: Vec<glin::Property>) -> Builder{
    	self.properties = properties;
        self
    }

    pub fn create(self) -> BasicMaterial{
        BasicMaterial{
            color: self.color,
            texture: None,
            properties: self.properties,
        }
    }
}

impl<T: Borrow<glin::Texture>> BuilderWithTexture<T>{
    pub fn color<C: ToRgba>(mut self, color: &C) -> BuilderWithTexture<T>{
        self.color = Some(color.to_rgba());
        self
    }

    pub fn texture(self, texture: T) -> BuilderWithTexture<T>{
        BuilderWithTexture{
            properties: self.properties,
            color: self.color,
            texture: texture
        }
    }

    pub fn properties(mut self, properties: Vec<glin::Property>) -> BuilderWithTexture<T>{
    	self.properties = properties;
        self
    }

    pub fn create(self) -> BasicMaterial<T>{
        BasicMaterial{
            color: self.color,
            texture: Some(self.texture),
            properties: self.properties,
        }
    }
}

impl Default for BasicMaterial{
    fn default() -> BasicMaterial{
        Builder::new().create()
    }
}

impl<T: Borrow<glin::Texture>> BasicMaterial<T>{
    pub fn color(&self) -> Rgba<f32>{
        self.color.unwrap_or(WHITE.to_rgba())
    }

    pub fn texture(&self) -> Option<&glin::Texture>{
        self.texture.as_ref().map(|t| t.borrow())
    }
}

impl<T: Borrow<glin::Texture>> Material for BasicMaterial<T>{
    fn program<R: RenderSurface>(&self, renderer: &::Renderer<R>) -> &glin::Program{
        if self.texture.is_some() {
            shaders::get_texture_program(renderer)
        }else if self.color.is_some(){
            shaders::get_color_program(renderer)
        }else{
            shaders::get_color_coords_program(renderer)
        }
    }

    fn uniforms<R: RenderSurface>(&self, _: &::Renderer<R>) -> Vec<glin::program::Uniform>{
        if let Some(t) = self.texture.as_ref(){
            uniforms!{
                color_uniform: self.color(),
                tex: (t.borrow(), 0),
            }
        }else{
            uniforms!{
                color_uniform: self.color(),
            }
        }
    }

    fn properties(&self) -> Vec<glin::Property>{
    	self.properties.clone()
    }
}


#[cfg(not(any(target_os="android", target_os="emscripten")))]
mod shaders{
    program_cache!("shaders/vertex.glsl", "shaders/fragment_color_coords.glsl", get_color_coords_program, SHADER);
    program_cache!("shaders/vertex.glsl", "shaders/fragment_color.glsl", get_color_program, COLOR_SHADER);
    program_cache!("shaders/vertex.glsl", "shaders/fragment_tex.glsl", get_texture_program, TEXTURE_SHADER);
}

#[cfg(any(target_os="android", target_os="emscripten"))]
mod shaders{
    program_cache!("shaders/vertex_gles.glsl", "shaders/fragment_color_coords_gles.glsl", get_color_coords_program, SHADER);
    program_cache!("shaders/vertex_gles.glsl", "shaders/fragment_color_gles.glsl", get_color_program, COLOR_SHADER);
    program_cache!("shaders/vertex_gles.glsl", "shaders/fragment_tex_gles.glsl", get_texture_program, TEXTURE_SHADER);
}