2022-02-02 07:18:03 +00:00
|
|
|
#include "Runtime/Graphics/CCubeModel.hpp"
|
|
|
|
#include "Runtime/Graphics/CGraphics.hpp"
|
|
|
|
#include "Runtime/CSimplePool.hpp"
|
2022-02-01 09:18:42 +00:00
|
|
|
|
2022-02-03 15:24:41 +00:00
|
|
|
// TODO Remove WIP once we've transitioned to this
|
2022-02-02 07:18:03 +00:00
|
|
|
namespace metaforce::WIP {
|
2022-02-01 09:18:42 +00:00
|
|
|
#pragma region CModel
|
|
|
|
u32 CModel::sTotalMemory = 0;
|
2022-02-01 08:14:49 +00:00
|
|
|
u32 CModel::sFrameCounter = 0;
|
|
|
|
bool CModel::sIsTextureTimeoutEnabled = true;
|
|
|
|
CModel* CModel::sThisFrameList = nullptr;
|
|
|
|
CModel* CModel::sOneFrameList = nullptr;
|
|
|
|
CModel* CModel::sTwoFrameList = nullptr;
|
|
|
|
|
2022-02-02 07:18:03 +00:00
|
|
|
static u8* MemoryFromPartData(u8*& dataCur, const u32*& secSizeCur) {
|
|
|
|
u8* ret = nullptr;
|
2022-02-01 00:06:54 +00:00
|
|
|
if (*secSizeCur != 0) {
|
|
|
|
ret = dataCur;
|
|
|
|
}
|
2022-02-16 05:21:24 +00:00
|
|
|
dataCur += SBig(*secSizeCur);
|
2022-02-01 00:06:54 +00:00
|
|
|
++secSizeCur;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
CModel::CModel(std::unique_ptr<u8[]> in, u32 dataLen, IObjectStore* store)
|
2022-02-01 09:18:42 +00:00
|
|
|
: x0_data(std::move(in))
|
|
|
|
, x4_dataLen(dataLen)
|
|
|
|
, x34_next(sThisFrameList)
|
|
|
|
, x38_lastFrame(CGraphics::GetFrameCounter() - 2) {
|
2022-02-02 07:18:03 +00:00
|
|
|
u8* data = x0_data.get();
|
2022-02-16 05:21:24 +00:00
|
|
|
u32 flags = SBig(*reinterpret_cast<u32*>(data + 8));
|
2022-02-01 00:06:54 +00:00
|
|
|
u32 sectionSizeStart = 0x2c;
|
2022-02-16 05:21:24 +00:00
|
|
|
if (SBig(*reinterpret_cast<u32*>(data + 4)) == 1) {
|
2022-02-01 00:06:54 +00:00
|
|
|
sectionSizeStart = 0x28;
|
|
|
|
}
|
2022-02-02 07:18:03 +00:00
|
|
|
const u32* secSizeCur = reinterpret_cast<u32*>(data + sectionSizeStart);
|
2022-02-01 00:06:54 +00:00
|
|
|
s32 numMatSets = 1;
|
2022-02-16 05:21:24 +00:00
|
|
|
if (SBig(*reinterpret_cast<u32*>(data + 4)) > 1) {
|
|
|
|
numMatSets = SBig(*reinterpret_cast<s32*>(data + 0x28));
|
2022-02-01 00:06:54 +00:00
|
|
|
}
|
2022-02-16 05:21:24 +00:00
|
|
|
u8* dataCur = data + ROUND_UP_32(sectionSizeStart + SBig(*reinterpret_cast<s32*>(data + 0x24)) * 4);
|
2022-02-01 00:06:54 +00:00
|
|
|
x18_matSets.reserve(numMatSets);
|
2022-02-01 08:14:49 +00:00
|
|
|
for (s32 i = 0; i < numMatSets; ++i) {
|
2022-02-02 07:18:03 +00:00
|
|
|
x18_matSets.emplace_back(static_cast<u8*>(MemoryFromPartData(dataCur, secSizeCur)));
|
2022-02-04 08:10:09 +00:00
|
|
|
auto& shader = x18_matSets.back();
|
|
|
|
CCubeModel::MakeTexturesFromMats(shader.x10_data, shader.x0_textures, store, true);
|
2022-02-01 09:18:42 +00:00
|
|
|
x4_dataLen += shader.x0_textures.size() * sizeof(TCachedToken<CTexture>);
|
|
|
|
}
|
|
|
|
|
2022-02-02 07:18:03 +00:00
|
|
|
/* Metaforce note: Due to padding in zeus types we need to convert these and store locally */
|
2022-02-16 05:21:24 +00:00
|
|
|
u32 numVertices = SBig(*secSizeCur) / (sizeof(float) * 3);
|
2022-02-01 09:18:42 +00:00
|
|
|
const u8* positions = MemoryFromPartData(dataCur, secSizeCur);
|
2022-02-02 07:18:03 +00:00
|
|
|
for (u32 i = 0; i < numVertices; ++i) {
|
|
|
|
const auto* pos = reinterpret_cast<const float*>(positions + (i * (sizeof(float) * 3)));
|
2022-02-16 05:21:24 +00:00
|
|
|
m_positions.emplace_back(SBig(pos[0]), SBig(pos[1]), SBig(pos[2]));
|
2022-02-02 07:18:03 +00:00
|
|
|
}
|
2022-02-16 05:21:24 +00:00
|
|
|
u32 numNormals = SBig(*secSizeCur);
|
2022-02-02 07:18:03 +00:00
|
|
|
numNormals /= ((flags & 2) == 0 ? sizeof(float) : sizeof(s16)) * 3;
|
2022-02-01 09:18:42 +00:00
|
|
|
const u8* normals = MemoryFromPartData(dataCur, secSizeCur);
|
2022-02-02 07:18:03 +00:00
|
|
|
|
|
|
|
for (u32 i = 0; i < numNormals; ++i) {
|
|
|
|
if ((flags & 2) == 0) {
|
|
|
|
const auto* norm = reinterpret_cast<const float*>(normals + (i * (sizeof(float) * 3)));
|
2022-02-16 05:21:24 +00:00
|
|
|
m_normals.emplace_back(SBig(norm[0]), SBig(norm[1]), SBig(norm[2]));
|
2022-02-02 07:18:03 +00:00
|
|
|
} else {
|
|
|
|
const auto* norm = reinterpret_cast<const s16*>(normals + (i * (sizeof(s16) * 3)));
|
2022-02-16 05:21:24 +00:00
|
|
|
m_normals.emplace_back(SBig(norm[0]) / 32767.f, SBig(norm[1]) / 32767.f,
|
|
|
|
SBig(norm[2]) / 32767.f);
|
2022-02-02 07:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-16 05:21:24 +00:00
|
|
|
u32 numColors = SBig(*secSizeCur) / (sizeof(int));
|
2022-02-01 09:18:42 +00:00
|
|
|
const u8* vtxColors = MemoryFromPartData(dataCur, secSizeCur);
|
2022-02-02 07:18:03 +00:00
|
|
|
|
|
|
|
for (u32 i = 0; i < numColors; ++i) {
|
2022-02-16 05:21:24 +00:00
|
|
|
const u32 col = SBig(*reinterpret_cast<const u32*>(vtxColors + (i * (sizeof(u32)))));
|
2022-02-02 07:18:03 +00:00
|
|
|
m_colors.emplace_back(zeus::CColor(zeus::Comp32(col)));
|
|
|
|
}
|
|
|
|
|
2022-02-16 05:21:24 +00:00
|
|
|
u32 numFloatUVs = SBig(*reinterpret_cast<const u32*>(secSizeCur)) / (sizeof(float) * 2);
|
2022-02-01 09:18:42 +00:00
|
|
|
const u8* floatUVs = MemoryFromPartData(dataCur, secSizeCur);
|
2022-02-02 07:18:03 +00:00
|
|
|
|
|
|
|
for (u32 i = 0; i < numFloatUVs; ++i) {
|
|
|
|
const auto* norm = reinterpret_cast<const float*>(floatUVs + (i * (sizeof(float) * 2)));
|
2022-02-16 05:21:24 +00:00
|
|
|
m_floatUVs.emplace_back(SBig(norm[0]), SBig(norm[1]));
|
2022-02-02 07:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & 4) != 0) {
|
2022-02-16 05:21:24 +00:00
|
|
|
u32 numShortUVs = SBig(*reinterpret_cast<const u32*>(secSizeCur)) / (sizeof(s16) * 2);
|
2022-02-02 07:18:03 +00:00
|
|
|
const u8* shortUVs = MemoryFromPartData(dataCur, secSizeCur);
|
|
|
|
|
|
|
|
for (u32 i = 0; i < numShortUVs; ++i) {
|
|
|
|
const auto* norm = reinterpret_cast<const s16*>(shortUVs + (i * (sizeof(s16) * 2)));
|
2022-02-16 05:21:24 +00:00
|
|
|
m_shortUVs.emplace_back(std::array{SBig(norm[0]), SBig(norm[1])});
|
2022-02-02 07:18:03 +00:00
|
|
|
}
|
2022-02-01 09:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const u8* surfaceInfo = MemoryFromPartData(dataCur, secSizeCur);
|
2022-02-16 05:21:24 +00:00
|
|
|
u32 surfaceCount = SBig(*reinterpret_cast<const u32*>(surfaceInfo));
|
2022-02-01 09:18:42 +00:00
|
|
|
x8_surfaces.reserve(surfaceCount);
|
|
|
|
|
|
|
|
for (u32 i = 0; i < surfaceCount; ++i) {
|
2022-02-02 07:18:03 +00:00
|
|
|
if (x8_surfaces.capacity() <= x8_surfaces.size()) {
|
|
|
|
x8_surfaces.reserve(x8_surfaces.capacity() * 2);
|
|
|
|
}
|
|
|
|
|
2022-02-03 15:24:41 +00:00
|
|
|
x8_surfaces.emplace_back(std::make_unique<CCubeSurface>(MemoryFromPartData(dataCur, secSizeCur)));
|
2022-02-01 09:18:42 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 07:18:03 +00:00
|
|
|
const float* bounds = reinterpret_cast<float*>(data + 12);
|
2022-02-03 15:24:41 +00:00
|
|
|
zeus::CAABox aabox = zeus::skNullBox;
|
2022-02-16 05:21:24 +00:00
|
|
|
aabox.min = {SBig(bounds[0]), SBig(bounds[1]), SBig(bounds[2])};
|
|
|
|
aabox.max = {SBig(bounds[3]), SBig(bounds[4]), SBig(bounds[5])};
|
2022-02-02 07:18:03 +00:00
|
|
|
|
|
|
|
/* This constructor has been changed from the original to take into account platform differences */
|
2022-02-03 15:24:41 +00:00
|
|
|
x28_modelInst =
|
|
|
|
std::make_unique<CCubeModel>(&x8_surfaces, &x18_matSets[0].x0_textures, x18_matSets[0].x10_data, &m_positions,
|
|
|
|
&m_colors, &m_normals, &m_floatUVs, &m_shortUVs, aabox, flags, true, -1);
|
2022-02-01 09:18:42 +00:00
|
|
|
|
|
|
|
sThisFrameList = this;
|
|
|
|
if (x34_next != nullptr) {
|
|
|
|
x34_next->x30_prev = this;
|
2022-02-01 08:14:49 +00:00
|
|
|
}
|
2022-02-01 09:18:42 +00:00
|
|
|
x4_dataLen += x8_surfaces.size() * 4;
|
|
|
|
sTotalMemory += x4_dataLen;
|
|
|
|
// DCFlushRange(x0_data, dataLen);
|
2022-02-01 08:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CModel::UpdateLastFrame() { x38_lastFrame = CGraphics::GetFrameCounter(); }
|
|
|
|
void CModel::MoveToThisFrameList() {
|
|
|
|
UpdateLastFrame();
|
2022-02-01 09:18:42 +00:00
|
|
|
if (sThisFrameList == this) {
|
|
|
|
return;
|
2022-02-01 00:06:54 +00:00
|
|
|
}
|
2022-02-01 08:14:49 +00:00
|
|
|
|
2022-02-02 07:18:03 +00:00
|
|
|
RemoveFromList();
|
2022-02-01 09:18:42 +00:00
|
|
|
if (sThisFrameList != nullptr) {
|
|
|
|
x34_next = sThisFrameList;
|
|
|
|
x34_next->x30_prev = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
sThisFrameList = this;
|
2022-02-01 00:06:54 +00:00
|
|
|
}
|
2022-02-01 08:14:49 +00:00
|
|
|
|
|
|
|
void CModel::RemoveFromList() {
|
|
|
|
if (x30_prev == nullptr) {
|
|
|
|
if (sThisFrameList == this) {
|
|
|
|
sThisFrameList = x34_next;
|
|
|
|
} else if (sOneFrameList == this) {
|
|
|
|
sOneFrameList = x34_next;
|
|
|
|
} else if (sTwoFrameList == this) {
|
|
|
|
sTwoFrameList = x34_next;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
x30_prev->x34_next = x34_next;
|
|
|
|
}
|
|
|
|
if (x34_next != nullptr) {
|
|
|
|
x34_next->x30_prev = x30_prev;
|
|
|
|
}
|
|
|
|
x30_prev = nullptr;
|
|
|
|
x34_next = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CModel::FrameDone() {
|
|
|
|
++sFrameCounter;
|
|
|
|
if (sIsTextureTimeoutEnabled) {
|
2022-02-02 07:18:03 +00:00
|
|
|
auto* iter = sTwoFrameList;
|
2022-02-01 08:14:49 +00:00
|
|
|
while (iter != nullptr) {
|
|
|
|
auto* next = iter->x34_next;
|
|
|
|
iter->VerifyCurrentShader(0);
|
|
|
|
for (auto& shader : iter->x18_matSets) {
|
|
|
|
shader.UnlockTextures();
|
|
|
|
}
|
|
|
|
|
|
|
|
iter->x28_modelInst->UnlockTextures();
|
|
|
|
iter->x34_next = nullptr;
|
|
|
|
iter->x30_prev = nullptr;
|
|
|
|
iter = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
sTwoFrameList = sOneFrameList;
|
|
|
|
sOneFrameList = sThisFrameList;
|
|
|
|
sThisFrameList = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CModel::EnableTextureTimeout() { sIsTextureTimeoutEnabled = true; }
|
|
|
|
void CModel::DisableTextureTimeout() { sIsTextureTimeoutEnabled = false; }
|
2022-02-01 09:18:42 +00:00
|
|
|
#pragma endregion
|
|
|
|
|
|
|
|
#pragma region CCubeModel
|
|
|
|
|
2022-02-05 15:22:30 +00:00
|
|
|
CCubeModel::CCubeModel(std::vector<std::unique_ptr<CCubeSurface>>* surfaces,
|
|
|
|
std::vector<TCachedToken<CTexture>>* textures, const u8* materialData,
|
2022-02-03 15:24:41 +00:00
|
|
|
const std::vector<zeus::CVector3f>* positions, const std::vector<zeus::CColor>* colors,
|
|
|
|
const std::vector<zeus::CVector3f>* normals, const std::vector<zeus::CVector2f>* texCoords,
|
|
|
|
const std::vector<std::array<s16, 2>>* packedTexCoords, const zeus::CAABox& aabox, u8 flags,
|
|
|
|
bool b1, u32 idx)
|
|
|
|
: x0_modelInstance(surfaces, materialData, positions, colors, normals, texCoords, packedTexCoords)
|
|
|
|
, x1c_textures(textures)
|
|
|
|
, x20_worldAABB(aabox)
|
|
|
|
, x40_24_(!b1)
|
|
|
|
, x41_visorFlags(flags)
|
|
|
|
, x44_idx(idx) {
|
|
|
|
for (const auto& surf : x0_modelInstance.Surfaces()) {
|
|
|
|
surf->SetParent(this);
|
|
|
|
}
|
2022-02-04 08:10:09 +00:00
|
|
|
|
|
|
|
for (u32 i = x0_modelInstance.Surfaces().size(); i > 0; --i) {
|
|
|
|
const auto& surf = x0_modelInstance.Surfaces()[i - 1];
|
|
|
|
if (!GetMaterialByIndex(surf->GetMaterialIndex()).IsFlagSet(EStateFlags::DepthSorting)) {
|
|
|
|
surf->SetNextSurface(x38_firstUnsortedSurf);
|
|
|
|
x38_firstUnsortedSurf = surf.get();
|
|
|
|
} else {
|
|
|
|
surf->SetNextSurface(x3c_firstSortedSurf);
|
|
|
|
x3c_firstSortedSurf = surf.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CCubeMaterial CCubeModel::GetMaterialByIndex(u32 idx) {
|
|
|
|
u32 materialOffset = 0;
|
|
|
|
const u8* matData = x0_modelInstance.GetMaterialPointer();
|
|
|
|
matData += (x1c_textures->size() + 1) * 4;
|
|
|
|
if (idx != 0) {
|
2022-02-16 05:21:24 +00:00
|
|
|
materialOffset = SBig(*reinterpret_cast<const u32*>(matData + (idx * 4)));
|
2022-02-04 08:10:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 05:21:24 +00:00
|
|
|
u32 materialCount = SBig(*reinterpret_cast<const u32*>(matData));
|
2022-02-04 08:10:09 +00:00
|
|
|
return CCubeMaterial(matData + materialOffset + (materialCount * 4) + 4);
|
2022-02-03 15:24:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 15:22:30 +00:00
|
|
|
void CCubeModel::UnlockTextures() {
|
|
|
|
for (TCachedToken<CTexture>& tex : *x1c_textures) {
|
|
|
|
tex.Unlock();
|
|
|
|
}
|
|
|
|
}
|
2022-02-01 09:18:42 +00:00
|
|
|
|
2022-02-04 08:10:09 +00:00
|
|
|
void CCubeModel::MakeTexturesFromMats(const u8* ptr, std::vector<TCachedToken<CTexture>>& textures, IObjectStore* store,
|
|
|
|
bool b1) {
|
|
|
|
const u32* curId = reinterpret_cast<const u32*>(ptr + 4);
|
2022-02-16 05:21:24 +00:00
|
|
|
u32 textureCount = SBig(*reinterpret_cast<const u32*>(ptr));
|
2022-02-04 08:10:09 +00:00
|
|
|
textures.reserve(textureCount);
|
|
|
|
for (u32 i = 0; i < textureCount; ++i) {
|
2022-02-16 05:21:24 +00:00
|
|
|
textures.emplace_back(store->GetObj({FOURCC('TXTR'), SBig(curId[i])}));
|
2022-02-04 08:10:09 +00:00
|
|
|
|
|
|
|
if (!b1 && textures.back().IsNull()) {
|
|
|
|
textures.back().GetObj();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-01 09:18:42 +00:00
|
|
|
|
|
|
|
#pragma endregion
|
|
|
|
|
2022-02-02 07:18:03 +00:00
|
|
|
#pragma region CCubeSurface
|
2022-02-04 08:10:09 +00:00
|
|
|
CCubeSurface::CCubeSurface(u8* ptr) : x0_data(ptr) {
|
2022-02-18 07:37:54 +00:00
|
|
|
CMemoryInStream mem(ptr, 10000, CMemoryInStream::EOwnerShip::NotOwned); // Oversized so we can read everything in
|
|
|
|
x0_center = mem.Get<zeus::CVector3f>();
|
|
|
|
xc_materialIndex = mem.ReadLong();
|
|
|
|
x10_displayListSize = mem.ReadLong();
|
|
|
|
x14_parent = reinterpret_cast<CCubeModel*>(mem.ReadLong());
|
|
|
|
x18_nextSurface = reinterpret_cast<CCubeSurface*>(mem.ReadLong());
|
|
|
|
x1c_extraSize = mem.ReadLong();
|
|
|
|
x20_normal = mem.Get<zeus::CVector3f>();
|
2022-02-03 15:24:41 +00:00
|
|
|
if (x1c_extraSize > 0) {
|
2022-02-18 07:37:54 +00:00
|
|
|
x2c_bounds = mem.Get<zeus::CAABox>();
|
2022-02-03 15:24:41 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-04 08:10:09 +00:00
|
|
|
|
2022-02-02 07:18:03 +00:00
|
|
|
#pragma endregion
|
|
|
|
|
|
|
|
CFactoryFnReturn FModelFactory(const metaforce::SObjectTag& tag, std::unique_ptr<u8[]>&& in, u32 len,
|
|
|
|
const metaforce::CVParamTransfer& vparms, CObjectReference* selfRef) {
|
|
|
|
CSimplePool* sp = vparms.GetOwnedObj<CSimplePool*>();
|
|
|
|
CFactoryFnReturn ret = TToken<CModel>::GetIObjObjectFor(std::make_unique<CModel>(std::move(in), len, sp));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-02-03 15:24:41 +00:00
|
|
|
} // namespace metaforce::WIP
|