summaryrefslogtreecommitdiff
path: root/intermediate/surface.glsl
diff options
context:
space:
mode:
Diffstat (limited to 'intermediate/surface.glsl')
-rw-r--r--intermediate/surface.glsl95
1 files changed, 67 insertions, 28 deletions
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);