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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use glium::Surface;
use glium::uniforms::Uniforms;

use shapes::{Shape, IndexType};
use shapes::mould::Mould;
use errors::ProcessingErr;

use Screen;

impl<'a> Screen<'a> {
	/// Take a given shape and draw it onto the screen. Since the shape's properties
	/// were precomputed and its buffers were already uploaded to the GPU, drawing
	/// many shapes should be faster than in a standard Processing environment.
    #[inline]
    pub fn draw<S: Shape>(&mut self, shape: &S) -> Result<(), ProcessingErr> {
        let framebuffer = &mut self.fbo;
        if let Some(tex) = shape.get_texture() {
            let prog = &self.shader_bank[1];
            let u = create_uniforms!{self, tex: *tex};
            if self.fill_stuff {
                match *shape.fill_indices() {
                    &IndexType::Buffer { ind: ref ib } => {
                        framebuffer
                            .draw(*shape.fill_buffer(), ib, prog, &u, &self.draw_params)
                            .map_err(|e| ProcessingErr::FBDrawFailed(e))?
                    }
                    &IndexType::NoBuffer { ind: ref ib } => {
                        framebuffer
                            .draw(*shape.fill_buffer(), ib, prog, &u, &self.draw_params)
                            .map_err(|e| ProcessingErr::FBDrawFailed(e))?
                    }
                }
            };
            if self.stroke_stuff {
                match *shape.stroke_indices() {
                    &IndexType::NoBuffer { ind: ref ib } => {
                        framebuffer
                            .draw(*shape.stroke_buffer(), ib, prog, &u, &self.draw_params)
                            .map_err(|e| ProcessingErr::FBDrawFailed(e))?
                    }
                    _ => {}
                }
            };
        } else {
            let prog = &self.shader_bank[0];
            let u = create_uniforms!{self};
            if self.fill_stuff {
                match *shape.fill_indices() {
                    &IndexType::Buffer { ind: ref ib } => {
                        framebuffer
                            .draw(*shape.fill_buffer(), ib, prog, &u, &self.draw_params)
                            .map_err(|e| ProcessingErr::FBDrawFailed(e))?
                    }
                    &IndexType::NoBuffer { ind: ref ib } => {
                        framebuffer
                            .draw(*shape.fill_buffer(), ib, prog, &u, &self.draw_params)
                            .map_err(|e| ProcessingErr::FBDrawFailed(e))?
                    }
                }
            };
            if self.stroke_stuff {
                match *shape.stroke_indices() {
                    &IndexType::NoBuffer { ind: ref ib } => {
                        framebuffer
                            .draw(*shape.stroke_buffer(), ib, prog, &u, &self.draw_params)
                            .map_err(|e| ProcessingErr::FBDrawFailed(e))?
                    }
                    _ => {}
                }
            };
        }
        
        Ok(())

        // if shape.drew_points {
        // self.draw_params.smooth = Some(glium::draw_parameters::Smooth::Nicest);
        // self.drew_points = false;
        // }
    }

	/// The same as screen.draw(), except now a Mould will be drawn to the screen.
	/// A Mould is just a shape that has been paired with a given shader which
	/// alters how it is typically drawn to the screen. This allows one to have
	/// a shader affect only one object instead of the whole drawing process.
	/// The concept is borrowed from libCinder.
    #[inline]
    pub fn draw_mould<S: Shape, U: Uniforms>(&mut self, mould: &Mould<U, S>) -> Result<(), ProcessingErr> {
        let shader = mould.get_shader();
        let shape = mould.get_shape();
        let prog = &self.shader_bank[shader.get_idx()];
        let uniforms = shader.get_uniforms();
        let framebuffer = &mut self.fbo;
        if self.fill_stuff {
            match *shape.fill_indices() {
                &IndexType::Buffer { ind: ref ib } => {
                    framebuffer
                        .draw(*shape.fill_buffer(), ib, prog, uniforms, &self.draw_params)
                        .map_err(|e| ProcessingErr::FBDrawFailed(e))?
                }
                &IndexType::NoBuffer { ind: ref ib } => {
                    framebuffer
                        .draw(*shape.fill_buffer(), ib, prog, uniforms, &self.draw_params)
                        .map_err(|e| ProcessingErr::FBDrawFailed(e))?
                }
            }
        };
        if self.stroke_stuff {
            match *shape.stroke_indices() {
                &IndexType::NoBuffer { ind: ref ib } => {
                    framebuffer
                        .draw(
                            *shape.stroke_buffer(),
                            ib,
                            &prog,
                            uniforms,
                            &self.draw_params,
                        )
                        .map_err(|e| ProcessingErr::FBDrawFailed(e))?
                }
                _ => {}
            }
        };
        
        Ok(())
    }

    // #[inline]
    // pub fn draw_onto_texture<S: Shape>(&self, shape: &S, tex: &glium::texture::Texture2d) {
    //     let mut framebuffer = tex.as_surface();
    //     let mut t = self.draw_params.clone();
    //     t.depth.write = false;
    //     if let Some(tex) = shape.get_texture() {
    //         let prog = &self.shader_bank[1];
    //         let u = create_uniforms!{self, tex: *tex};
    //         if self.fill_stuff {
    //             match *shape.fill_indices() {
    //                 &IndexType::Buffer { ind: ref ib } => {
    //                     framebuffer.draw(*shape.fill_buffer(), ib, prog, &u, &t)
    //                         .unwrap()
    //                 }
    //                 &IndexType::NoBuffer { ind: ref ib } => {
    //                     framebuffer.draw(*shape.fill_buffer(), ib, prog, &u, &t)
    //                         .unwrap()
    //                 }
    //             }
    //         }
    //         if self.stroke_stuff {
    //             match *shape.stroke_indices() {
    //                 &IndexType::NoBuffer { ind: ref ib } => {
    //                     framebuffer.draw(*shape.stroke_buffer(), ib, prog, &u, &t)
    //                         .unwrap()
    //                 }
    //                 _ => {}
    //             }
    //         }
    //     } else {
    //         let prog = &self.shader_bank[0];
    //         let u = create_uniforms!{self};
    //         if self.fill_stuff {
    //             match *shape.fill_indices() {
    //                 &IndexType::Buffer { ind: ref ib } => {
    //                     framebuffer.draw(*shape.fill_buffer(), ib, prog, &u, &t)
    //                         .unwrap()
    //                 }
    //                 &IndexType::NoBuffer { ind: ref ib } => {
    //                     framebuffer.draw(*shape.fill_buffer(), ib, prog, &u, &t)
    //                         .unwrap()
    //                 }
    //             }
    //         }
    //         if self.stroke_stuff {
    //             match *shape.stroke_indices() {
    //                 &IndexType::NoBuffer { ind: ref ib } => {
    //                     framebuffer.draw(*shape.stroke_buffer(), ib, prog, &u, &t)
    //                         .unwrap()
    //                 }
    //                 _ => {}
    //             }
    //         }
    //     }

    //     // if self.drew_points {
    //     // self.draw_params.smooth = Some(glium::draw_parameters::Smooth::Nicest);
    //     // self.drew_points = false;
    //     // }
    // }

    // #[inline]
    // pub fn draw_mould_onto_texture<S: Shape, U: Uniforms>(&self,
    //                                                       mould: &Mould<U, S>,
    //                                                       tex: &glium::texture::Texture2d) {
    //     let shader = mould.get_shader();
    //     let shape = mould.get_shape();
    //     let prog = &self.shader_bank[shader.get_idx()];
    //     let uniforms = shader.get_uniforms();
    //     let mut framebuffer = tex.as_surface();
    //     let mut t = self.draw_params.clone();
    //     t.depth.write = false;
    //     if self.fill_stuff {
    //         match *shape.fill_indices() {
    //             &IndexType::Buffer { ind: ref ib } => {
    //                 framebuffer.draw(*shape.fill_buffer(), ib, prog, uniforms, &t)
    //                     .unwrap()
    //             }
    //             &IndexType::NoBuffer { ind: ref ib } => {
    //                 framebuffer.draw(*shape.fill_buffer(), ib, prog, uniforms, &t)
    //                     .unwrap()
    //             }
    //         }
    //     }
    //     if self.stroke_stuff {
    //         match *shape.stroke_indices() {
    //             &IndexType::NoBuffer { ind: ref ib } => {
    //                 framebuffer.draw(*shape.stroke_buffer(), ib, &prog, uniforms, &t)
    //                     .unwrap()
    //             }
    //             _ => {}
    //         }
    //     }
    // }

    pub fn stroke_weight(&mut self, new_weight: f32) {
        self.draw_params.point_size = Some(new_weight);
        self.draw_params.line_width = Some(new_weight);
    }

    pub fn ellipse_mode(&mut self, mode: &str) {
        self.ellipse_mode = mode.to_owned();
    }

    pub fn rect_mode(&mut self, mode: &str) {
        self.rect_mode = mode.to_owned();
    }

    pub fn shape_mode(&mut self, mode: &str) {
        self.shape_mode = mode.to_owned();
    }
}