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
use std::fs;
use std::path::Path;
use std::rc::Rc;
use error::{Result, TetraError};
use graphics::opengl::GLProgram;
use Context;
#[derive(Clone)]
pub struct Shader {
pub(crate) handle: Rc<GLProgram>,
}
impl Shader {
pub fn new(ctx: &mut Context, vertex_shader: &str, fragment_shader: &str) -> Shader {
Shader::from_handle(ctx.gl.compile_program(vertex_shader, fragment_shader))
}
pub fn from_file<P: AsRef<Path>>(
ctx: &mut Context,
vertex_path: P,
fragment_path: P,
) -> Result<Shader> {
Ok(Shader::new(
ctx,
&fs::read_to_string(vertex_path).map_err(TetraError::Io)?,
&fs::read_to_string(fragment_path).map_err(TetraError::Io)?,
))
}
pub(crate) fn from_handle(handle: GLProgram) -> Shader {
Shader {
handle: Rc::new(handle),
}
}
}