From 9f607b858b6117e58f89ef6dfadf859f06f0ecb6 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Wed, 10 Aug 2016 12:22:41 -0600 Subject: [PATCH] light: Add GetLightDist function --- light/ltface.cc | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/light/ltface.cc b/light/ltface.cc index 579821be..ba9b8645 100644 --- a/light/ltface.cc +++ b/light/ltface.cc @@ -927,6 +927,43 @@ GetLightValue(const light_t *entity, vec_t dist) } } +#define SQR(x) ((x)*(x)) + +// this is the inverse of GetLightValue +float +GetLightDist(const light_t *entity, vec_t desiredLight) +{ + float fadedist; + if (entity->formula.intValue() == LF_LINEAR) { + /* Linear formula always has a falloff point */ + fadedist = fabs(entity->light.floatValue()) - desiredLight; + fadedist = fadedist / entity->atten.floatValue() / scaledist.floatValue(); + fadedist = qmax(0.0f, fadedist); + } else { + /* Calculate the distance at which brightness falls to desiredLight */ + switch (entity->formula.intValue()) { + case LF_INFINITE: + case LF_LOCALMIN: + fadedist = VECT_MAX; + break; + case LF_INVERSE: + fadedist = (LF_SCALE * fabs(entity->light.floatValue())) / (scaledist.floatValue() * entity->atten.floatValue() * desiredLight); + break; + case LF_INVERSE2: + case LF_INVERSE2A: + fadedist = sqrt(fabs(entity->light.floatValue() * SQR(LF_SCALE) / (SQR(scaledist.floatValue()) * SQR(entity->atten.floatValue()) * desiredLight))); + if (entity->formula.intValue() == LF_INVERSE2A) { + fadedist -= (LF_SCALE / (scaledist.floatValue() * entity->atten.floatValue())); + } + fadedist = qmax(0.0f, fadedist); + break; + default: + Error("Internal error: formula not handled in %s", __func__); + } + } + return fadedist; +} + static inline void Light_Add(lightsample_t *sample, const vec_t light, const vec3_t color, const vec3_t direction) {