Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : /* 19 : * Raw description of a skeleton animation 20 : */ 21 : 22 : #include "precompiled.h" 23 : 24 : #include "SkeletonAnimDef.h" 25 : #include "maths/MathUtil.h" 26 : #include "maths/Matrix3D.h" 27 : #include "ps/CStr.h" 28 : #include "ps/CLogger.h" 29 : #include "ps/FileIo.h" 30 : 31 : namespace 32 : { 33 : // Start IDs at 1 to leave 0 as a special value. 34 : std::uint32_t g_NextSkeletonDefUID = 1; 35 : } 36 : 37 : /////////////////////////////////////////////////////////////////////////////////////////// 38 : // CSkeletonAnimDef constructor 39 0 : CSkeletonAnimDef::CSkeletonAnimDef() : m_FrameTime(0), m_NumKeys(0), m_NumFrames(0) 40 : { 41 0 : m_UID = g_NextSkeletonDefUID++; 42 : // Log a warning if we ever overflow. Should that not result from a bug, bumping to std::uint64_t 43 : // ought to suffice. 44 0 : if (g_NextSkeletonDefUID == 0) 45 : { 46 : // Reset to 1. 47 0 : g_NextSkeletonDefUID++; 48 0 : LOGWARNING("CSkeletonAnimDef unique ID overflowed to 0 - model-animation bounds may be incorrect."); 49 : } 50 0 : } 51 : 52 : /////////////////////////////////////////////////////////////////////////////////////////// 53 : // CSkeletonAnimDef destructor 54 0 : CSkeletonAnimDef::~CSkeletonAnimDef() 55 : { 56 0 : } 57 : 58 : /////////////////////////////////////////////////////////////////////////////////////////// 59 : // BuildBoneMatrices: build matrices for all bones at the given time (in MS) in this 60 : // animation 61 0 : void CSkeletonAnimDef::BuildBoneMatrices(float time, CMatrix3D* matrices, bool loop) const 62 : { 63 0 : float fstartframe = time/m_FrameTime; 64 0 : size_t startframe = (size_t)(int)(time/m_FrameTime); 65 0 : float deltatime = fstartframe-startframe; 66 : 67 0 : startframe %= m_NumFrames; 68 : 69 0 : size_t endframe = startframe + 1; 70 0 : endframe %= m_NumFrames; 71 : 72 0 : if (!loop && endframe == 0) 73 : { 74 : // This might be something like a death animation, and interpolating 75 : // between the final frame and the initial frame is wrong, because they're 76 : // totally different. So if we've looped around to endframe==0, just display 77 : // the animation's final frame with no interpolation. 78 0 : for (size_t i = 0; i < m_NumKeys; i++) 79 : { 80 0 : const Key& key = GetKey(startframe, i); 81 0 : matrices[i].SetIdentity(); 82 0 : matrices[i].Rotate(key.m_Rotation); 83 0 : matrices[i].Translate(key.m_Translation); 84 : } 85 : } 86 : else 87 : { 88 0 : for (size_t i = 0; i < m_NumKeys; i++) 89 : { 90 0 : const Key& startkey = GetKey(startframe, i); 91 0 : const Key& endkey = GetKey(endframe, i); 92 : 93 0 : CVector3D trans = Interpolate(startkey.m_Translation, endkey.m_Translation, deltatime); 94 : // TODO: is slerp the best thing to use here? 95 0 : CQuaternion rot; 96 0 : rot.Slerp(startkey.m_Rotation, endkey.m_Rotation, deltatime); 97 : 98 0 : rot.ToMatrix(matrices[i]); 99 0 : matrices[i].Translate(trans); 100 : } 101 : } 102 0 : } 103 : 104 : /////////////////////////////////////////////////////////////////////////////////////////// 105 : // Load: try to load the anim from given file; return a new anim if successful 106 0 : std::unique_ptr<CSkeletonAnimDef> CSkeletonAnimDef::Load(const VfsPath& filename) 107 : { 108 0 : CFileUnpacker unpacker; 109 0 : unpacker.Read(filename,"PSSA"); 110 : 111 : // check version 112 0 : if (unpacker.GetVersion()<FILE_READ_VERSION) { 113 0 : throw PSERROR_File_InvalidVersion(); 114 : } 115 : 116 : // unpack the data 117 0 : auto anim = std::make_unique<CSkeletonAnimDef>(); 118 0 : try { 119 0 : CStr name; // unused - just here to maintain compatibility with the animation files 120 0 : unpacker.UnpackString(name); 121 0 : unpacker.UnpackRaw(&anim->m_FrameTime,sizeof(anim->m_FrameTime)); 122 0 : anim->m_NumKeys = unpacker.UnpackSize(); 123 0 : anim->m_NumFrames = unpacker.UnpackSize(); 124 0 : anim->m_Keys.resize(anim->m_NumKeys*anim->m_NumFrames); 125 0 : unpacker.UnpackRaw(anim->m_Keys.data(), anim->m_Keys.size() * sizeof(decltype(anim->m_Keys)::value_type)); 126 0 : } catch (PSERROR_File&) { 127 0 : anim.reset(); 128 0 : throw; 129 0 : } 130 : 131 0 : return anim; 132 0 : } 133 : 134 : /////////////////////////////////////////////////////////////////////////////////////////// 135 : // Save: try to save anim to file 136 0 : void CSkeletonAnimDef::Save(const VfsPath& pathname, const CSkeletonAnimDef& anim) 137 : { 138 0 : CFilePacker packer(FILE_VERSION, "PSSA"); 139 : 140 : // pack up all the data 141 0 : packer.PackString(""); 142 0 : packer.PackRaw(&anim.m_FrameTime,sizeof(anim.m_FrameTime)); 143 0 : const size_t numKeys = anim.m_NumKeys; 144 0 : packer.PackSize(numKeys); 145 0 : const size_t numFrames = anim.m_NumFrames; 146 0 : packer.PackSize(numFrames); 147 0 : packer.PackRaw(anim.m_Keys.data(), anim.m_Keys.size() * sizeof(decltype(anim.m_Keys)::value_type)); 148 : 149 : // now write it 150 0 : packer.Write(pathname); 151 0 : }