From d0fb0bd4100fe6fd76272906918c66d925e78aa5 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sun, 25 Oct 2015 23:18:17 -0600 Subject: [PATCH] light: remove entity->fadedist, it was error-prone, and instead just check light level. it's only very slightly slower. --- include/light/entities.h | 1 - light/entities.c | 31 ------------------------------- light/ltface.c | 17 +++++++++-------- 3 files changed, 9 insertions(+), 40 deletions(-) diff --git a/include/light/entities.h b/include/light/entities.h index 59837c98..4f6d8828 100644 --- a/include/light/entities.h +++ b/include/light/entities.h @@ -63,7 +63,6 @@ typedef struct entity_s { lightsample_t light; light_formula_t formula; - vec_t fadedist; float atten; float anglescale; int style; diff --git a/light/entities.c b/light/entities.c index 54231dce..ec026ac4 100644 --- a/light/entities.c +++ b/light/entities.c @@ -254,37 +254,6 @@ CheckEntityFields(entity_t *entity) } else { VectorCopy(vec3_white, entity->light.color); } - - if (entity->formula == LF_LINEAR) { - /* Linear formula always has a falloff point */ - entity->fadedist = fabs(entity->light.light) - fadegate; - entity->fadedist = entity->fadedist / entity->atten / scaledist; - entity->fadedist = qmax(0.0f, entity->fadedist); - } else if (fadegate < EQUAL_EPSILON) { - /* If fadegate is tiny, other lights have effectively infinite reach */ - entity->fadedist = VECT_MAX; - } else { - /* Calculate the distance at which brightness falls to zero */ - switch (entity->formula) { - case LF_INFINITE: - case LF_LOCALMIN: - entity->fadedist = VECT_MAX; - break; - case LF_INVERSE: - entity->fadedist = (LF_SCALE * fabs(entity->light.light)) / (scaledist * entity->atten * fadegate); - break; - case LF_INVERSE2: - case LF_INVERSE2A: - entity->fadedist = sqrt(fabs(entity->light.light * SQR(LF_SCALE) / (SQR(scaledist) * SQR(entity->atten) * fadegate))); - if (entity->formula == LF_INVERSE2A) { - entity->fadedist -= (LF_SCALE / (scaledist * entity->atten)); - } - entity->fadedist = qmax(0.0f, entity->fadedist); - break; - default: - Error("Internal error: formula not handled in %s", __func__); - } - } } /* diff --git a/light/ltface.c b/light/ltface.c index dc3c6317..1b99b617 100644 --- a/light/ltface.c +++ b/light/ltface.c @@ -813,10 +813,15 @@ CullLight(const entity_t *entity, const lightsurf_t *lightsurf) vec_t dist; VectorSubtract(entity->origin, lightsurf->origin, distvec); - dist = VectorLength(distvec) - lightsurf->radius - entity->fadedist; + dist = VectorLength(distvec) - lightsurf->radius; - /* return true if the spheres don't intersect */ - return dist > 0; + /* light is inside surface bounding sphere => can't cull */ + if (dist < 0) + return false; + + /* return true if the light level at the closest point on the + surface bounding sphere to the light source is <= fadegate */ + return GetLightValue(&entity->light, entity, dist) <= fadegate; } /* @@ -844,10 +849,6 @@ LightFace_Entity(const entity_t *entity, const lightsample_t *light, if (dist < 0) return; - /* don't bother with light too far away from plane */ - if (dist > entity->fadedist) - return; - /* sphere cull surface and light */ if (CullLight(entity, lightsurf)) return; @@ -867,7 +868,7 @@ LightFace_Entity(const entity_t *entity, const lightsample_t *light, dist = VectorLength(ray); /* Quick distance check first */ - if (dist > entity->fadedist) + if (GetLightValue(&entity->light, entity, dist) <= fadegate) continue; /* Check spotlight cone */