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/renderer/mod.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) (limited to 'engine/src/renderer') diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index 7d5e104..d46612f 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -5,6 +5,8 @@ use cstr::cstr; use glfw::WindowSize; use crate::camera::Camera; +use crate::color::Color; +use crate::lighting::{LightSettings, LightSource}; use crate::object::Object; use crate::opengl::buffer::{ ArrayKind as ArrayBufferKind, @@ -19,7 +21,7 @@ use crate::opengl::shader::Program as ShaderProgram; use crate::opengl::vertex_array::{PrimitiveKind, VertexArray}; use crate::opengl::{clear_buffers, enable, BufferClearMask, Capability}; use crate::projection::new_perspective; -use crate::vector::Vec2; +use crate::vector::{Vec2, Vec3}; use crate::vertex::Vertex; #[derive(Debug)] @@ -65,6 +67,8 @@ impl Renderer pub fn render<'obj>( &self, objects: impl IntoIterator, + light_source: Option<&LightSource>, + light_settings: &LightSettings, window_size: &WindowSize, ) { @@ -81,6 +85,14 @@ impl Renderer &shader_program_curr_bound, ); + apply_light( + obj, + light_source, + &self.camera, + &shader_program_curr_bound, + light_settings, + ); + if let Some(texture) = obj.texture() { texture.inner().bind(|_| { Self::draw_object(obj); @@ -245,6 +257,57 @@ fn apply_transformation_matrices( ); } +fn apply_light( + obj: &Object, + light_source: Option<&LightSource>, + camera: &Camera, + shader_program_curr_bound: &CurrentlyBound, + light_settings: &LightSettings, +) +{ + obj.renderable().shader_program.set_uniform_vec_3fv( + shader_program_curr_bound, + cstr!("light_color"), + &light_source + .map_or(Color::WHITE_F32, |light_source| { + light_source.color().clone() + }) + .into(), + ); + + obj.renderable().shader_program.set_uniform_vec_3fv( + shader_program_curr_bound, + cstr!("light_pos"), + &light_source.map_or_else(Vec3::default, |light_source| { + light_source.position().clone() + }), + ); + + obj.renderable().shader_program.set_uniform_1fv( + shader_program_curr_bound, + cstr!("ambient_light_strength"), + light_settings.ambient_light_strength, + ); + + obj.renderable().shader_program.set_uniform_1fv( + shader_program_curr_bound, + cstr!("specular_light_strength"), + light_settings.specular_light_strength, + ); + + obj.renderable().shader_program.set_uniform_1uiv( + shader_program_curr_bound, + cstr!("specular_shininess"), + light_settings.specular_shininess, + ); + + obj.renderable().shader_program.set_uniform_vec_3fv( + shader_program_curr_bound, + cstr!("view_pos"), + camera.position(), + ); +} + #[cfg(feature = "debug")] #[tracing::instrument(skip_all)] fn opengl_debug_message_cb( -- cgit v1.2.3-18-g5258