Skip to content

Commit a17fe8c

Browse files
committed
* Sort players back to front so player infills make sense.
1 parent 8e782d6 commit a17fe8c

2 files changed

Lines changed: 55 additions & 52 deletions

File tree

CastingEssentials/Modules/Graphics.cpp

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ Graphics::Graphics()
6565
m_ApplyEntityGlowEffectsHook = GetHooks()->AddHook<CGlowObjectManager_ApplyEntityGlowEffects>(std::bind(&Graphics::ApplyEntityGlowEffectsOverride, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5, std::placeholders::_6, std::placeholders::_7, std::placeholders::_8, std::placeholders::_9));
6666

6767
m_ForcedMaterialOverrideHook = GetHooks()->AddHook<IStudioRender_ForcedMaterialOverride>(std::bind(&Graphics::ForcedMaterialOverrideOverride, this, std::placeholders::_1, std::placeholders::_2));
68-
69-
m_GlowObjectDefinitions = nullptr;
7068
}
7169

7270
Graphics::~Graphics()
@@ -290,10 +288,10 @@ void Graphics::ForcedMaterialOverrideOverride(IMaterial* material, OverrideType_
290288

291289
Graphics::ExtraGlowData* Graphics::FindExtraGlowData(int entindex)
292290
{
293-
for (int i = 0; i < m_GlowObjectDefinitions->Count(); i++)
291+
for (auto& extraGlowData : m_ExtraGlowData)
294292
{
295-
if (m_GlowObjectDefinitions->Element(i).m_hEntity.GetEntryIndex() == entindex)
296-
return &m_ExtraGlowData[i];
293+
if (extraGlowData.m_Base->m_hEntity.GetEntryIndex() == entindex)
294+
return &extraGlowData;
297295
}
298296

299297
return nullptr;
@@ -526,6 +524,7 @@ void Graphics::BuildExtraGlowData(CGlowObjectManager* glowMgr)
526524
uint8_t stencilIndex = 0;
527525

528526
const bool infillsEnable = ce_infills_enable->GetBool();
527+
const bool bInfillDebug = ce_infills_debug->GetBool();
529528

530529
const Color redInfillNormal = ColorFromConVar(*ce_infills_hurt_red);
531530
const Color blueInfillNormal = ColorFromConVar(*ce_infills_hurt_blue);
@@ -536,14 +535,13 @@ void Graphics::BuildExtraGlowData(CGlowObjectManager* glowMgr)
536535
if (infillsEnable)
537536
Interfaces::GetEngineTool()->GetWorldToScreenMatrixForView(*m_View, &worldToScreen);
538537

539-
const bool bInfillDebug = ce_infills_debug->GetBool();
540538

541539
//int nidx = 0;
542540
for (int i = 0; i < glowMgr->m_GlowObjectDefinitions.Count(); i++)
543541
{
544-
const auto& current = glowMgr->m_GlowObjectDefinitions[i];
542+
auto& current = glowMgr->m_GlowObjectDefinitions[i];
545543

546-
m_ExtraGlowData.emplace_back();
544+
m_ExtraGlowData.emplace_back(&current);
547545
auto& currentExtra = m_ExtraGlowData.back();
548546

549547
if (Player::IsValidIndex(current.m_hEntity.GetEntryIndex()))
@@ -642,6 +640,22 @@ void Graphics::BuildExtraGlowData(CGlowObjectManager* glowMgr)
642640
}
643641
}
644642

643+
// If we're doing infills, sort the vector so we render back to front
644+
if (infillsEnable)
645+
{
646+
std::sort(m_ExtraGlowData.begin(), m_ExtraGlowData.end(), [](const ExtraGlowData& lhs, const ExtraGlowData& rhs) -> bool
647+
{
648+
const auto& origin = Graphics::GetModule()->m_View->origin;
649+
const auto& ent1 = lhs.m_Base->m_hEntity.Get();
650+
const auto& ent2 = rhs.m_Base->m_hEntity.Get();
651+
652+
const float dist1 = ent1 ? ent1->GetAbsOrigin().DistToSqr(origin) : 0;
653+
const float dist2 = ent2 ? ent2->GetAbsOrigin().DistToSqr(origin) : 0;
654+
655+
return dist2 < dist1;
656+
});
657+
}
658+
645659
// Build ourselves a map of move children (we need extra glow data before we can do this)
646660
BuildMoveChildLists();
647661
}
@@ -717,9 +731,8 @@ void Graphics::DrawInfills(CMatRenderContextPtr& pRenderContext)
717731
pRenderContext->LoadIdentity();
718732
#endif
719733

720-
for (int i = 0; i < m_GlowObjectDefinitions->Count(); i++)
734+
for (const auto& currentExtra : m_ExtraGlowData)
721735
{
722-
const auto& currentExtra = m_ExtraGlowData[i];
723736
if (!currentExtra.m_InfillEnabled)
724737
continue;
725738

@@ -864,31 +877,28 @@ void Graphics::DrawGlowAlways(int nSplitScreenSlot, CMatRenderContextPtr& pRende
864877

865878
pRenderContext->OverrideDepthEnable(false, false);
866879
render->SetBlend(1);
867-
for (int i = 0; i < m_GlowObjectDefinitions->Count(); i++)
880+
for (const auto& current : m_ExtraGlowData)
868881
{
869-
auto& current = m_GlowObjectDefinitions->Element(i);
870-
if (current.IsUnused() || !current.ShouldDraw(nSplitScreenSlot) || !current.m_bRenderWhenOccluded || !current.m_bRenderWhenUnoccluded)
882+
if (current.m_Base->IsUnused() || !current.m_Base->ShouldDraw(nSplitScreenSlot) || !current.m_Base->m_bRenderWhenOccluded || !current.m_Base->m_bRenderWhenUnoccluded)
871883
continue;
872884

873-
const auto& extra = m_ExtraGlowData[i];
874-
875-
if (extra.m_ShouldOverrideGlowColor)
876-
render->SetColorModulation(extra.m_GlowColorOverride.Base());
885+
if (current.m_ShouldOverrideGlowColor)
886+
render->SetColorModulation(current.m_GlowColorOverride.Base());
877887
else
878888
{
879-
const Vector vGlowColor = current.m_vGlowColor * (current.m_flGlowAlpha * ce_graphics_glow_intensity->GetFloat());
889+
const Vector vGlowColor = current.m_Base->m_vGlowColor * (current.m_Base->m_flGlowAlpha * ce_graphics_glow_intensity->GetFloat());
880890
render->SetColorModulation(vGlowColor.Base());
881891
}
882892

883-
if (extra.m_InfillEnabled)
893+
if (current.m_InfillEnabled)
884894
{
885895
pRenderContext->SetStencilWriteMask(0xFFFFFFFF);
886-
pRenderContext->SetStencilReferenceValue((extra.m_StencilIndex << 2) | 1);
896+
pRenderContext->SetStencilReferenceValue((current.m_StencilIndex << 2) | 1);
887897
}
888898
else
889899
pRenderContext->SetStencilWriteMask(1);
890900

891-
current.DrawModel();
901+
current.m_Base->DrawModel();
892902
}
893903
}
894904

@@ -949,13 +959,12 @@ void Graphics::DrawGlowOccluded(int nSplitScreenSlot, CMatRenderContextPtr& pRen
949959
pRenderContext->OverrideAlphaWriteEnable(true, false);
950960
pRenderContext->OverrideColorWriteEnable(true, false);
951961

952-
for (int i = 0; i < m_GlowObjectDefinitions->Count(); i++)
962+
for (const auto& current : m_ExtraGlowData)
953963
{
954-
auto& current = m_GlowObjectDefinitions->Element(i);
955-
if (current.IsUnused() || !current.ShouldDraw(nSplitScreenSlot) || !current.m_bRenderWhenOccluded || current.m_bRenderWhenUnoccluded)
964+
if (current.m_Base->IsUnused() || !current.m_Base->ShouldDraw(nSplitScreenSlot) || !current.m_Base->m_bRenderWhenOccluded || current.m_Base->m_bRenderWhenUnoccluded)
956965
continue;
957966

958-
current.DrawModel();
967+
current.m_Base->DrawModel();
959968
}
960969
}
961970

@@ -967,7 +976,7 @@ void Graphics::DrawGlowOccluded(int nSplitScreenSlot, CMatRenderContextPtr& pRen
967976
stencilState.m_bEnable = true;
968977
stencilState.m_nReferenceValue = 3;
969978
stencilState.m_nTestMask = 2;
970-
stencilState.m_nWriteMask = 0xFFFFFFFF;
979+
stencilState.m_nWriteMask = 1;
971980
stencilState.m_CompareFunc = STENCILCOMPARISONFUNCTION_NOTEQUAL;
972981
stencilState.m_PassOp = STENCILOPERATION_REPLACE;
973982
stencilState.m_ZFailOp = STENCILOPERATION_REPLACE;
@@ -976,31 +985,28 @@ void Graphics::DrawGlowOccluded(int nSplitScreenSlot, CMatRenderContextPtr& pRen
976985

977986
// Draw color+alpha, stenciling out pixels from the first pass
978987
render->SetBlend(1);
979-
for (int i = 0; i < m_GlowObjectDefinitions->Count(); i++)
988+
for (const auto& current : m_ExtraGlowData)
980989
{
981-
auto& current = m_GlowObjectDefinitions->Element(i);
982-
if (current.IsUnused() || !current.ShouldDraw(nSplitScreenSlot) || !current.m_bRenderWhenOccluded || current.m_bRenderWhenUnoccluded)
990+
if (current.m_Base->IsUnused() || !current.m_Base->ShouldDraw(nSplitScreenSlot) || !current.m_Base->m_bRenderWhenOccluded || current.m_Base->m_bRenderWhenUnoccluded)
983991
continue;
984992

985-
const auto& extra = m_ExtraGlowData[i];
986-
987-
if (extra.m_ShouldOverrideGlowColor)
988-
render->SetColorModulation(extra.m_GlowColorOverride.Base());
993+
if (current.m_ShouldOverrideGlowColor)
994+
render->SetColorModulation(current.m_GlowColorOverride.Base());
989995
else
990996
{
991-
const Vector vGlowColor = current.m_vGlowColor * (current.m_flGlowAlpha * ce_graphics_glow_intensity->GetFloat());
997+
const Vector vGlowColor = current.m_Base->m_vGlowColor * (current.m_Base->m_flGlowAlpha * ce_graphics_glow_intensity->GetFloat());
992998
render->SetColorModulation(vGlowColor.Base());
993999
}
9941000

995-
if (extra.m_InfillEnabled)
1001+
if (current.m_InfillEnabled)
9961002
{
9971003
pRenderContext->SetStencilWriteMask(0xFFFFFFFF);
998-
pRenderContext->SetStencilReferenceValue((extra.m_StencilIndex << 2) | 3);
1004+
pRenderContext->SetStencilReferenceValue((current.m_StencilIndex << 2) | 3);
9991005
}
10001006
else
10011007
pRenderContext->SetStencilWriteMask(1);
10021008

1003-
current.DrawModel();
1009+
current.m_Base->DrawModel();
10041010
}
10051011
#endif
10061012
}
@@ -1019,31 +1025,28 @@ void Graphics::DrawGlowVisible(int nSplitScreenSlot, CMatRenderContextPtr& pRend
10191025

10201026
pRenderContext->OverrideDepthEnable(true, false);
10211027
render->SetBlend(1);
1022-
for (int i = 0; i < m_GlowObjectDefinitions->Count(); i++)
1028+
for (const auto& current : m_ExtraGlowData)
10231029
{
1024-
auto& current = m_GlowObjectDefinitions->Element(i);
1025-
if (current.IsUnused() || !current.ShouldDraw(nSplitScreenSlot) || current.m_bRenderWhenOccluded || !current.m_bRenderWhenUnoccluded)
1030+
if (current.m_Base->IsUnused() || !current.m_Base->ShouldDraw(nSplitScreenSlot) || current.m_Base->m_bRenderWhenOccluded || !current.m_Base->m_bRenderWhenUnoccluded)
10261031
continue;
10271032

1028-
const auto& extra = m_ExtraGlowData[i];
1029-
1030-
if (extra.m_ShouldOverrideGlowColor)
1031-
render->SetColorModulation(extra.m_GlowColorOverride.Base());
1033+
if (current.m_ShouldOverrideGlowColor)
1034+
render->SetColorModulation(current.m_GlowColorOverride.Base());
10321035
else
10331036
{
1034-
const Vector vGlowColor = current.m_vGlowColor * (current.m_flGlowAlpha * ce_graphics_glow_intensity->GetFloat());
1037+
const Vector vGlowColor = current.m_Base->m_vGlowColor * (current.m_Base->m_flGlowAlpha * ce_graphics_glow_intensity->GetFloat());
10351038
render->SetColorModulation(vGlowColor.Base());
10361039
}
10371040

1038-
if (extra.m_InfillEnabled)
1041+
if (current.m_InfillEnabled)
10391042
{
10401043
pRenderContext->SetStencilWriteMask(0xFFFFFFFF);
1041-
pRenderContext->SetStencilReferenceValue((extra.m_StencilIndex << 2) | 1);
1044+
pRenderContext->SetStencilReferenceValue((current.m_StencilIndex << 2) | 1);
10421045
}
10431046
else
10441047
pRenderContext->SetStencilWriteMask(1);
10451048

1046-
current.DrawModel();
1049+
current.m_Base->DrawModel();
10471050
}
10481051
}
10491052

@@ -1076,7 +1079,6 @@ void CGlowObjectManager::ApplyEntityGlowEffects(const CViewSetup* pSetup, int nS
10761079
}
10771080

10781081
auto const graphicsModule = Graphics::GetModule();
1079-
VariablePusher<decltype(&m_GlowObjectDefinitions)> _saveGODefinitions(graphicsModule->m_GlowObjectDefinitions, &m_GlowObjectDefinitions);
10801082
VariablePusher<const CViewSetup*> _saveViewSetup(graphicsModule->m_View, pSetup);
10811083

10821084
// Collect extra glow data we'll use in multiple upcoming loops -- This used to be right
@@ -1371,7 +1373,7 @@ void Graphics::OnTick(bool inGame)
13711373
}
13721374
}
13731375

1374-
Graphics::ExtraGlowData::ExtraGlowData()
1376+
Graphics::ExtraGlowData::ExtraGlowData(CGlowObjectManager::GlowObjectDefinition_t* base) : m_Base(base)
13751377
{
13761378
m_ShouldOverrideGlowColor = false;
13771379
m_InfillEnabled = false;

CastingEssentials/Modules/Graphics.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ class Graphics final : public Module<Graphics>
7272
friend class CGlowObjectManager;
7373
struct ExtraGlowData
7474
{
75-
ExtraGlowData();
75+
ExtraGlowData(CGlowObjectManager::GlowObjectDefinition_t* base);
76+
77+
CGlowObjectManager::GlowObjectDefinition_t* m_Base;
7678

7779
bool m_ShouldOverrideGlowColor;
7880
Vector m_GlowColorOverride;
@@ -95,7 +97,6 @@ class Graphics final : public Module<Graphics>
9597
std::vector<C_BaseEntity*> m_MoveChildren;
9698
};
9799
std::vector<ExtraGlowData> m_ExtraGlowData;
98-
CUtlVector<CGlowObjectManager::GlowObjectDefinition_t>* m_GlowObjectDefinitions;
99100
const CViewSetup* m_View;
100101

101102
void BuildMoveChildLists();

0 commit comments

Comments
 (0)