MP2 MREA geometry

This commit is contained in:
Jack Andersen 2015-09-08 14:49:20 -10:00
parent c55a3e0ac1
commit 850fcd2747
8 changed files with 488 additions and 93 deletions

View File

@ -666,7 +666,7 @@ void _ConstructMaterial(Stream& out,
char a_regs[4][64] = {"ONE", "ONE", "ONE", "ONE"};
/* Has Lightmap? */
if (material.tevStages[0].colorInB() == GX::CC_C1)
if (material.flags.lightmap() && material.tevStages[0].colorInB() == GX::CC_C1)
{
if (material.tevStageTexInfo[0].texSlot != 0xff)
out << "new_material.retro_lightmap = tex_maps[0].name\n"

View File

@ -0,0 +1,106 @@
#include "MREA.hpp"
namespace Retro
{
namespace DNAMP1
{
bool MREA::Extract(const SpecBase& dataSpec,
PAKEntryReadStream& rs,
const HECL::ProjectPath& outPath,
PAKRouter<PAKBridge>& pakRouter,
const PAK::Entry& entry,
bool,
std::function<void(const HECL::SystemChar*)>)
{
using RigPair = std::pair<CSKR*, CINF*>;
RigPair dummy(nullptr, nullptr);
/* Do extract */
Header head;
head.read(rs);
rs.seekAlign32();
HECL::BlenderConnection& conn = HECL::BlenderConnection::SharedConnection();
if (!conn.createBlend(outPath.getWithExtension(_S(".blend")).getAbsolutePath()))
return false;
/* Open Py Stream and read sections */
HECL::BlenderConnection::PyOutStream os = conn.beginPythonOut(true);
os.format("import bpy\n"
"import bmesh\n"
"\n"
"bpy.context.scene.name = '%s'\n"
"bpy.context.scene.hecl_type = 'AREA'\n",
pakRouter.getBestEntryName(entry).c_str());
DNACMDL::InitGeomBlenderContext(os, dataSpec.getMasterShaderPath());
MaterialSet::RegisterMaterialProps(os);
os << "# Clear Scene\n"
"for ob in bpy.data.objects:\n"
" bpy.context.scene.objects.unlink(ob)\n"
" bpy.data.objects.remove(ob)\n"
"bpy.types.Object.retro_disable_enviro_visor = bpy.props.BoolProperty(name='Retro: Disable in Combat/Scan Visor')\n"
"bpy.types.Object.retro_disable_thermal_visor = bpy.props.BoolProperty(name='Retro: Disable in Thermal Visor')\n"
"bpy.types.Object.retro_disable_xray_visor = bpy.props.BoolProperty(name='Retro: Disable in X-Ray Visor')\n"
"bpy.types.Object.retro_thermal_level = bpy.props.EnumProperty(items=[('COOL', 'Cool', 'Cool Temperature'),"
"('HOT', 'Hot', 'Hot Temperature'),"
"('WARM', 'Warm', 'Warm Temperature')],"
"name='Retro: Thermal Visor Level')\n"
"\n";
/* One shared material set for all meshes */
os << "# Materials\n"
"materials = []\n"
"\n";
MaterialSet matSet;
atUint64 secStart = rs.position();
matSet.read(rs);
matSet.readToBlender(os, pakRouter, entry, 0, dataSpec);
rs.seek(secStart + head.secSizes[0], Athena::Begin);
std::vector<DNACMDL::VertexAttributes> vertAttribs;
DNACMDL::GetVertexAttributes(matSet, vertAttribs);
/* Read meshes */
atUint32 curSec = 1;
for (int m=0 ; m<head.meshCount ; ++m)
{
MeshHeader mHeader;
secStart = rs.position();
mHeader.read(rs);
rs.seek(secStart + head.secSizes[curSec++], Athena::Begin);
curSec += DNACMDL::ReadGeomSectionsToBlender<PAKRouter<PAKBridge>, MaterialSet, RigPair>
(os, rs, pakRouter, entry, dataSpec, dummy, true,
true, vertAttribs, m, head.secCount, 0, &head.secSizes[curSec]);
os.format("obj.retro_disable_enviro_visor = %s\n"
"obj.retro_disable_thermal_visor = %s\n"
"obj.retro_disable_xray_visor = %s\n"
"obj.retro_thermal_level = '%s'\n",
mHeader.visorFlags.disableEnviro() ? "True" : "False",
mHeader.visorFlags.disableThermal() ? "True" : "False",
mHeader.visorFlags.disableXray() ? "True" : "False",
mHeader.visorFlags.thermalLevelStr());
}
/* Origins to center of mass */
os << "bpy.ops.object.select_by_type(type='MESH')\n"
"bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS')\n"
"bpy.ops.object.select_all(action='DESELECT')\n";
/* Center view */
os << "bpy.context.user_preferences.view.smooth_view = 0\n"
"for window in bpy.context.window_manager.windows:\n"
" screen = window.screen\n"
" for area in screen.areas:\n"
" if area.type == 'VIEW_3D':\n"
" for region in area.regions:\n"
" if region.type == 'WINDOW':\n"
" override = {'scene': bpy.context.scene, 'window': window, 'screen': screen, 'area': area, 'region': region}\n"
" bpy.ops.view3d.view_all(override)\n"
" break\n";
os.close();
return conn.saveBlend();
}
}
}

View File

@ -75,96 +75,7 @@ struct MREA
PAKRouter<PAKBridge>& pakRouter,
const PAK::Entry& entry,
bool,
std::function<void(const HECL::SystemChar*)>)
{
using RigPair = std::pair<CSKR*, CINF*>;
RigPair dummy(nullptr, nullptr);
/* Do extract */
Header head;
head.read(rs);
rs.seekAlign32();
HECL::BlenderConnection& conn = HECL::BlenderConnection::SharedConnection();
if (!conn.createBlend(outPath.getWithExtension(_S(".blend")).getAbsolutePath()))
return false;
/* Open Py Stream and read sections */
HECL::BlenderConnection::PyOutStream os = conn.beginPythonOut(true);
os.format("import bpy\n"
"import bmesh\n"
"\n"
"bpy.context.scene.name = '%s'\n"
"bpy.context.scene.hecl_type = 'AREA'\n",
pakRouter.getBestEntryName(entry).c_str());
DNACMDL::InitGeomBlenderContext(os, dataSpec.getMasterShaderPath());
MaterialSet::RegisterMaterialProps(os);
os << "# Clear Scene\n"
"for ob in bpy.data.objects:\n"
" bpy.context.scene.objects.unlink(ob)\n"
" bpy.data.objects.remove(ob)\n"
"bpy.types.Object.retro_disable_enviro_visor = bpy.props.BoolProperty(name='Retro: Disable in Combat/Scan Visor')\n"
"bpy.types.Object.retro_disable_thermal_visor = bpy.props.BoolProperty(name='Retro: Disable in Thermal Visor')\n"
"bpy.types.Object.retro_disable_xray_visor = bpy.props.BoolProperty(name='Retro: Disable in X-Ray Visor')\n"
"bpy.types.Object.retro_thermal_level = bpy.props.EnumProperty(items=[('COOL', 'Cool', 'Cool Temperature'),"
"('HOT', 'Hot', 'Hot Temperature'),"
"('WARM', 'Warm', 'Warm Temperature')],"
"name='Retro: Thermal Visor Level')\n"
"\n";
/* One shared material set for all meshes */
os << "# Materials\n"
"materials = []\n"
"\n";
MaterialSet matSet;
atUint64 secStart = rs.position();
matSet.read(rs);
matSet.readToBlender(os, pakRouter, entry, 0, dataSpec);
rs.seek(secStart + head.secSizes[0], Athena::Begin);
std::vector<DNACMDL::VertexAttributes> vertAttribs;
DNACMDL::GetVertexAttributes(matSet, vertAttribs);
/* Read meshes */
atUint32 curSec = 1;
for (int m=0 ; m<head.meshCount ; ++m)
{
MeshHeader mHeader;
secStart = rs.position();
mHeader.read(rs);
rs.seek(secStart + head.secSizes[curSec++], Athena::Begin);
curSec += DNACMDL::ReadGeomSectionsToBlender<PAKRouter<PAKBridge>, MaterialSet, RigPair>
(os, rs, pakRouter, entry, dataSpec, dummy, true,
true, vertAttribs, m, head.secCount, 0, &head.secSizes[curSec]);
os.format("obj.retro_disable_enviro_visor = %s\n"
"obj.retro_disable_thermal_visor = %s\n"
"obj.retro_disable_xray_visor = %s\n"
"obj.retro_thermal_level = '%s'\n",
mHeader.visorFlags.disableEnviro() ? "True" : "False",
mHeader.visorFlags.disableThermal() ? "True" : "False",
mHeader.visorFlags.disableXray() ? "True" : "False",
mHeader.visorFlags.thermalLevelStr());
}
/* Origins to center of mass */
os << "bpy.ops.object.select_by_type(type='MESH')\n"
"bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS')\n"
"bpy.ops.object.select_all(action='DESELECT')\n";
/* Center view */
os << "bpy.context.user_preferences.view.smooth_view = 0\n"
"for window in bpy.context.window_manager.windows:\n"
" screen = window.screen\n"
" for area in screen.areas:\n"
" if area.type == 'VIEW_3D':\n"
" for region in area.regions:\n"
" if region.type == 'WINDOW':\n"
" override = {'scene': bpy.context.scene, 'window': window, 'screen': screen, 'area': area, 'region': region}\n"
" bpy.ops.view3d.view_all(override)\n"
" break\n";
os.close();
return conn.saveBlend();
}
std::function<void(const HECL::SystemChar*)>);
};
}

View File

@ -4,11 +4,13 @@ make_dnalist(liblist
ANCS
CMDLMaterials
CINF
CSKR)
CSKR
MREA)
add_library(DNAMP2
DNAMP2.hpp DNAMP2.cpp
${liblist}
ANIM.cpp
ANCS.cpp
CMDL.hpp
MREA.cpp
STRG.hpp STRG.cpp)

View File

@ -4,6 +4,7 @@
#include "MLVL.hpp"
#include "CMDL.hpp"
#include "ANCS.hpp"
#include "MREA.hpp"
#include "../DNACommon/TXTR.hpp"
namespace Retro
@ -224,6 +225,8 @@ ResExtractor<PAKBridge> PAKBridge::LookupExtractor(const DNAMP1::PAK::Entry& ent
return {nullptr, CMDL::Extract, {_S(".blend")}, 1};
case SBIG('ANCS'):
return {nullptr, ANCS::Extract, {_S(".yaml"), _S(".blend")}, 2};
case SBIG('MREA'):
return {nullptr, MREA::Extract, {_S(".yaml"), _S(".blend")}, 3};
}
return {};
}

248
DataSpec/DNAMP2/MREA.cpp Normal file
View File

@ -0,0 +1,248 @@
#include <lzo/lzo1x.h>
#include "MREA.hpp"
namespace Retro
{
namespace DNAMP2
{
void MREA::StreamReader::nextBlock()
{
if (m_nextBlk >= m_blkCount)
Log.report(LogVisor::FatalError, "MREA stream overrun");
BlockInfo& info = m_blockInfos[m_nextBlk++];
/* Reallocate read buffer if needed */
if (info.bufSize > m_compBufSz)
{
m_compBufSz = info.bufSize;
m_compBuf.reset(new atUint8[m_compBufSz]);
}
/* Reallocate decompress buffer if needed */
if (info.decompSize > m_decompBufSz)
{
m_decompBufSz = info.decompSize;
m_decompBuf.reset(new atUint8[m_decompBufSz]);
}
if (info.compSize == 0)
{
/* Read uncompressed block */
m_source.readUBytesToBuf(m_decompBuf.get(), info.decompSize);
}
else
{
/* Read compressed segments */
atUint32 blockStart = ROUND_UP_32(info.compSize) - info.compSize;
m_source.seek(blockStart);
atUint32 rem = info.decompSize;
atUint8* bufCur = m_decompBuf.get();
while (rem)
{
atInt16 chunkSz = m_source.readInt16Big();
if (chunkSz < 0)
{
chunkSz = -chunkSz;
m_source.readUBytesToBuf(bufCur, chunkSz);
bufCur += chunkSz;
rem -= chunkSz;
}
else
{
m_source.readUBytesToBuf(m_compBuf.get(), chunkSz);
lzo_uint dsz = rem;
lzo1x_decompress(m_compBuf.get(), chunkSz, bufCur, &dsz, nullptr);
bufCur += dsz;
rem -= dsz;
}
}
}
m_posInBlk = 0;
m_blkSz = info.decompSize;
}
MREA::StreamReader::StreamReader(Athena::io::IStreamReader& source, atUint32 blkCount)
: m_compBufSz(0x4120), m_compBuf(new atUint8[0x4120]),
m_decompBufSz(0x4120), m_decompBuf(new atUint8[0x4120]),
m_source(source), m_blkCount(blkCount)
{
m_blockInfos.reserve(blkCount);
for (int i=0 ; i<blkCount ; ++i)
{
m_blockInfos.emplace_back();
BlockInfo& info = m_blockInfos.back();
info.read(source);
m_totalDecompLen += info.decompSize;
}
source.seekAlign32();
m_blkBase = source.position();
nextBlock();
}
void MREA::StreamReader::seek(atInt64 diff, Athena::SeekOrigin whence)
{
atUint64 target = diff;
if (whence == Athena::Current)
target = m_pos + diff;
else if (whence == Athena::End)
target = m_totalDecompLen - diff;
if (target >= m_totalDecompLen)
Log.report(LogVisor::FatalError, "MREA stream seek overrun");
/* Determine which block contains position */
atUint32 dAccum = 0;
atUint32 cAccum = 0;
atUint32 bIdx = 0;
for (BlockInfo& info : m_blockInfos)
{
atUint32 newAccum = dAccum + info.decompSize;
if (newAccum > target)
break;
dAccum = newAccum;
++bIdx;
if (info.compSize)
cAccum += ROUND_UP_32(info.compSize);
else
cAccum += info.decompSize;
}
/* Seek source if needed */
if (bIdx != m_nextBlk-1)
{
m_source.seek(m_blkBase + cAccum, Athena::Begin);
m_nextBlk = bIdx;
nextBlock();
}
m_pos = target;
m_posInBlk = target - dAccum;
}
atUint64 MREA::StreamReader::readUBytesToBuf(void* buf, atUint64 len)
{
atUint8* bufCur = reinterpret_cast<atUint8*>(buf);
atUint64 rem = len;
while (rem)
{
atUint64 lRem = rem;
atUint64 blkRem = m_blkSz - m_posInBlk;
if (lRem > blkRem)
lRem = blkRem;
memcpy(bufCur, &m_decompBuf[m_posInBlk], lRem);
bufCur += lRem;
rem -= lRem;
m_posInBlk += lRem;
m_pos += lRem;
if (rem)
nextBlock();
}
return len;
}
bool MREA::Extract(const SpecBase& dataSpec,
PAKEntryReadStream& rs,
const HECL::ProjectPath& outPath,
PAKRouter<PAKBridge>& pakRouter,
const DNAMP1::PAK::Entry& entry,
bool,
std::function<void(const HECL::SystemChar*)>)
{
using RigPair = std::pair<CSKR*, CINF*>;
RigPair dummy(nullptr, nullptr);
/* Do extract */
Header head;
head.read(rs);
rs.seekAlign32();
/* MREA decompression stream */
StreamReader drs(rs, head.compressedBlockCount);
/* Start up blender connection */
HECL::BlenderConnection& conn = HECL::BlenderConnection::SharedConnection();
if (!conn.createBlend(outPath.getWithExtension(_S(".blend")).getAbsolutePath()))
return false;
/* Open Py Stream and read sections */
HECL::BlenderConnection::PyOutStream os = conn.beginPythonOut(true);
os.format("import bpy\n"
"import bmesh\n"
"\n"
"bpy.context.scene.name = '%s'\n"
"bpy.context.scene.hecl_type = 'AREA'\n",
pakRouter.getBestEntryName(entry).c_str());
DNACMDL::InitGeomBlenderContext(os, dataSpec.getMasterShaderPath());
MaterialSet::RegisterMaterialProps(os);
os << "# Clear Scene\n"
"for ob in bpy.data.objects:\n"
" bpy.context.scene.objects.unlink(ob)\n"
" bpy.data.objects.remove(ob)\n"
"bpy.types.Object.retro_disable_enviro_visor = bpy.props.BoolProperty(name='Retro: Disable in Combat/Scan Visor')\n"
"bpy.types.Object.retro_disable_thermal_visor = bpy.props.BoolProperty(name='Retro: Disable in Thermal Visor')\n"
"bpy.types.Object.retro_disable_xray_visor = bpy.props.BoolProperty(name='Retro: Disable in X-Ray Visor')\n"
"bpy.types.Object.retro_thermal_level = bpy.props.EnumProperty(items=[('COOL', 'Cool', 'Cool Temperature'),"
"('HOT', 'Hot', 'Hot Temperature'),"
"('WARM', 'Warm', 'Warm Temperature')],"
"name='Retro: Thermal Visor Level')\n"
"\n";
/* One shared material set for all meshes */
os << "# Materials\n"
"materials = []\n"
"\n";
MaterialSet matSet;
atUint64 secStart = drs.position();
matSet.read(drs);
matSet.readToBlender(os, pakRouter, entry, 0, dataSpec);
drs.seek(secStart + head.secSizes[0], Athena::Begin);
std::vector<DNACMDL::VertexAttributes> vertAttribs;
DNACMDL::GetVertexAttributes(matSet, vertAttribs);
/* Read meshes */
atUint32 curSec = 1;
for (int m=0 ; m<head.meshCount ; ++m)
{
MeshHeader mHeader;
secStart = drs.position();
mHeader.read(drs);
drs.seek(secStart + head.secSizes[curSec++], Athena::Begin);
curSec += DNACMDL::ReadGeomSectionsToBlender<PAKRouter<PAKBridge>, MaterialSet, RigPair>
(os, drs, pakRouter, entry, dataSpec, dummy, true,
true, vertAttribs, m, head.secCount, 0, &head.secSizes[curSec]);
os.format("obj.retro_disable_enviro_visor = %s\n"
"obj.retro_disable_thermal_visor = %s\n"
"obj.retro_disable_xray_visor = %s\n"
"obj.retro_thermal_level = '%s'\n",
mHeader.visorFlags.disableEnviro() ? "True" : "False",
mHeader.visorFlags.disableThermal() ? "True" : "False",
mHeader.visorFlags.disableXray() ? "True" : "False",
mHeader.visorFlags.thermalLevelStr());
}
/* Origins to center of mass */
os << "bpy.ops.object.select_by_type(type='MESH')\n"
"bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS')\n"
"bpy.ops.object.select_all(action='DESELECT')\n";
/* Center view */
os << "bpy.context.user_preferences.view.smooth_view = 0\n"
"for window in bpy.context.window_manager.windows:\n"
" screen = window.screen\n"
" for area in screen.areas:\n"
" if area.type == 'VIEW_3D':\n"
" for region in area.regions:\n"
" if region.type == 'WINDOW':\n"
" override = {'scene': bpy.context.scene, 'window': window, 'screen': screen, 'area': area, 'region': region}\n"
" bpy.ops.view3d.view_all(override)\n"
" break\n";
os.close();
return conn.saveBlend();
}
}
}

125
DataSpec/DNAMP2/MREA.hpp Normal file
View File

@ -0,0 +1,125 @@
#ifndef __DNAMP1_MREA_HPP__
#define __DNAMP1_MREA_HPP__
#include "../DNACommon/DNACommon.hpp"
#include "CMDLMaterials.hpp"
#include "CSKR.hpp"
namespace Retro
{
namespace DNAMP2
{
struct MREA
{
class StreamReader : public Athena::io::IStreamReader
{
struct BlockInfo : BigDNA
{
DECL_DNA
Value<atUint32> bufSize;
Value<atUint32> decompSize;
Value<atUint32> compSize;
Value<atUint32> secCount;
};
std::vector<BlockInfo> m_blockInfos;
size_t m_compBufSz;
std::unique_ptr<atUint8[]> m_compBuf;
size_t m_decompBufSz;
std::unique_ptr<atUint8[]> m_decompBuf;
Athena::io::IStreamReader& m_source;
atUint64 m_blkBase;
atUint32 m_blkCount;
atUint32 m_totalDecompLen = 0;
atUint32 m_pos = 0;
atUint32 m_nextBlk = 0;
atUint32 m_posInBlk = 0;
atUint32 m_blkSz = 0;
void nextBlock();
public:
StreamReader(Athena::io::IStreamReader& source, atUint32 blkCount);
void seek(atInt64 diff, Athena::SeekOrigin whence);
atUint64 position() const {return m_pos;}
atUint64 length() const {return m_totalDecompLen;}
atUint64 readUBytesToBuf(void* buf, atUint64 len);
};
struct Header : BigDNA
{
DECL_DNA
Value<atUint32> magic;
Value<atUint32> version;
Value<atVec4f> localToWorldMtx[3];
Value<atUint32> meshCount;
Value<atUint32> sclyLayerCount;
Value<atUint32> secCount;
Value<atUint32> geomSecIdx;
Value<atUint32> sclySecIdx;
Value<atUint32> scgnSecIdx;
Value<atUint32> collisionSecIdx;
Value<atUint32> unkSecIdx;
Value<atUint32> lightSecIdx;
Value<atUint32> emptySecIdx;
Value<atUint32> pathSecIdx;
Value<atUint32> unk2SecIdx;
Value<atUint32> unk3SecIdx;
Value<atUint32> egmcSecIdx;
Value<atUint32> compressedBlockCount;
Seek<12, Athena::Current> align1;
Vector<atUint32, DNA_COUNT(secCount)> secSizes;
};
struct MeshHeader : BigDNA
{
DECL_DNA
struct VisorFlags : BigDNA
{
DECL_DNA
Value<atUint32> flags;
enum ThermalLevel
{
ThermalCool,
ThermalHot,
ThermalWarm
};
static const char* GetThermalLevelStr(ThermalLevel t)
{
switch (t)
{
case ThermalCool: return "COOL";
case ThermalHot: return "HOT";
case ThermalWarm: return "WARM";
default: break;
}
return nullptr;
}
bool disableEnviro() const {return flags >> 1 & 0x1;}
void setDisableEnviro(bool v) {flags &= ~0x2; flags |= v << 1;}
bool disableThermal() const {return flags >> 2 & 0x1;}
void setDisableThermal(bool v) {flags &= ~0x4; flags |= v << 2;}
bool disableXray() const {return flags >> 3 & 0x1;}
void setDisableXray(bool v) {flags &= ~0x8; flags |= v << 3;}
ThermalLevel thermalLevel() const {return ThermalLevel(flags >> 4 & 0x3);}
void setThermalLevel(ThermalLevel v) {flags &= ~0x30; flags |= v << 4;}
const char* thermalLevelStr() const {return GetThermalLevelStr(thermalLevel());}
} visorFlags;
Value<atVec4f> xfMtx[3];
Value<atVec3f> aabb[2];
};
static bool Extract(const SpecBase& dataSpec,
PAKEntryReadStream& rs,
const HECL::ProjectPath& outPath,
PAKRouter<PAKBridge>& pakRouter,
const DNAMP1::PAK::Entry& entry,
bool,
std::function<void(const HECL::SystemChar*)>);
};
}
}
#endif

2
hecl

@ -1 +1 @@
Subproject commit 1e643175a85cea0d72c0023384e522d3cd4bb472
Subproject commit 168429814320c39b063cfee37c85261dc01c3305