diff options
author | HampusM <hampus@hampusmat.com> | 2023-11-12 22:38:52 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-11-12 22:57:19 +0100 |
commit | 8d821588cd4f51d4ae9c4ef52d45c0af0e1ce9e5 (patch) | |
tree | ed1b0dd30e801b9a70537b9806a445e4212978b3 /engine/src/lib.rs | |
parent | 67023d6a095f457a2579367d59d13c6c804e7108 (diff) |
feat(engine): add basic flat lighting
Diffstat (limited to 'engine/src/lib.rs')
-rw-r--r-- | engine/src/lib.rs | 46 |
1 files changed, 45 insertions, 1 deletions
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<ObjectId, Object>, + light_source: Option<LightSource>, + light_settings: Option<LightSettings>, 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<Instant> = 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<Instant>) { let frame_start_time = Instant::now(); |