Skip to content

Commit 4a2bbe4

Browse files
committed
gltfpack: Refactor attribute bounds computation
Instead of updating the bounds in-place, split the computation and merging; this allows us to record the original mesh bounds as a side effect.
1 parent 160f94b commit 4a2bbe4

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

gltf/stream.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,20 @@ struct Bounds
2424
{
2525
return min.f[0] <= max.f[0] && min.f[1] <= max.f[1] && min.f[2] <= max.f[2] && min.f[3] <= max.f[3];
2626
}
27+
28+
void merge(const Bounds& other)
29+
{
30+
for (int k = 0; k < 4; ++k)
31+
{
32+
min.f[k] = std::min(min.f[k], other.min.f[k]);
33+
max.f[k] = std::max(max.f[k], other.max.f[k]);
34+
}
35+
}
2736
};
2837

29-
static void updateAttributeBounds(const Mesh& mesh, cgltf_attribute_type type, Bounds& b)
38+
static Bounds computeBounds(const Mesh& mesh, cgltf_attribute_type type)
3039
{
40+
Bounds b;
3141
Attr pad = {};
3242

3343
for (size_t j = 0; j < mesh.streams.size(); ++j)
@@ -73,6 +83,8 @@ static void updateAttributeBounds(const Mesh& mesh, cgltf_attribute_type type, B
7383
b.min.f[k] -= pad.f[k];
7484
b.max.f[k] += pad.f[k];
7585
}
86+
87+
return b;
7688
}
7789

7890
QuantizationPosition prepareQuantizationPosition(const std::vector<Mesh>& meshes, const Settings& settings)
@@ -82,12 +94,14 @@ QuantizationPosition prepareQuantizationPosition(const std::vector<Mesh>& meshes
8294
result.bits = settings.pos_bits;
8395
result.normalized = settings.pos_normalized;
8496

85-
Bounds b;
97+
std::vector<Bounds> bounds(meshes.size());
8698

8799
for (size_t i = 0; i < meshes.size(); ++i)
88-
{
89-
updateAttributeBounds(meshes[i], cgltf_attribute_type_position, b);
90-
}
100+
bounds[i] = computeBounds(meshes[i], cgltf_attribute_type_position);
101+
102+
Bounds b;
103+
for (size_t i = 0; i < meshes.size(); ++i)
104+
b.merge(bounds[i]);
91105

92106
if (b.isValid())
93107
{
@@ -154,7 +168,9 @@ void prepareQuantizationTexture(cgltf_data* data, std::vector<QuantizationTextur
154168
continue;
155169

156170
indices[i] = follow(parents, indices[i]);
157-
updateAttributeBounds(mesh, cgltf_attribute_type_texcoord, bounds[indices[i]]);
171+
172+
Bounds mb = computeBounds(mesh, cgltf_attribute_type_texcoord);
173+
bounds[indices[i]].merge(mb);
158174
}
159175

160176
// update all material data using canonical bounds

0 commit comments

Comments
 (0)