2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2026-05-12 21:31:43 +00:00

chore: use references when possible in xmodel dumper

This commit is contained in:
Jan Laupetin
2026-05-03 12:54:45 +02:00
parent d6a736eb5d
commit 2527f23c10
+90 -90
View File
@@ -236,18 +236,18 @@ namespace
}
}
void AddXModelBones(XModelCommon& out, const AssetDumpingContext& context, const XModel* model)
void AddXModelBones(XModelCommon& out, const AssetDumpingContext& context, const XModel& model)
{
for (auto boneNum = 0u; boneNum < model->numBones; boneNum++)
for (auto boneNum = 0u; boneNum < model.numBones; boneNum++)
{
XModelBone bone;
if (model->boneNames[boneNum] < context.m_zone.m_script_strings.Count())
bone.name = context.m_zone.m_script_strings[model->boneNames[boneNum]];
if (model.boneNames[boneNum] < context.m_zone.m_script_strings.Count())
bone.name = context.m_zone.m_script_strings[model.boneNames[boneNum]];
else
bone.name = "INVALID_BONE_NAME";
if (boneNum >= model->numRootBones)
bone.parentIndex = static_cast<int>(boneNum - static_cast<unsigned int>(model->parentList[boneNum - model->numRootBones]));
if (boneNum >= model.numRootBones)
bone.parentIndex = static_cast<int>(boneNum - static_cast<unsigned int>(model.parentList[boneNum - model.numRootBones]));
else
bone.parentIndex = std::nullopt;
@@ -255,7 +255,7 @@ namespace
bone.scale[1] = 1.0f;
bone.scale[2] = 1.0f;
const auto& baseMat = model->baseMat[boneNum];
const auto& baseMat = model.baseMat[boneNum];
bone.globalOffset[0] = baseMat.trans.x;
bone.globalOffset[1] = baseMat.trans.y;
bone.globalOffset[2] = baseMat.trans.z;
@@ -266,7 +266,7 @@ namespace
.w = baseMat.quat.w,
};
if (boneNum < model->numRootBones)
if (boneNum < model.numRootBones)
{
bone.localOffset[0] = 0;
bone.localOffset[1] = 0;
@@ -275,12 +275,12 @@ namespace
}
else
{
const auto* trans = &model->trans[(boneNum - model->numRootBones) * 3];
const auto* trans = &model.trans[(boneNum - model.numRootBones) * 3];
bone.localOffset[0] = trans[0];
bone.localOffset[1] = trans[1];
bone.localOffset[2] = trans[2];
const auto& quat = model->quats[boneNum - model->numRootBones];
const auto& quat = model.quats[boneNum - model.numRootBones];
bone.localRotation = {
.x = QuatInt16::ToFloat(quat.v[0]),
.y = QuatInt16::ToFloat(quat.v[1]),
@@ -301,11 +301,11 @@ namespace
return input;
}
void AddXModelMaterials(XModelCommon& out, DistinctMapper<Material*>& materialMapper, const XModel* model)
void AddXModelMaterials(XModelCommon& out, DistinctMapper<Material*>& materialMapper, const XModel& model)
{
for (auto surfaceMaterialNum = 0u; surfaceMaterialNum < model->numsurfs; surfaceMaterialNum++)
for (auto surfaceMaterialNum = 0u; surfaceMaterialNum < model.numsurfs; surfaceMaterialNum++)
{
Material* material = model->materialHandles[surfaceMaterialNum];
Material* material = model.materialHandles[surfaceMaterialNum];
if (materialMapper.Add(material))
{
XModelMaterial xMaterial;
@@ -329,10 +329,10 @@ namespace
}
}
void AddXModelObjects(XModelCommon& out, const XModel* model, const unsigned lod, const DistinctMapper<Material*>& materialMapper)
void AddXModelObjects(XModelCommon& out, const XModel& model, const unsigned lod, const DistinctMapper<Material*>& materialMapper)
{
const auto surfCount = model->lodInfo[lod].numsurfs;
const auto baseSurfaceIndex = model->lodInfo[lod].surfIndex;
const auto surfCount = model.lodInfo[lod].numsurfs;
const auto baseSurfaceIndex = model.lodInfo[lod].surfIndex;
for (auto surfIndex = 0u; surfIndex < surfCount; surfIndex++)
{
@@ -344,11 +344,11 @@ namespace
}
}
void AddXModelVertices(XModelCommon& out, const XModel* model, const unsigned lod)
void AddXModelVertices(XModelCommon& out, const XModel& model, const unsigned lod)
{
XSurface* surfs;
unsigned surfCount;
if (!GetSurfaces(*model, lod, surfs, surfCount))
if (!GetSurfaces(model, lod, surfs, surfCount))
return;
for (auto surfIndex = 0u; surfIndex < surfCount; surfIndex++)
@@ -372,11 +372,11 @@ namespace
}
}
void AllocateXModelBoneWeights(const XModel* model, const unsigned lod, XModelVertexBoneWeightCollection& weightCollection)
void AllocateXModelBoneWeights(const XModel& model, const unsigned lod, XModelVertexBoneWeightCollection& weightCollection)
{
XSurface* surfs;
unsigned surfCount;
if (!GetSurfaces(*model, lod, surfs, surfCount))
if (!GetSurfaces(model, lod, surfs, surfCount))
return;
auto totalWeightCount = 0u;
@@ -406,11 +406,11 @@ namespace
return static_cast<float>(value) / static_cast<float>(std::numeric_limits<uint16_t>::max());
}
void AddXModelVertexBoneWeights(XModelCommon& out, const XModel* model, const unsigned lod)
void AddXModelVertexBoneWeights(XModelCommon& out, const XModel& model, const unsigned lod)
{
XSurface* surfs;
unsigned surfCount;
if (!GetSurfaces(*model, lod, surfs, surfCount))
if (!GetSurfaces(model, lod, surfs, surfCount))
return;
auto& weightCollection = out.m_bone_weight_data;
@@ -525,11 +525,11 @@ namespace
}
}
void AddXModelFaces(XModelCommon& out, const XModel* model, const unsigned lod)
void AddXModelFaces(XModelCommon& out, const XModel& model, const unsigned lod)
{
XSurface* surfs;
unsigned surfCount;
if (!GetSurfaces(*model, lod, surfs, surfCount))
if (!GetSurfaces(model, lod, surfs, surfCount))
return;
for (auto surfIndex = 0u; surfIndex < surfCount; surfIndex++)
@@ -557,12 +557,12 @@ namespace
&& ObjWriting::Configuration.ModelOutputFormat != ObjWriting::Configuration_t::ModelOutputFormat_e::XMODEL_BIN;
}
void PopulateXModelWriter(XModelCommon& out, const AssetDumpingContext& context, const unsigned lod, const XModel* model)
void PopulateXModelWriter(XModelCommon& out, const AssetDumpingContext& context, const unsigned lod, const XModel& model)
{
DistinctMapper<Material*> materialMapper(model->numsurfs);
DistinctMapper<Material*> materialMapper(model.numsurfs);
AllocateXModelBoneWeights(model, lod, out.m_bone_weight_data);
out.m_name = std::format("{}_lod{}", model->name, lod);
out.m_name = std::format("{}_lod{}", model.name, lod);
AddXModelMaterials(out, materialMapper, model);
AddXModelObjects(out, model, lod, materialMapper);
AddXModelVertices(out, model, lod);
@@ -570,7 +570,7 @@ namespace
// Keep armature handling consistent across all LODs so dumped GLTF/GLB round-trips
// preserve the same bone layout when re-imported.
if (!CanOmitDefaultArmature() || !HasDefaultArmatureForAllLods(*model))
if (!CanOmitDefaultArmature() || !HasDefaultArmatureForAllLods(model))
{
AddXModelBones(out, context, model);
AddXModelVertexBoneWeights(out, model, lod);
@@ -583,39 +583,39 @@ namespace
void DumpObjMtl(const XModelCommon& common, const AssetDumpingContext& context, const XAssetInfo<XModel>& asset)
{
const auto* model = asset.Asset();
const auto mtlFile = context.OpenAssetFile(std::format("model_export/{}.mtl", model->name));
const auto& model = *asset.Asset();
const auto mtlFile = context.OpenAssetFile(std::format("model_export/{}.mtl", model.name));
if (!mtlFile)
return;
const auto* game = IGame::GetGameById(context.m_zone.m_game_id);
const auto writer = obj::CreateMtlWriter(*mtlFile, game->GetShortName(), context.m_zone.m_name);
DistinctMapper<Material*> materialMapper(model->numsurfs);
DistinctMapper<Material*> materialMapper(model.numsurfs);
writer->Write(common);
}
void DumpObjLod(const XModelCommon& common, const AssetDumpingContext& context, const XAssetInfo<XModel>& asset, const unsigned lod)
{
const auto* model = asset.Asset();
const auto assetFile = context.OpenAssetFile(GetFileNameForLod(model->name, lod, ".obj"));
const auto& model = *asset.Asset();
const auto assetFile = context.OpenAssetFile(GetFileNameForLod(model.name, lod, ".obj"));
if (!assetFile)
return;
const auto* game = IGame::GetGameById(context.m_zone.m_game_id);
const auto writer =
obj::CreateObjWriter(*assetFile, std::format("{}.mtl", model->name), game->GetShortName(), context.m_zone.m_name);
DistinctMapper<Material*> materialMapper(model->numsurfs);
obj::CreateObjWriter(*assetFile, std::format("{}.mtl", model.name), game->GetShortName(), context.m_zone.m_name);
DistinctMapper<Material*> materialMapper(model.numsurfs);
writer->Write(common);
}
void DumpXModelExportLod(const XModelCommon& common, const AssetDumpingContext& context, const XAssetInfo<XModel>& asset, const unsigned lod)
{
const auto* model = asset.Asset();
const auto assetFile = context.OpenAssetFile(GetFileNameForLod(model->name, lod, ".xmodel_export"));
const auto model = *asset.Asset();
const auto assetFile = context.OpenAssetFile(GetFileNameForLod(model.name, lod, ".xmodel_export"));
if (!assetFile)
return;
@@ -627,8 +627,8 @@ namespace
void DumpXModelBinLod(const XModelCommon& common, const AssetDumpingContext& context, const XAssetInfo<XModel>& asset, const unsigned lod)
{
const auto* model = asset.Asset();
const auto assetFile = context.OpenAssetFile(GetFileNameForLod(model->name, lod, ".xmodel_bin"));
const auto& model = *asset.Asset();
const auto assetFile = context.OpenAssetFile(GetFileNameForLod(model.name, lod, ".xmodel_bin"));
if (!assetFile)
return;
@@ -642,8 +642,8 @@ namespace
void DumpGltfLod(
const XModelCommon& common, const AssetDumpingContext& context, const XAssetInfo<XModel>& asset, const unsigned lod, const std::string& extension)
{
const auto* model = asset.Asset();
const auto assetFile = context.OpenAssetFile(GetFileNameForLod(model->name, lod, extension));
const auto& model = *asset.Asset();
const auto assetFile = context.OpenAssetFile(GetFileNameForLod(model.name, lod, extension));
if (!assetFile)
return;
@@ -657,12 +657,12 @@ namespace
void DumpXModelSurfs(const AssetDumpingContext& context, const XAssetInfo<XModel>& asset)
{
const auto* model = asset.Asset();
const auto& model = *asset.Asset();
for (auto currentLod = 0u; currentLod < model->numLods; currentLod++)
for (auto currentLod = 0u; currentLod < model.numLods; currentLod++)
{
XModelCommon common;
PopulateXModelWriter(common, context, currentLod, asset.Asset());
PopulateXModelWriter(common, context, currentLod, model);
switch (ObjWriting::Configuration.ModelOutputFormat)
{
@@ -703,10 +703,10 @@ namespace
{
}
void Dump(AssetDumpingContext& context, const XModel* xmodel) const
void Dump(AssetDumpingContext& context, const XModel& model) const
{
JsonXModel jsonXModel;
CreateJsonXModel(context, jsonXModel, *xmodel);
CreateJsonXModel(context, jsonXModel, model);
nlohmann::json jRoot = jsonXModel;
jRoot["$schema"] = "http://openassettools.dev/schema/xmodel.v1.json";
@@ -746,26 +746,26 @@ namespace
}
}
static bool IsAnimated(const XModel& xmodel)
static bool IsAnimated(const XModel& model)
{
#if defined(FEATURE_IW4) || defined(FEATURE_IW5)
for (auto i = 0u; i < xmodel.numLods; i++)
for (auto i = 0u; i < model.numLods; i++)
{
const auto& lod = xmodel.lodInfo[i];
const auto& lod = model.lodInfo[i];
if (lod.modelSurfs == nullptr || lod.modelSurfs->surfs == nullptr)
continue;
for (auto j = 0u; j < lod.modelSurfs->numsurfs; j++)
{
const auto& surf = xmodel.lodInfo[i].modelSurfs->surfs[j];
const auto& surf = model.lodInfo[i].modelSurfs->surfs[j];
if (surf.vertInfo.vertsBlend)
return true;
}
}
#else
for (auto i = 0u; i < xmodel.numsurfs; i++)
for (auto i = 0u; i < model.numsurfs; i++)
{
const auto& surf = xmodel.surfs[i];
const auto& surf = model.surfs[i];
if (surf.vertInfo.vertsBlend)
return true;
}
@@ -774,29 +774,29 @@ namespace
return false;
}
static bool HasNulledTrans(const XModel& xmodel)
static bool HasNulledTrans(const XModel& model)
{
if (xmodel.trans == nullptr)
if (model.trans == nullptr)
return true;
const auto transCount = (xmodel.numBones - xmodel.numRootBones) * 3u;
const auto transCount = (model.numBones - model.numRootBones) * 3u;
for (auto i = 0u; i < transCount; i++)
{
if (xmodel.trans[i] != 0)
if (model.trans[i] != 0)
return false;
}
return true;
}
static bool HasNonNullBoneInfoTrans(const XModel& xmodel)
static bool HasNonNullBoneInfoTrans(const XModel& model)
{
if (xmodel.boneInfo == nullptr)
if (model.boneInfo == nullptr)
return false;
for (auto i = 0u; i < xmodel.numBones; i++)
for (auto i = 0u; i < model.numBones; i++)
{
const auto& boneInfo = xmodel.boneInfo[i];
const auto& boneInfo = model.boneInfo[i];
#if defined(FEATURE_IW4) || defined(FEATURE_IW5)
if (boneInfo.bounds.midPoint.x != 0 || boneInfo.bounds.midPoint.y != 0 || boneInfo.bounds.midPoint.z != 0)
return true;
@@ -809,73 +809,73 @@ namespace
return false;
}
static JsonXModelType GetType(const XModel& xmodel)
static JsonXModelType GetType(const XModel& model)
{
if (!IsAnimated(xmodel))
if (!IsAnimated(model))
return JsonXModelType::RIGID;
if (HasNulledTrans(xmodel) && HasNonNullBoneInfoTrans(xmodel))
if (HasNulledTrans(model) && HasNonNullBoneInfoTrans(model))
return JsonXModelType::VIEWHANDS;
return JsonXModelType::ANIMATED;
}
static void SetJsonRootBoneName(AssetDumpingContext& context, JsonXModel& jXModel, const XModel& xmodel)
static void SetJsonRootBoneName(AssetDumpingContext& context, JsonXModel& jXModel, const XModel& model)
{
assert(xmodel.boneNames);
assert(xmodel.boneNames[0] < context.m_zone.m_script_strings.Count());
assert(xmodel.numRootBones <= 1);
assert(model.boneNames);
assert(model.boneNames[0] < context.m_zone.m_script_strings.Count());
assert(model.numRootBones <= 1);
if (!xmodel.boneNames || xmodel.boneNames[0] >= context.m_zone.m_script_strings.Count() || xmodel.numRootBones == 0)
if (!model.boneNames || model.boneNames[0] >= context.m_zone.m_script_strings.Count() || model.numRootBones == 0)
return;
jXModel.rootBoneName = context.m_zone.m_script_strings[xmodel.boneNames[0]];
jXModel.rootBoneName = context.m_zone.m_script_strings[model.boneNames[0]];
}
static void CreateJsonXModel(AssetDumpingContext& context, JsonXModel& jXModel, const XModel& xmodel)
static void CreateJsonXModel(AssetDumpingContext& context, JsonXModel& jXModel, const XModel& model)
{
if (xmodel.collLod >= 0)
jXModel.collLod = xmodel.collLod;
if (model.collLod >= 0)
jXModel.collLod = model.collLod;
jXModel.type = GetType(xmodel);
jXModel.type = GetType(model);
if (CanOmitDefaultArmature() && HasDefaultArmatureForAllLods(xmodel))
if (CanOmitDefaultArmature() && HasDefaultArmatureForAllLods(model))
{
// If we are going to omit the armature, we need to make sure we remember the root bone name.
// It does not follow a specific pattern, may not be identical to the xmodel name and
// may be required for attaching the xmodel.
SetJsonRootBoneName(context, jXModel, xmodel);
SetJsonRootBoneName(context, jXModel, model);
}
for (auto lodNumber = 0u; lodNumber < xmodel.numLods; lodNumber++)
for (auto lodNumber = 0u; lodNumber < model.numLods; lodNumber++)
{
JsonXModelLod lod;
lod.file = std::format("model_export/{}_lod{}{}", xmodel.name, lodNumber, GetExtensionForModelByConfig());
lod.distance = xmodel.lodInfo[lodNumber].dist;
lod.file = std::format("model_export/{}_lod{}{}", model.name, lodNumber, GetExtensionForModelByConfig());
lod.distance = model.lodInfo[lodNumber].dist;
jXModel.lods.emplace_back(std::move(lod));
}
if (xmodel.physPreset && xmodel.physPreset->name)
jXModel.physPreset = AssetName(xmodel.physPreset->name);
if (model.physPreset && model.physPreset->name)
jXModel.physPreset = AssetName(model.physPreset->name);
#if defined(FEATURE_IW4) || defined(FEATURE_IW5)
if (xmodel.physCollmap && xmodel.physCollmap->name)
jXModel.physCollmap = AssetName(xmodel.physCollmap->name);
if (model.physCollmap && model.physCollmap->name)
jXModel.physCollmap = AssetName(model.physCollmap->name);
#endif
#if defined(FEATURE_T5) || defined(FEATURE_T6)
if (xmodel.physConstraints && xmodel.physConstraints->name)
jXModel.physConstraints = AssetName(xmodel.physConstraints->name);
if (model.physConstraints && model.physConstraints->name)
jXModel.physConstraints = AssetName(model.physConstraints->name);
#endif
jXModel.flags = xmodel.flags;
jXModel.flags = model.flags;
#ifdef FEATURE_T6
jXModel.lightingOriginOffset.x = xmodel.lightingOriginOffset.x;
jXModel.lightingOriginOffset.y = xmodel.lightingOriginOffset.y;
jXModel.lightingOriginOffset.z = xmodel.lightingOriginOffset.z;
jXModel.lightingOriginRange = xmodel.lightingOriginRange;
jXModel.lightingOriginOffset.x = model.lightingOriginOffset.x;
jXModel.lightingOriginOffset.y = model.lightingOriginOffset.y;
jXModel.lightingOriginOffset.z = model.lightingOriginOffset.z;
jXModel.lightingOriginRange = model.lightingOriginRange;
#endif
}
@@ -889,7 +889,7 @@ namespace
return;
const JsonDumper dumper(*assetFile);
dumper.Dump(context, asset.Asset());
dumper.Dump(context, *asset.Asset());
}
} // namespace