diff options
Diffstat (limited to 'intermediate')
-rw-r--r-- | intermediate/forward.h | 13 | ||||
-rw-r--r-- | intermediate/surface.glsl | 95 |
2 files changed, 73 insertions, 35 deletions
diff --git a/intermediate/forward.h b/intermediate/forward.h index 377a3cb..3bc2d50 100644 --- a/intermediate/forward.h +++ b/intermediate/forward.h @@ -17,9 +17,6 @@ type: vec3 name: caster_id type: int [variable] -name: type -type: int -[variable] name: range type: float @@ -40,6 +37,12 @@ type: int [variable] name: frame type: int +[variable] +name: sun_irange +type: ivec2 +[variable] +name: point_irange +type: ivec2 [cbuffer] name: globals @@ -48,8 +51,4 @@ 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 79f5f65..ce80259 100644 --- a/intermediate/surface.glsl +++ b/intermediate/surface.glsl @@ -215,8 +215,32 @@ float get_shadow(Light l, vec3 wpos) { return d; } +vec3 apply_light( + Light l, + vec3 p, + vec2 uv, + vec3 base_diffuse, + vec3 spec_col, + vec3 ref, + vec3 light_dir, + vec3 view_dir, + vec3 nrm, + float cos_theta_i, + float atten +) { + vec3 diffuse = base_diffuse * cos_theta_i; + vec3 spec = + spec_col * + specular_brdf(uv, ref, light_dir, view_dir, nrm) * + cos_theta_i; + float shadow = 1.0f; + if (l.caster_id >= 0) + shadow = get_shadow(l, p); + return (diffuse + spec) * atten * l.brightness * l.colour * shadow; +} + void main() { - int i; + int i, e; vec2 uv = interpolator.uv; vec3 p = interpolator.position.xyz; @@ -242,34 +266,49 @@ void main() { vec3 base_diffuse = diffuse_brdf(uv) * (1.0 - met); vec3 light = 0.0.xxx; - for (i = 0; i < globals.light_count; i++) { + e = globals.sun_irange.y; + for (i = globals.sun_irange.x; i < e; i++) { + Light l = lights[i]; + vec3 light_dir = l.pos; + float cos_theta_i = max(dot(nrm, light_dir), 0.0); + float atten = 1.0; + light += apply_light( + l, + p, + uv, + base_diffuse, + spec_col, + ref, + light_dir, + view_dir, + nrm, + cos_theta_i, + atten + ); + } + e = globals.point_irange.y; + for (i = globals.point_irange.x; i < e; i++) { Light l = lights[i]; - 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 * - specular_brdf(uv, ref, light_dir, view_dir, nrm) * - cos_theta_i; - float shadow = 1.0f; - if (l.caster_id >= 0) - shadow = get_shadow(l, p); - light += (diffuse + spec) * atten * l.brightness * l.colour * shadow; + float cos_theta_i; + vec3 light_dir = l.pos - p; + float d = length(light_dir); + float atten = max(d, 0.01); + atten = 1.0 / (atten * atten); + light_dir /= d; + cos_theta_i = max(dot(nrm, light_dir), 0.0); + light += apply_light( + l, + p, + uv, + base_diffuse, + spec_col, + ref, + light_dir, + view_dir, + nrm, + cos_theta_i, + atten + ); } colour = vec4(ambient + light, 1.0); |