diff options
Diffstat (limited to 'engine/src/object.rs')
-rw-r--r-- | engine/src/object.rs | 123 |
1 files changed, 77 insertions, 46 deletions
diff --git a/engine/src/object.rs b/engine/src/object.rs index dffd240..f2591f2 100644 --- a/engine/src/object.rs +++ b/engine/src/object.rs @@ -19,52 +19,6 @@ pub struct Object impl Object { - /// Returns a new `Object`. - /// - /// # Errors - /// Will return `Err` if shader creation fails or if shader program linking fails. - pub fn new( - id: Id, - vertices: &[Vertex], - indices: Option<&[u32]>, - ) -> Result<Self, Error> - { - let vertex_shader = Shader::new(ShaderKind::Vertex); - - vertex_shader - .set_source(include_str!("../vertex.glsl")) - .map_err(Error::CreateVertexShaderFailed)?; - - vertex_shader - .compile() - .map_err(Error::CreateVertexShaderFailed)?; - - let fragment_shader = Shader::new(ShaderKind::Fragment); - - fragment_shader - .set_source(include_str!("../fragment.glsl")) - .map_err(Error::CreateFragmentShaderFailed)?; - - fragment_shader - .compile() - .map_err(Error::CreateFragmentShaderFailed)?; - - let shader_program = ShaderProgram::new(); - - shader_program.attach(&vertex_shader); - shader_program.attach(&fragment_shader); - - shader_program - .link() - .map_err(Error::LinkShaderProgramFailed)?; - - let renderable = Renderable::new(shader_program, vertices, indices); - - let transform = Transform::new(); - - Ok(Self { id, renderable, transform }) - } - /// Returns the object ID. #[must_use] pub fn id(&self) -> Id @@ -99,6 +53,83 @@ impl Object } } +/// Object builder. +#[derive(Debug, Clone, Default)] +pub struct Builder +{ + vertices: Vec<Vertex>, + indices: Option<Vec<u32>>, +} + +impl Builder +{ + /// Returns a new [`Object`] builder. + #[must_use] + pub fn new() -> Self + { + Self::default() + } + + #[must_use] + pub fn vertices(mut self, vertices: impl IntoIterator<Item = Vertex>) -> Self + { + self.vertices = vertices.into_iter().collect(); + + self + } + + #[must_use] + pub fn indices(mut self, indices: impl IntoIterator<Item = u32>) -> Self + { + self.indices = Some(indices.into_iter().collect()); + + self + } + + /// Builds a new [`Object`]. + /// + /// # Errors + /// Will return `Err` if: + /// - Creating shaders fails + /// - Linking the shader program fails + pub fn build(&self, id: Id) -> Result<Object, Error> + { + let vertex_shader = + Self::create_shader(ShaderKind::Vertex, include_str!("../vertex.glsl")) + .map_err(Error::CreateVertexShaderFailed)?; + + let fragment_shader = + Self::create_shader(ShaderKind::Fragment, include_str!("../fragment.glsl")) + .map_err(Error::CreateFragmentShaderFailed)?; + + let shader_program = ShaderProgram::new(); + + shader_program.attach(&vertex_shader); + shader_program.attach(&fragment_shader); + + shader_program + .link() + .map_err(Error::LinkShaderProgramFailed)?; + + let renderable = + Renderable::new(shader_program, &self.vertices, self.indices.as_deref()); + + let transform = Transform::new(); + + Ok(Object { id, renderable, transform }) + } + + fn create_shader(kind: ShaderKind, source: &str) -> Result<Shader, ShaderError> + { + let shader = Shader::new(kind); + + shader.set_source(source)?; + shader.compile()?; + + Ok(shader) + } +} + /// Object error #[derive(Debug, thiserror::Error)] pub enum Error |