From 2b8f8ba255ef772f043cb27a49a55086df12718f Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 14 Apr 2024 22:36:20 +0200 Subject: feat(engine): add default shaders when creating shader program --- engine/src/shader.rs | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) (limited to 'engine/src') diff --git a/engine/src/shader.rs b/engine/src/shader.rs index a0066f1..89f7b7c 100644 --- a/engine/src/shader.rs +++ b/engine/src/shader.rs @@ -7,6 +7,11 @@ use ecs::Component; use crate::shader_preprocessor::{Error as ShaderPreprocessorError, ShaderPreprocessor}; +const VERTEX_SHADER_FILE: &str = "vertex.glsl"; +const FRAGMENT_SHADER_FILE: &str = "fragment.glsl"; + +const SHADER_DIR: &str = "engine"; + /// Shader program #[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Component)] pub struct Program @@ -16,10 +21,33 @@ pub struct Program impl Program { - #[must_use] - pub fn new() -> Self + /// Creates a new shader program with the default shaders. + /// + /// # Errors + /// Returns `Err` if: + /// - Reading a default shader file Fails + /// - Preprocessing a shader fails. + pub fn new() -> Result { - Self { shaders: Vec::new() } + let mut program = Self { shaders: Vec::new() }; + + program.push_shader( + Shader::read_shader_file( + Kind::Vertex, + &Path::new(SHADER_DIR).join(VERTEX_SHADER_FILE), + )? + .preprocess()?, + ); + + program.push_shader( + Shader::read_shader_file( + Kind::Fragment, + &Path::new(SHADER_DIR).join(FRAGMENT_SHADER_FILE), + )? + .preprocess()?, + ); + + Ok(program) } pub fn push_shader(&mut self, shader: Shader) -- cgit v1.2.3-18-g5258