Skip to content

Commit 7fabd88

Browse files
committed
Check for weights
1 parent b43befc commit 7fabd88

File tree

3 files changed

+31
-34
lines changed

3 files changed

+31
-34
lines changed

irr/include/SkinnedMesh.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class SkinnedMesh : public IAnimatedMesh
4343
//! constructor
4444
SkinnedMesh(SourceFormat src_format) :
4545
EndFrame(0.f), FramesPerSecond(25.f),
46-
HasAnimation(false), PreparedForSkinning(false),
4746
SrcFormat(src_format)
4847
{
4948
SkinningBuffers = &LocalBuffers;
@@ -128,9 +127,9 @@ class SkinnedMesh : public IAnimatedMesh
128127
void convertMeshToTangents();
129128

130129
//! Does the mesh have no animation
131-
bool isStatic() const {
132-
return !HasAnimation;
133-
}
130+
bool isStatic() const { return !HasAnimation; }
131+
//! Does the mesh have skinning weights?
132+
bool hasWeights() const { return HasWeights; }
134133

135134
//! Back up static pose after local buffers have been modified directly
136135
void updateStaticPose();
@@ -334,7 +333,8 @@ class SkinnedMesh : public IAnimatedMesh
334333
}
335334

336335
protected:
337-
bool checkForAnimation() const;
336+
bool checkForWeights() const;
337+
bool checkForKeys() const;
338338

339339
void prepareForSkinning();
340340

@@ -368,8 +368,9 @@ class SkinnedMesh : public IAnimatedMesh
368368
f32 EndFrame;
369369
f32 FramesPerSecond;
370370

371-
bool HasAnimation;
372-
bool PreparedForSkinning;
371+
bool HasAnimation = false;
372+
bool HasWeights = false;
373+
bool PreparedForSkinning = false;
373374
bool UseSwSkinning = false;
374375

375376
SourceFormat SrcFormat;

irr/src/AnimatedMeshSceneNode.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,10 @@ void AnimatedMeshSceneNode::render()
198198

199199
if (auto *sm = dynamic_cast<SkinnedMesh *>(Mesh)) {
200200
sm->rigidAnimation(PerJoint.GlobalMatrices);
201-
// TODO check whether there are any weights to make use of these joint transforms
202201
if (sm->useSoftwareSkinning()) {
203202
// Perform software skinning; matrices have already been calculated in OnAnimate
204203
sm->skinMesh(PerJoint.GlobalMatrices);
205-
} else {
204+
} else if (sm->hasWeights()) {
206205
driver->setJointTransforms(sm->calculateSkinMatrices(PerJoint.GlobalMatrices));
207206
}
208207
}

irr/src/SkinnedMesh.cpp

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -234,27 +234,23 @@ void SkinnedMesh::calculateGlobalMatrices(std::vector<core::matrix4> &matrices)
234234
}
235235
}
236236

237-
bool SkinnedMesh::checkForAnimation() const
237+
bool SkinnedMesh::checkForWeights() const
238238
{
239-
for (auto *joint : AllJoints) {
240-
if (!joint->keys.empty()) {
241-
return true;
242-
}
243-
}
244-
245-
// meshes with weights are animatable
246-
for (auto *buf : LocalBuffers) {
247-
if (buf->getWeights()) {
248-
return true;
249-
}
250-
}
239+
return std::any_of(LocalBuffers.begin(), LocalBuffers.end(),
240+
[](const auto *buf) { return buf->getWeights() != nullptr; });
241+
}
251242

252-
return false;
243+
bool SkinnedMesh::checkForKeys() const
244+
{
245+
return std::any_of(AllJoints.begin(), AllJoints.end(),
246+
[](const auto *joint) { return !joint->keys.empty(); });
253247
}
254248

255249
void SkinnedMesh::prepareForSkinning()
256250
{
257-
HasAnimation = checkForAnimation();
251+
HasWeights = checkForWeights();
252+
// Meshes with weights are animatable (e.g. with bone overrides)
253+
HasAnimation = HasWeights || checkForKeys();
258254
if (!HasAnimation || PreparedForSkinning)
259255
return;
260256

@@ -400,6 +396,17 @@ SkinnedMesh *SkinnedMeshBuilder::finalize() &&
400396
// (see e.g. SkinnedMesh::calculateGlobalMatrices)
401397
topoSortJoints();
402398

399+
// Add all weights such that checkForWeights() works as expected
400+
for (const auto &weight : weights) {
401+
auto *buf = mesh->LocalBuffers.at(weight.buffer_id);
402+
auto *weights = buf->getWeights();
403+
if (!weights) {
404+
buf->addWeightBuffer();
405+
weights = buf->getWeights();
406+
}
407+
weights->addWeight(weight.vertex_id, weight.joint_id, weight.strength);
408+
}
409+
403410
mesh->prepareForSkinning();
404411

405412
std::vector<core::matrix4> matrices;
@@ -425,16 +432,6 @@ SkinnedMesh *SkinnedMeshBuilder::finalize() &&
425432
}
426433
}
427434

428-
for (const auto &weight : weights) {
429-
auto *buf = mesh->LocalBuffers.at(weight.buffer_id);
430-
auto *weights = buf->getWeights();
431-
if (!weights) {
432-
buf->addWeightBuffer();
433-
weights = buf->getWeights();
434-
}
435-
weights->addWeight(weight.vertex_id, weight.joint_id, weight.strength);
436-
}
437-
438435
for (auto *buffer : mesh->LocalBuffers) {
439436
// With HW skinning, the VBOs should be static by default
440437
buffer->setHardwareMappingHint(EHM_STATIC);

0 commit comments

Comments
 (0)