metaforce/Runtime/Graphics/CLight.cpp

99 lines
3.2 KiB
C++
Raw Normal View History

#include "Runtime/Graphics/CLight.hpp"
2016-03-16 13:49:35 -07:00
#include <cfloat>
2016-03-04 15:04:53 -08:00
2021-04-10 01:42:06 -07:00
namespace metaforce {
2016-03-04 15:04:53 -08:00
constexpr zeus::CVector3f kDefaultPosition(0.f, 0.f, 0.f);
constexpr zeus::CVector3f kDefaultDirection(0.f, -1.f, 0.f);
2016-03-16 13:49:35 -07:00
2018-12-07 21:30:43 -08:00
float CLight::CalculateLightRadius() const {
2022-10-06 07:25:26 -07:00
if (x28_distL < FLT_EPSILON && x2c_distQ < FLT_EPSILON) {
2022-05-11 22:10:06 -07:00
return FLT_MAX;
2018-12-07 21:30:43 -08:00
}
2016-03-16 13:49:35 -07:00
2021-05-06 10:51:54 -07:00
float intensity = GetIntensity();
2022-10-06 07:25:26 -07:00
float ret = 0.f;
if (x2c_distQ > FLT_EPSILON) {
2021-05-06 10:51:54 -07:00
constexpr float mulVal = std::min(0.05882353f, 0.2f); // Yes, retro really did do this
2022-10-06 07:25:26 -07:00
if (intensity > FLT_EPSILON) {
return std::sqrt(intensity / (mulVal * x2c_distQ));
2021-05-06 10:51:54 -07:00
}
} else {
constexpr float mulVal = std::min(0.05882353f, 0.2f); // See above comment
2022-10-06 07:25:26 -07:00
if (x28_distL > FLT_EPSILON) {
return intensity / (mulVal * x28_distL);
2021-05-06 10:51:54 -07:00
}
}
2019-01-22 23:52:19 -08:00
return 0.f;
2016-03-16 13:49:35 -07:00
}
2018-12-07 21:30:43 -08:00
float CLight::GetIntensity() const {
if (x4c_24_intensityDirty) {
x4c_24_intensityDirty = false;
2018-12-07 21:30:43 -08:00
float coef = 1.f;
if (x1c_type == ELightType::Custom) {
2018-12-07 21:30:43 -08:00
coef = x30_angleC;
}
x48_cachedIntensity = coef * std::max({x18_color.r(), x18_color.g(), x18_color.b()});
2018-12-07 21:30:43 -08:00
}
return x48_cachedIntensity;
2016-03-16 13:49:35 -07:00
}
2019-02-07 23:56:54 -08:00
float CLight::GetRadius() const {
if (x4c_25_radiusDirty) {
x44_cachedRadius = CalculateLightRadius();
x4c_25_radiusDirty = false;
2019-02-07 23:56:54 -08:00
}
return x44_cachedRadius;
}
2018-12-07 21:30:43 -08: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)
, x24_distC(distC)
, x28_distL(distL)
, x2c_distQ(distQ)
, x30_angleC(angleC)
, x34_angleL(angleL)
, x38_angleQ(angleQ) {}
2018-12-07 21:30:43 -08:00
CLight::CLight(ELightType type, const zeus::CVector3f& pos, const zeus::CVector3f& dir, const zeus::CColor& color,
2016-04-03 19:32:57 -07:00
float cutoff)
: x0_pos(pos), xc_dir(dir), x18_color(color), x1c_type(type), x20_spotCutoff(cutoff) {}
2016-04-03 19:32:57 -07:00
2018-12-07 21:30:43 -08:00
zeus::CColor CLight::GetNormalIndependentLightingAtPoint(const zeus::CVector3f& point) const {
if (x1c_type == ELightType::LocalAmbient)
return x18_color;
2018-12-07 21:30:43 -08:00
float dist = std::max((x0_pos - point).magnitude(), FLT_EPSILON);
2022-10-06 07:25:26 -07:00
return x18_color * (1.f / (dist * (x2c_distQ * dist) + (x28_distL * dist + x24_distC)));
}
2018-12-07 21:30:43 -08:00
CLight CLight::BuildDirectional(const zeus::CVector3f& dir, const zeus::CColor& color) {
return CLight(ELightType::Directional, kDefaultPosition, dir, color, 180.f);
2016-03-04 15:04:53 -08:00
}
2018-12-07 21:30:43 -08:00
CLight CLight::BuildSpot(const zeus::CVector3f& pos, const zeus::CVector3f& dir, const zeus::CColor& color,
2022-05-11 22:10:06 -07:00
float cutoff) {
return CLight(ELightType::Spot, pos, dir, color, cutoff);
2016-03-16 13:49:35 -07:00
}
2018-12-07 21:30:43 -08:00
CLight CLight::BuildPoint(const zeus::CVector3f& pos, const zeus::CColor& color) {
return CLight(ELightType::Point, pos, kDefaultDirection, color, 180.f);
2016-03-04 15:04:53 -08:00
}
2018-12-07 21:30:43 -08: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 13:49:35 -07:00
}
2018-12-07 21:30:43 -08:00
CLight CLight::BuildLocalAmbient(const zeus::CVector3f& pos, const zeus::CColor& color) {
return CLight(ELightType::LocalAmbient, pos, kDefaultDirection, color, 180.f);
2016-03-04 15:04:53 -08:00
}
2021-04-10 01:42:06 -07:00
} // namespace metaforce