2016-03-04 23:04:53 +00:00
|
|
|
#include "CLight.hpp"
|
2016-03-16 20:49:35 +00:00
|
|
|
#include <cfloat>
|
2016-03-04 23:04:53 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
namespace urde {
|
2016-03-04 23:04:53 +00:00
|
|
|
|
2016-03-16 20:49:35 +00:00
|
|
|
static const zeus::CVector3f kDefaultPosition(0.f, 0.f, 0.f);
|
|
|
|
static const zeus::CVector3f kDefaultDirection(0.f, -1.f, 0.f);
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
float CLight::CalculateLightRadius() const {
|
|
|
|
if (x28_distL < FLT_EPSILON && x2c_distQ < FLT_EPSILON)
|
|
|
|
return FLT_MAX;
|
2016-03-16 20:49:35 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
float intens = GetIntensity();
|
2016-03-16 20:49:35 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (x2c_distQ > FLT_EPSILON) {
|
|
|
|
if (intens <= FLT_EPSILON)
|
|
|
|
return 0.f;
|
|
|
|
return std::sqrt(intens / 5.f * intens / 255.f * x2c_distQ);
|
|
|
|
}
|
2016-03-16 20:49:35 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
float nextIntens = 5.f * intens / 255.f;
|
|
|
|
return intens / std::min(0.2f, nextIntens) * x28_distL;
|
2016-03-16 20:49:35 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
float CLight::GetIntensity() const {
|
|
|
|
if (x4c_24_intensityDirty) {
|
|
|
|
const_cast<CLight*>(this)->x4c_24_intensityDirty = false;
|
|
|
|
float coef = 1.f;
|
|
|
|
if (x1c_type == ELightType::Custom)
|
|
|
|
coef = x30_angleC;
|
|
|
|
const_cast<CLight*>(this)->x48_cachedIntensity =
|
|
|
|
coef * std::max(x18_color.r(), std::max(x18_color.g(), x18_color.b()));
|
|
|
|
}
|
|
|
|
return x48_cachedIntensity;
|
2016-03-16 20:49:35 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
CLight::CLight(const zeus::CVector3f& pos, const zeus::CVector3f& dir, const zeus::CColor& color, float distC,
|
|
|
|
float distL, float distQ, float angleC, float angleL, float angleQ)
|
|
|
|
: x0_pos(pos)
|
|
|
|
, xc_dir(dir)
|
|
|
|
, x18_color(color)
|
|
|
|
, x1c_type(ELightType::Custom)
|
|
|
|
, x20_spotCutoff(0.f)
|
|
|
|
, x24_distC(distC)
|
|
|
|
, x28_distL(distL)
|
|
|
|
, x2c_distQ(distQ)
|
|
|
|
, x30_angleC(angleC)
|
|
|
|
, x34_angleL(angleL)
|
|
|
|
, x38_angleQ(angleQ)
|
|
|
|
, x44_cachedRadius(0.f)
|
|
|
|
, x48_cachedIntensity(0.f)
|
|
|
|
, x4c_24_intensityDirty(true)
|
|
|
|
, x4c_25_radiusDirty(true) {}
|
|
|
|
|
|
|
|
CLight::CLight(ELightType type, const zeus::CVector3f& pos, const zeus::CVector3f& dir, const zeus::CColor& color,
|
2016-04-04 02:32:57 +00:00
|
|
|
float cutoff)
|
2018-12-08 05:30:43 +00:00
|
|
|
: x0_pos(pos)
|
|
|
|
, xc_dir(dir)
|
|
|
|
, x18_color(color)
|
|
|
|
, x1c_type(type)
|
|
|
|
, x20_spotCutoff(cutoff)
|
|
|
|
, x24_distC(1.f)
|
|
|
|
, x28_distL(0.f)
|
|
|
|
, x2c_distQ(0.f)
|
|
|
|
, x30_angleC(1.f)
|
|
|
|
, x34_angleL(0.f)
|
|
|
|
, x38_angleQ(0.f)
|
|
|
|
, x44_cachedRadius(0.f)
|
|
|
|
, x48_cachedIntensity(0.f)
|
|
|
|
, x4c_24_intensityDirty(true)
|
|
|
|
, x4c_25_radiusDirty(true) {
|
|
|
|
switch (type) {
|
|
|
|
case ELightType::Spot: {
|
|
|
|
float cosCutoff = std::cos(zeus::degToRad(cutoff));
|
|
|
|
x30_angleC = 0.f;
|
|
|
|
x34_angleL = -cosCutoff / (1.0 - cosCutoff);
|
|
|
|
x38_angleQ = 1.f / (1.0 - cosCutoff);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ELightType::Directional: {
|
|
|
|
x24_distC = 1.f;
|
|
|
|
x28_distL = 0.f;
|
|
|
|
x2c_distQ = 0.f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-04-04 02:32:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
zeus::CColor CLight::GetNormalIndependentLightingAtPoint(const zeus::CVector3f& point) const {
|
|
|
|
if (x1c_type == ELightType::LocalAmbient)
|
|
|
|
return x18_color;
|
2017-04-10 06:57:00 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
float dist = std::max((x0_pos - point).magnitude(), FLT_EPSILON);
|
|
|
|
return x18_color * (1.f / (x2c_distQ * dist * dist + x28_distL * dist + x24_distC));
|
2017-04-10 06:57:00 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
CLight CLight::BuildDirectional(const zeus::CVector3f& dir, const zeus::CColor& color) {
|
|
|
|
return CLight(ELightType::Directional, kDefaultPosition, dir, color, 180.f);
|
2016-03-04 23:04:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
CLight CLight::BuildSpot(const zeus::CVector3f& pos, const zeus::CVector3f& dir, const zeus::CColor& color,
|
|
|
|
float angle) {
|
|
|
|
return CLight(ELightType::Spot, pos, dir, color, angle);
|
2016-03-16 20:49:35 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
CLight CLight::BuildPoint(const zeus::CVector3f& pos, const zeus::CColor& color) {
|
|
|
|
return CLight(ELightType::Point, pos, kDefaultDirection, color, 180.f);
|
2016-03-04 23:04:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
CLight CLight::BuildCustom(const zeus::CVector3f& pos, const zeus::CVector3f& dir, const zeus::CColor& color,
|
|
|
|
float distC, float distL, float distQ, float angleC, float angleL, float angleQ) {
|
|
|
|
return CLight(pos, dir, color, distC, distL, distQ, angleC, angleL, angleQ);
|
2016-03-16 20:49:35 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
CLight CLight::BuildLocalAmbient(const zeus::CVector3f& pos, const zeus::CColor& color) {
|
|
|
|
return CLight(ELightType::LocalAmbient, pos, kDefaultDirection, color, 180.f);
|
2016-03-04 23:04:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
} // namespace urde
|