diff options
Diffstat (limited to 'engine/src/renderer/mod.rs')
-rw-r--r-- | engine/src/renderer/mod.rs | 42 |
1 files changed, 28 insertions, 14 deletions
diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index 6fa692c..593b55f 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -18,6 +18,7 @@ use crate::opengl::currently_bound::CurrentlyBound; #[cfg(feature = "debug")] use crate::opengl::debug::{MessageSeverity, MessageSource, MessageType}; use crate::opengl::shader::Program as ShaderProgram; +use crate::opengl::texture::{set_active_texture_unit, TextureUnit}; use crate::opengl::vertex_array::{PrimitiveKind, VertexArray}; use crate::opengl::{clear_buffers, enable, BufferClearMask, Capability}; use crate::projection::new_perspective; @@ -69,7 +70,7 @@ impl Renderer objects: impl IntoIterator<Item = &'obj Object>, light_source: Option<&LightSource>, window_size: &WindowSize, - ) + ) -> Result<(), Error> { clear_buffers(BufferClearMask::COLOR | BufferClearMask::DEPTH); @@ -91,15 +92,22 @@ impl Renderer &shader_program_curr_bound, ); - if let Some(texture) = obj.texture() { - texture.inner().bind(|_| { - Self::draw_object(obj); - }); - } else { - Self::draw_object(obj); + for (texture_id, texture) in obj.textures() { + let texture_unit = TextureUnit::from_texture_id(*texture_id) + .ok_or(Error::TextureIdIsInvalidTextureUnit)?; + + set_active_texture_unit(texture_unit); + + texture.inner().bind(|_| {}); } - }); + + Self::draw_object(obj); + + Ok(()) + })?; } + + Ok(()) } pub fn set_viewport(position: &Vec2<u32>, size: &WindowSize) @@ -217,6 +225,9 @@ pub enum Error { #[error("Failed to get window size")] GetWindowSizeFailed(#[source] glfw::Error), + + #[error("Texture ID is a invalid texture unit")] + TextureIdIsInvalidTextureUnit, } fn apply_transformation_matrices( @@ -300,22 +311,25 @@ fn apply_light( .into(), ); - obj.renderable().shader_program.set_uniform_vec_3fv( + #[allow(clippy::cast_possible_wrap)] + obj.renderable().shader_program.set_uniform_1i( shader_program_curr_bound, cstr!("material.ambient"), - &obj.material().ambient().clone().into(), + obj.material().ambient_map().into_inner() as i32, ); - obj.renderable().shader_program.set_uniform_vec_3fv( + #[allow(clippy::cast_possible_wrap)] + obj.renderable().shader_program.set_uniform_1i( shader_program_curr_bound, cstr!("material.diffuse"), - &obj.material().diffuse().clone().into(), + obj.material().diffuse_map().into_inner() as i32, ); - obj.renderable().shader_program.set_uniform_vec_3fv( + #[allow(clippy::cast_possible_wrap)] + obj.renderable().shader_program.set_uniform_1i( shader_program_curr_bound, cstr!("material.specular"), - &obj.material().specular().clone().into(), + obj.material().specular_map().into_inner() as i32, ); obj.renderable().shader_program.set_uniform_1fv( |