From 8d821588cd4f51d4ae9c4ef52d45c0af0e1ce9e5 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 12 Nov 2023 22:38:52 +0100 Subject: feat(engine): add basic flat lighting --- engine/src/lib.rs | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'engine/src/lib.rs') diff --git a/engine/src/lib.rs b/engine/src/lib.rs index 0eca914..e6568a1 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -7,6 +7,7 @@ use glfw::window::KeyState; use glfw::{Window, WindowBuilder}; use crate::camera::Camera; +use crate::lighting::{LightSettings, LightSource}; use crate::object::{Id as ObjectId, Object}; use crate::renderer::Renderer; use crate::vector::Vec2; @@ -20,6 +21,8 @@ mod transform; pub mod camera; pub mod color; +pub mod lighting; +pub mod math; pub mod object; pub mod texture; pub mod vector; @@ -33,6 +36,8 @@ pub struct Engine { /// Objects have to be dropped before window. Otherwise, UB. objects: BTreeMap, + light_source: Option, + light_settings: Option, window: Window, renderer: Renderer, delta_time: Duration, @@ -70,6 +75,8 @@ impl Engine window, renderer, objects: BTreeMap::new(), + light_source: None, + light_settings: None, delta_time: Duration::ZERO, }) } @@ -85,6 +92,23 @@ impl Engine .extend(objects.into_iter().map(|object| (object.id(), object))); } + pub fn set_light_source(&mut self, light_source: LightSource) + { + self.light_source = Some(light_source); + } + + #[must_use] + pub fn light_source(&self) -> Option<&LightSource> + { + self.light_source.as_ref() + } + + #[must_use] + pub fn light_source_mut(&mut self) -> Option<&mut LightSource> + { + self.light_source.as_mut() + } + #[must_use] pub fn get_object_by_id(&self, id: ObjectId) -> Option<&Object> { @@ -105,6 +129,8 @@ impl Engine { let mut prev_frame_start: Option = None; + let default_light_settings = LightSettings::default(); + while !self.window.should_close() { self.update_delta_time(&mut prev_frame_start); @@ -112,7 +138,14 @@ impl Engine let window_size = self.window.size().map_err(Error::GetWindowSizeFailed)?; - self.renderer.render(self.objects.values(), &window_size); + self.renderer.render( + self.objects.values(), + self.light_source.as_ref(), + self.light_settings + .as_ref() + .unwrap_or(&default_light_settings), + &window_size, + ); self.window .swap_buffers() @@ -171,6 +204,17 @@ impl Engine Ok(Vec2 { x: pos.x, y: pos.y }) } + pub fn set_light_settings(&mut self, light_settings: LightSettings) + { + self.light_settings = Some(light_settings); + } + + #[must_use] + pub fn light_settings(&self) -> Option<&LightSettings> + { + self.light_settings.as_ref() + } + fn update_delta_time(&mut self, prev_frame_start: &mut Option) { let frame_start_time = Instant::now(); -- cgit v1.2.3-18-g5258