metaforce/Runtime/Character/CHierarchyPoseBuilder.cpp

153 lines
5.3 KiB
C++
Raw Normal View History

#include "Runtime/Character/CHierarchyPoseBuilder.hpp"
#include "Runtime/Character/CAnimData.hpp"
#include "Runtime/Character/CCharLayoutInfo.hpp"
#include <zeus/CEulerAngles.hpp>
2016-04-13 23:07:18 +00:00
2021-04-10 08:42:06 +00:00
namespace metaforce {
2018-12-08 05:30:43 +00:00
void CHierarchyPoseBuilder::BuildIntoHierarchy(const CCharLayoutInfo& layout, const CSegId& boneId,
const CSegId& nullId) {
if (!x38_treeMap.HasElement(boneId)) {
const CCharLayoutNode::Bone& bone = layout.GetRootNode()->GetBoneMap()[boneId];
if (bone.x0_parentId == nullId) {
2020-03-13 22:29:20 +00:00
x30_rootId = boneId;
x34_hasRoot = true;
2018-12-08 05:30:43 +00:00
zeus::CVector3f origin = layout.GetFromParentUnrotated(boneId);
CTreeNode& node = x38_treeMap[boneId];
node.x14_offset = origin;
} else {
BuildIntoHierarchy(layout, bone.x0_parentId, nullId);
zeus::CVector3f origin = layout.GetFromParentUnrotated(boneId);
CTreeNode& pNode = x38_treeMap[bone.x0_parentId];
CTreeNode& node = x38_treeMap[boneId];
node.x14_offset = origin;
node.x1_sibling = pNode.x0_child;
pNode.x0_child = boneId;
2016-04-13 23:07:18 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-04-13 23:07:18 +00:00
}
void CHierarchyPoseBuilder::RecursivelyBuildNoScale(const CSegId& boneId, const CTreeNode& node,
CPoseAsTransforms& pose, const zeus::CQuaternion& parentRot,
2018-12-08 05:30:43 +00:00
const zeus::CMatrix3f& parentXf,
const zeus::CVector3f& parentOffset) const {
zeus::CQuaternion quat = parentRot * node.x4_rotation;
zeus::CMatrix3f xf = quat;
zeus::CVector3f xfOffset = parentXf * node.x14_offset + parentOffset;
pose.Insert(boneId, quat, xfOffset);
2018-12-08 05:30:43 +00:00
CSegId curBone = node.x0_child;
while (curBone != 0) {
const CTreeNode& node = x38_treeMap[curBone];
RecursivelyBuild(curBone, node, pose, quat, xf, xfOffset);
curBone = node.x1_sibling;
}
2016-04-13 23:07:18 +00:00
}
2018-12-08 05:30:43 +00:00
void CHierarchyPoseBuilder::RecursivelyBuild(const CSegId& boneId, const CTreeNode& node, CPoseAsTransforms& pose,
const zeus::CQuaternion& parentRot, const zeus::CMatrix3f& parentXf,
const zeus::CVector3f& parentOffset) const {
zeus::CQuaternion quat = parentRot * node.x4_rotation;
float scale;
if (x0_layoutDesc.GetScaledLayoutDescription()) {
scale = x0_layoutDesc.GetScaledLayoutDescription()->GlobalScale();
2018-12-08 05:30:43 +00:00
} else {
scale = 1.f;
}
zeus::CMatrix3f rotation;
2018-12-08 05:30:43 +00:00
if (scale == 1.f)
rotation = quat;
2018-12-08 05:30:43 +00:00
else
rotation = parentXf * (zeus::CMatrix3f{node.x4_rotation} * zeus::CMatrix3f{scale});
2018-12-08 05:30:43 +00:00
zeus::CVector3f offset = parentOffset + (parentXf * node.x14_offset);
pose.Insert(boneId, rotation, offset);
2018-12-08 05:30:43 +00:00
CSegId curBone = node.x0_child;
while (curBone != 0) {
const CTreeNode& node = x38_treeMap[curBone];
RecursivelyBuild(curBone, node, pose, quat, quat, offset);
2018-12-08 05:30:43 +00:00
curBone = node.x1_sibling;
}
2016-04-13 23:07:18 +00:00
}
2018-12-08 05:30:43 +00:00
void CHierarchyPoseBuilder::BuildTransform(const CSegId& boneId, zeus::CTransform& xfOut) const {
TLockedToken<CCharLayoutInfo> layoutInfoTok;
float scale;
if (x0_layoutDesc.GetScaledLayoutDescription()) {
layoutInfoTok = x0_layoutDesc.GetScaledLayoutDescription()->ScaledLayout();
scale = x0_layoutDesc.GetScaledLayoutDescription()->GlobalScale();
} else {
layoutInfoTok = x0_layoutDesc.GetCharLayoutInfo();
scale = 1.f;
}
const CCharLayoutInfo& layoutInfo = *layoutInfoTok.GetObj();
u32 idCount = 0;
CSegId buildIDs[100];
{
CSegId curId = boneId;
while (curId != 2) {
buildIDs[idCount++] = curId;
curId = layoutInfo.GetRootNode()->GetBoneMap()[curId].x0_parentId;
2016-04-13 23:07:18 +00:00
}
2018-12-08 05:30:43 +00:00
}
zeus::CQuaternion accumRot;
zeus::CMatrix3f accumXF;
zeus::CVector3f accumPos;
for (CSegId* id = &buildIDs[idCount]; id != buildIDs; --id) {
CSegId& thisId = id[-1];
const CTreeNode& node = x38_treeMap[thisId];
accumRot *= node.x4_rotation;
accumPos += accumXF * node.x14_offset;
if (scale == 1.f)
accumXF = accumRot;
2016-04-13 23:07:18 +00:00
else
2018-12-08 05:30:43 +00:00
accumXF = accumXF * zeus::CMatrix3f(node.x4_rotation) * zeus::CMatrix3f(scale);
}
2016-04-13 23:07:18 +00:00
2018-12-08 05:30:43 +00:00
xfOut.setRotation(accumXF);
xfOut.origin = accumPos;
2016-04-13 23:07:18 +00:00
}
2018-12-08 05:30:43 +00:00
void CHierarchyPoseBuilder::BuildNoScale(CPoseAsTransforms& pose) {
pose.Clear();
2020-03-13 22:29:20 +00:00
const CTreeNode& node = x38_treeMap[x30_rootId];
2018-12-08 05:30:43 +00:00
zeus::CQuaternion quat;
zeus::CMatrix3f mtx;
zeus::CVector3f vec;
2020-03-13 22:29:20 +00:00
RecursivelyBuildNoScale(x30_rootId, node, pose, quat, mtx, vec);
2016-04-13 23:07:18 +00:00
}
2018-12-08 05:30:43 +00:00
void CHierarchyPoseBuilder::Insert(const CSegId& boneId, const zeus::CQuaternion& quat) {
CTreeNode& node = x38_treeMap[boneId];
node.x4_rotation = quat;
2016-08-21 00:04:50 +00:00
}
2018-12-08 05:30:43 +00:00
void CHierarchyPoseBuilder::Insert(const CSegId& boneId, const zeus::CQuaternion& quat, const zeus::CVector3f& offset) {
CTreeNode& node = x38_treeMap[boneId];
node.x4_rotation = quat;
node.x14_offset = offset;
2016-08-21 00:04:50 +00:00
}
2016-04-13 23:07:18 +00:00
CHierarchyPoseBuilder::CHierarchyPoseBuilder(const CLayoutDescription& layout)
2018-12-08 05:30:43 +00:00
: x0_layoutDesc(layout), x38_treeMap(layout.GetCharLayoutInfo()->GetSegIdList().GetList().size()) {
TLockedToken<CCharLayoutInfo> layoutInfoTok;
if (layout.GetScaledLayoutDescription())
layoutInfoTok = layout.GetScaledLayoutDescription()->ScaledLayout();
else
layoutInfoTok = layout.GetCharLayoutInfo();
const CCharLayoutInfo& layoutInfo = *layoutInfoTok.GetObj();
const CSegIdList& segIDs = layoutInfo.GetSegIdList();
for (const CSegId& id : segIDs.GetList())
BuildIntoHierarchy(layoutInfo, id, 2);
2016-04-13 23:07:18 +00:00
}
2021-04-10 08:42:06 +00:00
} // namespace metaforce