From c9d5bbfe490aa4aab0e9de02b67bf8fee6dca5a2 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 5 May 2024 21:28:53 +0200 Subject: refactor(engine): make Material hold textures --- engine/src/material.rs | 22 ++++++++++++++++++++-- engine/src/renderer/mod.rs | 8 ++++---- engine/src/texture.rs | 17 ----------------- 3 files changed, 24 insertions(+), 23 deletions(-) (limited to 'engine') diff --git a/engine/src/material.rs b/engine/src/material.rs index c292af4..6839fd1 100644 --- a/engine/src/material.rs +++ b/engine/src/material.rs @@ -1,7 +1,7 @@ use ecs::Component; use crate::color::Color; -use crate::texture::Id as TextureId; +use crate::texture::{Id as TextureId, Texture}; #[derive(Debug, Clone, Component)] pub struct Material @@ -12,6 +12,7 @@ pub struct Material ambient_map: TextureId, diffuse_map: TextureId, specular_map: TextureId, + textures: Vec, shininess: f32, } @@ -53,6 +54,12 @@ impl Material &self.specular_map } + #[must_use] + pub fn textures(&self) -> &[Texture] + { + &self.textures + } + #[must_use] pub fn shininess(&self) -> f32 { @@ -70,6 +77,7 @@ pub struct Builder ambient_map: Option, diffuse_map: Option, specular_map: Option, + textures: Vec, shininess: f32, } @@ -85,6 +93,7 @@ impl Builder ambient_map: None, diffuse_map: None, specular_map: None, + textures: Vec::new(), shininess: 32.0, } } @@ -137,6 +146,14 @@ impl Builder self } + #[must_use] + pub fn textures(mut self, textures: impl IntoIterator) -> Self + { + self.textures = textures.into_iter().collect(); + + self + } + #[must_use] pub fn shininess(mut self, shininess: f32) -> Self { @@ -150,7 +167,7 @@ impl Builder /// # Panics /// Will panic if no ambient map, diffuse map or specular map is set. #[must_use] - pub fn build(&self) -> Material + pub fn build(self) -> Material { Material { ambient: self.ambient.clone(), @@ -159,6 +176,7 @@ impl Builder ambient_map: self.ambient_map.unwrap(), diffuse_map: self.diffuse_map.unwrap(), specular_map: self.specular_map.unwrap(), + textures: self.textures, shininess: self.shininess, } } diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index af91257..fbb86f7 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -37,7 +37,7 @@ use crate::opengl::vertex_array::{ use crate::opengl::{clear_buffers, enable, BufferClearMask, Capability}; use crate::projection::{new_perspective_matrix, Projection}; use crate::shader::Program as ShaderProgram; -use crate::texture::{Id as TextureId, List as TextureMap, Texture}; +use crate::texture::{Id as TextureId, Texture}; use crate::transform::Transform; use crate::vector::{Vec2, Vec3}; use crate::vertex::{AttributeComponentType, Vertex}; @@ -91,7 +91,7 @@ fn initialize(window: Single) } fn render( - query: Query<(Mesh, TextureMap, ShaderProgram, Material, Transform)>, + query: Query<(Mesh, ShaderProgram, Material, Transform)>, light_source_query: Query<(LightSource, Transform)>, camera_query: Query<(Camera,)>, window: Single, @@ -124,7 +124,7 @@ fn render( clear_buffers(BufferClearMask::COLOR | BufferClearMask::DEPTH); - for (mesh, texture_list, shader_program, material, transform) in &query { + for (mesh, shader_program, material, transform) in &query { let shader_program = gl_shader_programs .entry(shader_program.u64_hash()) .or_insert_with(|| create_gl_shader_program(&shader_program).unwrap()); @@ -147,7 +147,7 @@ fn render( &camera, ); - for texture in &texture_list.list { + for texture in material.textures() { let gl_texture = gl_textures .entry(texture.id()) .or_insert_with(|| create_gl_texture(texture)); diff --git a/engine/src/texture.rs b/engine/src/texture.rs index 88c57bd..f9bed1e 100644 --- a/engine/src/texture.rs +++ b/engine/src/texture.rs @@ -196,20 +196,3 @@ impl Display for Id self.id.fmt(formatter) } } - -/// Texture list. -#[derive(Component)] -pub struct List -{ - pub list: Vec, -} - -impl FromIterator for List -{ - fn from_iter(iter: Iter) -> Self - where - Iter: IntoIterator, - { - Self { list: iter.into_iter().collect() } - } -} -- cgit v1.2.3-18-g5258