light: Add GetLightDist function

This commit is contained in:
Eric Wasylishen 2016-08-10 12:22:41 -06:00
parent 28b034a172
commit 9f607b858b
1 changed files with 37 additions and 0 deletions

View File

@ -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)
{