summaryrefslogtreecommitdiff
path: root/engine/src/renderer
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-11-12 22:38:52 +0100
committerHampusM <hampus@hampusmat.com>2023-11-12 22:57:19 +0100
commit8d821588cd4f51d4ae9c4ef52d45c0af0e1ce9e5 (patch)
treeed1b0dd30e801b9a70537b9806a445e4212978b3 /engine/src/renderer
parent67023d6a095f457a2579367d59d13c6c804e7108 (diff)
feat(engine): add basic flat lighting
Diffstat (limited to 'engine/src/renderer')
-rw-r--r--engine/src/renderer/mod.rs65
1 files changed, 64 insertions, 1 deletions
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<Item = &'obj Object>,
+ 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<ShaderProgram>,
+ 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(