| 1 |
/* +------------------------------------------------------------------------+ |
| 2 |
| Mobile Robot Programming Toolkit (MRPT) | |
| 3 |
| https://www.mrpt.org/ | |
| 4 |
| | |
| 5 |
| Copyright (c) 2005-2026, Individual contributors, see AUTHORS file | |
| 6 |
| See: https://www.mrpt.org/Authors - All rights reserved. | |
| 7 |
| Released under BSD License. See: https://www.mrpt.org/License | |
| 8 |
+------------------------------------------------------------------------+ */ |
| 9 |
|
| 10 |
#include "opengl-precomp.h" // Precompiled header |
| 11 |
// |
| 12 |
#include <mrpt/opengl/CAnimatedAssimpModel.h> |
| 13 |
#include <mrpt/serialization/CArchive.h> |
| 14 |
|
| 15 |
#if MRPT_HAS_ASSIMP |
| 16 |
#include <assimp/anim.h> |
| 17 |
#include <assimp/scene.h> |
| 18 |
#endif |
| 19 |
|
| 20 |
#include <Eigen/Dense> |
| 21 |
#include <cmath> |
| 22 |
|
| 23 |
using namespace mrpt::opengl; |
| 24 |
using namespace mrpt::math; |
| 25 |
|
| 26 |
IMPLEMENTS_SERIALIZABLE(CAnimatedAssimpModel, CAssimpModel, mrpt::opengl) |
| 27 |
|
| 28 |
// ============================================================================ |
| 29 |
// Serialization (delegates to base; skeleton is re-extracted on load) |
| 30 |
// ============================================================================ |
| 31 |
|
| 32 |
uint8_t CAnimatedAssimpModel::serializeGetVersion() const { return 0; } |
| 33 |
|
| 34 |
void CAnimatedAssimpModel::serializeTo(mrpt::serialization::CArchive& out) const |
| 35 |
{ |
| 36 |
// Delegate to base class which stores the assimp blob: |
| 37 |
out << CAssimpModel::serializeGetVersion(); |
| 38 |
CAssimpModel::serializeTo(out); |
| 39 |
// Animation state (cheap to store): |
| 40 |
out << activeAnimation_ << currentTime_ << looping_; |
| 41 |
} |
| 42 |
|
| 43 |
void CAnimatedAssimpModel::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version) |
| 44 |
{ |
| 45 |
switch (version) |
| 46 |
{ |
| 47 |
case 0: |
| 48 |
{ |
| 49 |
uint8_t baseVersion = in.ReadAs<uint8_t>(); |
| 50 |
CAssimpModel::serializeFrom(in, baseVersion); |
| 51 |
in >> activeAnimation_ >> currentTime_ >> looping_; |
| 52 |
// Skeleton will be re-extracted via onAfterLoadScene() triggered |
| 53 |
// by the base class deserialization path. |
| 54 |
} |
| 55 |
break; |
| 56 |
default: |
| 57 |
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// ============================================================================ |
| 62 |
// Animation Control |
| 63 |
// ============================================================================ |
| 64 |
|
| 65 |
void CAnimatedAssimpModel::setAnimationTime(double timeSeconds) |
| 66 |
{ |
| 67 |
if (!hasSkeleton_ || animations_.empty()) |
| 68 |
{ |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
currentTime_ = timeSeconds; |
| 73 |
|
| 74 |
// Update bone local transforms from animation |
| 75 |
updateBoneTransforms(); |
| 76 |
|
| 77 |
// Mark buffers as needing update |
| 78 |
notifyChange(); |
| 79 |
} |
| 80 |
|
| 81 |
double CAnimatedAssimpModel::getAnimationDuration(size_t animIndex) const |
| 82 |
{ |
| 83 |
if (animIndex >= animations_.size()) |
| 84 |
{ |
| 85 |
return 0.0; |
| 86 |
} |
| 87 |
const auto& anim = animations_[animIndex]; |
| 88 |
const double ticksPerSec = (anim.ticksPerSecond > 0.0) ? anim.ticksPerSecond : 25.0; |
| 89 |
return anim.duration / ticksPerSec; |
| 90 |
} |
| 91 |
|
| 92 |
size_t CAnimatedAssimpModel::getAnimationCount() const { return animations_.size(); } |
| 93 |
|
| 94 |
std::string CAnimatedAssimpModel::getAnimationName(size_t animIndex) const |
| 95 |
{ |
| 96 |
if (animIndex >= animations_.size()) |
| 97 |
{ |
| 98 |
return {}; |
| 99 |
} |
| 100 |
return animations_[animIndex].name; |
| 101 |
} |
| 102 |
|
| 103 |
void CAnimatedAssimpModel::setActiveAnimation(size_t animIndex) |
| 104 |
{ |
| 105 |
if (animIndex < animations_.size()) |
| 106 |
{ |
| 107 |
activeAnimation_ = animIndex; |
| 108 |
currentTime_ = 0.0; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
void CAnimatedAssimpModel::setActiveAnimation(const std::string& animName) |
| 113 |
{ |
| 114 |
for (size_t i = 0; i < animations_.size(); ++i) |
| 115 |
{ |
| 116 |
if (animations_[i].name == animName) |
| 117 |
{ |
| 118 |
setActiveAnimation(i); |
| 119 |
return; |
| 120 |
} |
| 121 |
} |
| 122 |
// Not found — do nothing (user can check getAnimationCount / names). |
| 123 |
} |
| 124 |
|
| 125 |
double CAnimatedAssimpModel::getAnimationProgress() const |
| 126 |
{ |
| 127 |
if (animations_.empty() || activeAnimation_ >= animations_.size()) |
| 128 |
{ |
| 129 |
return 0.0; |
| 130 |
} |
| 131 |
const double dur = getAnimationDuration(activeAnimation_); |
| 132 |
if (dur < 1e-9) |
| 133 |
{ |
| 134 |
return 0.0; |
| 135 |
} |
| 136 |
const double t = looping_ ? std::fmod(currentTime_, dur) : currentTime_; |
| 137 |
return std::clamp(t / dur, 0.0, 1.0); |
| 138 |
} |
| 139 |
|
| 140 |
// ========== Bone Access ===================================================== |
| 141 |
|
| 142 |
int CAnimatedAssimpModel::getBoneIndex(const std::string& boneName) const |
| 143 |
{ |
| 144 |
auto it = boneNameToIndex_.find(boneName); |
| 145 |
if (it == boneNameToIndex_.end()) |
| 146 |
{ |
| 147 |
return -1; |
| 148 |
} |
| 149 |
return static_cast<int>(it->second); |
| 150 |
} |
| 151 |
|
| 152 |
void CAnimatedAssimpModel::setBoneLocalTransform( |
| 153 |
size_t boneIndex, const CMatrixDouble44& localTransform) |
| 154 |
{ |
| 155 |
if (boneIndex >= bones_.size()) |
| 156 |
{ |
| 157 |
return; |
| 158 |
} |
| 159 |
bones_[boneIndex].hasOverride = true; |
| 160 |
bones_[boneIndex].overrideTransform = localTransform; |
| 161 |
} |
| 162 |
|
| 163 |
void CAnimatedAssimpModel::clearBoneOverrides() |
| 164 |
{ |
| 165 |
for (auto& bone : bones_) |
| 166 |
{ |
| 167 |
bone.hasOverride = false; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
// ============================================================================ |
| 172 |
// onAfterLoadScene — called by base class after loadScene / deserialize |
| 173 |
// ============================================================================ |
| 174 |
|
| 175 |
void CAnimatedAssimpModel::onAfterLoadScene() { extractSkeletonData(); } |
| 176 |
|
| 177 |
// ============================================================================ |
| 178 |
// Skeleton Extraction |
| 179 |
// ============================================================================ |
| 180 |
|
| 181 |
#if MRPT_HAS_ASSIMP |
| 182 |
namespace |
| 183 |
{ |
| 184 |
CMatrixDouble44 aiMatrixToMrpt(const aiMatrix4x4& m) |
| 185 |
{ |
| 186 |
CMatrixDouble44 M; |
| 187 |
M(0, 0) = m.a1; |
| 188 |
M(0, 1) = m.a2; |
| 189 |
M(0, 2) = m.a3; |
| 190 |
M(0, 3) = m.a4; |
| 191 |
M(1, 0) = m.b1; |
| 192 |
M(1, 1) = m.b2; |
| 193 |
M(1, 2) = m.b3; |
| 194 |
M(1, 3) = m.b4; |
| 195 |
M(2, 0) = m.c1; |
| 196 |
M(2, 1) = m.c2; |
| 197 |
M(2, 2) = m.c3; |
| 198 |
M(2, 3) = m.c4; |
| 199 |
M(3, 0) = m.d1; |
| 200 |
M(3, 1) = m.d2; |
| 201 |
M(3, 2) = m.d3; |
| 202 |
M(3, 3) = m.d4; |
| 203 |
return M; |
| 204 |
} |
| 205 |
|
| 206 |
TPoint3Df aiVecToMrpt(const aiVector3D& v) { return {v.x, v.y, v.z}; } |
| 207 |
|
| 208 |
} // anonymous namespace |
| 209 |
#endif |
| 210 |
|
| 211 |
void CAnimatedAssimpModel::extractSkeletonData() |
| 212 |
{ |
| 213 |
bones_.clear(); |
| 214 |
boneNameToIndex_.clear(); |
| 215 |
vertexBoneMap_.clear(); |
| 216 |
animations_.clear(); |
| 217 |
hasSkeleton_ = false; |
| 218 |
|
| 219 |
#if MRPT_HAS_ASSIMP |
| 220 |
const aiScene* scene = getAssimpScene(); |
| 221 |
if (!scene) |
| 222 |
{ |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
// --- 1) Extract bones from all meshes -------------------------------- |
| 227 |
for (unsigned int m = 0; m < scene->mNumMeshes; ++m) |
| 228 |
{ |
| 229 |
const aiMesh* mesh = scene->mMeshes[m]; |
| 230 |
if (!mesh->HasBones()) |
| 231 |
{ |
| 232 |
continue; |
| 233 |
} |
| 234 |
|
| 235 |
for (unsigned int b = 0; b < mesh->mNumBones; ++b) |
| 236 |
{ |
| 237 |
const aiBone* bone = mesh->mBones[b]; |
| 238 |
const std::string boneName(bone->mName.data); |
| 239 |
|
| 240 |
// Get or create bone entry |
| 241 |
size_t boneIndex; |
| 242 |
auto it = boneNameToIndex_.find(boneName); |
| 243 |
if (it == boneNameToIndex_.end()) |
| 244 |
{ |
| 245 |
boneIndex = bones_.size(); |
| 246 |
boneNameToIndex_[boneName] = boneIndex; |
| 247 |
|
| 248 |
Bone newBone; |
| 249 |
newBone.name = boneName; |
| 250 |
newBone.offsetMatrix = aiMatrixToMrpt(bone->mOffsetMatrix); |
| 251 |
newBone.localTransform = CMatrixDouble44::Identity(); |
| 252 |
newBone.globalTransform = CMatrixDouble44::Identity(); |
| 253 |
newBone.finalTransform = CMatrixDouble44::Identity(); |
| 254 |
bones_.push_back(newBone); |
| 255 |
} |
| 256 |
else |
| 257 |
{ |
| 258 |
boneIndex = it->second; |
| 259 |
} |
| 260 |
|
| 261 |
// Store per-vertex weights using the composite key |
| 262 |
for (unsigned int w = 0; w < bone->mNumWeights; ++w) |
| 263 |
{ |
| 264 |
const aiVertexWeight& vw = bone->mWeights[w]; |
| 265 |
const uint64_t key = vertexKey(m, vw.mVertexId); |
| 266 |
vertexBoneMap_[key].addBoneWeight(static_cast<int>(boneIndex), vw.mWeight); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
if (bones_.empty()) |
| 272 |
{ |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
// --- 2) Build bone hierarchy from node tree -------------------------- |
| 277 |
buildBoneHierarchy(scene->mRootNode, -1); |
| 278 |
|
| 279 |
// Store global inverse transform (root node) |
| 280 |
globalInverseTransform_ = aiMatrixToMrpt(scene->mRootNode->mTransformation); |
| 281 |
// Invert it: |
| 282 |
{ |
| 283 |
CMatrixDouble44 tmp = globalInverseTransform_; |
| 284 |
globalInverseTransform_ = tmp.asEigen().inverse().eval(); |
| 285 |
} |
| 286 |
|
| 287 |
// --- 3) Extract animations ------------------------------------------- |
| 288 |
for (unsigned int a = 0; a < scene->mNumAnimations; ++a) |
| 289 |
{ |
| 290 |
const aiAnimation* aiAnim = scene->mAnimations[a]; |
| 291 |
|
| 292 |
Animation anim; |
| 293 |
anim.name = aiAnim->mName.data; |
| 294 |
anim.duration = aiAnim->mDuration; |
| 295 |
anim.ticksPerSecond = (aiAnim->mTicksPerSecond > 0.0) ? aiAnim->mTicksPerSecond : 25.0; |
| 296 |
|
| 297 |
for (unsigned int c = 0; c < aiAnim->mNumChannels; ++c) |
| 298 |
{ |
| 299 |
const aiNodeAnim* ch = aiAnim->mChannels[c]; |
| 300 |
|
| 301 |
BoneAnimation ba; |
| 302 |
ba.boneName = ch->mNodeName.data; |
| 303 |
|
| 304 |
auto itB = boneNameToIndex_.find(ba.boneName); |
| 305 |
if (itB != boneNameToIndex_.end()) |
| 306 |
{ |
| 307 |
ba.boneIndex = static_cast<int>(itB->second); |
| 308 |
} |
| 309 |
|
| 310 |
// Position keys |
| 311 |
ba.positionKeys.reserve(ch->mNumPositionKeys); |
| 312 |
for (unsigned int k = 0; k < ch->mNumPositionKeys; ++k) |
| 313 |
{ |
| 314 |
VectorKey vk; |
| 315 |
vk.time = ch->mPositionKeys[k].mTime; |
| 316 |
vk.value = aiVecToMrpt(ch->mPositionKeys[k].mValue); |
| 317 |
ba.positionKeys.push_back(vk); |
| 318 |
} |
| 319 |
|
| 320 |
// Rotation keys |
| 321 |
ba.rotationKeys.reserve(ch->mNumRotationKeys); |
| 322 |
for (unsigned int k = 0; k < ch->mNumRotationKeys; ++k) |
| 323 |
{ |
| 324 |
QuatKey qk; |
| 325 |
qk.time = ch->mRotationKeys[k].mTime; |
| 326 |
const auto& q = ch->mRotationKeys[k].mValue; |
| 327 |
qk.w = static_cast<float>(q.w); |
| 328 |
qk.x = static_cast<float>(q.x); |
| 329 |
qk.y = static_cast<float>(q.y); |
| 330 |
qk.z = static_cast<float>(q.z); |
| 331 |
ba.rotationKeys.push_back(qk); |
| 332 |
} |
| 333 |
|
| 334 |
// Scaling keys |
| 335 |
ba.scalingKeys.reserve(ch->mNumScalingKeys); |
| 336 |
for (unsigned int k = 0; k < ch->mNumScalingKeys; ++k) |
| 337 |
{ |
| 338 |
VectorKey sk; |
| 339 |
sk.time = ch->mScalingKeys[k].mTime; |
| 340 |
sk.value = aiVecToMrpt(ch->mScalingKeys[k].mValue); |
| 341 |
ba.scalingKeys.push_back(sk); |
| 342 |
} |
| 343 |
|
| 344 |
anim.channels.push_back(std::move(ba)); |
| 345 |
} |
| 346 |
|
| 347 |
animations_.push_back(std::move(anim)); |
| 348 |
} |
| 349 |
|
| 350 |
hasSkeleton_ = true; |
| 351 |
|
| 352 |
// --- 4) Build the vertex-origin map (must match recursive_render order) |
| 353 |
captureBindPose(); |
| 354 |
|
| 355 |
#endif // MRPT_HAS_ASSIMP |
| 356 |
} |
| 357 |
|
| 358 |
// ============================================================================ |
| 359 |
// Build bone hierarchy |
| 360 |
// ============================================================================ |
| 361 |
|
| 362 |
void CAnimatedAssimpModel::buildBoneHierarchy(const aiNode* node, int parentBoneIdx) |
| 363 |
{ |
| 364 |
#if MRPT_HAS_ASSIMP |
| 365 |
if (!node) |
| 366 |
{ |
| 367 |
return; |
| 368 |
} |
| 369 |
|
| 370 |
const std::string nodeName(node->mName.data); |
| 371 |
int thisBoneIdx = parentBoneIdx; |
| 372 |
|
| 373 |
// If this node corresponds to a bone, set its parent |
| 374 |
auto it = boneNameToIndex_.find(nodeName); |
| 375 |
if (it != boneNameToIndex_.end()) |
| 376 |
{ |
| 377 |
thisBoneIdx = static_cast<int>(it->second); |
| 378 |
bones_[it->second].parentIndex = parentBoneIdx; |
| 379 |
} |
| 380 |
|
| 381 |
// Recurse children |
| 382 |
for (unsigned int i = 0; i < node->mNumChildren; ++i) |
| 383 |
{ |
| 384 |
buildBoneHierarchy(node->mChildren[i], thisBoneIdx); |
| 385 |
} |
| 386 |
#else |
| 387 |
(void)node; |
| 388 |
(void)parentBoneIdx; |
| 389 |
#endif |
| 390 |
} |
| 391 |
|
| 392 |
void CAnimatedAssimpModel::captureBindPose() |
| 393 |
{ |
| 394 |
#if MRPT_HAS_ASSIMP |
| 395 |
const aiScene* scene = getAssimpScene(); |
| 396 |
if (!scene) |
| 397 |
{ |
| 398 |
return; |
| 399 |
} |
| 400 |
|
| 401 |
meshBindPoses_.resize(scene->mNumMeshes); |
| 402 |
|
| 403 |
for (unsigned int m = 0; m < scene->mNumMeshes; ++m) |
| 404 |
{ |
| 405 |
const aiMesh* mesh = scene->mMeshes[m]; |
| 406 |
auto& bp = meshBindPoses_[m]; |
| 407 |
|
| 408 |
bp.positions.resize(mesh->mNumVertices); |
| 409 |
bp.normals.resize(mesh->mNumVertices); |
| 410 |
|
| 411 |
for (unsigned int v = 0; v < mesh->mNumVertices; ++v) |
| 412 |
{ |
| 413 |
const auto& sv = mesh->mVertices[v]; |
| 414 |
bp.positions[v] = {sv.x, sv.y, sv.z}; |
| 415 |
|
| 416 |
if (mesh->mNormals) |
| 417 |
{ |
| 418 |
const auto& sn = mesh->mNormals[v]; |
| 419 |
bp.normals[v] = {sn.x, sn.y, sn.z}; |
| 420 |
} |
| 421 |
else |
| 422 |
{ |
| 423 |
bp.normals[v] = {0.f, 0.f, 1.f}; |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
#endif |
| 428 |
} |
| 429 |
|
| 430 |
void CAnimatedAssimpModel::applySkinningToScene() const |
| 431 |
{ |
| 432 |
#if MRPT_HAS_ASSIMP |
| 433 |
// const_cast is safe: we restore the original data in |
| 434 |
// restoreBindPoseToScene() immediately after the base class |
| 435 |
// regenerates its triangle buffers. |
| 436 |
aiScene* scene = const_cast<aiScene*>(getAssimpScene()); |
| 437 |
if (!scene) |
| 438 |
{ |
| 439 |
return; |
| 440 |
} |
| 441 |
|
| 442 |
for (unsigned int m = 0; m < scene->mNumMeshes; ++m) |
| 443 |
{ |
| 444 |
aiMesh* mesh = scene->mMeshes[m]; |
| 445 |
if (!mesh->HasBones()) |
| 446 |
{ |
| 447 |
continue; |
| 448 |
} |
| 449 |
if (m >= meshBindPoses_.size()) |
| 450 |
{ |
| 451 |
continue; |
| 452 |
} |
| 453 |
|
| 454 |
const auto& bp = meshBindPoses_[m]; |
| 455 |
if (bp.positions.size() != mesh->mNumVertices) |
| 456 |
{ |
| 457 |
continue; // safety — mesh/bind-pose size mismatch |
| 458 |
} |
| 459 |
|
| 460 |
for (unsigned int v = 0; v < mesh->mNumVertices; ++v) |
| 461 |
{ |
| 462 |
const uint64_t vk = vertexKey(m, v); |
| 463 |
|
| 464 |
auto it = vertexBoneMap_.find(vk); |
| 465 |
if (it == vertexBoneMap_.end()) |
| 466 |
{ |
| 467 |
continue; // vertex has no bone weights |
| 468 |
} |
| 469 |
|
| 470 |
// Skin position (from the bind-pose, NOT from the current |
| 471 |
// mesh data which may already have been skinned in a |
| 472 |
// previous frame). |
| 473 |
const auto skinnedPos = skinVertex(bp.positions[v], vk); |
| 474 |
mesh->mVertices[v].x = skinnedPos.x; |
| 475 |
mesh->mVertices[v].y = skinnedPos.y; |
| 476 |
mesh->mVertices[v].z = skinnedPos.z; |
| 477 |
|
| 478 |
// Skin normal |
| 479 |
if (mesh->mNormals) |
| 480 |
{ |
| 481 |
const auto skinnedNrm = skinNormal(bp.normals[v], vk); |
| 482 |
mesh->mNormals[v].x = skinnedNrm.x; |
| 483 |
mesh->mNormals[v].y = skinnedNrm.y; |
| 484 |
mesh->mNormals[v].z = skinnedNrm.z; |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
#endif |
| 489 |
} |
| 490 |
|
| 491 |
void CAnimatedAssimpModel::restoreBindPoseToScene() const |
| 492 |
{ |
| 493 |
#if MRPT_HAS_ASSIMP |
| 494 |
aiScene* scene = const_cast<aiScene*>(getAssimpScene()); |
| 495 |
if (!scene) |
| 496 |
{ |
| 497 |
return; |
| 498 |
} |
| 499 |
|
| 500 |
for (unsigned int m = 0; m < scene->mNumMeshes; ++m) |
| 501 |
{ |
| 502 |
aiMesh* mesh = scene->mMeshes[m]; |
| 503 |
if (!mesh->HasBones()) |
| 504 |
{ |
| 505 |
continue; |
| 506 |
} |
| 507 |
if (m >= meshBindPoses_.size()) |
| 508 |
{ |
| 509 |
continue; |
| 510 |
} |
| 511 |
|
| 512 |
const auto& bp = meshBindPoses_[m]; |
| 513 |
if (bp.positions.size() != mesh->mNumVertices) |
| 514 |
{ |
| 515 |
continue; |
| 516 |
} |
| 517 |
|
| 518 |
for (unsigned int v = 0; v < mesh->mNumVertices; ++v) |
| 519 |
{ |
| 520 |
mesh->mVertices[v].x = bp.positions[v].x; |
| 521 |
mesh->mVertices[v].y = bp.positions[v].y; |
| 522 |
mesh->mVertices[v].z = bp.positions[v].z; |
| 523 |
|
| 524 |
if (mesh->mNormals) |
| 525 |
{ |
| 526 |
mesh->mNormals[v].x = bp.normals[v].x; |
| 527 |
mesh->mNormals[v].y = bp.normals[v].y; |
| 528 |
mesh->mNormals[v].z = bp.normals[v].z; |
| 529 |
} |
| 530 |
} |
| 531 |
} |
| 532 |
#endif |
| 533 |
} |
| 534 |
|
| 535 |
// ============================================================================ |
| 536 |
// Bone Transform Update |
| 537 |
// ============================================================================ |
| 538 |
|
| 539 |
void CAnimatedAssimpModel::updateBoneTransforms() |
| 540 |
{ |
| 541 |
if (animations_.empty() || activeAnimation_ >= animations_.size()) |
| 542 |
{ |
| 543 |
return; |
| 544 |
} |
| 545 |
|
| 546 |
const auto& anim = animations_[activeAnimation_]; |
| 547 |
const double ticksPerSec = (anim.ticksPerSecond > 0.0) ? anim.ticksPerSecond : 25.0; |
| 548 |
|
| 549 |
double animTime = currentTime_ * ticksPerSec; |
| 550 |
if (looping_ && anim.duration > 0.0) |
| 551 |
{ |
| 552 |
animTime = std::fmod(animTime, anim.duration); |
| 553 |
} |
| 554 |
else if (anim.duration > 0.0) |
| 555 |
{ |
| 556 |
animTime = std::min(animTime, anim.duration); |
| 557 |
} |
| 558 |
|
| 559 |
// Update each bone's local transform from animation channels |
| 560 |
for (const auto& channel : anim.channels) |
| 561 |
{ |
| 562 |
if (channel.boneIndex < 0 || static_cast<size_t>(channel.boneIndex) >= bones_.size()) |
| 563 |
{ |
| 564 |
continue; |
| 565 |
} |
| 566 |
|
| 567 |
Bone& bone = bones_[static_cast<size_t>(channel.boneIndex)]; |
| 568 |
|
| 569 |
// Skip if bone has manual override |
| 570 |
if (bone.hasOverride) |
| 571 |
{ |
| 572 |
bone.localTransform = bone.overrideTransform; |
| 573 |
continue; |
| 574 |
} |
| 575 |
|
| 576 |
// Interpolate position, rotation, scale |
| 577 |
const auto pos = interpolatePosition(channel, animTime); |
| 578 |
float qw, qx, qy, qz; |
| 579 |
interpolateRotation(channel, animTime, qw, qx, qy, qz); |
| 580 |
const auto scl = interpolateScaling(channel, animTime); |
| 581 |
|
| 582 |
// Build local transform matrix: T * R * S |
| 583 |
bone.localTransform = buildTransformMatrix(pos, qw, qx, qy, qz, scl); |
| 584 |
} |
| 585 |
|
| 586 |
// Compute global transforms recursively from root bones |
| 587 |
for (size_t i = 0; i < bones_.size(); ++i) |
| 588 |
{ |
| 589 |
if (bones_[i].parentIndex < 0) |
| 590 |
{ |
| 591 |
computeGlobalTransforms(i); |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
// Compute final transforms for skinning: |
| 596 |
// finalTransform = globalInverse * boneGlobal * offset |
| 597 |
for (size_t i = 0; i < bones_.size(); ++i) |
| 598 |
{ |
| 599 |
auto& b = bones_[i]; |
| 600 |
// Using Eigen under the hood for the triple product: |
| 601 |
b.finalTransform.asEigen() = |
| 602 |
globalInverseTransform_.asEigen() * b.globalTransform.asEigen() * b.offsetMatrix.asEigen(); |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
void CAnimatedAssimpModel::computeGlobalTransforms(size_t boneIndex) |
| 607 |
{ |
| 608 |
Bone& bone = bones_[boneIndex]; |
| 609 |
|
| 610 |
if (bone.parentIndex >= 0) |
| 611 |
{ |
| 612 |
const Bone& par = bones_[static_cast<size_t>(bone.parentIndex)]; |
| 613 |
bone.globalTransform.asEigen() = par.globalTransform.asEigen() * bone.localTransform.asEigen(); |
| 614 |
} |
| 615 |
else |
| 616 |
{ |
| 617 |
bone.globalTransform = bone.localTransform; |
| 618 |
} |
| 619 |
|
| 620 |
// Recursively update children |
| 621 |
for (size_t i = 0; i < bones_.size(); ++i) |
| 622 |
{ |
| 623 |
if (bones_[i].parentIndex == static_cast<int>(boneIndex)) |
| 624 |
{ |
| 625 |
computeGlobalTransforms(i); |
| 626 |
} |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
// ============================================================================ |
| 631 |
// Build Transform Matrix (T * R * S) |
| 632 |
// ============================================================================ |
| 633 |
|
| 634 |
CMatrixDouble44 CAnimatedAssimpModel::buildTransformMatrix( |
| 635 |
const TPoint3Df& pos, float qw, float qx, float qy, float qz, const TPoint3Df& scale) |
| 636 |
{ |
| 637 |
// Normalize quaternion |
| 638 |
const float len = std::sqrt(qw * qw + qx * qx + qy * qy + qz * qz); |
| 639 |
if (len > 1e-7f) |
| 640 |
{ |
| 641 |
const float invLen = 1.0f / len; |
| 642 |
qw *= invLen; |
| 643 |
qx *= invLen; |
| 644 |
qy *= invLen; |
| 645 |
qz *= invLen; |
| 646 |
} |
| 647 |
|
| 648 |
// Rotation matrix from quaternion |
| 649 |
const float xx = qx * qx; |
| 650 |
const float yy = qy * qy; |
| 651 |
const float zz = qz * qz; |
| 652 |
const float xy = qx * qy; |
| 653 |
const float xz = qx * qz; |
| 654 |
const float yz = qy * qz; |
| 655 |
const float wx = qw * qx; |
| 656 |
const float wy = qw * qy; |
| 657 |
const float wz = qw * qz; |
| 658 |
|
| 659 |
CMatrixDouble44 M; |
| 660 |
// Row 0 |
| 661 |
M(0, 0) = static_cast<double>((1.0f - 2.0f * (yy + zz)) * scale.x); |
| 662 |
M(0, 1) = static_cast<double>((2.0f * (xy - wz)) * scale.y); |
| 663 |
M(0, 2) = static_cast<double>((2.0f * (xz + wy)) * scale.z); |
| 664 |
M(0, 3) = static_cast<double>(pos.x); |
| 665 |
|
| 666 |
// Row 1 |
| 667 |
M(1, 0) = static_cast<double>((2.0f * (xy + wz)) * scale.x); |
| 668 |
M(1, 1) = static_cast<double>((1.0f - 2.0f * (xx + zz)) * scale.y); |
| 669 |
M(1, 2) = static_cast<double>((2.0f * (yz - wx)) * scale.z); |
| 670 |
M(1, 3) = static_cast<double>(pos.y); |
| 671 |
|
| 672 |
// Row 2 |
| 673 |
M(2, 0) = static_cast<double>((2.0f * (xz - wy)) * scale.x); |
| 674 |
M(2, 1) = static_cast<double>((2.0f * (yz + wx)) * scale.y); |
| 675 |
M(2, 2) = static_cast<double>((1.0f - 2.0f * (xx + yy)) * scale.z); |
| 676 |
M(2, 3) = static_cast<double>(pos.z); |
| 677 |
|
| 678 |
// Row 3 |
| 679 |
M(3, 0) = 0.0; |
| 680 |
M(3, 1) = 0.0; |
| 681 |
M(3, 2) = 0.0; |
| 682 |
M(3, 3) = 1.0; |
| 683 |
|
| 684 |
return M; |
| 685 |
} |
| 686 |
|
| 687 |
// ============================================================================ |
| 688 |
// Vertex Skinning |
| 689 |
// ============================================================================ |
| 690 |
|
| 691 |
TPoint3Df CAnimatedAssimpModel::skinVertex(const TPoint3Df& vertex, uint64_t vkey) const |
| 692 |
{ |
| 693 |
auto it = vertexBoneMap_.find(vkey); |
| 694 |
if (it == vertexBoneMap_.end()) |
| 695 |
{ |
| 696 |
return vertex; // not skinned |
| 697 |
} |
| 698 |
|
| 699 |
const auto& vbd = it->second; |
| 700 |
TPoint3Df result(0.f, 0.f, 0.f); |
| 701 |
float totalWeight = 0.f; |
| 702 |
|
| 703 |
for (int i = 0; i < VertexBoneData::MAX_BONES_PER_VERTEX; ++i) |
| 704 |
{ |
| 705 |
if (vbd.boneIds[i] < 0) |
| 706 |
{ |
| 707 |
continue; |
| 708 |
} |
| 709 |
|
| 710 |
const auto boneIdx = static_cast<size_t>(vbd.boneIds[i]); |
| 711 |
if (boneIdx >= bones_.size()) |
| 712 |
{ |
| 713 |
continue; |
| 714 |
} |
| 715 |
|
| 716 |
const float weight = vbd.weights[i]; |
| 717 |
if (weight < 1e-6f) |
| 718 |
{ |
| 719 |
continue; |
| 720 |
} |
| 721 |
|
| 722 |
const auto& M = bones_[boneIdx].finalTransform; |
| 723 |
|
| 724 |
const float x = |
| 725 |
static_cast<float>(M(0, 0) * vertex.x + M(0, 1) * vertex.y + M(0, 2) * vertex.z + M(0, 3)); |
| 726 |
const float y = |
| 727 |
static_cast<float>(M(1, 0) * vertex.x + M(1, 1) * vertex.y + M(1, 2) * vertex.z + M(1, 3)); |
| 728 |
const float z = |
| 729 |
static_cast<float>(M(2, 0) * vertex.x + M(2, 1) * vertex.y + M(2, 2) * vertex.z + M(2, 3)); |
| 730 |
|
| 731 |
result.x += x * weight; |
| 732 |
result.y += y * weight; |
| 733 |
result.z += z * weight; |
| 734 |
totalWeight += weight; |
| 735 |
} |
| 736 |
|
| 737 |
// Blend remaining weight with un-transformed position |
| 738 |
if (totalWeight < 0.999f) |
| 739 |
{ |
| 740 |
const float rem = 1.0f - totalWeight; |
| 741 |
result.x += vertex.x * rem; |
| 742 |
result.y += vertex.y * rem; |
| 743 |
result.z += vertex.z * rem; |
| 744 |
} |
| 745 |
|
| 746 |
return result; |
| 747 |
} |
| 748 |
|
| 749 |
TPoint3Df CAnimatedAssimpModel::skinNormal(const TPoint3Df& normal, uint64_t vkey) const |
| 750 |
{ |
| 751 |
auto it = vertexBoneMap_.find(vkey); |
| 752 |
if (it == vertexBoneMap_.end()) |
| 753 |
{ |
| 754 |
return normal; |
| 755 |
} |
| 756 |
|
| 757 |
const auto& vbd = it->second; |
| 758 |
TPoint3Df result(0.f, 0.f, 0.f); |
| 759 |
float totalWeight = 0.f; |
| 760 |
|
| 761 |
for (int i = 0; i < VertexBoneData::MAX_BONES_PER_VERTEX; ++i) |
| 762 |
{ |
| 763 |
if (vbd.boneIds[i] < 0) |
| 764 |
{ |
| 765 |
continue; |
| 766 |
} |
| 767 |
|
| 768 |
const auto boneIdx = static_cast<size_t>(vbd.boneIds[i]); |
| 769 |
if (boneIdx >= bones_.size()) |
| 770 |
{ |
| 771 |
continue; |
| 772 |
} |
| 773 |
|
| 774 |
const float weight = vbd.weights[i]; |
| 775 |
if (weight < 1e-6f) |
| 776 |
{ |
| 777 |
continue; |
| 778 |
} |
| 779 |
|
| 780 |
const auto& M = bones_[boneIdx].finalTransform; |
| 781 |
|
| 782 |
// Rotate only (no translation for normals) |
| 783 |
const float nx = |
| 784 |
static_cast<float>(M(0, 0) * normal.x + M(0, 1) * normal.y + M(0, 2) * normal.z); |
| 785 |
const float ny = |
| 786 |
static_cast<float>(M(1, 0) * normal.x + M(1, 1) * normal.y + M(1, 2) * normal.z); |
| 787 |
const float nz = |
| 788 |
static_cast<float>(M(2, 0) * normal.x + M(2, 1) * normal.y + M(2, 2) * normal.z); |
| 789 |
|
| 790 |
result.x += nx * weight; |
| 791 |
result.y += ny * weight; |
| 792 |
result.z += nz * weight; |
| 793 |
totalWeight += weight; |
| 794 |
} |
| 795 |
|
| 796 |
if (totalWeight < 0.999f) |
| 797 |
{ |
| 798 |
const float rem = 1.0f - totalWeight; |
| 799 |
result.x += normal.x * rem; |
| 800 |
result.y += normal.y * rem; |
| 801 |
result.z += normal.z * rem; |
| 802 |
} |
| 803 |
|
| 804 |
// Re-normalize |
| 805 |
const float len = std::sqrt(result.x * result.x + result.y * result.y + result.z * result.z); |
| 806 |
if (len > 1e-6f) |
| 807 |
{ |
| 808 |
result.x /= len; |
| 809 |
result.y /= len; |
| 810 |
result.z /= len; |
| 811 |
} |
| 812 |
|
| 813 |
return result; |
| 814 |
} |
| 815 |
|
| 816 |
// ============================================================================ |
| 817 |
// Override to apply CPU skinning |
| 818 |
// ============================================================================ |
| 819 |
|
| 820 |
void CAnimatedAssimpModel::renderUpdateBuffers() const |
| 821 |
{ |
| 822 |
if (!hasSkeleton_) |
| 823 |
{ |
| 824 |
// No skeleton — delegate to the base class. |
| 825 |
CAssimpModel::renderUpdateBuffers(); |
| 826 |
return; |
| 827 |
} |
| 828 |
|
| 829 |
// --- 1. Write skinned positions into the aiScene mesh data --------- |
| 830 |
// (modifies aiMesh::mVertices / mNormals via const_cast) |
| 831 |
applySkinningToScene(); |
| 832 |
|
| 833 |
// --- 2. Regenerate ALL triangle buffers from the (now-skinned) |
| 834 |
// mesh data. onUpdateBuffers_all() handles textured-vs- |
| 835 |
// non-textured routing, texture assignment, and voxel split |
| 836 |
// correctly because the source data is already skinned. |
| 837 |
const_cast<CAnimatedAssimpModel&>(*this).onUpdateBuffers_all(); |
| 838 |
|
| 839 |
// --- 3. Upload every CPU buffer to its GPU VBO --------------------- |
| 840 |
CAssimpModel::renderUpdateBuffers(); |
| 841 |
|
| 842 |
// --- 4. Restore bind-pose positions so the next frame starts clean - |
| 843 |
restoreBindPoseToScene(); |
| 844 |
} |
| 845 |
// ============================================================================ |
| 846 |
// Helper: Add bone weight to vertex |
| 847 |
// ============================================================================ |
| 848 |
|
| 849 |
void CAnimatedAssimpModel::VertexBoneData::addBoneWeight(int boneId, float weight) |
| 850 |
{ |
| 851 |
// Find first empty slot |
| 852 |
for (int i = 0; i < MAX_BONES_PER_VERTEX; ++i) |
| 853 |
{ |
| 854 |
if (boneIds[i] < 0) |
| 855 |
{ |
| 856 |
boneIds[i] = boneId; |
| 857 |
weights[i] = weight; |
| 858 |
return; |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
// All slots full — replace smallest if new weight is larger |
| 863 |
int minIdx = 0; |
| 864 |
float minWeight = weights[0]; |
| 865 |
for (int i = 1; i < MAX_BONES_PER_VERTEX; ++i) |
| 866 |
{ |
| 867 |
if (weights[i] < minWeight) |
| 868 |
{ |
| 869 |
minWeight = weights[i]; |
| 870 |
minIdx = i; |
| 871 |
} |
| 872 |
} |
| 873 |
if (weight > minWeight) |
| 874 |
{ |
| 875 |
boneIds[minIdx] = boneId; |
| 876 |
weights[minIdx] = weight; |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
// ============================================================================ |
| 881 |
// Interpolation: Position (linear) |
| 882 |
// ============================================================================ |
| 883 |
|
| 884 |
TPoint3Df CAnimatedAssimpModel::interpolatePosition( |
| 885 |
const BoneAnimation& channel, double animTime) const |
| 886 |
{ |
| 887 |
if (channel.positionKeys.empty()) |
| 888 |
{ |
| 889 |
return {0.f, 0.f, 0.f}; |
| 890 |
} |
| 891 |
if (channel.positionKeys.size() == 1) |
| 892 |
{ |
| 893 |
return channel.positionKeys[0].value; |
| 894 |
} |
| 895 |
|
| 896 |
// Find surrounding keyframes |
| 897 |
size_t idx0 = 0; |
| 898 |
for (size_t i = 0; i + 1 < channel.positionKeys.size(); ++i) |
| 899 |
{ |
| 900 |
if (animTime < channel.positionKeys[i + 1].time) |
| 901 |
{ |
| 902 |
idx0 = i; |
| 903 |
break; |
| 904 |
} |
| 905 |
idx0 = i; |
| 906 |
} |
| 907 |
|
| 908 |
const size_t idx1 = idx0 + 1; |
| 909 |
if (idx1 >= channel.positionKeys.size()) |
| 910 |
{ |
| 911 |
return channel.positionKeys.back().value; |
| 912 |
} |
| 913 |
|
| 914 |
const auto& k0 = channel.positionKeys[idx0]; |
| 915 |
const auto& k1 = channel.positionKeys[idx1]; |
| 916 |
|
| 917 |
const double dt = k1.time - k0.time; |
| 918 |
const float t = (dt > 1e-6) ? static_cast<float>((animTime - k0.time) / dt) : 0.f; |
| 919 |
|
| 920 |
return { |
| 921 |
k0.value.x + t * (k1.value.x - k0.value.x), k0.value.y + t * (k1.value.y - k0.value.y), |
| 922 |
k0.value.z + t * (k1.value.z - k0.value.z)}; |
| 923 |
} |
| 924 |
|
| 925 |
// ============================================================================ |
| 926 |
// Interpolation: Rotation (SLERP) |
| 927 |
// ============================================================================ |
| 928 |
|
| 929 |
void CAnimatedAssimpModel::interpolateRotation( |
| 930 |
const BoneAnimation& channel, double animTime, float& qw, float& qx, float& qy, float& qz) const |
| 931 |
{ |
| 932 |
if (channel.rotationKeys.empty()) |
| 933 |
{ |
| 934 |
qw = 1.f; |
| 935 |
qx = qy = qz = 0.f; |
| 936 |
return; |
| 937 |
} |
| 938 |
if (channel.rotationKeys.size() == 1) |
| 939 |
{ |
| 940 |
const auto& k = channel.rotationKeys[0]; |
| 941 |
qw = k.w; |
| 942 |
qx = k.x; |
| 943 |
qy = k.y; |
| 944 |
qz = k.z; |
| 945 |
return; |
| 946 |
} |
| 947 |
|
| 948 |
size_t idx0 = 0; |
| 949 |
for (size_t i = 0; i + 1 < channel.rotationKeys.size(); ++i) |
| 950 |
{ |
| 951 |
if (animTime < channel.rotationKeys[i + 1].time) |
| 952 |
{ |
| 953 |
idx0 = i; |
| 954 |
break; |
| 955 |
} |
| 956 |
idx0 = i; |
| 957 |
} |
| 958 |
|
| 959 |
const size_t idx1 = idx0 + 1; |
| 960 |
if (idx1 >= channel.rotationKeys.size()) |
| 961 |
{ |
| 962 |
const auto& k = channel.rotationKeys.back(); |
| 963 |
qw = k.w; |
| 964 |
qx = k.x; |
| 965 |
qy = k.y; |
| 966 |
qz = k.z; |
| 967 |
return; |
| 968 |
} |
| 969 |
|
| 970 |
const auto& k0 = channel.rotationKeys[idx0]; |
| 971 |
const auto& k1 = channel.rotationKeys[idx1]; |
| 972 |
|
| 973 |
const double dt = k1.time - k0.time; |
| 974 |
const float t = (dt > 1e-6) ? static_cast<float>((animTime - k0.time) / dt) : 0.f; |
| 975 |
|
| 976 |
// SLERP |
| 977 |
float dot = k0.w * k1.w + k0.x * k1.x + k0.y * k1.y + k0.z * k1.z; |
| 978 |
|
| 979 |
float w1 = k1.w, x1 = k1.x, y1 = k1.y, z1 = k1.z; |
| 980 |
if (dot < 0.f) |
| 981 |
{ |
| 982 |
dot = -dot; |
| 983 |
w1 = -w1; |
| 984 |
x1 = -x1; |
| 985 |
y1 = -y1; |
| 986 |
z1 = -z1; |
| 987 |
} |
| 988 |
|
| 989 |
float s0, s1; |
| 990 |
if (dot > 0.9995f) |
| 991 |
{ |
| 992 |
// Very close — linear interpolation |
| 993 |
s0 = 1.f - t; |
| 994 |
s1 = t; |
| 995 |
} |
| 996 |
else |
| 997 |
{ |
| 998 |
const float theta = std::acos(std::clamp(dot, -1.0f, 1.0f)); |
| 999 |
const float sinTheta = std::sin(theta); |
| 1000 |
s0 = std::sin((1.f - t) * theta) / sinTheta; |
| 1001 |
s1 = std::sin(t * theta) / sinTheta; |
| 1002 |
} |
| 1003 |
|
| 1004 |
qw = s0 * k0.w + s1 * w1; |
| 1005 |
qx = s0 * k0.x + s1 * x1; |
| 1006 |
qy = s0 * k0.y + s1 * y1; |
| 1007 |
qz = s0 * k0.z + s1 * z1; |
| 1008 |
} |
| 1009 |
|
| 1010 |
// ============================================================================ |
| 1011 |
// Interpolation: Scaling (linear) |
| 1012 |
// ============================================================================ |
| 1013 |
|
| 1014 |
TPoint3Df CAnimatedAssimpModel::interpolateScaling( |
| 1015 |
const BoneAnimation& channel, double animTime) const |
| 1016 |
{ |
| 1017 |
if (channel.scalingKeys.empty()) |
| 1018 |
{ |
| 1019 |
return {1.f, 1.f, 1.f}; |
| 1020 |
} |
| 1021 |
if (channel.scalingKeys.size() == 1) |
| 1022 |
{ |
| 1023 |
return channel.scalingKeys[0].value; |
| 1024 |
} |
| 1025 |
|
| 1026 |
size_t idx0 = 0; |
| 1027 |
for (size_t i = 0; i + 1 < channel.scalingKeys.size(); ++i) |
| 1028 |
{ |
| 1029 |
if (animTime < channel.scalingKeys[i + 1].time) |
| 1030 |
{ |
| 1031 |
idx0 = i; |
| 1032 |
break; |
| 1033 |
} |
| 1034 |
idx0 = i; |
| 1035 |
} |
| 1036 |
|
| 1037 |
const size_t idx1 = idx0 + 1; |
| 1038 |
if (idx1 >= channel.scalingKeys.size()) |
| 1039 |
{ |
| 1040 |
return channel.scalingKeys.back().value; |
| 1041 |
} |
| 1042 |
|
| 1043 |
const auto& k0 = channel.scalingKeys[idx0]; |
| 1044 |
const auto& k1 = channel.scalingKeys[idx1]; |
| 1045 |
|
| 1046 |
const double dt = k1.time - k0.time; |
| 1047 |
const float t = (dt > 1e-6) ? static_cast<float>((animTime - k0.time) / dt) : 0.f; |
| 1048 |
|
| 1049 |
return { |
| 1050 |
k0.value.x + t * (k1.value.x - k0.value.x), k0.value.y + t * (k1.value.y - k0.value.y), |
| 1051 |
k0.value.z + t * (k1.value.z - k0.value.z)}; |
| 1052 |
} |