metaforce/DataSpec/DNACommon/DeafBabe.cpp

266 lines
9.8 KiB
C++
Raw Normal View History

#include "DeafBabe.hpp"
#include "AROTBuilder.hpp"
#include "DataSpec/DNAMP1/DeafBabe.hpp"
#include "DataSpec/DNAMP2/DeafBabe.hpp"
#include "DataSpec/DNAMP1/DCLN.hpp"
2017-12-29 08:08:12 +00:00
#include "hecl/Blender/Connection.hpp"
#include <cinttypes>
namespace DataSpec
{
template<class DEAFBABE>
2017-12-29 08:08:12 +00:00
void DeafBabeSendToBlender(hecl::blender::PyOutStream& os, const DEAFBABE& db, bool isDcln, atInt32 idx)
{
os << "material_index = []\n"
"col_bm = bmesh.new()\n";
for (const atVec3f& vert : db.verts)
os.format("col_bm.verts.new((%f,%f,%f))\n",
vert.vec[0],
vert.vec[1],
vert.vec[2]);
os << "col_bm.verts.ensure_lookup_table()\n";
int triIdx = 0;
for (const typename DEAFBABE::Triangle& tri : db.triangleEdgeConnections)
{
const typename DEAFBABE::Material& triMat = db.materials[db.triMats[triIdx++]];
const typename DEAFBABE::Edge& edge0 = db.edgeVertConnections[tri.edges[0]];
const typename DEAFBABE::Edge& edge1 = db.edgeVertConnections[tri.edges[1]];
const typename DEAFBABE::Edge& edge2 = db.edgeVertConnections[tri.edges[2]];
if (!edge0.verts[0] && !edge1.verts[0] && !edge2.verts[0])
break;
int vindices[3];
vindices[2] =
(edge1.verts[0] != edge0.verts[0] && edge1.verts[0] != edge0.verts[1]) ?
edge1.verts[0] : edge1.verts[1];
if (triMat.flipFace())
{
vindices[0] = edge0.verts[1];
vindices[1] = edge0.verts[0];
}
else
{
vindices[0] = edge0.verts[0];
vindices[1] = edge0.verts[1];
}
os << "tri_verts = []\n";
os.format("tri_verts.append(col_bm.verts[%u])\n", vindices[0]);
os.format("tri_verts.append(col_bm.verts[%u])\n", vindices[1]);
os.format("tri_verts.append(col_bm.verts[%u])\n", vindices[2]);
os.format("face = col_bm.faces.get(tri_verts)\n"
"if face is None:\n"
" face = col_bm.faces.new(tri_verts)\n"
"else:\n"
" face = face.copy()\n"
" for i in range(3):\n"
" face.verts[i].co = tri_verts[i].co\n"
" col_bm.verts.ensure_lookup_table()\n"
2016-12-28 19:41:43 +00:00
"face.material_index = select_material(0x%016" PRIX64 ")\n"
"face.smooth = False\n"
"\n",
2016-12-28 19:41:43 +00:00
atUint64(triMat.material));
}
db.insertNoClimb(os);
if (isDcln)
os.format("col_mesh = bpy.data.meshes.new('CMESH_%i')\n", idx);
else
os << "col_mesh = bpy.data.meshes.new('CMESH')\n";
os << "col_bm.to_mesh(col_mesh)\n"
"col_mesh_obj = bpy.data.objects.new(col_mesh.name, col_mesh)\n"
"\n"
"for mat_name in material_index:\n"
" mat = material_dict[mat_name]\n"
" col_mesh.materials.append(mat)\n"
"\n"
"bpy.context.scene.objects.link(col_mesh_obj)\n"
2016-12-28 19:41:43 +00:00
"bpy.context.scene.objects.active = col_mesh_obj\n"
"bpy.ops.object.mode_set(mode='EDIT')\n"
"bpy.ops.mesh.tris_convert_to_quads()\n"
2016-12-28 19:41:43 +00:00
"bpy.ops.object.mode_set(mode='OBJECT')\n"
"bpy.context.scene.objects.active = None\n";
if (!isDcln)
os << "col_mesh_obj.layers[1] = True\n"
"col_mesh_obj.layers[0] = False\n";
os << "col_mesh_obj.draw_type = 'SOLID'\n"
"col_mesh_obj.game.physics_type = 'STATIC'\n"
"\n";
}
2017-12-29 08:08:12 +00:00
template void DeafBabeSendToBlender<DNAMP1::DeafBabe>(hecl::blender::PyOutStream& os, const DNAMP1::DeafBabe& db, bool isDcln, atInt32 idx);
template void DeafBabeSendToBlender<DNAMP2::DeafBabe>(hecl::blender::PyOutStream& os, const DNAMP2::DeafBabe& db, bool isDcln, atInt32 idx);
template void DeafBabeSendToBlender<DNAMP1::DCLN::Collision>(hecl::blender::PyOutStream& os, const DNAMP1::DCLN::Collision& db, bool isDcln, atInt32 idx);
template<class DEAFBABE>
2017-10-17 05:51:53 +00:00
static void PopulateAreaFields(DEAFBABE& db,
2017-12-29 08:08:12 +00:00
const hecl::blender::ColMesh& colMesh,
2017-10-17 05:51:53 +00:00
const zeus::CAABox& fullAABB,
std::enable_if_t<std::is_same<DEAFBABE, DNAMP1::DeafBabe>::value ||
std::is_same<DEAFBABE, DNAMP2::DeafBabe>::value, int>* = 0)
{
2017-10-17 05:51:53 +00:00
AROTBuilder builder;
auto octree = builder.buildCol(colMesh, db.rootNodeType);
static_cast<std::unique_ptr<atUint8[]>&>(db.bspTree) = std::move(octree.first);
db.bspSize = octree.second;
2017-10-17 05:51:53 +00:00
db.unk1 = 0x1000000;
2018-02-22 07:24:51 +00:00
size_t dbSize = 0;
db.binarySize(dbSize);
db.length = dbSize - 8;
2017-10-17 05:51:53 +00:00
db.magic = 0xDEAFBABE;
db.version = 3;
db.aabb[0] = fullAABB.min;
db.aabb[1] = fullAABB.max;
}
template<class DEAFBABE>
static void PopulateAreaFields(DEAFBABE& db,
2017-12-29 08:08:12 +00:00
const hecl::blender::ColMesh& colMesh,
2017-10-17 05:51:53 +00:00
const zeus::CAABox& fullAABB,
std::enable_if_t<std::is_same<DEAFBABE, DNAMP1::DCLN::Collision>::value, int>* = 0)
{
db.magic = 0xDEAFBABE;
db.version = 2;
db.memSize = 0;
}
2017-12-16 00:19:15 +00:00
class MaterialPool
{
std::unordered_map<u64, int> m_materials;
public:
template <class M, class V>
int AddOrLookup(const M& mat, V& vec)
{
auto search = m_materials.find(mat.material);
if (search != m_materials.end())
return search->second;
auto idx = int(vec.size());
vec.push_back(mat);
m_materials[mat.material] = idx;
return idx;
}
};
2017-10-17 05:51:53 +00:00
template<class DEAFBABE>
2017-12-29 08:08:12 +00:00
void DeafBabeBuildFromBlender(DEAFBABE& db, const hecl::blender::ColMesh& colMesh)
2017-10-17 05:51:53 +00:00
{
2017-12-29 08:08:12 +00:00
using BlendMat = hecl::blender::ColMesh::Material;
2017-12-16 00:19:15 +00:00
auto MakeMat = [](const BlendMat& mat, bool flipFace) -> typename DEAFBABE::Material
{
2017-12-16 00:19:15 +00:00
typename DEAFBABE::Material dbMat = {};
dbMat.setUnknown(mat.unknown);
dbMat.setSurfaceStone(mat.surfaceStone);
dbMat.setSurfaceMetal(mat.surfaceMetal);
dbMat.setSurfaceGrass(mat.surfaceGrass);
dbMat.setSurfaceIce(mat.surfaceIce);
dbMat.setPillar(mat.pillar);
dbMat.setSurfaceMetalGrating(mat.surfaceMetalGrating);
dbMat.setSurfacePhazon(mat.surfacePhazon);
dbMat.setSurfaceDirt(mat.surfaceDirt);
dbMat.setSurfaceLava(mat.surfaceLava);
dbMat.setSurfaceSPMetal(mat.surfaceSPMetal);
dbMat.setSurfaceStoneRock(mat.surfaceStoneRock);
dbMat.setSurfaceSnow(mat.surfaceSnow);
dbMat.setSurfaceMudSlow(mat.surfaceMudSlow);
dbMat.setSurfaceFabric(mat.surfaceFabric);
dbMat.setHalfPipe(mat.halfPipe);
dbMat.setSurfaceMud(mat.surfaceMud);
dbMat.setSurfaceGlass(mat.surfaceGlass);
dbMat.setUnused3(mat.unused3);
dbMat.setUnused4(mat.unused4);
dbMat.setSurfaceShield(mat.surfaceShield);
dbMat.setSurfaceSand(mat.surfaceSand);
dbMat.setSurfaceMothOrSeedOrganics(mat.surfaceMothOrSeedOrganics);
dbMat.setSurfaceWeb(mat.surfaceWeb);
dbMat.setProjectilePassthrough(mat.projPassthrough);
dbMat.setSolid(mat.solid);
dbMat.setU20(mat.u20);
dbMat.setCameraPassthrough(mat.camPassthrough);
dbMat.setSurfaceWood(mat.surfaceWood);
dbMat.setSurfaceOrganic(mat.surfaceOrganic);
dbMat.setU24(mat.u24);
dbMat.setSurfaceRubber(mat.surfaceRubber);
dbMat.setSeeThrough(mat.seeThrough);
dbMat.setScanPassthrough(mat.scanPassthrough);
dbMat.setAiPassthrough(mat.aiPassthrough);
dbMat.setCeiling(mat.ceiling);
dbMat.setWall(mat.wall);
dbMat.setFloor(mat.floor);
dbMat.setAiBlock(mat.aiBlock);
dbMat.setJumpNotAllowed(mat.jumpNotAllowed);
dbMat.setSpiderBall(mat.spiderBall);
dbMat.setScrewAttackWallJump(mat.screwAttackWallJump);
dbMat.setFlipFace(flipFace);
return dbMat;
};
MaterialPool matPool;
db.materials.reserve(colMesh.materials.size() * 2);
zeus::CAABox fullAABB;
db.verts.reserve(colMesh.verts.size());
db.vertMats.resize(colMesh.verts.size());
for (const auto& vert : colMesh.verts)
{
fullAABB.accumulateBounds(zeus::CVector3f(vert));
db.verts.push_back(vert);
}
db.vertMatsCount = colMesh.verts.size();
db.vertCount = colMesh.verts.size();
db.edgeVertConnections.reserve(colMesh.edges.size());
db.edgeMats.resize(colMesh.edges.size());
for (const auto& edge : colMesh.edges)
{
db.edgeVertConnections.emplace_back();
db.edgeVertConnections.back().verts[0] = edge.verts[0];
db.edgeVertConnections.back().verts[1] = edge.verts[1];
}
db.edgeMatsCount = colMesh.edges.size();
db.edgeVertsCount = colMesh.edges.size();
db.triMats.reserve(colMesh.trianges.size());
db.triangleEdgeConnections.reserve(colMesh.trianges.size());
for (const auto& tri : colMesh.trianges)
{
2017-12-16 00:19:15 +00:00
int triMatIdx = matPool.AddOrLookup(MakeMat(colMesh.materials[tri.matIdx], tri.flip), db.materials);
db.triMats.push_back(triMatIdx);
db.triangleEdgeConnections.emplace_back();
db.triangleEdgeConnections.back().edges[0] = tri.edges[0];
db.triangleEdgeConnections.back().edges[1] = tri.edges[1];
db.triangleEdgeConnections.back().edges[2] = tri.edges[2];
for (int e=0 ; e<3 ; ++e)
{
2017-12-16 00:19:15 +00:00
db.edgeMats[tri.edges[e]] = triMatIdx;
for (int v=0 ; v<2 ; ++v)
2017-12-16 00:19:15 +00:00
db.vertMats[colMesh.edges[e].verts[v]] = triMatIdx;
}
}
db.triMatsCount = colMesh.trianges.size();
db.triangleEdgesCount = colMesh.trianges.size() * 3;
db.materialCount = db.materials.size();
2017-10-17 05:51:53 +00:00
PopulateAreaFields(db, colMesh, fullAABB);
}
2017-12-29 08:08:12 +00:00
template void DeafBabeBuildFromBlender<DNAMP1::DeafBabe>(DNAMP1::DeafBabe& db, const hecl::blender::ColMesh& colMesh);
template void DeafBabeBuildFromBlender<DNAMP2::DeafBabe>(DNAMP2::DeafBabe& db, const hecl::blender::ColMesh& colMesh);
template void DeafBabeBuildFromBlender<DNAMP1::DCLN::Collision>(DNAMP1::DCLN::Collision& db, const hecl::blender::ColMesh& colMesh);
}