Skip to content

Commit e72b05a

Browse files
committed
Reimpl CWorldShadow, OnScreenTex fixes
1 parent 166f901 commit e72b05a

20 files changed

+268
-225
lines changed

Runtime/AutoMapper/CAutoMapper.cpp

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,17 +1449,9 @@ void CAutoMapper::Draw(const CStateManager& mgr, const zeus::CTransform& xf, flo
14491449
if (IsInMapperState(EAutoMapperState::MapScreen)) {
14501450
CAssetId wldMlvl = x24_world->IGetWorldAssetId();
14511451
const CMapWorld* mw = x24_world->IGetMapWorld();
1452-
// std::vector<CTexturedQuadFilter>& hintBeaconFilters = m_hintBeaconFilters;
1453-
// if (hintBeaconFilters.size() < x1f8_hintLocations.size()) {
1454-
// hintBeaconFilters.reserve(x1f8_hintLocations.size());
1455-
// for (u32 i = hintBeaconFilters.size(); i < x1f8_hintLocations.size(); ++i)
1456-
// hintBeaconFilters.emplace_back(EFilterType::Add, x3c_hintBeacon);
1457-
// }
14581452
auto locIt = x1f8_hintLocations.cbegin();
1459-
// auto filterIt = hintBeaconFilters.begin();
1460-
for (; locIt != x1f8_hintLocations.cend(); ++locIt/*, ++filterIt*/) {
1453+
for (; locIt != x1f8_hintLocations.cend(); ++locIt) {
14611454
const SAutoMapperHintLocation& loc = *locIt;
1462-
// CTexturedQuadFilter& filter = *filterIt;
14631455
if (loc.x8_worldId != wldMlvl)
14641456
continue;
14651457
const CMapArea* mapa = mw->GetMapArea(loc.xc_areaId);
@@ -1474,22 +1466,26 @@ void CAutoMapper::Draw(const CStateManager& mgr, const zeus::CTransform& xf, flo
14741466
beaconAlpha = loc.x4_beaconAlpha;
14751467
}
14761468
if (beaconAlpha > 0.f) {
1477-
// constexpr std::array<CTexturedQuadFilter::Vert, 4> verts{{
1478-
// {{-4.f, -8.f, 8.f}, {0.f, 1.f}},
1479-
// {{-4.f, -8.f, 0.f}, {0.f, 0.f}},
1480-
// {{4.f, -8.f, 8.f}, {1.f, 1.f}},
1481-
// {{4.f, -8.f, 0.f}, {1.f, 0.f}},
1482-
// }};
1483-
float colorAlpha = beaconAlpha;
1484-
if (x1bc_state != EAutoMapperState::MiniMap && x1c0_nextState != EAutoMapperState::MiniMap) {
1485-
} else {
1486-
colorAlpha *= xa8_renderStates[0].x34_alphaSurfaceVisited;
1487-
}
1488-
colorAlpha *= mapAlpha;
1469+
CGraphics::SetTevOp(ERglTevStage::Stage0, CTevCombiners::kEnvModulate);
1470+
x3c_hintBeacon->Load(GX_TEXMAP0, EClampMode::Repeat);
1471+
g_Renderer->SetBlendMode_AdditiveAlpha();
1472+
CGraphics::StreamBegin(ERglPrimitive::TriangleStrip);
14891473
zeus::CColor color = zeus::skWhite;
1490-
color.a() = colorAlpha;
1491-
// TODO
1492-
// filter.drawVerts(color, verts);
1474+
color.a() = beaconAlpha *
1475+
((x1bc_state != EAutoMapperState::MiniMap && x1c0_nextState != EAutoMapperState::MiniMap)
1476+
? 1.f
1477+
: xa8_renderStates[0].x34_alphaSurfaceVisited) *
1478+
mapAlpha;
1479+
CGraphics::StreamColor(color);
1480+
CGraphics::StreamTexcoord(0.f, 1.f);
1481+
CGraphics::StreamVertex(zeus::CVector3f(-4.f, -8.f, 8.f));
1482+
CGraphics::StreamTexcoord(0.f, 0.f);
1483+
CGraphics::StreamVertex(zeus::CVector3f(-4.f, -8.f, 0.f));
1484+
CGraphics::StreamTexcoord(1.f, 1.f);
1485+
CGraphics::StreamVertex(zeus::CVector3f(4.f, -8.f, 8.f));
1486+
CGraphics::StreamTexcoord(1.f, 0.f);
1487+
CGraphics::StreamVertex(zeus::CVector3f(4.f, -8.f, 0.f));
1488+
CGraphics::StreamEnd();
14931489
}
14941490
}
14951491
}

Runtime/AutoMapper/CMapUniverse.cpp

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ CMapUniverse::CMapUniverse(CInputStream& in, u32 version) : x0_hexagonId(in.Get<
1616

1717
CMapUniverse::CMapWorldData::CMapWorldData(CInputStream& in, u32 version)
1818
: x0_label(in.Get<std::string>()), x10_worldAssetId(in) {
19-
x14_transform = in.Get<zeus::CTransform>();
19+
x14_transform = in.Get<zeus::CTransform>();
2020
const u32 worldCount = in.ReadLong();
2121
x44_hexagonXfs.reserve(worldCount);
2222
for (u32 i = 0; i < worldCount; ++i) {
@@ -79,37 +79,40 @@ void CMapUniverse::Draw(const CMapUniverseDrawParms& parms, const zeus::CVector3
7979
}
8080
}
8181

82-
std::sort(sortInfos.begin(), sortInfos.end(), [](const CMapObjectSortInfo& a, const CMapObjectSortInfo& b) {
83-
return a.GetZDistance() > b.GetZDistance();
84-
});
85-
86-
int lastWldIdx = -1;
87-
int lastHexIdx = -1;
88-
for (const CMapObjectSortInfo& info : sortInfos) {
89-
const CMapWorldData& mwData = x10_worldDatas[info.GetWorldIndex()];
90-
zeus::CColor surfColor = info.GetSurfaceColor();
91-
zeus::CColor outlineColor = info.GetOutlineColor();
92-
if (parms.GetWorldAssetId() == mwData.GetWorldAssetId() && parms.GetClosestArea() == info.GetAreaIndex()) {
93-
surfColor = zeus::CColor::lerp(g_tweakAutoMapper->GetSurfaceSelectVisitedColor(),
94-
g_tweakAutoMapper->GetAreaFlashPulseColor(), parms.GetFlashPulse());
95-
surfColor.a() = info.GetSurfaceColor().a();
96-
outlineColor = zeus::CColor::lerp(g_tweakAutoMapper->GetOutlineSelectVisitedColor(),
97-
g_tweakAutoMapper->GetAreaFlashPulseColor(), parms.GetFlashPulse());
98-
outlineColor.a() = info.GetOutlineColor().a();
99-
}
82+
if (!sortInfos.empty()) {
83+
std::sort(sortInfos.begin(), sortInfos.end(), [](const CMapObjectSortInfo& a, const CMapObjectSortInfo& b) {
84+
return a.GetZDistance() > b.GetZDistance();
85+
});
86+
CMapArea::CMapAreaSurface::SetupGXMaterial();
87+
88+
int lastWldIdx = -1;
89+
int lastHexIdx = -1;
90+
for (const CMapObjectSortInfo& info : sortInfos) {
91+
const CMapWorldData& mwData = x10_worldDatas[info.GetWorldIndex()];
92+
zeus::CColor surfColor = info.GetSurfaceColor();
93+
zeus::CColor outlineColor = info.GetOutlineColor();
94+
if (parms.GetWorldAssetId() == mwData.GetWorldAssetId() && parms.GetClosestArea() == info.GetAreaIndex()) {
95+
surfColor = zeus::CColor::lerp(g_tweakAutoMapper->GetSurfaceSelectVisitedColor(),
96+
g_tweakAutoMapper->GetAreaFlashPulseColor(), parms.GetFlashPulse());
97+
surfColor.a() = info.GetSurfaceColor().a();
98+
outlineColor = zeus::CColor::lerp(g_tweakAutoMapper->GetOutlineSelectVisitedColor(),
99+
g_tweakAutoMapper->GetAreaFlashPulseColor(), parms.GetFlashPulse());
100+
outlineColor.a() = info.GetOutlineColor().a();
101+
}
100102

101-
zeus::CTransform hexXf = mwData.GetMapAreaData(info.GetAreaIndex());
102-
hexXf.orthonormalize();
103-
CMapArea::CMapAreaSurface& surf = x4_hexagonToken->GetSurface(info.GetObjectIndex());
104-
zeus::CColor color(std::max(0.f, (-parms.GetCameraTransform().basis[1]).dot(hexXf.rotate(surf.GetNormal()))) *
105-
g_tweakAutoMapper->GetMapSurfaceNormColorLinear() +
106-
g_tweakAutoMapper->GetMapSurfaceNormColorConstant());
107-
surfColor *= color;
103+
zeus::CTransform hexXf = mwData.GetMapAreaData(info.GetAreaIndex());
104+
hexXf.orthonormalize();
105+
CMapArea::CMapAreaSurface& surf = x4_hexagonToken->GetSurface(info.GetObjectIndex());
106+
zeus::CColor color(std::max(0.f, (-parms.GetCameraTransform().basis[1]).dot(hexXf.rotate(surf.GetNormal()))) *
107+
g_tweakAutoMapper->GetMapSurfaceNormColorLinear() +
108+
g_tweakAutoMapper->GetMapSurfaceNormColorConstant());
109+
surfColor *= color;
108110

109-
if (info.GetAreaIndex() != lastHexIdx || info.GetWorldIndex() != lastWldIdx)
110-
CGraphics::SetModelMatrix(parms.GetPaneProjectionTransform() * mwData.GetMapAreaData(info.GetAreaIndex()));
111+
if (info.GetAreaIndex() != lastHexIdx || info.GetWorldIndex() != lastWldIdx)
112+
CGraphics::SetModelMatrix(parms.GetPaneProjectionTransform() * mwData.GetMapAreaData(info.GetAreaIndex()));
111113

112-
surf.Draw(x4_hexagonToken->GetVertices(), surfColor, outlineColor, 2.f);
114+
surf.Draw(x4_hexagonToken->GetVertices(), surfColor, outlineColor, 2.f);
115+
}
113116
}
114117
}
115118

Runtime/CStateManager.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ struct SScriptObjectStream {
6969

7070
struct SOnScreenTex {
7171
CAssetId x0_id;
72-
zeus::CVector2i x4_origin;
73-
zeus::CVector2i xc_extent;
72+
zeus::CVector2i x4_extent;
73+
zeus::CVector2i xc_origin;
7474
};
7575

7676
enum class EStateManagerTransition { InGame, MapScreen, PauseGame, LogBook, SaveGame, MessageScreen };
@@ -428,8 +428,8 @@ class CStateManager {
428428
u32 GetBossStringIdx() const { return xf20_bossStringIdx; }
429429
void SetPendingOnScreenTex(CAssetId texId, const zeus::CVector2i& origin, const zeus::CVector2i& extent) {
430430
xef4_pendingScreenTex.x0_id = texId;
431-
xef4_pendingScreenTex.x4_origin = origin;
432-
xef4_pendingScreenTex.xc_extent = extent;
431+
xef4_pendingScreenTex.x4_extent = origin;
432+
xef4_pendingScreenTex.xc_origin = extent;
433433
}
434434
const SOnScreenTex& GetPendingScreenTex() const { return xef4_pendingScreenTex; }
435435
void SetViewportScale(const zeus::CVector2f& scale) { xf2c_viewportScale = scale; }

Runtime/Character/CAnimData.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,9 @@ void CAnimData::SetupRender(CSkinnedModel& model, CVertexMorphEffect* morphEffec
560560
}
561561

562562
void CAnimData::DrawSkinnedModel(CSkinnedModel& model, const CModelFlags& flags) {
563-
CGX::SetChanCtrl(CGX::EChannelId::Channel0, CGraphics::mLightActive);
563+
CGX::SetChanCtrl(CGX::EChannelId::Channel0, GX_ENABLE, GX_SRC_REG, GX_SRC_REG, CGraphics::mLightActive,
564+
CGraphics::mLightActive.any() ? GX_DF_CLAMP : GX_DF_NONE,
565+
CGraphics::mLightActive.any() ? GX_AF_SPOT : GX_AF_NONE);
564566
model.Draw(flags);
565567
}
566568

Runtime/Graphics/CCubeMaterial.cpp

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "Graphics/CCubeSurface.hpp"
77
#include "Graphics/CGX.hpp"
88
#include "Graphics/CModel.hpp"
9+
#include "Logging.hpp"
910

1011
namespace metaforce {
1112
static u32 sReflectionType = 0;
@@ -307,16 +308,21 @@ void CCubeMaterial::EnsureViewDepStateCached(const CCubeSurface* surface) {
307308
}
308309

309310
u32 CCubeMaterial::HandleColorChannels(u32 chanCount, u32 firstChan) {
311+
static constexpr GXColor sGXBlack = {0, 0, 0, 0};
312+
static constexpr GXColor sGXWhite = {0xFF, 0xFF, 0xFF, 0xFF};
313+
310314
if (CCubeModel::sRenderModelShadow) {
311315
if (chanCount != 0) {
312-
CGX::SetChanAmbColor(CGX::EChannelId::Channel1, GX_BLACK);
313-
CGX::SetChanMatColor(CGX::EChannelId::Channel1, GX_WHITE);
316+
CGX::SetChanAmbColor(CGX::EChannelId::Channel1, sGXBlack);
317+
CGX::SetChanMatColor(CGX::EChannelId::Channel1, sGXWhite);
318+
319+
CGX::SetChanCtrl(CGX::EChannelId::Channel1, true, GX_SRC_REG, GX_SRC_REG, CCubeModel::sChannel1EnableLightMask,
320+
GX_DF_CLAMP, GX_AF_SPOT);
314321

315-
auto chan0Lights = CGraphics::mLightActive & ~CCubeModel::sChannel0DisableLightMask;
316-
CGX::SetChanCtrl(CGX::EChannelId::Channel0, firstChan, chan0Lights);
317-
CGX::SetChanCtrl(CGX::EChannelId::Channel1, CCubeModel::sChannel1EnableLightMask);
322+
const auto chan0Lights = CGraphics::GetLightMask() & ~CCubeModel::sChannel0DisableLightMask;
323+
CGX::SetChanCtrl_Compressed(CGX::EChannelId::Channel0, chan0Lights, firstChan);
318324
if (chan0Lights.any()) {
319-
CGX::SetChanMatColor(CGX::EChannelId::Channel0, GX_WHITE);
325+
CGX::SetChanMatColor(CGX::EChannelId::Channel0, sGXWhite);
320326
} else {
321327
CGX::SetChanMatColor(CGX::EChannelId::Channel0, CGX::GetChanAmbColor(CGX::EChannelId::Channel0));
322328
}
@@ -325,21 +331,22 @@ u32 CCubeMaterial::HandleColorChannels(u32 chanCount, u32 firstChan) {
325331
}
326332

327333
if (chanCount == 2) {
328-
CGX::SetChanAmbColor(CGX::EChannelId::Channel1, GX_BLACK);
329-
CGX::SetChanMatColor(CGX::EChannelId::Channel1, GX_WHITE);
334+
CGX::SetChanAmbColor(CGX::EChannelId::Channel1, sGXBlack);
335+
CGX::SetChanMatColor(CGX::EChannelId::Channel1, sGXWhite);
330336
} else {
331-
CGX::SetChanCtrl(CGX::EChannelId::Channel1, {});
337+
CGX::SetChanCtrl(CGX::EChannelId::Channel1, false, GX_SRC_REG, GX_SRC_REG, GX_LIGHT_NULL, GX_DF_NONE, GX_AF_NONE);
332338
}
333339

334-
if (chanCount == 0) {
335-
CGX::SetChanCtrl(CGX::EChannelId::Channel0, {});
336-
} else {
337-
CGX::SetChanCtrl(CGX::EChannelId::Channel0, firstChan, CGraphics::mLightActive);
338-
if (CGraphics::mLightActive.any()) {
339-
CGX::SetChanMatColor(CGX::EChannelId::Channel0, GX_WHITE);
340+
if (chanCount >= 1) {
341+
const auto lightMask = CGraphics::GetLightMask();
342+
CGX::SetChanCtrl_Compressed(CGX::EChannelId::Channel0, lightMask, firstChan);
343+
if (lightMask.any()) {
344+
CGX::SetChanMatColor(CGX::EChannelId::Channel0, sGXWhite);
340345
} else {
341346
CGX::SetChanMatColor(CGX::EChannelId::Channel0, CGX::GetChanAmbColor(CGX::EChannelId::Channel0));
342347
}
348+
} else {
349+
CGX::SetChanCtrl(CGX::EChannelId::Channel0, false, GX_SRC_REG, GX_SRC_REG, GX_LIGHT_NULL, GX_DF_NONE, GX_AF_NONE);
343350
}
344351

345352
return chanCount;
@@ -603,8 +610,29 @@ void CCubeMaterial::DoPassthru(u32 finalTevCount) {
603610
}
604611

605612
void CCubeMaterial::DoModelShadow(u32 texCount, u32 tcgCount) {
606-
// CCubeModel::sShadowTexture->Load(texCount, EClampMode::One);
607-
// TODO
613+
CCubeModel::sShadowTexture->Load(static_cast<GXTexMapID>(texCount), EClampMode::Repeat);
614+
const auto& xf = CCubeModel::sTextureProjectionTransform;
615+
Mtx mtx = {
616+
{xf.basis[0][0], xf.basis[1][0], xf.basis[2][0], xf.origin.x()},
617+
{xf.basis[0][2], xf.basis[1][2], xf.basis[2][2], xf.origin.z()},
618+
{0.f, 0.f, 0.f, 1.f},
619+
};
620+
GXLoadTexMtxImm(mtx, GX_TEXMTX5, GX_MTX3x4);
621+
622+
CGX::SetTexCoordGen(static_cast<GXTexCoordID>(tcgCount), GX_TG_MTX3x4, GX_TG_POS, GX_TEXMTX5, GX_FALSE,
623+
GX_PTIDENTITY);
624+
CGX::SetTevColorOp(GX_TEVSTAGE0, GX_TEV_ADD, GX_TB_ZERO, GX_CS_SCALE_1, GX_TRUE, GX_TEVREG0);
625+
CGX::SetTevAlphaOp(GX_TEVSTAGE0, GX_TEV_ADD, GX_TB_ZERO, GX_CS_SCALE_1, GX_TRUE, GX_TEVREG0);
626+
CGX::SetTevColorIn(GX_TEVSTAGE0, GX_CC_ZERO, GX_CC_RASC, GX_CC_TEXC, GX_CC_ZERO);
627+
CGX::SetTevAlphaIn(GX_TEVSTAGE0, GX_CA_ZERO, GX_CA_ZERO, GX_CA_ZERO, GX_CA_RASA);
628+
CGX::SetTevOrder(GX_TEVSTAGE0, static_cast<GXTexCoordID>(tcgCount), static_cast<GXTexMapID>(texCount), GX_COLOR1A1);
629+
630+
CGX::SetTevColorOp(GX_TEVSTAGE1, GX_TEV_ADD, GX_TB_ZERO, GX_CS_SCALE_1, GX_TRUE, GX_TEVREG0);
631+
CGX::SetTevAlphaOp(GX_TEVSTAGE1, GX_TEV_ADD, GX_TB_ZERO, GX_CS_SCALE_1, GX_TRUE, GX_TEVREG0);
632+
CGX::SetTevColorIn(GX_TEVSTAGE1, GX_CC_ZERO, GX_CC_RASC, GX_CC_ONE, GX_CC_C0);
633+
CGX::SetTevAlphaIn(GX_TEVSTAGE1, GX_CA_ZERO, GX_CA_RASA, GX_CA_KONST, GX_CA_A0);
634+
CGX::SetTevKAlphaSel(GX_TEVSTAGE1, GX_TEV_KASEL_1);
635+
CGX::SetTevOrder(GX_TEVSTAGE1, GX_TEXCOORD_NULL, GX_TEXMAP_NULL, GX_COLOR0A0);
608636
}
609637

610638
static GXTevStageID sCurrentTevStage = GX_MAX_TEVSTAGE;

Runtime/Graphics/CCubeModel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace metaforce {
1111
bool CCubeModel::sRenderModelBlack = false;
1212
bool CCubeModel::sRenderModelShadow = false;
1313
bool CCubeModel::sUsingPackedLightmaps = false;
14-
const CTexture* CCubeModel::sShadowTexture = nullptr;
14+
CTexture* CCubeModel::sShadowTexture = nullptr;
1515
zeus::CTransform CCubeModel::sTextureProjectionTransform;
1616
GX::LightMask CCubeModel::sChannel0DisableLightMask;
1717
GX::LightMask CCubeModel::sChannel1EnableLightMask;
@@ -246,7 +246,7 @@ void CCubeModel::DrawSurfaceWireframe(const CCubeSurface& surface) {
246246
// TODO convert vertices to line strips and draw
247247
}
248248

249-
void CCubeModel::EnableShadowMaps(const CTexture& shadowTex, const zeus::CTransform& textureProjXf,
249+
void CCubeModel::EnableShadowMaps(CTexture& shadowTex, const zeus::CTransform& textureProjXf,
250250
GX::LightMask chan0DisableMask, GX::LightMask chan1EnableLightMask) {
251251
sRenderModelShadow = true;
252252
sShadowTexture = &shadowTex;

Runtime/Graphics/CCubeModel.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class CCubeModel {
121121
}
122122
[[nodiscard]] TCachedToken<CTexture>& GetTexture(u32 idx) const { return x1c_textures->at(idx); }
123123

124-
static void EnableShadowMaps(const CTexture& shadowTex, const zeus::CTransform& textureProjXf,
124+
static void EnableShadowMaps(CTexture& shadowTex, const zeus::CTransform& textureProjXf,
125125
GX::LightMask chan0DisableMask, GX::LightMask chan1EnableLightMask);
126126
static void DisableShadowMaps();
127127
static void MakeTexturesFromMats(const u8* ptr, std::vector<TCachedToken<CTexture>>& texture, IObjectStore* store,
@@ -142,7 +142,7 @@ class CCubeModel {
142142
static bool sRenderModelBlack;
143143
static bool sUsingPackedLightmaps;
144144
static bool sRenderModelShadow;
145-
static const CTexture* sShadowTexture;
145+
static CTexture* sShadowTexture;
146146
static zeus::CTransform sTextureProjectionTransform;
147147
static GX::LightMask sChannel0DisableLightMask;
148148
static GX::LightMask sChannel1EnableLightMask;

0 commit comments

Comments
 (0)