diff options
author | HampusM <hampus@hampusmat.com> | 2024-04-14 22:36:20 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-04-14 22:36:20 +0200 |
commit | 2b8f8ba255ef772f043cb27a49a55086df12718f (patch) | |
tree | 761e92536b5c622e60f810be5d589c8a288fb0d7 | |
parent | c2419bfee851810a27672b40c77c829e4b4c2829 (diff) |
feat(engine): add default shaders when creating shader program
-rw-r--r-- | engine/src/shader.rs | 34 |
1 files changed, 31 insertions, 3 deletions
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, Error> { - 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) |