metaforce/Runtime/Character/CModelData.cpp

470 lines
16 KiB
C++
Raw Normal View History

#include "Runtime/Character/CModelData.hpp"
#include "Runtime/CPlayerState.hpp"
#include "Runtime/CStateManager.hpp"
#include "Runtime/GameGlobalObjects.hpp"
#include "Runtime/Character/CActorLights.hpp"
#include "Runtime/Character/CAdditiveAnimPlayback.hpp"
#include "Runtime/Character/CAnimData.hpp"
#include "Runtime/Character/CAssetFactory.hpp"
#include "Runtime/Character/CCharacterFactory.hpp"
#include "Runtime/Character/IAnimReader.hpp"
#include "Runtime/Graphics/CGraphics.hpp"
#include "Runtime/Graphics/CSkinnedModel.hpp"
#include "Runtime/Graphics/CVertexMorphEffect.hpp"
#include "Runtime/Graphics/CCubeRenderer.hpp"
#include <logvisor/logvisor.hpp>
2016-04-14 03:32:27 +00:00
2021-04-10 08:42:06 +00:00
namespace metaforce {
static logvisor::Module Log("metaforce::CModelData");
2016-04-14 03:32:27 +00:00
CModelData::~CModelData() = default;
2016-04-15 03:02:21 +00:00
2016-04-14 03:32:27 +00:00
CModelData::CModelData() {}
2018-12-08 05:30:43 +00:00
CModelData CModelData::CModelDataNull() { return CModelData(); }
2017-09-05 03:00:19 +00:00
2022-02-01 00:06:54 +00:00
CModelData::CModelData(const CStaticRes& res) : x0_scale(res.GetScale()) {
2018-12-08 05:30:43 +00:00
x1c_normalModel = g_SimplePool->GetObj({SBIG('CMDL'), res.GetId()});
if (!x1c_normalModel)
2020-04-11 22:51:39 +00:00
Log.report(logvisor::Fatal, FMT_STRING("unable to find CMDL {}"), res.GetId());
2017-09-05 03:00:19 +00:00
}
2022-02-01 00:06:54 +00:00
CModelData::CModelData(const CAnimRes& res) : x0_scale(res.GetScale()) {
2018-12-08 05:30:43 +00:00
TToken<CCharacterFactory> factory = g_CharFactoryBuilder->GetFactory(res);
x10_animData = factory->CreateCharacter(res.GetCharacterNodeId(), res.CanLoop(), factory, res.GetDefaultAnim());
2016-04-14 03:32:27 +00:00
}
2018-12-08 05:30:43 +00:00
SAdvancementDeltas CModelData::GetAdvancementDeltas(const CCharAnimTime& a, const CCharAnimTime& b) const {
if (x10_animData)
return x10_animData->GetAdvancementDeltas(a, b);
else
return {};
2016-04-14 03:32:27 +00:00
}
2018-12-08 05:30:43 +00:00
void CModelData::Render(const CStateManager& stateMgr, const zeus::CTransform& xf, const CActorLights* lights,
const CModelFlags& drawFlags) {
2018-12-08 05:30:43 +00:00
Render(GetRenderingModel(stateMgr), xf, lights, drawFlags);
2016-04-14 03:32:27 +00:00
}
bool CModelData::IsLoaded(int shaderIdx) {
2018-12-08 05:30:43 +00:00
if (x10_animData) {
if (!x10_animData->xd8_modelData->GetModel()->IsLoaded(shaderIdx))
return false;
if (auto* model = x10_animData->xf4_xrayModel.get())
2018-12-08 05:30:43 +00:00
if (!model->GetModel()->IsLoaded(shaderIdx))
return false;
if (auto* model = x10_animData->xf8_infraModel.get())
2018-12-08 05:30:43 +00:00
if (!model->GetModel()->IsLoaded(shaderIdx))
return false;
}
if (auto* model = x1c_normalModel.GetObj())
2018-12-08 05:30:43 +00:00
if (!model->IsLoaded(shaderIdx))
return false;
if (auto* model = x2c_xrayModel.GetObj())
2018-12-08 05:30:43 +00:00
if (!model->IsLoaded(shaderIdx))
return false;
if (auto* model = x3c_infraModel.GetObj())
2018-12-08 05:30:43 +00:00
if (!model->IsLoaded(shaderIdx))
return false;
return true;
}
2019-11-09 23:48:46 +00:00
u32 CModelData::GetNumMaterialSets() const {
if (x10_animData)
return x10_animData->GetModelData()->GetModel()->GetNumMaterialSets();
2019-11-09 23:48:46 +00:00
if (x1c_normalModel)
return x1c_normalModel->GetNumMaterialSets();
2019-11-09 23:48:46 +00:00
return 1;
2019-11-09 23:48:46 +00:00
}
2018-12-08 05:30:43 +00:00
CModelData::EWhichModel CModelData::GetRenderingModel(const CStateManager& stateMgr) {
switch (stateMgr.GetPlayerState()->GetActiveVisor(stateMgr)) {
case CPlayerState::EPlayerVisor::XRay:
return CModelData::EWhichModel::XRay;
case CPlayerState::EPlayerVisor::Thermal:
if (stateMgr.GetThermalDrawFlag() == EThermalDrawFlag::Cold)
return CModelData::EWhichModel::Thermal;
return CModelData::EWhichModel::ThermalHot;
default:
return CModelData::EWhichModel::Normal;
}
}
CSkinnedModel& CModelData::PickAnimatedModel(EWhichModel which) const {
CSkinnedModel* ret = nullptr;
switch (which) {
case EWhichModel::XRay:
ret = x10_animData->xf4_xrayModel.get();
break;
case EWhichModel::Thermal:
case EWhichModel::ThermalHot:
ret = x10_animData->xf8_infraModel.get();
break;
default:
break;
}
if (ret)
return *ret;
return *x10_animData->xd8_modelData.GetObj();
}
TLockedToken<CModel>& CModelData::PickStaticModel(EWhichModel which) {
2018-12-08 05:30:43 +00:00
switch (which) {
case EWhichModel::XRay:
if (x2c_xrayModel) {
return x2c_xrayModel;
}
2018-12-08 05:30:43 +00:00
break;
case EWhichModel::Thermal:
case EWhichModel::ThermalHot:
if (x3c_infraModel) {
return x3c_infraModel;
}
2018-12-08 05:30:43 +00:00
break;
default:
break;
}
return x1c_normalModel;
2018-12-08 05:30:43 +00:00
}
void CModelData::SetXRayModel(const std::pair<CAssetId, CAssetId>& modelSkin) {
if (modelSkin.first.IsValid()) {
if (g_ResFactory->GetResourceTypeById(modelSkin.first) == SBIG('CMDL')) {
if (x10_animData && modelSkin.second.IsValid() &&
g_ResFactory->GetResourceTypeById(modelSkin.second) == SBIG('CSKR')) {
x10_animData->SetXRayModel(g_SimplePool->GetObj({SBIG('CMDL'), modelSkin.first}),
g_SimplePool->GetObj({SBIG('CSKR'), modelSkin.second}));
} else {
x2c_xrayModel = g_SimplePool->GetObj({SBIG('CMDL'), modelSkin.first});
if (!x2c_xrayModel)
2020-04-11 22:51:39 +00:00
Log.report(logvisor::Fatal, FMT_STRING("unable to find CMDL {}"), modelSkin.first);
2018-12-08 05:30:43 +00:00
}
2016-04-14 21:42:47 +00:00
}
2018-12-08 05:30:43 +00:00
}
}
void CModelData::SetInfraModel(const std::pair<CAssetId, CAssetId>& modelSkin) {
if (modelSkin.first.IsValid()) {
if (g_ResFactory->GetResourceTypeById(modelSkin.first) == SBIG('CMDL')) {
if (x10_animData && modelSkin.second.IsValid() &&
g_ResFactory->GetResourceTypeById(modelSkin.second) == SBIG('CSKR')) {
x10_animData->SetInfraModel(g_SimplePool->GetObj({SBIG('CMDL'), modelSkin.first}),
g_SimplePool->GetObj({SBIG('CSKR'), modelSkin.second}));
} else {
x3c_infraModel = g_SimplePool->GetObj({SBIG('CMDL'), modelSkin.first});
if (!x3c_infraModel)
2020-04-11 22:51:39 +00:00
Log.report(logvisor::Fatal, FMT_STRING("unable to find CMDL {}"), modelSkin.first);
2018-12-08 05:30:43 +00:00
}
2016-04-14 21:42:47 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-04-14 03:32:27 +00:00
}
bool CModelData::IsDefinitelyOpaque(EWhichModel which) const {
2018-12-08 05:30:43 +00:00
if (x10_animData) {
CSkinnedModel& model = PickAnimatedModel(which);
return model.GetModel()->IsOpaque();
2018-12-08 05:30:43 +00:00
} else {
const auto& model = PickStaticModel(which);
return model->IsOpaque();
}
2016-04-14 03:32:27 +00:00
}
2018-12-08 05:30:43 +00:00
bool CModelData::GetIsLoop() const {
if (!x10_animData)
return false;
return x10_animData->GetIsLoop();
2016-04-14 03:32:27 +00:00
}
2018-12-08 05:30:43 +00:00
float CModelData::GetAnimationDuration(int idx) const {
if (!x10_animData)
return 0.f;
return x10_animData->GetAnimationDuration(idx);
2016-04-14 03:32:27 +00:00
}
2018-12-08 05:30:43 +00:00
void CModelData::EnableLooping(bool enable) {
if (!x10_animData)
return;
x10_animData->EnableLooping(enable);
2016-04-14 03:32:27 +00:00
}
2018-12-08 05:30:43 +00:00
void CModelData::AdvanceParticles(const zeus::CTransform& xf, float dt, CStateManager& stateMgr) {
if (!x10_animData)
return;
x10_animData->AdvanceParticles(xf, dt, x0_scale, stateMgr);
2016-04-14 03:32:27 +00:00
}
2018-12-08 05:30:43 +00:00
zeus::CAABox CModelData::GetBounds() const {
if (x10_animData) {
return x10_animData->GetBoundingBox(zeus::CTransform::Scale(x0_scale));
} else {
const zeus::CAABox& aabb = x1c_normalModel->GetAABB();
return zeus::CAABox(aabb.min * x0_scale, aabb.max * x0_scale);
}
2016-04-14 03:32:27 +00:00
}
2018-12-08 05:30:43 +00:00
zeus::CAABox CModelData::GetBounds(const zeus::CTransform& xf) const {
zeus::CTransform xf2 = xf * zeus::CTransform::Scale(x0_scale);
if (x10_animData)
return x10_animData->GetBoundingBox(xf2);
else
return x1c_normalModel->GetAABB().getTransformedAABox(xf2);
2016-04-14 03:32:27 +00:00
}
2018-12-08 05:30:43 +00:00
zeus::CTransform CModelData::GetScaledLocatorTransformDynamic(std::string_view name, const CCharAnimTime* time) const {
zeus::CTransform xf = GetLocatorTransformDynamic(name, time);
xf.origin *= x0_scale;
return xf;
2016-04-14 03:32:27 +00:00
}
2018-12-08 05:30:43 +00:00
zeus::CTransform CModelData::GetScaledLocatorTransform(std::string_view name) const {
zeus::CTransform xf = GetLocatorTransform(name);
xf.origin *= x0_scale;
return xf;
2016-04-14 03:32:27 +00:00
}
2018-12-08 05:30:43 +00:00
zeus::CTransform CModelData::GetLocatorTransformDynamic(std::string_view name, const CCharAnimTime* time) const {
if (x10_animData)
return x10_animData->GetLocatorTransform(name, time);
else
return {};
2016-04-14 03:32:27 +00:00
}
2018-12-08 05:30:43 +00:00
zeus::CTransform CModelData::GetLocatorTransform(std::string_view name) const {
if (x10_animData)
return x10_animData->GetLocatorTransform(name, nullptr);
else
return {};
2016-04-14 03:32:27 +00:00
}
2018-12-08 05:30:43 +00:00
SAdvancementDeltas CModelData::AdvanceAnimationIgnoreParticles(float dt, CRandom16& rand, bool advTree) {
if (x10_animData)
return x10_animData->AdvanceIgnoreParticles(dt, rand, advTree);
else
return {};
2016-04-14 03:32:27 +00:00
}
2018-12-08 05:30:43 +00:00
SAdvancementDeltas CModelData::AdvanceAnimation(float dt, CStateManager& stateMgr, TAreaId aid, bool advTree) {
if (x10_animData)
return x10_animData->Advance(dt, x0_scale, stateMgr, aid, advTree);
else
return {};
2016-04-14 03:32:27 +00:00
}
2018-12-08 05:30:43 +00:00
bool CModelData::IsAnimating() const {
if (!x10_animData)
return false;
return x10_animData->IsAnimating();
2016-04-14 03:32:27 +00:00
}
2018-12-08 05:30:43 +00:00
bool CModelData::IsInFrustum(const zeus::CTransform& xf, const zeus::CFrustum& frustum) const {
if (!x10_animData && !x1c_normalModel)
return true;
return frustum.aabbFrustumTest(GetBounds(xf));
}
void CModelData::RenderParticles(const zeus::CFrustum& frustum) const {
if (x10_animData)
x10_animData->RenderAuxiliary(frustum);
}
void CModelData::Touch(EWhichModel which, int shaderIdx) {
2018-12-08 05:30:43 +00:00
if (x10_animData)
x10_animData->Touch(PickAnimatedModel(which), shaderIdx);
else
PickStaticModel(which)->Touch(shaderIdx);
}
void CModelData::Touch(const CStateManager& stateMgr, int shaderIdx) { Touch(GetRenderingModel(stateMgr), shaderIdx); }
void CModelData::RenderThermal(const zeus::CColor& mulColor, const zeus::CColor& addColor, const CModelFlags& flags) {
// TODO float* params and xc_
// g_Renderer->DrawThermalModel(xc_, mulColor, addColor, nullptr, nullptr, flags);
2018-12-08 05:30:43 +00:00
}
void CModelData::RenderThermal(const zeus::CTransform& xf, const zeus::CColor& mulColor, const zeus::CColor& addColor,
const CModelFlags& flags) {
CGraphics::SetModelMatrix(xf * zeus::CTransform::Scale(x0_scale));
CGraphics::DisableAllLights();
2018-12-08 05:30:43 +00:00
if (x10_animData) {
CSkinnedModel& model = PickAnimatedModel(EWhichModel::ThermalHot);
x10_animData->SetupRender(model, nullptr, nullptr);
ThermalDraw(model, mulColor, addColor, flags);
2018-12-08 05:30:43 +00:00
} else {
auto& model = PickStaticModel(EWhichModel::ThermalHot);
g_Renderer->DrawThermalModel(*model, mulColor, addColor, {}, {}, flags);
2018-12-08 05:30:43 +00:00
}
}
void CModelData::RenderUnsortedParts(EWhichModel which, const zeus::CTransform& xf, const CActorLights* lights,
const CModelFlags& drawFlags) {
2018-12-08 05:30:43 +00:00
if ((x14_25_sortThermal && which == EWhichModel::ThermalHot) || x10_animData || !x1c_normalModel ||
drawFlags.x0_blendMode > 4) {
x14_24_renderSorted = false;
2018-12-08 05:30:43 +00:00
return;
}
CGraphics::SetModelMatrix(xf * zeus::CTransform::Scale(x0_scale));
if (lights != nullptr) {
lights->ActivateLights();
2018-12-08 05:30:43 +00:00
} else {
CGraphics::DisableAllLights();
g_Renderer->SetAmbientColor(x18_ambientColor);
2018-12-08 05:30:43 +00:00
}
PickStaticModel(which)->DrawUnsortedParts(drawFlags);
g_Renderer->SetAmbientColor(zeus::skWhite);
2018-12-08 05:30:43 +00:00
CGraphics::DisableAllLights();
x14_24_renderSorted = true;
2018-12-08 05:30:43 +00:00
}
void CModelData::Render(EWhichModel which, const zeus::CTransform& xf, const CActorLights* lights,
const CModelFlags& drawFlags) {
2018-12-08 05:30:43 +00:00
if (x14_25_sortThermal && which == EWhichModel::ThermalHot) {
zeus::CColor mul(drawFlags.x4_color.a(), drawFlags.x4_color.a());
RenderThermal(xf, mul, {0.f, 0.f, 0.f, 0.25f}, drawFlags);
return;
}
2016-04-14 21:42:47 +00:00
CGraphics::SetModelMatrix(xf * zeus::CTransform::Scale(x0_scale));
2016-08-21 00:04:50 +00:00
if (lights != nullptr) {
lights->ActivateLights();
} else {
CGraphics::DisableAllLights();
g_Renderer->SetAmbientColor(x18_ambientColor);
}
if (x10_animData) {
x10_animData->Render(PickAnimatedModel(which), drawFlags, nullptr, nullptr);
} else {
// TODO supposed to be optional_object?
if (x1c_normalModel) {
auto& model = PickStaticModel(which);
if (x14_24_renderSorted) {
model->DrawSortedParts(drawFlags);
2018-12-08 05:30:43 +00:00
} else {
model->Draw(drawFlags);
2018-12-08 05:30:43 +00:00
}
2018-02-09 07:12:26 +00:00
}
2018-12-08 05:30:43 +00:00
}
g_Renderer->SetAmbientColor(zeus::skWhite);
CGraphics::DisableAllLights();
x14_24_renderSorted = false;
2016-04-14 03:32:27 +00:00
}
void CModelData::FlatDraw(EWhichModel which, const zeus::CTransform& xf, bool unsortedOnly, const CModelFlags& flags) {
g_Renderer->SetModelMatrix(xf * zeus::CTransform::Scale(x0_scale));
CGraphics::DisableAllLights();
if (!x10_animData) {
g_Renderer->DrawModelFlat(*PickStaticModel(which), flags, unsortedOnly, nullptr, nullptr);
} else {
auto model = PickAnimatedModel(which);
x10_animData->SetupRender(model, nullptr, nullptr);
model.DoDrawCallback([=](TConstVectorRef positions, TConstVectorRef normals) {
auto m = model.GetModel();
g_Renderer->DrawModelFlat(*m, flags, unsortedOnly, positions, normals);
});
}
}
void CModelData::MultiLightingDraw(EWhichModel which, const zeus::CTransform& xf, const CActorLights* lights,
const zeus::CColor& alphaColor, const zeus::CColor& additiveColor) {
CModel* model = nullptr;
const auto callback = [&](auto positions, auto normals) {
CGraphics::DisableAllLights();
constexpr CModelFlags flags1{5, 0, 3, zeus::CColor{1.f, 0.f}};
const CModelFlags flags2{5, 0, 1, alphaColor};
const CModelFlags flags3{7, 0, 1, additiveColor};
if (positions == nullptr) {
model->Draw(flags1);
if (lights != nullptr) {
lights->ActivateLights();
}
model->Draw(flags2);
model->Draw(flags3);
} else {
model->Draw(positions, normals, flags1);
if (lights != nullptr) {
lights->ActivateLights();
}
model->Draw(positions, normals, flags2);
model->Draw(positions, normals, flags3);
}
};
2018-12-08 05:30:43 +00:00
CGraphics::SetModelMatrix(xf * zeus::CTransform::Scale(x0_scale));
if (x10_animData) {
auto& skinnedModel = PickAnimatedModel(which);
x10_animData->SetupRender(skinnedModel, nullptr, nullptr);
model = skinnedModel.GetModel().GetObj();
skinnedModel.DoDrawCallback(callback);
} else {
model = PickStaticModel(which).GetObj();
callback(nullptr, nullptr);
}
}
void CModelData::MultiPassDraw(EWhichModel which, const zeus::CTransform& xf, const CActorLights* lights,
const CModelFlags* flags, u32 count) {
CGraphics::SetModelMatrix(xf * zeus::CTransform::Scale(x0_scale));
if (lights == nullptr) {
CGraphics::DisableAllLights();
g_Renderer->SetAmbientColor(x18_ambientColor);
} else {
lights->ActivateLights();
}
if (x10_animData) {
auto& skinnedModel = PickAnimatedModel(which);
x10_animData->SetupRender(skinnedModel, nullptr, nullptr);
auto& model = *skinnedModel.GetModel();
skinnedModel.DoDrawCallback([&](auto positions, auto normals) {
for (int i = 0; i < count; ++i) {
model.Draw(positions, normals, flags[i]);
}
});
} else {
auto& model = *PickStaticModel(which);
for (int i = 0; i < count; ++i) {
model.Draw(flags[i]);
}
}
2018-12-08 05:30:43 +00:00
}
void CModelData::DisintegrateDraw(const CStateManager& mgr, const zeus::CTransform& xf, CTexture& tex,
2018-12-08 05:30:43 +00:00
const zeus::CColor& addColor, float t) {
DisintegrateDraw(GetRenderingModel(mgr), xf, tex, addColor, t);
}
void CModelData::DisintegrateDraw(EWhichModel which, const zeus::CTransform& xf, CTexture& tex,
2018-12-08 05:30:43 +00:00
const zeus::CColor& addColor, float t) {
zeus::CTransform scaledXf = xf * zeus::CTransform::Scale(x0_scale);
CGraphics::SetModelMatrix(scaledXf);
CGraphics::DisableAllLights();
const auto aabb = GetBounds(scaledXf);
2018-12-08 05:30:43 +00:00
if (x10_animData) {
auto& model = PickAnimatedModel(which);
x10_animData->SetupRender(model, nullptr, nullptr);
model.DoDrawCallback([&](auto positions, auto normals) {
g_Renderer->DrawModelDisintegrate(*model.GetModel(), tex, addColor, positions, normals, t);
});
2018-12-08 05:30:43 +00:00
} else {
g_Renderer->DrawModelDisintegrate(*PickStaticModel(which), tex, addColor, nullptr, nullptr, t);
2018-12-08 05:30:43 +00:00
}
}
void CModelData::ThermalDraw(CSkinnedModel& model, const zeus::CColor& mulColor, const zeus::CColor& addColor,
const CModelFlags& flags) {
model.DoDrawCallback([&](auto positions, auto normals) {
g_Renderer->DrawThermalModel(*model.GetModel(), mulColor, addColor, positions, normals, flags);
});
}
2018-12-08 05:30:43 +00:00
2021-04-10 08:42:06 +00:00
} // namespace metaforce