Export vertex weights for xmodel export

This commit is contained in:
Jan
2021-08-13 17:33:13 +02:00
parent 767daca2ea
commit 0478a88d15
10 changed files with 344 additions and 134 deletions

View File

@ -195,19 +195,24 @@ void AssetDumperXModel::AddBonesToWriter(const AssetDumpingContext& context, Abs
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 = Quaternion32(model->baseMat[boneNum].quat[0], model->baseMat[boneNum].quat[1], model->baseMat[boneNum].quat[2], model->baseMat[boneNum].quat[3]);
if (boneNum < model->numRootBones)
{
bone.offset[0] = 0;
bone.offset[1] = 0;
bone.offset[2] = 0;
bone.rotation = Quaternion32(0, 0, 0, 1);
bone.localOffset[0] = 0;
bone.localOffset[1] = 0;
bone.localOffset[2] = 0;
bone.localRotation = Quaternion32(0, 0, 0, 1);
}
else
{
bone.offset[0] = model->trans[boneNum - model->numRootBones][0];
bone.offset[1] = model->trans[boneNum - model->numRootBones][1];
bone.offset[2] = model->trans[boneNum - model->numRootBones][2];
bone.rotation = Quaternion32(
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 = Quaternion32(
QuatInt16::ToFloat(model->quats[boneNum - model->numRootBones][0]),
QuatInt16::ToFloat(model->quats[boneNum - model->numRootBones][1]),
QuatInt16::ToFloat(model->quats[boneNum - model->numRootBones][2]),
@ -262,6 +267,7 @@ void AssetDumperXModel::AddVerticesToWriter(AbstractXModelWriter& writer, const
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];
@ -286,11 +292,198 @@ void AssetDumperXModel::AddVerticesToWriter(AbstractXModelWriter& writer, const
vertex.color[3] = color[3];
vertex.uv[0] = uv[0];
vertex.uv[1] = uv[1];
writer.AddVertex(vertex);
}
}
}
void AssetDumperXModel::AllocateBoneWeights(const XModelSurfs* modelSurfs, XModelVertexBoneWeightCollection& weightCollection)
{
weightCollection.totalWeightCount = 0u;
for (auto surfIndex = 0u; surfIndex < modelSurfs->numsurfs; surfIndex++)
{
const auto& surface = modelSurfs->surfs[surfIndex];
if (surface.vertList)
{
weightCollection.totalWeightCount += surface.vertListCount;
}
if (surface.vertInfo.vertsBlend)
{
weightCollection.totalWeightCount += surface.vertInfo.vertCount[0] * 1;
weightCollection.totalWeightCount += surface.vertInfo.vertCount[1] * 2;
weightCollection.totalWeightCount += surface.vertInfo.vertCount[2] * 3;
weightCollection.totalWeightCount += surface.vertInfo.vertCount[3] * 4;
}
}
weightCollection.weights = std::make_unique<XModelBoneWeight[]>(weightCollection.totalWeightCount);
}
void AssetDumperXModel::AddVertexBoneWeights(AbstractXModelWriter& writer, const XModelSurfs* modelSurfs, XModelVertexBoneWeightCollection& weightCollection)
{
size_t 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 = &weightCollection.weights[weightOffset];
weightCollection.weights[weightOffset++] = XModelBoneWeight{
static_cast<int>(vertList.boneOffset / sizeof(DObjSkelMat)),
1.0f
};
for (auto vertListVertexOffset = 0u; vertListVertexOffset < vertList.vertCount; vertListVertexOffset++)
{
writer.AddVertexBoneWeights(XModelVertexBoneWeights{
boneWeightOffset,
1
});
}
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 = &weightCollection.weights[weightOffset];
const auto boneIndex0 = static_cast<int>(surface.vertInfo.vertsBlend[vertsBlendOffset + 0] / sizeof(DObjSkelMat));
weightCollection.weights[weightOffset++] = XModelBoneWeight{
boneIndex0,
1.0f
};
vertsBlendOffset += 1;
writer.AddVertexBoneWeights(XModelVertexBoneWeights{
boneWeightOffset,
1
});
}
// 2 bone weights
for (auto vertIndex = 0; vertIndex < surface.vertInfo.vertCount[1]; vertIndex++)
{
const auto* boneWeightOffset = &weightCollection.weights[weightOffset];
const auto boneIndex0 = static_cast<int>(surface.vertInfo.vertsBlend[vertsBlendOffset + 0] / sizeof(DObjSkelMat));
const auto boneIndex1 = static_cast<int>(surface.vertInfo.vertsBlend[vertsBlendOffset + 1] / sizeof(DObjSkelMat));
const auto boneWeight1 = HalfFloat::ToFloat(surface.vertInfo.vertsBlend[vertsBlendOffset + 2]);
const auto boneWeight0 = 1.0f - boneWeight1;
weightCollection.weights[weightOffset++] = XModelBoneWeight{
boneIndex0,
boneWeight0
};
weightCollection.weights[weightOffset++] = XModelBoneWeight{
boneIndex1,
boneWeight1
};
vertsBlendOffset += 3;
writer.AddVertexBoneWeights(XModelVertexBoneWeights{
boneWeightOffset,
2
});
}
// 3 bone weights
for (auto vertIndex = 0; vertIndex < surface.vertInfo.vertCount[2]; vertIndex++)
{
const auto* boneWeightOffset = &weightCollection.weights[weightOffset];
const auto boneIndex0 = static_cast<int>(surface.vertInfo.vertsBlend[vertsBlendOffset + 0] / sizeof(DObjSkelMat));
const auto boneIndex1 = static_cast<int>(surface.vertInfo.vertsBlend[vertsBlendOffset + 1] / sizeof(DObjSkelMat));
const auto boneWeight1 = HalfFloat::ToFloat(surface.vertInfo.vertsBlend[vertsBlendOffset + 2]);
const auto boneIndex2 = static_cast<int>(surface.vertInfo.vertsBlend[vertsBlendOffset + 3] / sizeof(DObjSkelMat));
const auto boneWeight2 = HalfFloat::ToFloat(surface.vertInfo.vertsBlend[vertsBlendOffset + 4]);
const auto boneWeight0 = 1.0f - boneWeight1 - boneWeight2;
weightCollection.weights[weightOffset++] = XModelBoneWeight{
boneIndex0,
boneWeight0
};
weightCollection.weights[weightOffset++] = XModelBoneWeight{
boneIndex1,
boneWeight1
};
weightCollection.weights[weightOffset++] = XModelBoneWeight{
boneIndex2,
boneWeight2
};
vertsBlendOffset += 5;
writer.AddVertexBoneWeights(XModelVertexBoneWeights{
boneWeightOffset,
3
});
}
// 4 bone weights
for (auto vertIndex = 0; vertIndex < surface.vertInfo.vertCount[3]; vertIndex++)
{
const auto* boneWeightOffset = &weightCollection.weights[weightOffset];
const auto boneIndex0 = static_cast<int>(surface.vertInfo.vertsBlend[vertsBlendOffset + 0] / sizeof(DObjSkelMat));
const auto boneIndex1 = static_cast<int>(surface.vertInfo.vertsBlend[vertsBlendOffset + 1] / sizeof(DObjSkelMat));
const auto boneWeight1 = HalfFloat::ToFloat(surface.vertInfo.vertsBlend[vertsBlendOffset + 2]);
const auto boneIndex2 = static_cast<int>(surface.vertInfo.vertsBlend[vertsBlendOffset + 3] / sizeof(DObjSkelMat));
const auto boneWeight2 = HalfFloat::ToFloat(surface.vertInfo.vertsBlend[vertsBlendOffset + 4]);
const auto boneIndex3 = static_cast<int>(surface.vertInfo.vertsBlend[vertsBlendOffset + 5] / sizeof(DObjSkelMat));
const auto boneWeight3 = HalfFloat::ToFloat(surface.vertInfo.vertsBlend[vertsBlendOffset + 6]);
const auto boneWeight0 = 1.0f - boneWeight1 - boneWeight2 - boneWeight3;
weightCollection.weights[weightOffset++] = XModelBoneWeight{
boneIndex0,
boneWeight0
};
weightCollection.weights[weightOffset++] = XModelBoneWeight{
boneIndex1,
boneWeight1
};
weightCollection.weights[weightOffset++] = XModelBoneWeight{
boneIndex2,
boneWeight2
};
weightCollection.weights[weightOffset++] = XModelBoneWeight{
boneIndex3,
boneWeight3
};
vertsBlendOffset += 7;
writer.AddVertexBoneWeights(XModelVertexBoneWeights{
boneWeightOffset,
4
});
}
handledVertices += surface.vertInfo.vertCount[0] + surface.vertInfo.vertCount[1] + surface.vertInfo.vertCount[2] + surface.vertInfo.vertCount[3];
}
for (; handledVertices < surface.vertCount; handledVertices++)
{
writer.AddVertexBoneWeights(XModelVertexBoneWeights{
nullptr,
0
});
}
}
}
void AssetDumperXModel::AddFacesToWriter(AbstractXModelWriter& writer, const DistinctMapper<Material*>& materialMapper, const XModelSurfs* modelSurfs,
const int baseSurfaceIndex)
{
@ -327,11 +520,14 @@ void AssetDumperXModel::DumpXModelExportLod(const AssetDumpingContext& context,
const auto writer = XModelExportWriter::CreateWriterForVersion6(context.m_zone->m_game->GetShortName(), context.m_zone->m_name);
DistinctMapper<Material*> materialMapper(model->numsurfs);
XModelVertexBoneWeightCollection boneWeightCollection;
AllocateBoneWeights(modelSurfs, boneWeightCollection);
AddBonesToWriter(context, *writer, model);
AddMaterialsToWriter(*writer, materialMapper, model);
AddObjectsToWriter(*writer, modelSurfs);
AddVerticesToWriter(*writer, modelSurfs);
AddVertexBoneWeights(*writer, modelSurfs, boneWeightCollection);
AddFacesToWriter(*writer, materialMapper, modelSurfs, model->lodInfo[lod].surfIndex);
writer->Write(*assetFile);

View File

@ -20,6 +20,8 @@ namespace IW4
static void AddMaterialsToWriter(AbstractXModelWriter& writer, DistinctMapper<Material*>& materialMapper, const XModel* model);
static void AddObjectsToWriter(AbstractXModelWriter& writer, const XModelSurfs* modelSurfs);
static void AddVerticesToWriter(AbstractXModelWriter& writer, const XModelSurfs* modelSurfs);
static void AllocateBoneWeights(const XModelSurfs* modelSurfs, XModelVertexBoneWeightCollection& weightCollection);
static void AddVertexBoneWeights(AbstractXModelWriter& writer, const XModelSurfs* modelSurfs, XModelVertexBoneWeightCollection& weightCollection);
static void AddFacesToWriter(AbstractXModelWriter& writer, const DistinctMapper<Material*>& materialMapper, const XModelSurfs* modelSurfs, int baseSurfaceIndex);
static void DumpXModelExportLod(const AssetDumpingContext& context, XAssetInfo<XModel>* asset, const unsigned lod);
static void DumpXModelExport(const AssetDumpingContext& context, XAssetInfo<XModel>* asset);

View File

@ -23,6 +23,11 @@ void AbstractXModelWriter::AddVertex(XModelVertex vertex)
m_vertices.emplace_back(vertex);
}
void AbstractXModelWriter::AddVertexBoneWeights(XModelVertexBoneWeights vertexBoneWeights)
{
m_vertex_bone_weights.emplace_back(vertexBoneWeights);
}
void AbstractXModelWriter::AddFace(XModelFace face)
{
m_faces.emplace_back(face);

View File

@ -11,6 +11,7 @@ protected:
std::vector<XModelBone> m_bones;
std::vector<XModelMaterial> m_materials;
std::vector<XModelVertex> m_vertices;
std::vector<XModelVertexBoneWeights> m_vertex_bone_weights;
std::vector<XModelFace> m_faces;
public:
@ -20,5 +21,6 @@ public:
void AddBone(XModelBone bone);
void AddMaterial(XModelMaterial material);
void AddVertex(XModelVertex vertex);
void AddVertexBoneWeights(XModelVertexBoneWeights vertexBoneWeights);
void AddFace(XModelFace face);
};

View File

@ -1,8 +1,8 @@
#include "XModelExportWriter.h"
#include <iomanip>
#include <iostream>
#include "Model/VertexMerger.h"
#include "Math/Quaternion.h"
class XModelExportWriterBase : public XModelExportWriter
@ -16,13 +16,27 @@ protected:
void PrepareVertexMerger()
{
m_vertex_merger = VertexMerger(m_vertices.size());
auto vertexOffset = 0u;
for (const auto& vertex : m_vertices)
{
XModelVertexBoneWeights weights{
nullptr,
0
};
if(vertexOffset < m_vertex_bone_weights.size())
weights = m_vertex_bone_weights[vertexOffset];
m_vertex_merger.Add(VertexMergerPos{
vertex.coordinates[0],
vertex.coordinates[1],
vertex.coordinates[2]
vertex.coordinates[2],
weights.weights,
weights.weightCount
});
vertexOffset++;
}
}
@ -58,16 +72,16 @@ protected:
{
stream << "BONE " << boneNum << "\n";
stream << "OFFSET ";
stream << std::setprecision(6) << std::fixed << bone.offset[0]
<< ", " << std::setprecision(6) << std::fixed << bone.offset[1]
<< ", " << std::setprecision(6) << std::fixed << bone.offset[2] << "\n";
stream << std::setprecision(6) << std::fixed << bone.globalOffset[0]
<< ", " << std::setprecision(6) << std::fixed << bone.globalOffset[1]
<< ", " << std::setprecision(6) << std::fixed << bone.globalOffset[2] << "\n";
stream << "SCALE ";
stream << std::setprecision(6) << std::fixed << bone.scale[0]
<< ", " << std::setprecision(6) << std::fixed << bone.scale[1]
<< ", " << std::setprecision(6) << std::fixed << bone.scale[2] << "\n";
const Matrix32 mat = bone.rotation.ToMatrix();
const Matrix32 mat = bone.globalRotation.ToMatrix();
stream << "X " << std::setprecision(6) << std::fixed << mat.m_data[0][0]
<< ", " << std::setprecision(6) << std::fixed << mat.m_data[1][0]
<< ", " << std::setprecision(6) << std::fixed << mat.m_data[2][0] << "\n";
@ -103,8 +117,13 @@ class XModelExportWriter6 final : public XModelExportWriterBase
stream << std::setprecision(6) << std::fixed << vertexPos.x
<< ", " << std::setprecision(6) << std::fixed << vertexPos.y
<< ", " << std::setprecision(6) << std::fixed << vertexPos.z << "\n";
stream << "BONES 1\n"; // TODO: FIXME with bone weights
stream << "BONE 0 1.000000\n"; // TODO: FIXME with bone weights
stream << "BONES " << vertexPos.weightCount << "\n";
for (auto weightIndex = 0u; weightIndex < vertexPos.weightCount; weightIndex++)
{
stream << "BONE " << vertexPos.weights[weightIndex].boneIndex
<< " " << std::setprecision(6) << std::fixed << vertexPos.weights[weightIndex].weight << "\n";
}
stream << "\n";
vertexNum++;
}
@ -166,7 +185,8 @@ class XModelExportWriter6 final : public XModelExportWriterBase
size_t materialNum = 0u;
for (const auto& material : m_materials)
{
stream << "MATERIAL " << materialNum << " \"" << material.name << "\" \"" << material.materialTypeName << "\" \"" << material.colorMapName << "\"\n";
const auto colorMapPath = "../images/" + material.colorMapName + ".dds";
stream << "MATERIAL " << materialNum << " \"" << material.name << "\" \"" << material.materialTypeName << "\" \"" << colorMapPath << "\"\n";
stream << "COLOR " << std::setprecision(6) << std::fixed << material.color[0]
<< " " << std::setprecision(6) << std::fixed << material.color[1]
<< " " << std::setprecision(6) << std::fixed << material.color[2]