From 37b929e148e5b003f68903eb9ee192d24517e683 Mon Sep 17 00:00:00 2001 From: quou Date: Mon, 10 Mar 2025 15:03:05 +1100 Subject: point light --- intermediate/forward.h | 13 ++++++++++++- intermediate/surface.glsl | 21 ++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) (limited to 'intermediate') diff --git a/intermediate/forward.h b/intermediate/forward.h index 7c4c9d3..377a3cb 100644 --- a/intermediate/forward.h +++ b/intermediate/forward.h @@ -5,7 +5,7 @@ [struct] name: Light [variable] -name: dir +name: pos type: vec3 [variable] name: brightness @@ -16,6 +16,12 @@ type: vec3 [variable] name: caster_id type: int +[variable] +name: type +type: int +[variable] +name: range +type: float [struct] name: Caster @@ -41,4 +47,9 @@ type: Globals stage: fragment #endif + +/* match Light::Type in lighting.hpp */ +#define LT_SUN 0 +#define LT_POINT 1 + #endif diff --git a/intermediate/surface.glsl b/intermediate/surface.glsl index 71a4307..79f5f65 100644 --- a/intermediate/surface.glsl +++ b/intermediate/surface.glsl @@ -244,8 +244,23 @@ void main() { vec3 light = 0.0.xxx; for (i = 0; i < globals.light_count; i++) { Light l = lights[i]; - vec3 light_dir = l.dir; - float cos_theta_i = max(dot(nrm, light_dir), 0.0); + vec3 light_dir; + float cos_theta_i, atten; + switch (l.type) { + case LT_SUN: + light_dir = l.pos; + cos_theta_i = max(dot(nrm, light_dir), 0.0); + atten = 1.0; + break; + case LT_POINT: { + float d; + light_dir = p - l.pos; + d = length(light_dir); + atten = max(d, 0.01); + atten = 1.0 / (atten * atten); + light_dir /= d; + } break; + } vec3 diffuse = base_diffuse * cos_theta_i; vec3 spec = spec_col * @@ -254,7 +269,7 @@ void main() { float shadow = 1.0f; if (l.caster_id >= 0) shadow = get_shadow(l, p); - light += (diffuse + spec) * l.brightness * l.colour * shadow; + light += (diffuse + spec) * atten * l.brightness * l.colour * shadow; } colour = vec4(ambient + light, 1.0); -- cgit v1.2.3-54-g00ecf