2022-02-25 07:45:25 +00:00
|
|
|
#include "CCubeModel.hpp"
|
2022-02-01 09:18:42 +00:00
|
|
|
|
2022-02-25 07:45:25 +00:00
|
|
|
#include "CSimplePool.hpp"
|
|
|
|
#include "Graphics/CCubeMaterial.hpp"
|
|
|
|
#include "Graphics/CCubeSurface.hpp"
|
|
|
|
#include "Graphics/CGraphics.hpp"
|
2022-02-26 07:05:59 +00:00
|
|
|
#include "Graphics/CModel.hpp"
|
2022-03-12 15:47:20 +00:00
|
|
|
#include "Graphics/CGX.hpp"
|
2022-03-06 20:58:06 +00:00
|
|
|
|
2022-02-25 07:45:25 +00:00
|
|
|
namespace metaforce {
|
2022-02-26 07:05:59 +00:00
|
|
|
bool CCubeModel::sRenderModelBlack = false;
|
|
|
|
bool CCubeModel::sRenderModelShadow = false;
|
|
|
|
bool CCubeModel::sUsingPackedLightmaps = false;
|
|
|
|
const CTexture* CCubeModel::sShadowTexture = nullptr;
|
2022-03-12 15:47:20 +00:00
|
|
|
zeus::CTransform CCubeModel::sTextureProjectionTransform;
|
|
|
|
GX::LightMask CCubeModel::sChannel0DisableLightMask;
|
|
|
|
GX::LightMask CCubeModel::sChannel1EnableLightMask;
|
2022-02-26 07:05:59 +00:00
|
|
|
|
|
|
|
static bool sDrawingOccluders = false;
|
|
|
|
static bool sDrawingWireframe = false;
|
|
|
|
|
|
|
|
static zeus::CVector3f sPlayerPosition;
|
2022-02-02 07:18:03 +00:00
|
|
|
|
2022-02-25 07:45:25 +00:00
|
|
|
CCubeModel::CCubeModel(std::vector<CCubeSurface>* surfaces, std::vector<TCachedToken<CTexture>>* textures,
|
|
|
|
u8* materialData, std::vector<zeus::CVector3f>* positions, std::vector<zeus::CColor>* colors,
|
2022-03-08 07:44:46 +00:00
|
|
|
std::vector<zeus::CVector3f>* normals, std::vector<aurora::Vec2<float>>* texCoords,
|
|
|
|
std::vector<aurora::Vec2<float>>* packedTexCoords, const zeus::CAABox& aabb, u8 flags, bool b1,
|
2022-02-25 07:45:25 +00:00
|
|
|
u32 idx)
|
2022-02-03 15:24:41 +00:00
|
|
|
: x0_modelInstance(surfaces, materialData, positions, colors, normals, texCoords, packedTexCoords)
|
|
|
|
, x1c_textures(textures)
|
2022-02-25 07:45:25 +00:00
|
|
|
, x20_worldAABB(aabb)
|
|
|
|
, x40_24_texturesLoaded(!b1)
|
2022-02-03 15:24:41 +00:00
|
|
|
, x41_visorFlags(flags)
|
|
|
|
, x44_idx(idx) {
|
2022-02-25 07:45:25 +00:00
|
|
|
for (auto& surf : *x0_modelInstance.Surfaces()) {
|
|
|
|
surf.SetParent(this);
|
2022-02-03 15:24:41 +00:00
|
|
|
}
|
2022-02-04 08:10:09 +00:00
|
|
|
|
2022-02-25 07:45:25 +00:00
|
|
|
for (u32 i = x0_modelInstance.Surfaces()->size(); i > 0; --i) {
|
|
|
|
auto& surf = (*x0_modelInstance.Surfaces())[i - 1];
|
2022-02-26 07:05:59 +00:00
|
|
|
const auto matFlags = GetMaterialByIndex(surf.GetMaterialIndex()).GetFlags();
|
|
|
|
if (!matFlags.IsSet(CCubeMaterialFlagBits::fDepthSorting)) {
|
2022-02-25 07:45:25 +00:00
|
|
|
surf.SetNextSurface(x38_firstUnsortedSurf);
|
|
|
|
x38_firstUnsortedSurf = &surf;
|
2022-02-04 08:10:09 +00:00
|
|
|
} else {
|
2022-02-25 07:45:25 +00:00
|
|
|
surf.SetNextSurface(x3c_firstSortedSurf);
|
|
|
|
x3c_firstSortedSurf = &surf;
|
2022-02-04 08:10:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-25 07:45:25 +00:00
|
|
|
bool CCubeModel::TryLockTextures() {
|
|
|
|
if (!x40_24_texturesLoaded) {
|
2022-03-08 22:21:15 +00:00
|
|
|
bool texturesLoading = false;
|
2022-02-25 07:45:25 +00:00
|
|
|
for (auto& texture : *x1c_textures) {
|
|
|
|
texture.Lock();
|
|
|
|
bool loadTexture = true;
|
|
|
|
if (!texture.HasReference()) {
|
|
|
|
if (!texture.IsLocked() || texture.IsNull()) {
|
|
|
|
loadTexture = false;
|
|
|
|
} else {
|
|
|
|
texture.GetObj();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (loadTexture) {
|
2022-03-08 22:21:15 +00:00
|
|
|
// if (!texture->LoadToMRAM()) {
|
|
|
|
// texturesLoading = true;
|
|
|
|
// }
|
2022-02-25 07:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-08 22:21:15 +00:00
|
|
|
if (!texturesLoading) {
|
2022-02-25 07:45:25 +00:00
|
|
|
x40_24_texturesLoaded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return x40_24_texturesLoaded;
|
|
|
|
}
|
|
|
|
|
2022-02-05 15:22:30 +00:00
|
|
|
void CCubeModel::UnlockTextures() {
|
2022-02-25 07:45:25 +00:00
|
|
|
for (auto& token : *x1c_textures) {
|
|
|
|
token.Unlock();
|
2022-02-05 15:22:30 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-01 09:18:42 +00:00
|
|
|
|
2022-02-25 07:45:25 +00:00
|
|
|
void CCubeModel::RemapMaterialData(u8* data, std::vector<TCachedToken<CTexture>>& textures) {
|
|
|
|
x0_modelInstance.SetMaterialPointer(data);
|
|
|
|
x1c_textures = &textures;
|
|
|
|
x40_24_texturesLoaded = false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-02-26 07:05:59 +00:00
|
|
|
void CCubeModel::Draw(const CModelFlags& flags) {
|
|
|
|
CCubeMaterial::KillCachedViewDepState();
|
|
|
|
SetArraysCurrent();
|
|
|
|
DrawSurfaces(flags);
|
|
|
|
}
|
|
|
|
|
2022-03-10 06:33:17 +00:00
|
|
|
void CCubeModel::Draw(TConstVectorRef positions, TConstVectorRef normals, const CModelFlags& flags) {
|
2022-02-26 07:05:59 +00:00
|
|
|
CCubeMaterial::KillCachedViewDepState();
|
|
|
|
SetSkinningArraysCurrent(positions, normals);
|
|
|
|
DrawSurfaces(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCubeModel::DrawAlpha(const CModelFlags& flags) {
|
|
|
|
CCubeMaterial::KillCachedViewDepState();
|
|
|
|
SetArraysCurrent();
|
|
|
|
DrawAlphaSurfaces(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCubeModel::DrawAlphaSurfaces(const CModelFlags& flags) {
|
|
|
|
if (sDrawingWireframe) {
|
|
|
|
const auto* surface = x3c_firstSortedSurf;
|
|
|
|
while (surface != nullptr) {
|
|
|
|
DrawSurfaceWireframe(*surface);
|
|
|
|
surface = surface->GetNextSurface();
|
|
|
|
}
|
|
|
|
} else if (TryLockTextures()) {
|
|
|
|
const auto* surface = x3c_firstSortedSurf;
|
|
|
|
while (surface != nullptr) {
|
|
|
|
DrawSurface(*surface, flags);
|
|
|
|
surface = surface->GetNextSurface();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 06:33:17 +00:00
|
|
|
void CCubeModel::DrawFlat(TConstVectorRef positions, TConstVectorRef normals, ESurfaceSelection surfaces) {
|
2022-02-26 07:05:59 +00:00
|
|
|
if (positions == nullptr) {
|
|
|
|
SetArraysCurrent();
|
|
|
|
} else {
|
|
|
|
SetSkinningArraysCurrent(positions, normals);
|
|
|
|
}
|
|
|
|
if (surfaces != ESurfaceSelection::Sorted) {
|
|
|
|
const auto* surface = x38_firstUnsortedSurf;
|
|
|
|
while (surface != nullptr) {
|
|
|
|
const auto mat = GetMaterialByIndex(surface->GetMaterialIndex());
|
2022-03-12 15:47:20 +00:00
|
|
|
CGX::SetVtxDescv_Compressed(mat.GetVertexDesc());
|
|
|
|
CGX::CallDisplayList(surface->GetDisplayList(), surface->GetDisplayListSize());
|
2022-02-27 21:58:27 +00:00
|
|
|
surface = surface->GetNextSurface();
|
2022-02-26 07:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (surfaces != ESurfaceSelection::Unsorted) {
|
|
|
|
const auto* surface = x3c_firstSortedSurf;
|
|
|
|
while (surface != nullptr) {
|
|
|
|
const auto mat = GetMaterialByIndex(surface->GetMaterialIndex());
|
2022-03-12 15:47:20 +00:00
|
|
|
CGX::SetVtxDescv_Compressed(mat.GetVertexDesc());
|
|
|
|
CGX::CallDisplayList(surface->GetDisplayList(), surface->GetDisplayListSize());
|
2022-02-27 21:58:27 +00:00
|
|
|
surface = surface->GetNextSurface();
|
2022-02-26 07:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 06:33:17 +00:00
|
|
|
void CCubeModel::DrawNormal(TConstVectorRef positions, TConstVectorRef normals, ESurfaceSelection surfaces) {
|
2022-03-12 15:47:20 +00:00
|
|
|
CGX::SetNumIndStages(0);
|
|
|
|
CGX::SetNumTevStages(1);
|
2022-03-14 22:00:03 +00:00
|
|
|
CGX::SetNumTexGens(1); // TODO should this be 0?
|
2022-03-12 15:47:20 +00:00
|
|
|
CGX::SetZMode(true, GX::LEQUAL, true);
|
|
|
|
CGX::SetTevOrder(GX::TEVSTAGE0, GX::TEXCOORD_NULL, GX::TEXMAP_NULL, GX::COLOR_NULL);
|
|
|
|
CGX::SetTevColorIn(GX::TEVSTAGE0, GX::CC_ZERO, GX::CC_ZERO, GX::CC_ZERO, GX::CC_ZERO);
|
|
|
|
CGX::SetTevAlphaIn(GX::TEVSTAGE0, GX::CA_ZERO, GX::CA_ZERO, GX::CA_ZERO, GX::CA_ZERO);
|
|
|
|
CGX::SetStandardTevColorAlphaOp(GX::TEVSTAGE0);
|
|
|
|
CGX::SetBlendMode(GX::BM_BLEND, GX::BL_ZERO, GX::BL_ONE, GX::LO_CLEAR);
|
2022-02-26 07:05:59 +00:00
|
|
|
DrawFlat(positions, normals, surfaces);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCubeModel::DrawNormal(const CModelFlags& flags) {
|
|
|
|
CCubeMaterial::KillCachedViewDepState();
|
|
|
|
SetArraysCurrent();
|
|
|
|
DrawNormalSurfaces(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCubeModel::DrawNormalSurfaces(const CModelFlags& flags) {
|
|
|
|
if (sDrawingWireframe) {
|
|
|
|
const auto* surface = x38_firstUnsortedSurf;
|
|
|
|
while (surface != nullptr) {
|
|
|
|
DrawSurfaceWireframe(*surface);
|
|
|
|
surface = surface->GetNextSurface();
|
|
|
|
}
|
|
|
|
} else if (TryLockTextures()) {
|
|
|
|
const auto* surface = x38_firstUnsortedSurf;
|
|
|
|
while (surface != nullptr) {
|
|
|
|
DrawSurface(*surface, flags);
|
|
|
|
surface = surface->GetNextSurface();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCubeModel::DrawSurface(const CCubeSurface& surface, const CModelFlags& flags) {
|
|
|
|
auto mat = GetMaterialByIndex(surface.GetMaterialIndex());
|
|
|
|
if (!mat.GetFlags().IsSet(CCubeMaterialFlagBits::fShadowOccluderMesh) || sDrawingOccluders) {
|
|
|
|
mat.SetCurrent(flags, surface, *this);
|
2022-03-12 15:47:20 +00:00
|
|
|
CGX::CallDisplayList(surface.GetDisplayList(), surface.GetDisplayListSize());
|
2022-02-26 07:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCubeModel::DrawSurfaces(const CModelFlags& flags) {
|
|
|
|
if (sDrawingWireframe) {
|
|
|
|
const auto* surface = x38_firstUnsortedSurf;
|
|
|
|
while (surface != nullptr) {
|
|
|
|
DrawSurfaceWireframe(*surface);
|
|
|
|
surface = surface->GetNextSurface();
|
|
|
|
}
|
|
|
|
surface = x3c_firstSortedSurf;
|
|
|
|
while (surface != nullptr) {
|
|
|
|
DrawSurfaceWireframe(*surface);
|
|
|
|
surface = surface->GetNextSurface();
|
|
|
|
}
|
|
|
|
} else if (TryLockTextures()) {
|
|
|
|
const auto* surface = x38_firstUnsortedSurf;
|
|
|
|
while (surface != nullptr) {
|
|
|
|
DrawSurface(*surface, flags);
|
|
|
|
surface = surface->GetNextSurface();
|
|
|
|
}
|
|
|
|
surface = x3c_firstSortedSurf;
|
|
|
|
while (surface != nullptr) {
|
|
|
|
DrawSurface(*surface, flags);
|
|
|
|
surface = surface->GetNextSurface();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCubeModel::DrawSurfaceWireframe(const CCubeSurface& surface) {
|
|
|
|
auto mat = GetMaterialByIndex(surface.GetMaterialIndex());
|
|
|
|
// TODO convert vertices to line strips and draw
|
|
|
|
}
|
|
|
|
|
2022-03-12 15:47:20 +00:00
|
|
|
void CCubeModel::EnableShadowMaps(const CTexture& shadowTex, const zeus::CTransform& textureProjXf,
|
|
|
|
GX::LightMask chan0DisableMask, GX::LightMask chan1EnableLightMask) {
|
2022-02-26 07:05:59 +00:00
|
|
|
sRenderModelShadow = true;
|
|
|
|
sShadowTexture = &shadowTex;
|
|
|
|
sTextureProjectionTransform = textureProjXf;
|
|
|
|
sChannel0DisableLightMask = chan0DisableMask;
|
|
|
|
sChannel1EnableLightMask = chan1EnableLightMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCubeModel::DisableShadowMaps() { sRenderModelShadow = false; }
|
|
|
|
|
|
|
|
void CCubeModel::SetArraysCurrent() {
|
2022-03-12 15:47:20 +00:00
|
|
|
CGX::SetArray(GX::VA_POS, x0_modelInstance.GetVertexPointer());
|
|
|
|
CGX::SetArray(GX::VA_NRM, x0_modelInstance.GetNormalPointer());
|
2022-02-26 07:05:59 +00:00
|
|
|
SetStaticArraysCurrent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCubeModel::SetDrawingOccluders(bool v) { sDrawingOccluders = v; }
|
|
|
|
|
|
|
|
void CCubeModel::SetModelWireframe(bool v) { sDrawingWireframe = v; }
|
|
|
|
|
|
|
|
void CCubeModel::SetNewPlayerPositionAndTime(const zeus::CVector3f& pos, const CStopwatch& time) {
|
|
|
|
sPlayerPosition = pos;
|
|
|
|
CCubeMaterial::KillCachedViewDepState();
|
|
|
|
// TODO time
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCubeModel::SetRenderModelBlack(bool v) {
|
|
|
|
sRenderModelBlack = v;
|
|
|
|
// TODO another value is set here, but always 0?
|
|
|
|
}
|
|
|
|
|
2022-03-10 06:33:17 +00:00
|
|
|
void CCubeModel::SetSkinningArraysCurrent(TConstVectorRef positions, TConstVectorRef normals) {
|
2022-03-12 15:47:20 +00:00
|
|
|
CGX::SetArray(GX::VA_POS, positions);
|
|
|
|
CGX::SetArray(GX::VA_NRM, normals);
|
2022-03-06 20:58:06 +00:00
|
|
|
// colors unused
|
2022-02-26 07:05:59 +00:00
|
|
|
SetStaticArraysCurrent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCubeModel::SetStaticArraysCurrent() {
|
2022-03-06 20:58:06 +00:00
|
|
|
// colors unused
|
2022-02-26 07:05:59 +00:00
|
|
|
const auto* packedTexCoords = x0_modelInstance.GetPackedTCPointer();
|
|
|
|
const auto* texCoords = x0_modelInstance.GetTCPointer();
|
|
|
|
if (packedTexCoords == nullptr) {
|
|
|
|
sUsingPackedLightmaps = false;
|
|
|
|
}
|
|
|
|
if (sUsingPackedLightmaps) {
|
2022-03-12 15:47:20 +00:00
|
|
|
CGX::SetArray(GX::VA_TEX0, packedTexCoords);
|
2022-03-08 07:44:46 +00:00
|
|
|
} else {
|
2022-03-12 15:47:20 +00:00
|
|
|
CGX::SetArray(GX::VA_TEX0, texCoords);
|
2022-02-26 07:05:59 +00:00
|
|
|
}
|
2022-03-12 15:47:20 +00:00
|
|
|
// TexCoord1 is currently used for all remaining
|
|
|
|
CGX::SetArray(GX::VA_TEX1, texCoords);
|
2022-02-26 07:05:59 +00:00
|
|
|
CCubeMaterial::KillCachedViewDepState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCubeModel::SetUsingPackedLightmaps(bool v) {
|
|
|
|
sUsingPackedLightmaps = v;
|
|
|
|
if (v) {
|
2022-03-12 15:47:20 +00:00
|
|
|
CGX::SetArray(GX::VA_TEX0, x0_modelInstance.GetPackedTCPointer());
|
2022-02-26 07:05:59 +00:00
|
|
|
} else {
|
2022-03-12 15:47:20 +00:00
|
|
|
CGX::SetArray(GX::VA_TEX0, x0_modelInstance.GetTCPointer());
|
2022-02-26 07:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 07:45:25 +00:00
|
|
|
} // namespace metaforce
|