Skip to content

Commit c010ebd

Browse files
committed
Various small things
1 parent 02e52f7 commit c010ebd

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

irr/include/WeightBuffer.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ struct WeightBuffer
2020
// ID-weight pairs for a joint
2121
struct VertexWeights {
2222
std::array<u16, MAX_WEIGHTS_PER_VERTEX> joint_ids = {};
23-
std::array<f32, MAX_WEIGHTS_PER_VERTEX> strengths = {};
23+
std::array<f32, MAX_WEIGHTS_PER_VERTEX> weights = {};
2424
void addWeight(u16 joint_id, f32 weight);
25+
/// Transform given position and normal with these weights
2526
void skinVertex(core::vector3df &pos, core::vector3df &normal,
2627
const std::vector<core::matrix4> &joint_transforms) const;
2728
};
@@ -40,13 +41,14 @@ struct WeightBuffer
4041
{ return weights[vertex_id].joint_ids; }
4142

4243
const std::array<f32, MAX_WEIGHTS_PER_VERTEX> &getWeights(u32 vertex_id) const
43-
{ return weights[vertex_id].strengths; }
44+
{ return weights[vertex_id].weights; }
4445

4546
size_t size() const
4647
{ return weights.size(); }
4748

4849
void addWeight(u32 vertex_id, u16 joint_id, f32 weight);
4950

51+
/// Transform position and normal using the weights of the given vertex
5052
void skinVertex(u32 vertex_id, core::vector3df &pos, core::vector3df &normal,
5153
const std::vector<core::matrix4> &joint_transforms) const;
5254

irr/src/SkinnedMesh.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -276,21 +276,21 @@ void SkinnedMesh::calculateJointBoundingBoxes()
276276
if (!weights)
277277
continue;
278278
for (u32 vert_id : weights->animated_vertices.value()) {
279+
const auto pos = buf->getVertex(vert_id)->Pos;
279280
for (u16 j = 0; j < WeightBuffer::MAX_WEIGHTS_PER_VERTEX; j++) {
280-
const auto joint_id = weights->getJointIds(vert_id)[j];
281-
const auto *joint = AllJoints[joint_id];
282-
const auto strength = weights->getWeights(vert_id)[j];
283-
if (core::equals(strength, 0.0f))
281+
const u16 joint_id = weights->getJointIds(vert_id)[j];
282+
const SJoint *joint = AllJoints[joint_id];
283+
const f32 weight = weights->getWeights(vert_id)[j];
284+
if (core::equals(weight, 0.0f))
284285
continue;
285-
auto pos = buf->getVertex(vert_id)->Pos;
286-
joint->GlobalInversedMatrix.value().transformVect(pos);
286+
auto trans_pos = pos;
287+
joint->GlobalInversedMatrix.value().transformVect(trans_pos);
287288
if (joint_boxes[joint_id]) {
288-
joint_boxes[joint_id]->addInternalPoint(pos);
289+
joint_boxes[joint_id]->addInternalPoint(trans_pos);
289290
} else {
290-
joint_boxes[joint_id] = core::aabbox3df{pos};
291+
joint_boxes[joint_id] = core::aabbox3df{trans_pos};
291292
}
292293
}
293-
weights->getWeights(vert_id);
294294
}
295295
}
296296
for (u16 joint_id = 0; joint_id < AllJoints.size(); joint_id++) {
@@ -317,7 +317,7 @@ void SkinnedMesh::recalculateBaseBoundingBoxes() {
317317
void SkinnedMeshBuilder::topoSortJoints()
318318
{
319319
auto &joints = getJoints();
320-
size_t n = joints.size();
320+
const size_t n = joints.size();
321321

322322
std::vector<u16> new_to_old_id;
323323

@@ -365,6 +365,9 @@ SkinnedMesh *SkinnedMeshBuilder::finalize() &&
365365
{
366366
os::Printer::log("Skinned Mesh - finalize", ELL_DEBUG);
367367

368+
// Topologically sort the joints such that parents come before their children.
369+
// From this point on, transformations can be calculated in linear order.
370+
// (see e.g. SkinnedMesh::calculateGlobalMatrices)
368371
topoSortJoints();
369372

370373
mesh->prepareForSkinning();

irr/src/WeightBuffer.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ namespace scene {
1111
void WeightBuffer::VertexWeights::addWeight(u16 joint_id, f32 weight)
1212
{
1313
assert(weight >= 0.0f);
14-
auto min_weight = std::min_element(strengths.begin(), strengths.end());
14+
auto min_weight = std::min_element(weights.begin(), weights.end());
1515
if (*min_weight > weight)
1616
return;
1717

1818
*min_weight = weight;
19-
joint_ids[std::distance(strengths.begin(), min_weight)] = joint_id;
19+
joint_ids[std::distance(weights.begin(), min_weight)] = joint_id;
2020
}
2121

2222
void WeightBuffer::addWeight(u32 vertex_id, u16 joint_id, f32 weight)
@@ -31,8 +31,8 @@ void WeightBuffer::VertexWeights::skinVertex(core::vector3df &pos, core::vector3
3131
core::vector3df skinned_pos;
3232
core::vector3df skinned_normal;
3333
for (u16 i = 0; i < MAX_WEIGHTS_PER_VERTEX; ++i) {
34-
u16 joint_id = joint_ids[i];
35-
f32 weight = strengths[i];
34+
const u16 joint_id = joint_ids[i];
35+
const f32 weight = weights[i];
3636
if (core::equals(weight, 0.0f))
3737
continue;
3838

@@ -81,16 +81,16 @@ void WeightBuffer::finalize()
8181
assert(!animated_vertices.has_value());
8282
animated_vertices.emplace();
8383
for (u32 i = 0; i < size(); ++i) {
84-
auto &strengths = weights[i].strengths;
85-
f32 total_weight = std::accumulate(strengths.begin(), strengths.end(), 0.0f);
84+
auto &weights_i = weights[i].weights;
85+
f32 total_weight = std::accumulate(weights_i.begin(), weights_i.end(), 0.0f);
8686
if (core::equals(total_weight, 0.0f)) {
87-
std::fill(strengths.begin(), strengths.end(), 0.0f);
87+
std::fill(weights_i.begin(), weights_i.end(), 0.0f);
8888
continue;
8989
}
9090
animated_vertices->emplace_back(i);
9191
if (core::equals(total_weight, 1.0f))
9292
continue;
93-
for (auto &strength : strengths)
93+
for (auto &strength : weights_i)
9494
strength /= total_weight;
9595
}
9696
animated_vertices->shrink_to_fit();

0 commit comments

Comments
 (0)