Add generic XModel Export dumper without bone support yet

This commit is contained in:
Jan
2021-08-13 09:40:12 +02:00
parent 56ebbbcfa8
commit 767daca2ea
28 changed files with 1112 additions and 30 deletions

View File

@ -1,14 +1,21 @@
#include "AssetDumperXModel.h"
#include <cassert>
#include <set>
#include <iomanip>
#include "ObjWriting.h"
#include "Game/IW4/CommonIW4.h"
#include "Math/Quaternion.h"
#include "Model/XModel/XModelExportWriter.h"
#include "Utils/HalfFloat.h"
#include "Utils/QuatInt16.h"
using namespace IW4;
bool AssetDumperXModel::ShouldDump(XAssetInfo<XModel>* asset)
{
return true;
return !asset->m_name.empty() && asset->m_name[0] != ',';
}
void AssetDumperXModel::DumpObjMatMaterial(AssetDumpingContext& context, const Material* material, std::ostream& stream)
@ -20,7 +27,7 @@ void AssetDumperXModel::DumpObjMatMaterial(AssetDumpingContext& context, const M
GfxImage* normalMap = nullptr;
GfxImage* specularMap = nullptr;
for(auto i = 0u; i < material->textureCount; i++)
for (auto i = 0u; i < material->textureCount; i++)
{
const auto& texture = material->textureTable[i];
@ -70,13 +77,13 @@ void AssetDumperXModel::DumpObjMat(AssetDumpingContext& context, XAssetInfo<XMod
std::set<Material*> uniqueMaterials;
for (auto i = 0u; i < model->numsurfs; i++)
{
if(model->materialHandles[i] != nullptr)
if (model->materialHandles[i] != nullptr)
uniqueMaterials.emplace(model->materialHandles[i]);
}
stream << "# Material count: " << uniqueMaterials.size() << "\n";
for(const auto* material : uniqueMaterials)
for (const auto* material : uniqueMaterials)
{
DumpObjMatMaterial(context, material, stream);
}
@ -129,13 +136,13 @@ void AssetDumperXModel::DumpObjLod(AssetDumpingContext& context, XAssetInfo<XMod
const auto* vertex = &surf->verts0[vi];
vec3_t normalVec;
Common::Vec3UnpackUnitVec(vertex->normal, &normalVec);
stream << "vn " << normalVec[0] << " " << normalVec[2] << " " << -normalVec[1] << "\n";
}
stream << "\n";
if(model->numsurfs > i && model->materialHandles && model->materialHandles[i])
if (model->numsurfs > i && model->materialHandles && model->materialHandles[i])
{
stream << "usemtl " << model->materialHandles[i]->info.name << "\n";
}
@ -169,15 +176,190 @@ void AssetDumperXModel::DumpObj(AssetDumpingContext& context, XAssetInfo<XModel>
}
}
void AssetDumperXModel::DumpXModelExportLod(AssetDumpingContext& context, XAssetInfo<XModel>* asset, unsigned lod)
void AssetDumperXModel::AddBonesToWriter(const AssetDumpingContext& context, AbstractXModelWriter& writer, 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 = -1;
else
bone.parentIndex = static_cast<int>(boneNum - static_cast<unsigned int>(model->parentList[boneNum - model->numRootBones]));
bone.scale[0] = 1.0f;
bone.scale[1] = 1.0f;
bone.scale[2] = 1.0f;
if (boneNum < model->numRootBones)
{
bone.offset[0] = 0;
bone.offset[1] = 0;
bone.offset[2] = 0;
bone.rotation = 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(
QuatInt16::ToFloat(model->quats[boneNum - model->numRootBones][0]),
QuatInt16::ToFloat(model->quats[boneNum - model->numRootBones][1]),
QuatInt16::ToFloat(model->quats[boneNum - model->numRootBones][2]),
QuatInt16::ToFloat(model->quats[boneNum - model->numRootBones][3])
);
}
writer.AddBone(std::move(bone));
}
}
void AssetDumperXModel::DumpXModelExport(AssetDumpingContext& context, XAssetInfo<XModel>* asset)
void AssetDumperXModel::AddMaterialsToWriter(AbstractXModelWriter& writer, DistinctMapper<Material*>& materialMapper, const XModel* model)
{
for (auto surfaceMaterialNum = 0; surfaceMaterialNum < model->numsurfs; surfaceMaterialNum++)
{
Material* material = model->materialHandles[surfaceMaterialNum];
if (materialMapper.Add(material))
{
XModelMaterial xMaterial;
xMaterial.ApplyDefaults();
xMaterial.name = material->info.name;
for (auto textureIndex = 0; textureIndex < material->textureCount; textureIndex++)
{
const auto* textureTableEntry = &material->textureTable[textureIndex];
if (textureTableEntry->semantic == TS_COLOR_MAP && textureTableEntry->u.image)
{
xMaterial.colorMapName = textureTableEntry->u.image->name;
break;
}
}
writer.AddMaterial(std::move(xMaterial));
}
}
}
void AssetDumperXModel::AddObjectsToWriter(AbstractXModelWriter& writer, const XModelSurfs* modelSurfs)
{
for (auto surfIndex = 0u; surfIndex < modelSurfs->numsurfs; surfIndex++)
{
XModelObject object;
object.name = "surf" + std::to_string(surfIndex);
writer.AddObject(std::move(object));
}
}
void AssetDumperXModel::AddVerticesToWriter(AbstractXModelWriter& writer, 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];
vec2_t uv;
vec3_t normalVec;
vec4_t color;
Common::Vec2UnpackTexCoords(v.texCoord, &uv);
Common::Vec3UnpackUnitVec(v.normal, &normalVec);
Common::Vec4UnpackGfxColor(v.color, &color);
XModelVertex vertex{};
vertex.coordinates[0] = v.xyz[0];
vertex.coordinates[1] = v.xyz[1];
vertex.coordinates[2] = v.xyz[2];
vertex.normal[0] = normalVec[0];
vertex.normal[1] = normalVec[1];
vertex.normal[2] = normalVec[2];
vertex.color[0] = color[0];
vertex.color[1] = color[1];
vertex.color[2] = color[2];
vertex.color[3] = color[3];
vertex.uv[0] = uv[0];
vertex.uv[1] = uv[1];
writer.AddVertex(vertex);
}
}
}
void AssetDumperXModel::AddFacesToWriter(AbstractXModelWriter& writer, const DistinctMapper<Material*>& materialMapper, const XModelSurfs* modelSurfs,
const int baseSurfaceIndex)
{
for (auto surfIndex = 0u; surfIndex < modelSurfs->numsurfs; surfIndex++)
{
const auto& surface = modelSurfs->surfs[surfIndex];
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;
face.objectIndex = static_cast<int>(surfIndex);
face.materialIndex = static_cast<int>(materialMapper.GetDistinctPositionByInputPosition(surfIndex + baseSurfaceIndex));
writer.AddFace(face);
}
}
}
void AssetDumperXModel::DumpXModelExportLod(const AssetDumpingContext& context, 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("model_export/" + std::string(modelSurfs->name) + ".XMODEL_EXPORT");
if (!assetFile)
return;
const auto writer = XModelExportWriter::CreateWriterForVersion6(context.m_zone->m_game->GetShortName(), context.m_zone->m_name);
DistinctMapper<Material*> materialMapper(model->numsurfs);
AddBonesToWriter(context, *writer, model);
AddMaterialsToWriter(*writer, materialMapper, model);
AddObjectsToWriter(*writer, modelSurfs);
AddVerticesToWriter(*writer, modelSurfs);
AddFacesToWriter(*writer, materialMapper, modelSurfs, model->lodInfo[lod].surfIndex);
writer->Write(*assetFile);
}
void AssetDumperXModel::DumpXModelExport(const AssetDumpingContext& context, XAssetInfo<XModel>* asset)
{
const auto* model = asset->Asset();
for (auto currentLod = 0u; currentLod < model->numLods; currentLod++)
{
DumpXModelExportLod(context, asset, currentLod);
}
}
void AssetDumperXModel::DumpAsset(AssetDumpingContext& context, XAssetInfo<XModel>* asset)
{
DumpObj(context, asset);
switch (ObjWriting::Configuration.ModelOutputFormat)
{
case ObjWriting::Configuration_t::ModelOutputFormat_e::OBJ:
DumpObj(context, asset);
break;
case ObjWriting::Configuration_t::ModelOutputFormat_e::XMODEL_EXPORT:
DumpXModelExport(context, asset);
break;
default:
assert(false);
break;
}
}

View File

@ -4,6 +4,8 @@
#include "Dumping/AbstractAssetDumper.h"
#include "Game/IW4/IW4.h"
#include "Model/XModel/XModelExportWriter.h"
#include "Utils/DistinctMapper.h"
namespace IW4
{
@ -13,8 +15,14 @@ namespace IW4
static void DumpObjMatMaterial(AssetDumpingContext& context, const Material* material, std::ostream& stream);
static void DumpObjMat(AssetDumpingContext& context, XAssetInfo<XModel>* asset);
static void DumpObj(AssetDumpingContext& context, XAssetInfo<XModel>* asset);
static void DumpXModelExportLod(AssetDumpingContext& context, XAssetInfo<XModel>* asset, unsigned lod);
static void DumpXModelExport(AssetDumpingContext& context, XAssetInfo<XModel>* asset);
static void AddBonesToWriter(const AssetDumpingContext& context, AbstractXModelWriter& writer, const XModel* model);
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 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);
protected:
bool ShouldDump(XAssetInfo<XModel>* asset) override;

View File

View File

View File

@ -0,0 +1,29 @@
#include "AbstractXModelWriter.h"
AbstractXModelWriter::AbstractXModelWriter()
= default;
void AbstractXModelWriter::AddObject(XModelObject object)
{
m_objects.emplace_back(std::move(object));
}
void AbstractXModelWriter::AddBone(XModelBone bone)
{
m_bones.emplace_back(std::move(bone));
}
void AbstractXModelWriter::AddMaterial(XModelMaterial material)
{
m_materials.emplace_back(std::move(material));
}
void AbstractXModelWriter::AddVertex(XModelVertex vertex)
{
m_vertices.emplace_back(vertex);
}
void AbstractXModelWriter::AddFace(XModelFace face)
{
m_faces.emplace_back(face);
}

View File

@ -0,0 +1,24 @@
#pragma once
#include <vector>
#include "Model/XModel/XModelCommon.h"
class AbstractXModelWriter
{
protected:
std::vector<XModelObject> m_objects;
std::vector<XModelBone> m_bones;
std::vector<XModelMaterial> m_materials;
std::vector<XModelVertex> m_vertices;
std::vector<XModelFace> m_faces;
public:
AbstractXModelWriter();
void AddObject(XModelObject object);
void AddBone(XModelBone bone);
void AddMaterial(XModelMaterial material);
void AddVertex(XModelVertex vertex);
void AddFace(XModelFace face);
};

View File

@ -0,0 +1,231 @@
#include "XModelExportWriter.h"
#include <iomanip>
#include "Model/VertexMerger.h"
#include "Math/Quaternion.h"
class XModelExportWriterBase : public XModelExportWriter
{
protected:
std::string m_game_name;
std::string m_zone_name;
VertexMerger m_vertex_merger;
void PrepareVertexMerger()
{
m_vertex_merger = VertexMerger(m_vertices.size());
for (const auto& vertex : m_vertices)
{
m_vertex_merger.Add(VertexMergerPos{
vertex.coordinates[0],
vertex.coordinates[1],
vertex.coordinates[2]
});
}
}
void WriteHeader(std::ostream& stream, const int version) const
{
stream << "// OpenAssetTools XMODEL_EXPORT File\n";
stream << "// Game Origin: " << m_game_name << "\n";
stream << "// Zone Origin: " << m_zone_name << "\n";
stream << "MODEL\n";
stream << "VERSION " << version << "\n";
stream << "\n";
}
void WriteBones(std::ostream& stream) const
{
stream << "NUMBONES " << m_bones.size() << "\n";
size_t boneNum = 0u;
for (const auto& bone : m_bones)
{
stream << "BONE " << boneNum << " ";
if (bone.parentIndex < 0)
stream << "-1";
else
stream << bone.parentIndex;
stream << " \"" << bone.name << "\"\n";
boneNum++;
}
stream << "\n";
boneNum = 0u;
for (const auto& bone : m_bones)
{
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 << "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();
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";
stream << "Y " << std::setprecision(6) << std::fixed << mat.m_data[0][1]
<< ", " << std::setprecision(6) << std::fixed << mat.m_data[1][1]
<< ", " << std::setprecision(6) << std::fixed << mat.m_data[2][1] << "\n";
stream << "Z " << std::setprecision(6) << std::fixed << mat.m_data[0][2]
<< ", " << std::setprecision(6) << std::fixed << mat.m_data[1][2]
<< ", " << std::setprecision(6) << std::fixed << mat.m_data[2][2] << "\n";
stream << "\n";
boneNum++;
}
}
XModelExportWriterBase(std::string gameName, std::string zoneName)
: m_game_name(std::move(gameName)),
m_zone_name(std::move(zoneName))
{
}
};
class XModelExportWriter6 final : public XModelExportWriterBase
{
void WriteVertices(std::ostream& stream) const
{
const auto& distinctVertexValues = m_vertex_merger.GetDistinctValues();
stream << "NUMVERTS " << distinctVertexValues.size() << "\n";
size_t vertexNum = 0u;
for (const auto& vertexPos : distinctVertexValues)
{
stream << "VERT " << vertexNum << "\n";
stream << "OFFSET ";
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 << "\n";
vertexNum++;
}
}
static void WriteFaceVertex(std::ostream& stream, const size_t index, const XModelVertex& vertex)
{
stream << "VERT " << index << "\n";
stream << "NORMAL " << std::setprecision(6) << std::fixed << vertex.normal[0]
<< " " << std::setprecision(6) << std::fixed << vertex.normal[1]
<< " " << std::setprecision(6) << std::fixed << vertex.normal[2] << "\n";
stream << "COLOR " << std::setprecision(6) << std::fixed << vertex.color[0]
<< " " << std::setprecision(6) << std::fixed << vertex.color[1]
<< " " << std::setprecision(6) << std::fixed << vertex.color[2]
<< " " << std::setprecision(6) << std::fixed << vertex.color[3] << "\n";
stream << "UV 1 " << std::setprecision(6) << std::fixed << vertex.uv[0]
<< " " << std::setprecision(6) << std::fixed << vertex.uv[1] << "\n";
}
void WriteFaces(std::ostream& stream) const
{
stream << "NUMFACES " << m_faces.size() << "\n";
for (const auto& face : m_faces)
{
const size_t distinctPositions[3]
{
m_vertex_merger.GetDistinctPositionByInputPosition(face.vertexIndex[0]),
m_vertex_merger.GetDistinctPositionByInputPosition(face.vertexIndex[1]),
m_vertex_merger.GetDistinctPositionByInputPosition(face.vertexIndex[2])
};
const XModelVertex& v0 = m_vertices[face.vertexIndex[0]];
const XModelVertex& v1 = m_vertices[face.vertexIndex[1]];
const XModelVertex& v2 = m_vertices[face.vertexIndex[2]];
stream << "TRI " << face.objectIndex << " " << face.materialIndex << " 0 0\n";
WriteFaceVertex(stream, distinctPositions[0], v0);
WriteFaceVertex(stream, distinctPositions[1], v1);
WriteFaceVertex(stream, distinctPositions[2], v2);
stream << "\n";
}
}
void WriteObjects(std::ostream& stream) const
{
stream << "NUMOBJECTS " << m_objects.size() << "\n";
size_t objectNum = 0u;
for (const auto& object : m_objects)
{
stream << "OBJECT " << objectNum << " \"" << object.name << "\"\n";
objectNum++;
}
stream << "\n";
}
void WriteMaterials(std::ostream& stream) const
{
stream << "NUMMATERIALS " << m_materials.size() << "\n";
size_t materialNum = 0u;
for (const auto& material : m_materials)
{
stream << "MATERIAL " << materialNum << " \"" << material.name << "\" \"" << material.materialTypeName << "\" \"" << material.colorMapName << "\"\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]
<< " " << std::setprecision(6) << std::fixed << material.color[3] << "\n";
stream << "TRANSPARENCY " << std::setprecision(6) << std::fixed << material.transparency[0]
<< " " << std::setprecision(6) << std::fixed << material.transparency[1]
<< " " << std::setprecision(6) << std::fixed << material.transparency[2]
<< " " << std::setprecision(6) << std::fixed << material.transparency[3] << "\n";
stream << "AMBIENTCOLOR " << std::setprecision(6) << std::fixed << material.ambientColor[0]
<< " " << std::setprecision(6) << std::fixed << material.ambientColor[1]
<< " " << std::setprecision(6) << std::fixed << material.ambientColor[2]
<< " " << std::setprecision(6) << std::fixed << material.ambientColor[3] << "\n";
stream << "INCANDESCENCE " << std::setprecision(6) << std::fixed << material.incandescence[0]
<< " " << std::setprecision(6) << std::fixed << material.incandescence[1]
<< " " << std::setprecision(6) << std::fixed << material.incandescence[2]
<< " " << std::setprecision(6) << std::fixed << material.incandescence[3] << "\n";
stream << "COEFFS " << std::setprecision(6) << std::fixed << material.coeffs[0]
<< " " << std::setprecision(6) << std::fixed << material.coeffs[1] << "\n";
stream << "GLOW " << std::setprecision(6) << std::fixed << material.glow.x
<< " " << material.glow.y << "\n";
stream << "REFRACTIVE " << material.refractive.x
<< " " << std::setprecision(6) << std::fixed << material.refractive.y << "\n";
stream << "SPECULARCOLOR " << std::setprecision(6) << std::fixed << material.specularColor[0]
<< " " << std::setprecision(6) << std::fixed << material.specularColor[1]
<< " " << std::setprecision(6) << std::fixed << material.specularColor[2]
<< " " << std::setprecision(6) << std::fixed << material.specularColor[3] << "\n";
stream << "REFLECTIVECOLOR " << std::setprecision(6) << std::fixed << material.reflectiveColor[0]
<< " " << std::setprecision(6) << std::fixed << material.reflectiveColor[1]
<< " " << std::setprecision(6) << std::fixed << material.reflectiveColor[2]
<< " " << std::setprecision(6) << std::fixed << material.reflectiveColor[3] << "\n";
stream << "REFLECTIVE " << material.reflective.x
<< " " << std::setprecision(6) << std::fixed << material.reflective.y << "\n";
stream << "BLINN " << std::setprecision(6) << std::fixed << material.blinn[0]
<< " " << std::setprecision(6) << std::fixed << material.blinn[1] << "\n";
stream << "PHONG " << std::setprecision(6) << std::fixed << material.phong << "\n";
stream << "\n";
materialNum++;
}
}
public:
XModelExportWriter6(std::string gameName, std::string zoneName)
: XModelExportWriterBase(std::move(gameName), std::move(zoneName))
{
}
void Write(std::ostream& stream) override
{
PrepareVertexMerger();
WriteHeader(stream, 6);
WriteBones(stream);
WriteVertices(stream);
WriteFaces(stream);
WriteObjects(stream);
WriteMaterials(stream);
}
};
std::unique_ptr<XModelExportWriter> XModelExportWriter::CreateWriterForVersion6(std::string gameName, std::string zoneName)
{
return std::make_unique<XModelExportWriter6>(std::move(gameName), std::move(zoneName));
}

View File

@ -0,0 +1,21 @@
#pragma once
#include <ostream>
#include <memory>
#include "AbstractXModelWriter.h"
class XModelExportWriter : public AbstractXModelWriter
{
public:
XModelExportWriter() = default;
virtual ~XModelExportWriter() = default;
XModelExportWriter(const XModelExportWriter& other) = default;
XModelExportWriter(XModelExportWriter&& other) noexcept = default;
XModelExportWriter& operator=(const XModelExportWriter& other) = default;
XModelExportWriter& operator=(XModelExportWriter&& other) noexcept = default;
virtual void Write(std::ostream& stream) = 0;
static std::unique_ptr<XModelExportWriter> CreateWriterForVersion6(std::string gameName, std::string zoneName);
};