2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-07-11 13:41:50 +00:00

refactor: use templated xmodel loading and dumping code for iw4

This commit is contained in:
Jan
2025-07-09 23:10:40 +01:00
parent a9c693d04d
commit 3daa8b0412
11 changed files with 70 additions and 563 deletions

View File

@ -113,6 +113,8 @@ namespace IW4
struct VehicleDef;
struct AddonMapEnts;
typedef unsigned short ScriptString;
union XAssetHeader
{
PhysPreset* physPreset;
@ -239,8 +241,8 @@ namespace IW4
struct Bounds
{
float midPoint[3];
float halfSize[3];
vec3_t midPoint;
vec3_t halfSize;
};
struct cplane_s
@ -325,7 +327,7 @@ namespace IW4
struct XAnimNotifyInfo
{
uint16_t name;
ScriptString name;
float time;
};
@ -448,7 +450,7 @@ namespace IW4
unsigned int indexCount;
float framerate;
float frequency;
uint16_t* names;
ScriptString* names;
char* dataByte;
int16_t* dataShort;
int* dataInt;
@ -491,7 +493,7 @@ namespace IW4
struct type_align(16) GfxPackedVertex
{
float xyz[3];
vec3_t xyz;
float binormalSign;
GfxColor color;
PackedTexCoords texCoord;
@ -536,7 +538,12 @@ namespace IW4
XSurfaceCollisionTree* collisionTree;
};
typedef tdef_align32(16) uint16_t r_index16_t;
struct XSurfaceTri
{
uint16_t i[3];
};
typedef tdef_align32(16) XSurfaceTri XSurfaceTri16;
struct XSurface
{
@ -547,7 +554,7 @@ namespace IW4
char zoneHandle;
uint16_t baseTriIndex;
uint16_t baseVertIndex;
r_index16_t (*triIndices)[3];
XSurfaceTri16* triIndices;
XSurfaceVertexInfo vertInfo;
GfxPackedVertex* verts0;
unsigned int vertListCount;
@ -602,11 +609,16 @@ namespace IW4
struct DObjAnimMat
{
float quat[4];
float trans[3];
vec4_t quat;
vec3_t trans;
float transWeight;
};
struct XModelQuat
{
int16_t v[4];
};
struct XModel
{
const char* name;
@ -616,10 +628,10 @@ namespace IW4
char lodRampType;
float scale;
unsigned int noScalePartBits[6];
uint16_t* boneNames;
ScriptString* boneNames;
unsigned char* parentList;
int16_t (*quats)[4];
float (*trans)[3];
XModelQuat* quats;
float* trans;
unsigned char* partClassification;
DObjAnimMat* baseMat;
Material** materialHandles;

View File

@ -1,10 +1,12 @@
#options GAME (IW5, T5, T6)
#options GAME (IW4, IW5, T5, T6)
#filename "Game/" + GAME + "/XModel/JsonXModel" + GAME + ".h"
#set GAME_HEADER "\"Game/" + GAME + "/" + GAME + ".h\""
#if GAME == "IW5"
#if GAME == "IW4"
#define FEATURE_IW4
#elif GAME == "IW5"
#define FEATURE_IW5
#elif GAME == "T5"
#define FEATURE_T5
@ -40,7 +42,7 @@ namespace GAME
std::vector<JsonXModelLod> lods;
std::optional<int> collLod;
std::optional<std::string> physPreset;
#if defined(FEATURE_IW5)
#if defined(FEATURE_IW4) || defined(FEATURE_IW5)
std::optional<std::string> physCollmap;
#elif defined(FEATURE_T5) || defined(FEATURE_T6)
std::optional<std::string> physConstraints;
@ -57,7 +59,7 @@ namespace GAME
lods,
collLod,
physPreset,
#if defined(FEATURE_IW5)
#if defined(FEATURE_IW4) || defined(FEATURE_IW5)
physCollmap,
#elif defined(FEATURE_T5) || defined(FEATURE_T6)
physConstraints,

View File

@ -1,10 +1,12 @@
#options GAME (IW5, T5, T6)
#options GAME (IW4, IW5, T5, T6)
#filename "Game/" + GAME + "/XModel/XModelConstants" + GAME + ".h"
#set GAME_HEADER "\"Game/" + GAME + "/" + GAME + ".h\""
#if GAME == "IW5"
#if GAME == "IW4"
#define FEATURE_IW4
#elif GAME == "IW5"
#define FEATURE_IW5
#elif GAME == "T5"
#define FEATURE_T5
@ -45,7 +47,7 @@ namespace GAME
"right_foot",
"left_foot",
"gun",
#if defined(FEATURE_IW5) || defined(FEATURE_T6)
#if defined(FEATURE_IW4) || defined(FEATURE_IW5) || defined(FEATURE_T6)
"shield",
#endif
};

View File

@ -3,6 +3,7 @@
#include "Asset/GlobalAssetPoolsLoader.h"
#include "Game/IW4/GameIW4.h"
#include "Game/IW4/IW4.h"
#include "Game/IW4/XModel/LoaderXModelIW4.h"
#include "Leaderboard/LoaderLeaderboardIW4.h"
#include "LightDef/LoaderLightDefIW4.h"
#include "Localize/LoaderLocalizeIW4.h"
@ -123,7 +124,7 @@ namespace
// collection.AddAssetCreator(std::make_unique<AssetLoaderPhysCollMap>(memory));
// collection.AddAssetCreator(std::make_unique<AssetLoaderXAnim>(memory));
// collection.AddAssetCreator(std::make_unique<AssetLoaderXModelSurfs>(memory));
// collection.AddAssetCreator(std::make_unique<AssetLoaderXModel>(memory));
collection.AddAssetCreator(CreateXModelLoader(memory, searchPath, zone));
collection.AddAssetCreator(CreateMaterialLoader(memory, searchPath));
collection.AddAssetCreator(CreatePixelShaderLoader(memory, searchPath));
collection.AddAssetCreator(CreateVertexShaderLoader(memory, searchPath));

View File

@ -1,4 +1,4 @@
#options GAME(IW5, T5, T6)
#options GAME(IW4, IW5, T5, T6)
#filename "Game/" + GAME + "/XModel/LoaderXModel" + GAME + ".cpp"
@ -7,7 +7,9 @@
#set CONSTANTS_HEADER "\"Game/" + GAME + "/XModel/XModelConstants" + GAME + ".h\""
#set JSON_HEADER "\"Game/" + GAME + "/XModel/JsonXModel" + GAME + ".h\""
#if GAME == "IW5"
#if GAME == "IW4"
#define FEATURE_IW4
#elif GAME == "IW5"
#define FEATURE_IW5
#elif GAME == "T5"
#define FEATURE_T5
@ -191,7 +193,7 @@ namespace
if (common.m_bone_weight_data.weights.empty())
return;
#ifdef FEATURE_IW5
#if defined(FEATURE_IW4) || defined(FEATURE_IW5)
vec3_t minCoordinate, maxCoordinate;
auto& offset = info.bounds.midPoint;
#else
@ -236,7 +238,7 @@ namespace
const Eigen::Vector3f maxEigen(maxCoordinate.x, maxCoordinate.y, maxCoordinate.z);
const Eigen::Vector3f boundsCenter = (minEigen + maxEigen) * 0.5f;
const Eigen::Vector3f halfSizeEigen = maxEigen - boundsCenter;
#ifdef FEATURE_IW5
#if defined(FEATURE_IW4) || defined(FEATURE_IW5)
info.bounds.halfSize.x = halfSizeEigen.x();
info.bounds.halfSize.y = halfSizeEigen.y();
@ -727,7 +729,7 @@ namespace
lodInfo.partBits[i] |= surface.partBits[i];
}
#ifdef FEATURE_IW5
#if defined(FEATURE_IW4) || defined(FEATURE_IW5)
auto* modelSurfs = m_memory.Alloc<XModelSurfs>();
const auto modelSurfsName = std::format("{}_lod{}", xmodel.name, lodNumber);
modelSurfs->name = m_memory.Dup(modelSurfsName.c_str());
@ -752,7 +754,7 @@ namespace
static void CalculateModelBounds(XModel& xmodel)
{
#ifdef FEATURE_IW5
#if defined(FEATURE_IW4) || defined(FEATURE_IW5)
if (!xmodel.lodInfo[0].modelSurfs || !xmodel.lodInfo[0].modelSurfs->surfs)
return;
@ -768,7 +770,7 @@ namespace
for (auto surfaceIndex = 0u; surfaceIndex < xmodel.lodInfo[0].numsurfs; surfaceIndex++)
{
#ifdef FEATURE_IW5
#if defined(FEATURE_IW4) || defined(FEATURE_IW5)
const auto& surface = surfs[surfaceIndex];
#else
const auto& surface = xmodel.surfs[surfaceIndex + xmodel.lodInfo[0].surfIndex];
@ -790,7 +792,7 @@ namespace
}
}
#ifdef FEATURE_IW5
#if defined(FEATURE_IW4) || defined(FEATURE_IW5)
const Eigen::Vector3f minEigen(minCoordinate.x, minCoordinate.y, minCoordinate.z);
const Eigen::Vector3f maxEigen(maxCoordinate.x, maxCoordinate.y, maxCoordinate.z);
const Eigen::Vector3f boundsCenter = (minEigen + maxEigen) * 0.5f;
@ -876,7 +878,7 @@ namespace
xmodel.physPreset = nullptr;
}
#if defined(FEATURE_IW5)
#if defined(FEATURE_IW4) || defined(FEATURE_IW5)
if (jXModel.physCollmap)
{
auto* physCollmap = context.LoadDependency<AssetPhysCollMap>(jXModel.physCollmap.value());

View File

@ -1,4 +1,4 @@
#options GAME (IW5, T5, T6)
#options GAME (IW4, IW5, T5, T6)
#filename "Game/" + GAME + "/XModel/LoaderXModel" + GAME + ".h"

View File

@ -52,20 +52,20 @@ void AssetDumperPhysCollmap::DumpAsset(AssetDumpingContext& context, XAssetInfo<
break;
case PHYS_GEOM_BOX:
mapFileDumper.WritePhysicsBox({
{geom.bounds.midPoint[0], geom.bounds.midPoint[1], geom.bounds.midPoint[2]},
{geom.bounds.halfSize[0], geom.bounds.halfSize[1], geom.bounds.halfSize[2]},
{geom.orientation[0][0], geom.orientation[0][1], geom.orientation[0][2] },
{geom.orientation[1][0], geom.orientation[1][1], geom.orientation[1][2] },
{geom.orientation[2][0], geom.orientation[2][1], geom.orientation[2][2] }
{geom.bounds.midPoint.v[0], geom.bounds.midPoint.v[1], geom.bounds.midPoint.v[2]},
{geom.bounds.halfSize.v[0], geom.bounds.halfSize.v[1], geom.bounds.halfSize.v[2]},
{geom.orientation[0][0], geom.orientation[0][1], geom.orientation[0][2] },
{geom.orientation[1][0], geom.orientation[1][1], geom.orientation[1][2] },
{geom.orientation[2][0], geom.orientation[2][1], geom.orientation[2][2] }
});
break;
case PHYS_GEOM_CYLINDER:
mapFileDumper.WritePhysicsCylinder({
{geom.bounds.midPoint[0], geom.bounds.midPoint[1], geom.bounds.midPoint[2]},
geom.bounds.halfSize[0],
geom.bounds.halfSize[2] * 2,
{geom.orientation[0][0], geom.orientation[0][1], geom.orientation[0][2] }
{geom.bounds.midPoint.v[0], geom.bounds.midPoint.v[1], geom.bounds.midPoint.v[2]},
geom.bounds.halfSize.v[0],
geom.bounds.halfSize.v[2] * 2,
{geom.orientation[0][0], geom.orientation[0][1], geom.orientation[0][2] }
});
break;

View File

@ -1,524 +1,9 @@
#include "AssetDumperXModel.h"
#include "Game/IW4/CommonIW4.h"
#include "ObjWriting.h"
#include "Utils/DistinctMapper.h"
#include "Utils/HalfFloat.h"
#include "Utils/QuatInt16.h"
#include "XModel/Export/XModelBinWriter.h"
#include "XModel/Export/XModelExportWriter.h"
#include "XModel/Gltf/GltfBinOutput.h"
#include "XModel/Gltf/GltfTextOutput.h"
#include "XModel/Gltf/GltfWriter.h"
#include "XModel/Obj/ObjWriter.h"
#include "XModel/XModelWriter.h"
#include <cassert>
#include <format>
#include "Game/IW4/XModel/XModelDumperIW4.h"
using namespace IW4;
namespace
{
GfxImage* GetMaterialColorMap(const Material* material)
{
std::vector<MaterialTextureDef*> potentialTextureDefs;
for (auto textureIndex = 0u; textureIndex < material->textureCount; textureIndex++)
{
MaterialTextureDef* def = &material->textureTable[textureIndex];
if (def->semantic == TS_COLOR_MAP)
potentialTextureDefs.push_back(def);
}
if (potentialTextureDefs.empty())
return nullptr;
if (potentialTextureDefs.size() == 1)
return potentialTextureDefs[0]->u.image;
for (const auto* def : potentialTextureDefs)
{
if (def->nameStart == 'c' && def->nameEnd == 'p')
return def->u.image;
}
return potentialTextureDefs[0]->u.image;
}
GfxImage* GetMaterialNormalMap(const Material* material)
{
std::vector<MaterialTextureDef*> potentialTextureDefs;
for (auto textureIndex = 0u; textureIndex < material->textureCount; textureIndex++)
{
MaterialTextureDef* def = &material->textureTable[textureIndex];
if (def->semantic == TS_NORMAL_MAP)
potentialTextureDefs.push_back(def);
}
if (potentialTextureDefs.empty())
return nullptr;
if (potentialTextureDefs.size() == 1)
return potentialTextureDefs[0]->u.image;
for (const auto* def : potentialTextureDefs)
{
if (def->nameStart == 'n' && def->nameEnd == 'p')
return def->u.image;
}
return potentialTextureDefs[0]->u.image;
}
GfxImage* GetMaterialSpecularMap(const Material* material)
{
std::vector<MaterialTextureDef*> potentialTextureDefs;
for (auto textureIndex = 0u; textureIndex < material->textureCount; textureIndex++)
{
MaterialTextureDef* def = &material->textureTable[textureIndex];
if (def->semantic == TS_SPECULAR_MAP)
potentialTextureDefs.push_back(def);
}
if (potentialTextureDefs.empty())
return nullptr;
if (potentialTextureDefs.size() == 1)
return potentialTextureDefs[0]->u.image;
for (const auto* def : potentialTextureDefs)
{
if (def->nameStart == 's' && def->nameEnd == 'p')
return def->u.image;
}
return potentialTextureDefs[0]->u.image;
}
void AddXModelBones(XModelCommon& out, const AssetDumpingContext& context, const XModel* model)
{
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]];
else
bone.name = "INVALID_BONE_NAME";
if (boneNum >= model->numRootBones)
bone.parentIndex = boneNum - static_cast<unsigned int>(model->parentList[boneNum - model->numRootBones]);
else
bone.parentIndex = std::nullopt;
bone.scale[0] = 1.0f;
bone.scale[1] = 1.0f;
bone.scale[2] = 1.0f;
bone.globalOffset[0] = model->baseMat[boneNum].trans[0];
bone.globalOffset[1] = model->baseMat[boneNum].trans[1];
bone.globalOffset[2] = model->baseMat[boneNum].trans[2];
bone.globalRotation = {
.x = model->baseMat[boneNum].quat[0],
.y = model->baseMat[boneNum].quat[1],
.z = model->baseMat[boneNum].quat[2],
.w = model->baseMat[boneNum].quat[3],
};
if (boneNum < model->numRootBones)
{
bone.localOffset[0] = 0;
bone.localOffset[1] = 0;
bone.localOffset[2] = 0;
bone.localRotation = {.x = 0, .y = 0, .z = 0, .w = 1};
}
else
{
bone.localOffset[0] = model->trans[boneNum - model->numRootBones][0];
bone.localOffset[1] = model->trans[boneNum - model->numRootBones][1];
bone.localOffset[2] = model->trans[boneNum - model->numRootBones][2];
bone.localRotation = {
.x = QuatInt16::ToFloat(model->quats[boneNum - model->numRootBones][0]),
.y = QuatInt16::ToFloat(model->quats[boneNum - model->numRootBones][1]),
.z = QuatInt16::ToFloat(model->quats[boneNum - model->numRootBones][2]),
.w = QuatInt16::ToFloat(model->quats[boneNum - model->numRootBones][3]),
};
}
out.m_bones.emplace_back(std::move(bone));
}
}
const char* AssetName(const char* input)
{
if (input && input[0] == ',')
return &input[1];
return input;
}
void AddXModelMaterials(XModelCommon& out, DistinctMapper<Material*>& materialMapper, const XModel* model)
{
for (auto surfaceMaterialNum = 0u; surfaceMaterialNum < model->numsurfs; surfaceMaterialNum++)
{
Material* material = model->materialHandles[surfaceMaterialNum];
if (materialMapper.Add(material))
{
XModelMaterial xMaterial;
xMaterial.ApplyDefaults();
xMaterial.name = AssetName(material->info.name);
const auto* colorMap = GetMaterialColorMap(material);
if (colorMap)
xMaterial.colorMapName = AssetName(colorMap->name);
const auto* normalMap = GetMaterialNormalMap(material);
if (normalMap)
xMaterial.normalMapName = AssetName(normalMap->name);
const auto* specularMap = GetMaterialSpecularMap(material);
if (specularMap)
xMaterial.specularMapName = AssetName(specularMap->name);
out.m_materials.emplace_back(std::move(xMaterial));
}
}
}
void AddXModelObjects(XModelCommon& out, const XModelSurfs* modelSurfs, const DistinctMapper<Material*>& materialMapper, const int baseSurfaceIndex)
{
for (auto surfIndex = 0u; surfIndex < modelSurfs->numsurfs; surfIndex++)
{
XModelObject object;
object.name = std::format("surf{}", surfIndex);
object.materialIndex = static_cast<int>(materialMapper.GetDistinctPositionByInputPosition(surfIndex + baseSurfaceIndex));
out.m_objects.emplace_back(std::move(object));
}
}
void AddXModelVertices(XModelCommon& out, const XModelSurfs* modelSurfs)
{
for (auto surfIndex = 0u; surfIndex < modelSurfs->numsurfs; surfIndex++)
{
const auto& surface = modelSurfs->surfs[surfIndex];
for (auto vertexIndex = 0u; vertexIndex < surface.vertCount; vertexIndex++)
{
const auto& v = surface.verts0[vertexIndex];
XModelVertex vertex{};
vertex.coordinates[0] = v.xyz[0];
vertex.coordinates[1] = v.xyz[1];
vertex.coordinates[2] = v.xyz[2];
Common::Vec3UnpackUnitVec(v.normal, vertex.normal);
Common::Vec4UnpackGfxColor(v.color, vertex.color);
Common::Vec2UnpackTexCoords(v.texCoord, vertex.uv);
out.m_vertices.emplace_back(vertex);
}
}
}
void AllocateXModelBoneWeights(const XModelSurfs* modelSurfs, XModelVertexBoneWeightCollection& weightCollection)
{
auto totalWeightCount = 0u;
for (auto surfIndex = 0u; surfIndex < modelSurfs->numsurfs; surfIndex++)
{
const auto& surface = modelSurfs->surfs[surfIndex];
if (surface.vertList)
{
totalWeightCount += surface.vertListCount;
}
if (surface.vertInfo.vertsBlend)
{
totalWeightCount += surface.vertInfo.vertCount[0] * 1;
totalWeightCount += surface.vertInfo.vertCount[1] * 2;
totalWeightCount += surface.vertInfo.vertCount[2] * 3;
totalWeightCount += surface.vertInfo.vertCount[3] * 4;
}
}
weightCollection.weights.resize(totalWeightCount);
}
float BoneWeight16(const uint16_t value)
{
return static_cast<float>(value) / static_cast<float>(std::numeric_limits<uint16_t>::max());
}
void AddXModelVertexBoneWeights(XModelCommon& out, const XModelSurfs* modelSurfs)
{
auto& weightCollection = out.m_bone_weight_data;
auto weightOffset = 0u;
for (auto surfIndex = 0u; surfIndex < modelSurfs->numsurfs; surfIndex++)
{
const auto& surface = modelSurfs->surfs[surfIndex];
auto handledVertices = 0u;
if (surface.vertList)
{
for (auto vertListIndex = 0u; vertListIndex < surface.vertListCount; vertListIndex++)
{
const auto& vertList = surface.vertList[vertListIndex];
const auto boneWeightOffset = weightOffset;
weightCollection.weights[weightOffset++] =
XModelBoneWeight{.boneIndex = static_cast<unsigned>(vertList.boneOffset / sizeof(DObjSkelMat)), .weight = 1.0f};
for (auto vertListVertexOffset = 0u; vertListVertexOffset < vertList.vertCount; vertListVertexOffset++)
{
out.m_vertex_bone_weights.emplace_back(boneWeightOffset, 1u);
}
handledVertices += vertList.vertCount;
}
}
auto vertsBlendOffset = 0u;
if (surface.vertInfo.vertsBlend)
{
// 1 bone weight
for (auto vertIndex = 0; vertIndex < surface.vertInfo.vertCount[0]; vertIndex++)
{
const auto boneWeightOffset = weightOffset;
const unsigned boneIndex0 = surface.vertInfo.vertsBlend[vertsBlendOffset + 0] / sizeof(DObjSkelMat);
weightCollection.weights[weightOffset++] = XModelBoneWeight{.boneIndex = boneIndex0, .weight = 1.0f};
vertsBlendOffset += 1;
out.m_vertex_bone_weights.emplace_back(boneWeightOffset, 1);
}
// 2 bone weights
for (auto vertIndex = 0; vertIndex < surface.vertInfo.vertCount[1]; vertIndex++)
{
const auto boneWeightOffset = weightOffset;
const unsigned boneIndex0 = surface.vertInfo.vertsBlend[vertsBlendOffset + 0] / sizeof(DObjSkelMat);
const unsigned boneIndex1 = surface.vertInfo.vertsBlend[vertsBlendOffset + 1] / sizeof(DObjSkelMat);
const auto boneWeight1 = BoneWeight16(surface.vertInfo.vertsBlend[vertsBlendOffset + 2]);
const auto boneWeight0 = 1.0f - boneWeight1;
weightCollection.weights[weightOffset++] = XModelBoneWeight{.boneIndex = boneIndex0, .weight = boneWeight0};
weightCollection.weights[weightOffset++] = XModelBoneWeight{.boneIndex = boneIndex1, .weight = boneWeight1};
vertsBlendOffset += 3;
out.m_vertex_bone_weights.emplace_back(boneWeightOffset, 2);
}
// 3 bone weights
for (auto vertIndex = 0; vertIndex < surface.vertInfo.vertCount[2]; vertIndex++)
{
const auto boneWeightOffset = weightOffset;
const unsigned boneIndex0 = surface.vertInfo.vertsBlend[vertsBlendOffset + 0] / sizeof(DObjSkelMat);
const unsigned boneIndex1 = surface.vertInfo.vertsBlend[vertsBlendOffset + 1] / sizeof(DObjSkelMat);
const auto boneWeight1 = BoneWeight16(surface.vertInfo.vertsBlend[vertsBlendOffset + 2]);
const unsigned boneIndex2 = surface.vertInfo.vertsBlend[vertsBlendOffset + 3] / sizeof(DObjSkelMat);
const auto boneWeight2 = BoneWeight16(surface.vertInfo.vertsBlend[vertsBlendOffset + 4]);
const auto boneWeight0 = 1.0f - boneWeight1 - boneWeight2;
weightCollection.weights[weightOffset++] = XModelBoneWeight{.boneIndex = boneIndex0, .weight = boneWeight0};
weightCollection.weights[weightOffset++] = XModelBoneWeight{.boneIndex = boneIndex1, .weight = boneWeight1};
weightCollection.weights[weightOffset++] = XModelBoneWeight{.boneIndex = boneIndex2, .weight = boneWeight2};
vertsBlendOffset += 5;
out.m_vertex_bone_weights.emplace_back(boneWeightOffset, 3);
}
// 4 bone weights
for (auto vertIndex = 0; vertIndex < surface.vertInfo.vertCount[3]; vertIndex++)
{
const auto boneWeightOffset = weightOffset;
const unsigned boneIndex0 = surface.vertInfo.vertsBlend[vertsBlendOffset + 0] / sizeof(DObjSkelMat);
const unsigned boneIndex1 = surface.vertInfo.vertsBlend[vertsBlendOffset + 1] / sizeof(DObjSkelMat);
const auto boneWeight1 = BoneWeight16(surface.vertInfo.vertsBlend[vertsBlendOffset + 2]);
const unsigned boneIndex2 = surface.vertInfo.vertsBlend[vertsBlendOffset + 3] / sizeof(DObjSkelMat);
const auto boneWeight2 = BoneWeight16(surface.vertInfo.vertsBlend[vertsBlendOffset + 4]);
const unsigned boneIndex3 = surface.vertInfo.vertsBlend[vertsBlendOffset + 5] / sizeof(DObjSkelMat);
const auto boneWeight3 = BoneWeight16(surface.vertInfo.vertsBlend[vertsBlendOffset + 6]);
const auto boneWeight0 = 1.0f - boneWeight1 - boneWeight2 - boneWeight3;
weightCollection.weights[weightOffset++] = XModelBoneWeight{.boneIndex = boneIndex0, .weight = boneWeight0};
weightCollection.weights[weightOffset++] = XModelBoneWeight{.boneIndex = boneIndex1, .weight = boneWeight1};
weightCollection.weights[weightOffset++] = XModelBoneWeight{.boneIndex = boneIndex2, .weight = boneWeight2};
weightCollection.weights[weightOffset++] = XModelBoneWeight{.boneIndex = boneIndex3, .weight = boneWeight3};
vertsBlendOffset += 7;
out.m_vertex_bone_weights.emplace_back(boneWeightOffset, 4);
}
handledVertices +=
surface.vertInfo.vertCount[0] + surface.vertInfo.vertCount[1] + surface.vertInfo.vertCount[2] + surface.vertInfo.vertCount[3];
}
for (; handledVertices < surface.vertCount; handledVertices++)
{
out.m_vertex_bone_weights.emplace_back(0, 0);
}
}
}
void AddXModelFaces(XModelCommon& out, const XModelSurfs* modelSurfs)
{
for (auto surfIndex = 0u; surfIndex < modelSurfs->numsurfs; surfIndex++)
{
const auto& surface = modelSurfs->surfs[surfIndex];
auto& object = out.m_objects[surfIndex];
object.m_faces.reserve(surface.triCount);
for (auto triIndex = 0u; triIndex < surface.triCount; triIndex++)
{
const auto& tri = surface.triIndices[triIndex];
XModelFace face{};
face.vertexIndex[0] = tri[0] + surface.baseVertIndex;
face.vertexIndex[1] = tri[1] + surface.baseVertIndex;
face.vertexIndex[2] = tri[2] + surface.baseVertIndex;
object.m_faces.emplace_back(face);
}
}
}
void PopulateXModelWriter(XModelCommon& out, const AssetDumpingContext& context, const unsigned lod, const XModel* model)
{
const auto* modelSurfs = model->lodInfo[lod].modelSurfs;
DistinctMapper<Material*> materialMapper(model->numsurfs);
AllocateXModelBoneWeights(modelSurfs, out.m_bone_weight_data);
out.m_name = modelSurfs->name;
AddXModelBones(out, context, model);
AddXModelMaterials(out, materialMapper, model);
AddXModelObjects(out, modelSurfs, materialMapper, model->lodInfo[lod].surfIndex);
AddXModelVertices(out, modelSurfs);
AddXModelVertexBoneWeights(out, modelSurfs);
AddXModelFaces(out, modelSurfs);
}
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));
if (!mtlFile)
return;
const auto writer = obj::CreateMtlWriter(*mtlFile, context.m_zone.m_game->GetShortName(), context.m_zone.m_name);
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* modelSurfs = model->lodInfo[lod].modelSurfs;
if (modelSurfs->name[0] == ',' || modelSurfs->surfs == nullptr)
return;
const auto assetFile = context.OpenAssetFile(std::format("model_export/{}.obj", modelSurfs->name));
if (!assetFile)
return;
const auto writer = obj::CreateObjWriter(*assetFile, std::format("{}.mtl", model->name), context.m_zone.m_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* modelSurfs = model->lodInfo[lod].modelSurfs;
const auto assetFile = context.OpenAssetFile(std::format("model_export/{}.XMODEL_EXPORT", modelSurfs->name));
if (!assetFile)
return;
const auto writer = xmodel_export::CreateWriterForVersion6(*assetFile, context.m_zone.m_game->GetShortName(), context.m_zone.m_name);
writer->Write(common);
}
void DumpXModelBinLod(const XModelCommon& common, const AssetDumpingContext& context, const XAssetInfo<XModel>* asset, const unsigned lod)
{
const auto* model = asset->Asset();
const auto* modelSurfs = model->lodInfo[lod].modelSurfs;
const auto assetFile = context.OpenAssetFile(std::format("model_export/{}.xmodel_bin", modelSurfs->name));
if (!assetFile)
return;
const auto writer = xmodel_bin::CreateWriterForVersion7(*assetFile, context.m_zone.m_game->GetShortName(), context.m_zone.m_name);
writer->Write(common);
}
template<typename T>
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* modelSurfs = model->lodInfo[lod].modelSurfs;
const auto assetFile = context.OpenAssetFile(std::format("model_export/{}{}", modelSurfs->name, extension));
if (!assetFile)
return;
const auto output = std::make_unique<T>(*assetFile);
const auto writer = gltf::Writer::CreateWriter(output.get(), context.m_zone.m_game->GetShortName(), context.m_zone.m_name);
writer->Write(common);
}
void DumpXModelSurfs(const AssetDumpingContext& context, const XAssetInfo<XModel>* asset)
{
const auto* model = asset->Asset();
for (auto currentLod = 0u; currentLod < model->numLods; currentLod++)
{
XModelCommon common;
PopulateXModelWriter(common, context, currentLod, asset->Asset());
switch (ObjWriting::Configuration.ModelOutputFormat)
{
case ObjWriting::Configuration_t::ModelOutputFormat_e::OBJ:
DumpObjLod(common, context, asset, currentLod);
if (currentLod == 0u)
DumpObjMtl(common, context, asset);
break;
case ObjWriting::Configuration_t::ModelOutputFormat_e::XMODEL_EXPORT:
DumpXModelExportLod(common, context, asset, currentLod);
break;
case ObjWriting::Configuration_t::ModelOutputFormat_e::XMODEL_BIN:
DumpXModelBinLod(common, context, asset, currentLod);
break;
case ObjWriting::Configuration_t::ModelOutputFormat_e::GLTF:
DumpGltfLod<gltf::TextOutput>(common, context, asset, currentLod, ".gltf");
break;
case ObjWriting::Configuration_t::ModelOutputFormat_e::GLB:
DumpGltfLod<gltf::BinOutput>(common, context, asset, currentLod, ".glb");
break;
default:
assert(false);
break;
}
}
}
} // namespace
bool AssetDumperXModel::ShouldDump(XAssetInfo<XModel>* asset)
{
return !asset->m_name.empty() && asset->m_name[0] != ',';
@ -526,5 +11,5 @@ bool AssetDumperXModel::ShouldDump(XAssetInfo<XModel>* asset)
void AssetDumperXModel::DumpAsset(AssetDumpingContext& context, XAssetInfo<XModel>* asset)
{
DumpXModelSurfs(context, asset);
DumpXModel(context, asset);
}

View File

@ -1,4 +1,4 @@
#options GAME (IW5, T5, T6)
#options GAME (IW4, IW5, T5, T6)
#filename "Game/" + GAME + "/XModel/XModelDumper" + GAME + ".cpp"
@ -6,7 +6,10 @@
#set COMMON_HEADER "\"Game/" + GAME + "/Common" + GAME + ".h\""
#set JSON_HEADER "\"Game/" + GAME + "/XModel/JsonXModel" + GAME + ".h\""
#if GAME == "IW5"
#if GAME == "IW4"
#define FEATURE_IW4
#define GAME_LOWER "iw4"
#elif GAME == "IW5"
#define FEATURE_IW5
#define GAME_LOWER "iw5"
#elif GAME == "T5"
@ -66,7 +69,7 @@ namespace
{
MaterialTextureDef* def = &material->textureTable[textureIndex];
#if defined(FEATURE_IW5)
#if defined(FEATURE_IW4) || defined(FEATURE_IW5)
if (def->semantic == TS_COLOR_MAP)
potentialTextureDefs.push_back(def);
#else
@ -155,7 +158,7 @@ namespace
bool GetSurfaces(const XModel* model, const unsigned lod, XSurface*& surfs, unsigned& surfCount)
{
#if defined(FEATURE_IW5)
#if defined(FEATURE_IW4) || defined(FEATURE_IW5)
if (!model->lodInfo[lod].modelSurfs || !model->lodInfo[lod].modelSurfs->surfs)
return false;
@ -722,7 +725,7 @@ namespace
if (xmodel.physPreset && xmodel.physPreset->name)
jXModel.physPreset = AssetName(xmodel.physPreset->name);
#if defined(FEATURE_IW5)
#if defined(FEATURE_IW4) || defined(FEATURE_IW5)
if (xmodel.physCollmap && xmodel.physCollmap->name)
jXModel.physCollmap = AssetName(xmodel.physCollmap->name);
#endif

View File

@ -1,4 +1,4 @@
#options GAME (IW5, T5, T6)
#options GAME (IW4, IW5, T5, T6)
#filename "Game/" + GAME + "/XModel/XModelDumper" + GAME + ".h"

View File

@ -12,7 +12,7 @@ set count parentList numBones - numRootBones;
set reusable quats;
set count quats numBones - numRootBones;
set reusable trans;
set count trans numBones - numRootBones;
set count trans (numBones - numRootBones) * 3;
set reusable partClassification;
set count partClassification numBones;
set reusable baseMat;