Skip to content

Commit 38019f8

Browse files
AssetLoader: share GLTF primitive bounds helper
Move primitive bounding-box computation out of MeshLoader into a GLTFBuilder-level helper so other GLTF mesh-loading paths can reuse it without depending on MeshLoader internals.
1 parent 204128d commit 38019f8

1 file changed

Lines changed: 42 additions & 28 deletions

File tree

AssetLoader/interface/GLTFBuilder.hpp

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@
3030
#include <unordered_set>
3131
#include <vector>
3232
#include <algorithm>
33+
#include <cstring>
34+
#include <limits>
3335
#include <string>
3436
#include <utility>
3537

38+
#include "DebugUtilities.hpp"
3639
#include "GLTFLoader.hpp"
3740
#include "GLTFVertexDataConverter.hpp"
3841
#include "GraphicsAccessories.hpp"
@@ -47,6 +50,45 @@ struct Model;
4750
struct ModelCreateInfo;
4851
struct Node;
4952

53+
template <typename GltfDataInfoType>
54+
bool ComputePrimitiveBoundingBox(const GltfDataInfoType& PosData, float3& Min, float3& Max)
55+
{
56+
if (PosData.pData == nullptr)
57+
{
58+
DEV_ERROR("GLTF vertex position data must not be null.");
59+
return false;
60+
}
61+
if (PosData.Accessor.GetComponentType() != VT_FLOAT32)
62+
{
63+
DEV_ERROR("Unexpected GLTF vertex position component type: ", GetValueTypeString(PosData.Accessor.GetComponentType()), ". float is expected.");
64+
return false;
65+
}
66+
if (PosData.Accessor.GetNumComponents() != 3)
67+
{
68+
DEV_ERROR("Unexpected GLTF vertex position component count: ", PosData.Accessor.GetNumComponents(), ". 3 is expected.");
69+
return false;
70+
}
71+
const auto SrcByteStride = PosData.ByteStride;
72+
if (SrcByteStride <= 0 || static_cast<size_t>(SrcByteStride) < sizeof(float3))
73+
{
74+
DEV_ERROR("Unexpected GLTF vertex position stride: ", SrcByteStride, ". Stride must be at least ", sizeof(float3), ".");
75+
return false;
76+
}
77+
78+
const size_t ByteStride = static_cast<size_t>(SrcByteStride);
79+
80+
Max = float3{-(std::numeric_limits<float>::max)()};
81+
Min = float3{+(std::numeric_limits<float>::max)()};
82+
for (size_t i = 0; i < PosData.Count; ++i)
83+
{
84+
float3 Pos;
85+
std::memcpy(&Pos, static_cast<const Uint8*>(PosData.pData) + ByteStride * i, sizeof(Pos));
86+
Max = max(Max, Pos);
87+
Min = min(Min, Pos);
88+
}
89+
return true;
90+
}
91+
5092
// {0BF00221-593F-40CE-B5BD-E47039D77F4A}
5193
static constexpr INTERFACE_ID IID_BufferInitData =
5294
{0xbf00221, 0x593f, 0x40ce, {0xb5, 0xbd, 0xe4, 0x70, 0x39, 0xd7, 0x7f, 0x4a}};
@@ -210,9 +252,6 @@ class MeshLoader
210252

211253
void WriteDefaultAttibutes(Uint32 BufferId, size_t StartOffset, size_t EndOffset);
212254

213-
template <typename GltfDataInfoType>
214-
static bool ComputePrimitiveBoundingBox(const GltfDataInfoType& PosData, float3& Min, float3& Max);
215-
216255
template <typename GltfModelType>
217256
Uint32 ConvertVertexData(const GltfModelType& GltfModel,
218257
const PrimitiveKey& Key,
@@ -372,31 +411,6 @@ void ModelBuilder::AllocateNode(const GltfModelType& GltfModel,
372411
}
373412

374413

375-
template <typename GltfDataInfoType>
376-
bool MeshLoader::ComputePrimitiveBoundingBox(const GltfDataInfoType& PosData, float3& Min, float3& Max)
377-
{
378-
if (PosData.Accessor.GetComponentType() != VT_FLOAT32)
379-
{
380-
DEV_ERROR("Unexpected GLTF vertex position component type: ", GetValueTypeString(PosData.Accessor.GetComponentType()), ". float is expected.");
381-
return false;
382-
}
383-
if (PosData.Accessor.GetNumComponents() != 3)
384-
{
385-
DEV_ERROR("Unexpected GLTF vertex position component count: ", PosData.Accessor.GetNumComponents(), ". 3 is expected.");
386-
return false;
387-
}
388-
389-
Max = float3{-FLT_MAX};
390-
Min = float3{+FLT_MAX};
391-
for (size_t i = 0; i < PosData.Count; ++i)
392-
{
393-
const auto& Pos{*reinterpret_cast<const float3*>(static_cast<const Uint8*>(PosData.pData) + PosData.ByteStride * i)};
394-
Max = max(Max, Pos);
395-
Min = min(Min, Pos);
396-
}
397-
return true;
398-
}
399-
400414
template <typename GltfModelType, typename MeshLoaderType>
401415
Mesh* ModelBuilder::LoadMesh(const GltfModelType& GltfModel,
402416
int GltfMeshIndex,

0 commit comments

Comments
 (0)