port from perforce

This commit is contained in:
2026-04-18 22:31:51 +02:00
commit 8d0ab5b7cc
8409 changed files with 3972376 additions and 0 deletions

View File

@@ -0,0 +1,107 @@
#if _DEBUG
#include "ObjExporter.h"
#include <iostream>
#include <fstream>
void ObjExporter::Export(mt::Mesh* mesh, const std::string& filePath)
{
ObjExporter exporter(mesh);
exporter.Export(filePath);
}
ObjExporter::ObjExporter(mt::Mesh* mesh)
: mesh{mesh}
{
}
void ObjExporter::Export(const std::string& filePath)
{
std::ostringstream builder;
std::ostringstream mtlBuilder;
builder << "mtllib " + replace(filePath, ".obj", ".mtl") << std::endl;
WritePositions(builder);
WriteNormals(builder);
WriteUVs(builder);
WriteFaceGroups(builder, mtlBuilder);
std::ofstream objFile(filePath);
objFile << builder.str();
objFile.close();
std::ofstream mtlFile(replace(filePath, ".obj", ".mtl"));
mtlFile << mtlBuilder.str();
mtlFile.close();
}
void ObjExporter::WriteFaceGroups(std::ostringstream& builder, std::ostringstream& mtlBuilder)
{
auto groups = mesh->GetSubMeshs();
for(const auto& group : groups)
{
auto materialId = group->Key;
builder << "usemtl " << materialId << std::endl;
builder << "g " << materialId << std::endl;
WriteFaces(builder, group->Value);
mtlBuilder << "newmtl " << materialId << std::endl;
mtlBuilder << "Ka 0.0 0.0 0.0" << std::endl;
mtlBuilder << "Kd 1.0 1.0 1.0" << std::endl;
mtlBuilder << "Ks 1.0 1.0 1.0" << std::endl;
mtlBuilder << "Ns 16.0" << std::endl;
mtlBuilder << "illum 0" << std::endl;
mtlBuilder << "map_Kd " << materialId << ".png" << std::endl;
}
}
void ObjExporter::WriteFaces(std::ostringstream& builder, const mt::FaceList& faces)
{
for (auto face : faces)
{
auto indices = face->GetIndices(true);
builder << "f";
for (int i = 0; i < indices.Count(); ++i)
{
builder << " ";
auto index = indices[i] + 1;
builder << index << "/" << index << "/" << index;
}
builder << std::endl;
}
}
void ObjExporter::WritePositions(std::ostringstream& builder)
{
for (const auto& v : mesh->GetVertices())
{
builder << "v " << v->Position.x << " " << v->Position.y << " " << v->Position.z << std::endl;
}
}
void ObjExporter::WriteNormals(std::ostringstream& builder)
{
for (const auto& v : mesh->GetVertices())
{
auto normal = v->GetNormal();
builder << "vn " << normal.x << " " << normal.y << " " << normal.z << std::endl;
}
}
void ObjExporter::WriteUVs(std::ostringstream& builder)
{
for (const auto& v : mesh->GetVertices())
{
builder << "vt " << v->UV[0] << " " << v->UV[1] << std::endl;
}
}
#endif