diff options
Diffstat (limited to 'engine/src/object.rs')
-rw-r--r-- | engine/src/object.rs | 90 |
1 files changed, 85 insertions, 5 deletions
diff --git a/engine/src/object.rs b/engine/src/object.rs index a8cd076..4112bcb 100644 --- a/engine/src/object.rs +++ b/engine/src/object.rs @@ -1,3 +1,7 @@ +use std::fs::read_to_string; +use std::io::Error as IoError; +use std::path::{Path, PathBuf}; + use crate::opengl::shader::{ Error as ShaderError, Kind as ShaderKind, @@ -5,13 +9,17 @@ use crate::opengl::shader::{ Shader, }; use crate::renderer::Renderable; +use crate::shader_preprocessor::{Error as ShaderPreprocessorError, ShaderPreprocessor}; use crate::texture::Texture; use crate::transform::Transform; use crate::vector::Vec3; use crate::vertex::Vertex; -const FRAG_SHADER_COLOR: &str = include_str!("../fragment-color.glsl"); -const FRAG_SHADER_TEXTURE: &str = include_str!("../fragment-texture.glsl"); +const SHADER_DIR: &str = "engine"; + +const VERTEX_SHADER_FILE: &str = "vertex.glsl"; +const FRAG_SHADER_COLOR_FILE: &str = "fragment-color.glsl"; +const FRAG_SHADER_TEXTURE_FILE: &str = "fragment-texture.glsl"; #[derive(Debug)] pub struct Object @@ -114,16 +122,72 @@ impl Builder /// - Linking the shader program fails pub fn build(self, id: Id) -> Result<Object, Error> { + let vertex_shader_file = Path::new(SHADER_DIR).join(VERTEX_SHADER_FILE); + + let fragment_shader_color_file = + Path::new(SHADER_DIR).join(FRAG_SHADER_COLOR_FILE); + + let fragment_shader_texture_file = + Path::new(SHADER_DIR).join(FRAG_SHADER_TEXTURE_FILE); + + let vertex_shader_content = + read_to_string(&vertex_shader_file).map_err(|err| { + Error::ReadShaderFailed { + source: err, + shader_file: vertex_shader_file.clone(), + } + })?; + + let fragment_shader_color_content = read_to_string(&fragment_shader_color_file) + .map_err(|err| Error::ReadShaderFailed { + source: err, + shader_file: fragment_shader_color_file.clone(), + })?; + + let fragment_shader_texture_content = + read_to_string(&fragment_shader_texture_file).map_err(|err| { + Error::ReadShaderFailed { + source: err, + shader_file: fragment_shader_texture_file.clone(), + } + })?; + + let shader_preprocessor = ShaderPreprocessor::new(PathBuf::from(SHADER_DIR)); + + let vertex_shader_content_preprocessed = shader_preprocessor + .preprocess(vertex_shader_content, &vertex_shader_file) + .map_err(|err| Error::PreprocessShaderFailed { + source: err, + shader_file: vertex_shader_file, + })?; + + let frag_shader_color_content_preprocessed = shader_preprocessor + .preprocess(fragment_shader_color_content, &fragment_shader_color_file) + .map_err(|err| Error::PreprocessShaderFailed { + source: err, + shader_file: fragment_shader_color_file, + })?; + + let frag_shader_texture_content_preprocessed = shader_preprocessor + .preprocess( + fragment_shader_texture_content, + &fragment_shader_texture_file, + ) + .map_err(|err| Error::PreprocessShaderFailed { + source: err, + shader_file: fragment_shader_texture_file, + })?; + let vertex_shader = - Self::create_shader(ShaderKind::Vertex, include_str!("../vertex.glsl")) + Self::create_shader(ShaderKind::Vertex, &vertex_shader_content_preprocessed) .map_err(Error::CreateVertexShaderFailed)?; let fragment_shader = Self::create_shader( ShaderKind::Fragment, if self.texture.is_some() { - FRAG_SHADER_TEXTURE + &frag_shader_texture_content_preprocessed } else { - FRAG_SHADER_COLOR + &frag_shader_color_content_preprocessed }, ) .map_err(Error::CreateFragmentShaderFailed)?; @@ -173,6 +237,22 @@ pub enum Error #[error("Failed to link shader program")] LinkShaderProgramFailed(#[source] ShaderError), + + #[error("Failed to read shader {}", shader_file.display())] + ReadShaderFailed + { + #[source] + source: IoError, + shader_file: PathBuf, + }, + + #[error("Failed to preprocess shader {}", shader_file.display())] + PreprocessShaderFailed + { + #[source] + source: ShaderPreprocessorError, + shader_file: PathBuf, + }, } /// Object ID. |