metaforce/Runtime/Camera/CGameCamera.cpp

137 lines
4.4 KiB
C++
Raw Normal View History

#include "Runtime/Camera/CGameCamera.hpp"
#include "Runtime/CStateManager.hpp"
#include "Runtime/Camera/CCameraManager.hpp"
#include "Runtime/World/CActorParameters.hpp"
2016-04-24 04:03:30 +00:00
2021-04-10 08:42:06 +00:00
namespace metaforce {
2016-04-24 04:03:30 +00:00
2017-11-13 06:19:18 +00:00
CGameCamera::CGameCamera(TUniqueId uid, bool active, std::string_view name, const CEntityInfo& info,
2017-10-08 02:58:13 +00:00
const zeus::CTransform& xf, float fovy, float znear, float zfar, float aspect,
TUniqueId watchedId, bool disableInput, u32 controllerIdx)
2017-12-19 03:05:50 +00:00
: CActor(uid, active, name, info, xf, CModelData::CModelDataNull(), CMaterialList(EMaterialTypes::NoStepLogic),
2016-09-14 05:45:46 +00:00
CActorParameters::None(), kInvalidUniqueId)
2017-10-08 02:58:13 +00:00
, xe8_watchedObject(watchedId)
2018-06-02 06:06:25 +00:00
, x12c_origXf(xf)
2016-10-31 22:56:44 +00:00
, x15c_currentFov(fovy)
2016-09-14 05:45:46 +00:00
, x160_znear(znear)
, x164_zfar(zfar)
, x168_aspect(aspect)
2017-03-24 05:30:16 +00:00
, x16c_controllerIdx(controllerIdx)
, x170_25_disablesInput(disableInput)
2018-06-02 06:06:25 +00:00
, x180_perspInterpStartFov(fovy)
2018-12-08 05:30:43 +00:00
, x184_perspInterpEndFov(fovy) {
2016-09-14 05:45:46 +00:00
2018-12-08 05:30:43 +00:00
xe7_29_drawEnabled = false;
2016-09-14 05:45:46 +00:00
}
2018-12-08 05:30:43 +00:00
void CGameCamera::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId uid, CStateManager& mgr) {
if (msg == EScriptObjectMessage::AddSplashInhabitant) {
mgr.GetCameraManager()->SetInsideFluid(true, uid);
return;
} else if (msg == EScriptObjectMessage::RemoveSplashInhabitant) {
mgr.GetCameraManager()->SetInsideFluid(false, kInvalidUniqueId);
return;
}
CActor::AcceptScriptMsg(msg, uid, mgr);
2016-09-14 05:45:46 +00:00
}
2018-12-08 05:30:43 +00:00
void CGameCamera::SetActive(bool active) {
CActor::SetActive(active);
xe7_29_drawEnabled = false;
2016-09-14 05:45:46 +00:00
}
2018-12-08 05:30:43 +00:00
zeus::CMatrix4f CGameCamera::GetPerspectiveMatrix() const {
if (x170_24_perspDirty) {
xec_perspectiveMatrix = CGraphics::CalculatePerspectiveMatrix(x15c_currentFov, x168_aspect, x160_znear, x164_zfar);
x170_24_perspDirty = false;
2018-12-08 05:30:43 +00:00
}
2016-09-14 05:45:46 +00:00
2018-12-08 05:30:43 +00:00
return xec_perspectiveMatrix;
2016-09-14 05:45:46 +00:00
}
2018-12-08 05:30:43 +00:00
zeus::CVector3f CGameCamera::ConvertToScreenSpace(const zeus::CVector3f& v) const {
zeus::CVector3f rVec = x34_transform.transposeRotate(v - x34_transform.origin);
2016-09-14 05:45:46 +00:00
2018-12-08 05:30:43 +00:00
if (rVec.isZero())
return {-1.f, -1.f, 1.f};
2016-09-14 05:45:46 +00:00
2018-12-08 05:30:43 +00:00
rVec = zeus::CVector3f(rVec.x(), rVec.z(), -rVec.y());
zeus::CMatrix4f mtx = GetPerspectiveMatrix();
return mtx.multiplyOneOverW(rVec);
2016-04-24 04:03:30 +00:00
}
zeus::CTransform CGameCamera::ValidateCameraTransform(const zeus::CTransform& newXf,
const zeus::CTransform& oldXf) const {
2018-12-08 05:30:43 +00:00
zeus::CTransform xfCpy(newXf);
if (!zeus::close_enough(newXf.rightVector().magnitude(), 1.f) ||
!zeus::close_enough(newXf.frontVector().magnitude(), 1.f) ||
!zeus::close_enough(newXf.upVector().magnitude(), 1.f))
xfCpy.orthonormalize();
float f2 = zeus::clamp(-1.f, newXf.frontVector().dot(zeus::skUp), 1.f);
2018-12-08 05:30:43 +00:00
if (std::fabs(f2) > 0.999f)
xfCpy = oldXf;
if (xfCpy.upVector().z() < -0.2f)
xfCpy = zeus::CQuaternion::fromAxisAngle(xfCpy.frontVector(), M_PIF).toTransform() * xfCpy;
if (!zeus::close_enough(xfCpy.rightVector().z(), 0.f) && !zeus::close_enough(xfCpy.upVector().z(), 0.f)) {
if (xfCpy.frontVector().canBeNormalized())
xfCpy = zeus::lookAt(zeus::skZero3f, xfCpy.frontVector());
else
2018-12-08 05:30:43 +00:00
xfCpy = oldXf;
}
2018-12-08 05:30:43 +00:00
xfCpy.origin = newXf.origin;
return xfCpy;
}
2018-12-08 05:30:43 +00:00
void CGameCamera::UpdatePerspective(float dt) {
if (x174_delayTime > 0.f) {
x174_delayTime -= dt;
return;
}
if (x178_perspInterpRemTime <= 0.f)
return;
x178_perspInterpRemTime -= dt;
if (x178_perspInterpRemTime <= 0.f) {
x15c_currentFov = x184_perspInterpEndFov;
x170_24_perspDirty = true;
} else {
x15c_currentFov = zeus::clamp(0.f, (x178_perspInterpRemTime / x17c_perspInterpDur), 1.f) *
(x180_perspInterpStartFov - x184_perspInterpEndFov) +
x184_perspInterpEndFov;
x170_24_perspDirty = true;
}
}
2018-12-08 05:30:43 +00:00
void CGameCamera::SetFovInterpolation(float start, float fov, float time, float delayTime) {
if (time < 0.f) {
x15c_currentFov = fov;
x170_24_perspDirty = true;
x184_perspInterpEndFov = fov;
2018-06-02 06:06:25 +00:00
x178_perspInterpRemTime = x174_delayTime = 0.f;
2018-12-08 05:30:43 +00:00
} else {
x174_delayTime = std::max(0.f, delayTime);
x17c_perspInterpDur = time;
x178_perspInterpRemTime = time;
x180_perspInterpStartFov = start;
x184_perspInterpEndFov = fov;
x15c_currentFov = start;
x170_24_perspDirty = true;
}
}
2018-12-08 05:30:43 +00:00
void CGameCamera::SkipFovInterpolation() {
if (x178_perspInterpRemTime > 0) {
x15c_currentFov = x184_perspInterpEndFov;
x170_24_perspDirty = true;
}
x178_perspInterpRemTime = x174_delayTime = 0.f;
2016-04-24 04:03:30 +00:00
}
2021-04-10 08:42:06 +00:00
} // namespace metaforce