diff options
Diffstat (limited to 'engine/src/object.rs')
-rw-r--r-- | engine/src/object.rs | 92 |
1 files changed, 39 insertions, 53 deletions
diff --git a/engine/src/object.rs b/engine/src/object.rs index 6749124..4e143a0 100644 --- a/engine/src/object.rs +++ b/engine/src/object.rs @@ -1,8 +1,9 @@ +use std::collections::HashMap; use std::fs::read_to_string; use std::io::Error as IoError; use std::path::{Path, PathBuf}; -use crate::material::{Builder as MaterialBuilder, Material}; +use crate::material::Material; use crate::opengl::shader::{ Error as ShaderError, Kind as ShaderKind, @@ -11,7 +12,7 @@ use crate::opengl::shader::{ }; use crate::renderer::Renderable; use crate::shader_preprocessor::{Error as ShaderPreprocessorError, ShaderPreprocessor}; -use crate::texture::Texture; +use crate::texture::{Id as TextureId, Texture}; use crate::transform::Transform; use crate::vector::Vec3; use crate::vertex::Vertex; @@ -19,8 +20,7 @@ use crate::vertex::Vertex; 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"; +const FRAGMENT_SHADER_FILE: &str = "fragment.glsl"; #[derive(Debug)] pub struct Object @@ -28,7 +28,7 @@ pub struct Object id: Id, renderable: Renderable, transform: Transform, - texture: Option<Texture>, + textures: HashMap<TextureId, Texture>, material: Material, } @@ -58,10 +58,9 @@ impl Object self.transform.set_scale(scaling); } - #[must_use] - pub fn texture(&self) -> Option<&Texture> + pub fn textures(&self) -> impl Iterator<Item = (&TextureId, &Texture)> { - self.texture.as_ref() + self.textures.iter() } #[must_use] @@ -87,8 +86,8 @@ pub struct Builder { vertices: Vec<Vertex>, indices: Option<Vec<u32>>, - texture: Option<Texture>, - material: Material, + textures: HashMap<TextureId, Texture>, + material: Option<Material>, } impl Builder @@ -100,8 +99,8 @@ impl Builder Self { vertices: Vec::new(), indices: None, - texture: None, - material: MaterialBuilder::new().build(), + textures: HashMap::new(), + material: None, } } @@ -122,9 +121,19 @@ impl Builder } #[must_use] - pub fn texture(mut self, texture: Texture) -> Self + pub fn texture(mut self, id: TextureId, texture: Texture) -> Self + { + self.textures.insert(id, texture); + + self + } + + #[must_use] + pub fn textures<Textures>(mut self, textures: Textures) -> Self + where + Textures: IntoIterator<Item = (TextureId, Texture)>, { - self.texture = Some(texture); + self.textures.extend(textures); self } @@ -132,7 +141,7 @@ impl Builder #[must_use] pub fn material(mut self, material: Material) -> Self { - self.material = material; + self.material = Some(material); self } @@ -147,11 +156,7 @@ impl Builder { 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 fragment_shader_file = Path::new(SHADER_DIR).join(FRAGMENT_SHADER_FILE); let vertex_shader_content = read_to_string(&vertex_shader_file).map_err(|err| { @@ -161,17 +166,11 @@ impl Builder } })?; - 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| { + let fragment_shader_content = + read_to_string(&fragment_shader_file).map_err(|err| { Error::ReadShaderFailed { source: err, - shader_file: fragment_shader_texture_file.clone(), + shader_file: fragment_shader_file.clone(), } })?; @@ -184,36 +183,20 @@ impl Builder shader_file: vertex_shader_file, })?; - let frag_shader_color_content_preprocessed = shader_preprocessor - .preprocess(fragment_shader_color_content, &fragment_shader_color_file) + let frag_shader_content_preprocessed = shader_preprocessor + .preprocess(fragment_shader_content, &fragment_shader_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, + shader_file: fragment_shader_file, })?; let vertex_shader = 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_content_preprocessed - } else { - &frag_shader_color_content_preprocessed - }, - ) - .map_err(Error::CreateFragmentShaderFailed)?; + let fragment_shader = + Self::create_shader(ShaderKind::Fragment, &frag_shader_content_preprocessed) + .map_err(Error::CreateFragmentShaderFailed)?; let shader_program = ShaderProgram::new(); @@ -233,8 +216,8 @@ impl Builder id, renderable, transform, - texture: self.texture, - material: self.material, + textures: self.textures, + material: self.material.ok_or(Error::NoMaterial)?, }) } @@ -285,6 +268,9 @@ pub enum Error source: ShaderPreprocessorError, shader_file: PathBuf, }, + + #[error("Object builder has no material")] + NoMaterial, } /// Object ID. |