Finish CModel constructors

This commit is contained in:
Phillip Stephens 2022-02-04 00:10:09 -08:00
parent dca8af4d96
commit 8d01afc632
Signed by: Antidote
GPG Key ID: F8BEE4C83DACA60D
3 changed files with 95 additions and 9 deletions

View File

@ -158,6 +158,8 @@ public:
m_obj = nullptr;
return *this;
}
bool IsNull() { return m_obj == nullptr; }
};
template <class T>

View File

@ -42,8 +42,8 @@ CModel::CModel(std::unique_ptr<u8[]> in, u32 dataLen, IObjectStore* store)
x18_matSets.reserve(numMatSets);
for (s32 i = 0; i < numMatSets; ++i) {
x18_matSets.emplace_back(static_cast<u8*>(MemoryFromPartData(dataCur, secSizeCur)));
auto shader = x18_matSets.back();
CCubeModel::MakeTexturesFromMats(shader.x10_data, shader.x0_textures, true);
auto& shader = x18_matSets.back();
CCubeModel::MakeTexturesFromMats(shader.x10_data, shader.x0_textures, store, true);
x4_dataLen += shader.x0_textures.size() * sizeof(TCachedToken<CTexture>);
}
@ -204,17 +204,52 @@ CCubeModel::CCubeModel(const std::vector<std::unique_ptr<CCubeSurface>>* surface
for (const auto& surf : x0_modelInstance.Surfaces()) {
surf->SetParent(this);
}
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) {
materialOffset = hecl::SBig(*reinterpret_cast<const u32*>(matData + (idx * 4)));
}
u32 materialCount = hecl::SBig(*reinterpret_cast<const u32*>(matData));
return CCubeMaterial(matData + materialOffset + (materialCount * 4) + 4);
}
void CCubeModel::UnlockTextures() {}
void CCubeModel::MakeTexturesFromMats(const u8* ptr, std::vector<TCachedToken<CTexture>>& texture, bool b1) {}
void CCubeModel::MakeTexturesFromMats(const u8* ptr, std::vector<TCachedToken<CTexture>>& textures, IObjectStore* store,
bool b1) {
const u32* curId = reinterpret_cast<const u32*>(ptr + 4);
u32 textureCount = hecl::SBig(*reinterpret_cast<const u32*>(ptr));
textures.reserve(textureCount);
for (u32 i = 0; i < textureCount; ++i) {
textures.emplace_back(store->GetObj({FOURCC('TXTR'), hecl::SBig(curId[i])}));
if (!b1 && textures.back().IsNull()) {
textures.back().GetObj();
}
}
}
#pragma endregion
#pragma region CCubeSurface
CCubeSurface::CCubeSurface(u8* ptr) {
CMemoryInStream mem(ptr, 64);
CCubeSurface::CCubeSurface(u8* ptr) : x0_data(ptr) {
CMemoryInStream mem(ptr, 10000); // Oversized so we can read everything in
x0_center.readBig(mem);
xc_materialIndex = mem.readUint32Big();
x10_displayListSize = mem.readUint32Big();
@ -226,6 +261,7 @@ CCubeSurface::CCubeSurface(u8* ptr) {
x2c_bounds = zeus::CAABox::ReadBoundingBoxBig(mem);
}
}
#pragma endregion
CFactoryFnReturn FModelFactory(const metaforce::SObjectTag& tag, std::unique_ptr<u8[]>&& in, u32 len,

View File

@ -11,6 +11,7 @@
namespace metaforce::WIP {
class CCubeSurface;
class CCubeModel;
class CCubeMaterial;
#pragma region CModel
class CModel {
@ -118,14 +119,16 @@ public:
const std::vector<std::array<s16, 2>>* packedTexCoords, const zeus::CAABox& aabox, u8 flags, bool b1,
u32 idx);
CCubeMaterial GetMaterialByIndex(u32 idx);
void UnlockTextures();
static void MakeTexturesFromMats(const u8* ptr, std::vector<TCachedToken<CTexture>>& texture, bool b1);
static void MakeTexturesFromMats(const u8* ptr, std::vector<TCachedToken<CTexture>>& texture, IObjectStore* store, bool b1);
};
#pragma endregion
#pragma region CCubeSurface
class CCubeSurface {
static constexpr zeus::CVector3f skDefaultNormal{1.f, 0.f, 0.f};
u8* x0_data;
zeus::CVector3f x0_center;
u32 xc_materialIndex;
u32 x10_displayListSize;
@ -136,14 +139,17 @@ class CCubeSurface {
zeus::CAABox x2c_bounds;
public:
CCubeSurface(u8* ptr);
explicit CCubeSurface(u8* ptr);
bool IsValid() const;
void SetParent(CCubeModel* parent) { x14_parent = parent; }
void SetNextSurface(CCubeSurface* next) { x18_nextSurface = next; }
[[nodiscard]] u32 GetMaterialIndex() const { return xc_materialIndex; }
[[nodiscard]] u32 GetDisplayListSize() const { return x10_displayListSize & 0x7fffffff; }
[[nodiscard]] u32 GetNormalHint() const { return (x10_displayListSize >> 31) & 1; }
[[nodiscard]] u8* GetDisplayList() const;
// u32 GetSurfaceHeaderSize() const { }
[[nodiscard]] u8* GetDisplayList() const {
return reinterpret_cast<u8*>(reinterpret_cast<uintptr_t>(x0_data) + GetSurfaceHeaderSize());
}
u32 GetSurfaceHeaderSize() const { return (0x4b + x1c_extraSize) & ~31; }
[[nodiscard]] zeus::CVector3f GetCenter() const { return x0_center; }
[[nodiscard]] zeus::CAABox GetBounds() const {
return x1c_extraSize != 0 ? x2c_bounds : zeus::CAABox{x0_center, x0_center};
@ -151,6 +157,48 @@ public:
};
#pragma endregion
#pragma region CCubeMaterial
enum class EStateFlags {
Unused1 = 1 << 0,
Unused2 = 1 << 1,
Unused3 = 1 << 2,
KonstEnabled = 1 << 3,
DepthSorting = 1 << 4,
AlphaTest = 1 << 5,
Reflection = 1 << 6,
DepthWrite = 1 << 7,
ReflectionSurfaceEye = 1 << 8,
OccluderMesh = 1 << 9,
ReflectionIndirectTexture = 1 << 10,
LightMap = 1 << 11,
Unused4 = 1 << 12,
LightmapUVArray = 1 << 13,
};
ENABLE_BITWISE_ENUM(EStateFlags);
class CCubeMaterial {
const u8* x0_data;
public:
explicit CCubeMaterial(const u8* data) : x0_data(data) {}
[[nodiscard]] u32 GetCompressedBlend() {
const u32* ptr = reinterpret_cast<const u32*>(x0_data[(GetTextureCount() * 4) + 16]);
if (IsFlagSet(EStateFlags::KonstEnabled)) {
ptr += hecl::SBig(*ptr) + 1;
}
return hecl::SBig(*ptr);
}
[[nodiscard]] EStateFlags GetFlags() const { return EStateFlags(hecl::SBig(*reinterpret_cast<const u32*>(x0_data))); }
[[nodiscard]] bool IsFlagSet(EStateFlags flag) const { return True(GetFlags() & flag); }
[[nodiscard]] u32 GetUsedTextureSlots() const { return static_cast<u32>(GetFlags()) >> 16; }
[[nodiscard]] u32 GetTextureCount() const { return hecl::SBig(*reinterpret_cast<const u32*>(&x0_data[4])); }
[[nodiscard]] u32 GetVertexDesc() const { return hecl::SBig(*reinterpret_cast<const u32*>(&x0_data[(GetTextureCount() * 4) + 8])); }
};
#pragma endregion
CFactoryFnReturn FModelFactory(const metaforce::SObjectTag& tag, std::unique_ptr<u8[]>&& in, u32 len,
const metaforce::CVParamTransfer& vparms, CObjectReference* selfRef);
} // namespace metaforce::WIP