From 715bbaf459b88575e11d90ec16bad3841bafd259 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 7 Oct 2023 20:52:09 +0200 Subject: feat(engine): add ability to render triangles --- engine/src/object.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 engine/src/object.rs (limited to 'engine/src/object.rs') diff --git a/engine/src/object.rs b/engine/src/object.rs new file mode 100644 index 0000000..ff84127 --- /dev/null +++ b/engine/src/object.rs @@ -0,0 +1,69 @@ +use crate::renderer::Renderable; +use crate::shader::{Kind as ShaderKind, Program as ShaderProgram, Shader}; + +pub struct Object +{ + renderable: Renderable, +} + +impl Object +{ + /// Returns a new `Object`. + /// + /// # Errors + /// Will return `Err` if shader creation fails or if shader program linking fails. + pub fn new(vertices: &[f32]) -> Result + { + 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); + + Ok(Self { renderable }) + } + + pub(crate) fn renderable(&self) -> &Renderable + { + &self.renderable + } +} + +/// Object error +#[derive(Debug, thiserror::Error)] +pub enum Error +{ + #[error("Failed to create vertex shader")] + CreateVertexShaderFailed(#[source] crate::shader::Error), + + #[error("Failed to create fragment shader")] + CreateFragmentShaderFailed(#[source] crate::shader::Error), + + #[error("Failed to link shader program")] + LinkShaderProgramFailed(#[source] crate::shader::Error), +} -- cgit v1.2.3-18-g5258