pub mod default;
pub mod dist;
pub mod height;
pub mod line;
pub mod post;
pub mod texture;
use glium::texture::compressed_srgb_texture2d::CompressedSrgbTexture2d;
use glium::{Display, Program};
use std::collections::HashMap;
use std::error::Error;
pub struct Shaders {
pub shaders: HashMap<&'static str, Program>,
pub post_shaders: HashMap<&'static str, Program>,
pub textures: HashMap<&'static str, CompressedSrgbTexture2d>,
}
impl Shaders {
pub fn new(display: &Display) -> Shaders {
let mut shaders = HashMap::new();
shaders.insert(
"dist",
program!(display,
330 => {
vertex: default::gl330::VERT,
fragment: dist::gl330::FRAG,
geometry: default::gl330::GEOM,
tessellation_control: default::gl330::TESS_CONTROL,
tessellation_evaluation: default::gl330::TESS_EVAL
})
.unwrap(),
);
shaders.insert(
"height",
program!(display,
330 => {
vertex: default::gl330::VERT,
fragment: height::gl330::FRAG,
geometry: default::gl330::GEOM,
tessellation_control: default::gl330::TESS_CONTROL,
tessellation_evaluation: default::gl330::TESS_EVAL
})
.unwrap(),
);
shaders.insert(
"line",
program!(display,
330 => {
vertex: default::gl330::VERT,
fragment: line::gl330::FRAG,
geometry: line::gl330::GEOM,
tessellation_control: default::gl330::TESS_CONTROL,
tessellation_evaluation: default::gl330::TESS_EVAL
})
.unwrap(),
);
shaders.insert(
"texture",
program!(display,
330 => {
vertex: default::gl330::VERT,
fragment: texture::gl330::FRAG,
geometry: default::gl330::GEOM,
tessellation_control:
default::gl330::TESS_CONTROL,
tessellation_evaluation:
default::gl330::TESS_EVAL
})
.unwrap(),
);
let mut post_shaders = HashMap::new();
post_shaders.insert(
"default",
program!(display,
330 => {
vertex: post::gl330::VERT,
fragment: post::gl330::FRAG,
})
.unwrap(),
);
let mut textures = HashMap::new();
textures.insert("default", load_texture!("../resources/caper.png", display));
textures.insert(
"default_normal",
load_texture!("../resources/normal.png", display),
);
Shaders {
shaders,
post_shaders,
textures,
}
}
pub fn add_shader(
&mut self,
display: &Display,
name: &'static str,
vert: &'static str,
frag: &'static str,
geom: &'static str,
tess_cont: &'static str,
tess_eval: &'static str,
) -> Result<&str, &str> {
let shader_prog = match program!(display,
330 => {
vertex: vert,
fragment: frag,
geometry: geom,
tessellation_control: tess_cont,
tessellation_evaluation: tess_eval,
}) {
Ok(s) => s,
Err(e) => {
println!("{}", e.source().unwrap_or(&e));
return Err("Could not create shader");
}
};
self.shaders.insert(name, shader_prog);
Ok("shader added")
}
pub fn add_post_shader(
&mut self,
display: &Display,
name: &'static str,
vert: &'static str,
frag: &'static str,
) -> Result<&str, &str> {
let post_shader_prog = match program!(display,
330 => {
vertex: vert,
fragment: frag
}) {
Ok(s) => s,
Err(e) => {
println!("{}", e.source().unwrap_or(&e));
return Err("Could not create post shader");
}
};
self.post_shaders.insert(name, post_shader_prog);
Ok("post shader added")
}
}