From b6a77dd839c7d8e70d5cf2ce388b3cd37c7b05c0 Mon Sep 17 00:00:00 2001 From: nullifiedcat Date: Mon, 17 Apr 2017 20:57:11 +0300 Subject: [PATCH 1/3] idk --- src/EffectChams.cpp | 6 +- src/EffectChams.hpp | 4 + src/EffectGlow.cpp | 335 +++++++++++++++++++++++++++---------------- src/EffectGlow.hpp | 23 +-- src/hack.cpp | 10 +- src/hooks/others.cpp | 2 +- 6 files changed, 236 insertions(+), 144 deletions(-) diff --git a/src/EffectChams.cpp b/src/EffectChams.cpp index 00e1aecf..f1f6f792 100644 --- a/src/EffectChams.cpp +++ b/src/EffectChams.cpp @@ -10,6 +10,8 @@ //static CatVar chams_experimental(CV_SWITCH, "chams_effect", "0", "Experimental Chams"); +namespace effect_chams { + CatVar enable(CV_SWITCH, "chams_enable", "0", "Enable"); static CatVar flat(CV_SWITCH, "chams_flat", "0", "Flat"); static CatVar health(CV_SWITCH, "chams_health", "0", "Health"); @@ -57,7 +59,6 @@ void EffectChams::Init() { void EffectChams::BeginRenderChams() { drawing = true; CMatRenderContextPtr ptr(vfunc(g_IMaterialSystem, 100, 0)(g_IMaterialSystem)); - g_IVRenderView->SetBlend(1.0f); } void EffectChams::EndRenderChams() { @@ -170,7 +171,6 @@ void EffectChams::Render(int x, int y, int w, int h) { if (g_IEngine->IsTakingScreenshot() && clean_screenshots) return; if (!enable) return; CMatRenderContextPtr ptr(g_IMaterialSystem->GetRenderContext()); - BeginRenderChams(); for (int i = 1; i < HIGHEST_ENTITY; i++) { IClientEntity* ent = g_IEntityList->GetClientEntity(i); @@ -183,3 +183,5 @@ void EffectChams::Render(int x, int y, int w, int h) { EffectChams g_EffectChams; CScreenSpaceEffectRegistration* g_pEffectChams = nullptr; + +} diff --git a/src/EffectChams.hpp b/src/EffectChams.hpp index 0b2962d0..946d5c71 100644 --- a/src/EffectChams.hpp +++ b/src/EffectChams.hpp @@ -10,6 +10,8 @@ #include "common.h" +namespace effect_chams { + class EffectChams : public IScreenSpaceEffect { public: virtual void Init( ); @@ -41,4 +43,6 @@ public: extern EffectChams g_EffectChams; extern CScreenSpaceEffectRegistration* g_pEffectChams; +} + #endif /* EFFECTCHAMS_HPP_ */ diff --git a/src/EffectGlow.cpp b/src/EffectGlow.cpp index 62a311ed..f9a20705 100644 --- a/src/EffectGlow.cpp +++ b/src/EffectGlow.cpp @@ -10,7 +10,6 @@ IMaterialSystem* materials = nullptr; -// FIXME move to sdk CScreenSpaceEffectRegistration *CScreenSpaceEffectRegistration::s_pHead = NULL; IScreenSpaceEffectManager* g_pScreenSpaceEffects = nullptr; CScreenSpaceEffectRegistration** g_ppScreenSpaceRegistrationHead = nullptr; @@ -24,156 +23,242 @@ CScreenSpaceEffectRegistration::CScreenSpaceEffectRegistration( const char *pNam logging::Info("New head: 0x%08x", *g_ppScreenSpaceRegistrationHead); } -/*IMaterial* GetRenderTarget() { - if (!tr_cathook_rt) { - tr_cathook_rt.InitRenderTarget(256, 256, RT_SIZE_FULL_FRAME_BUFFER, IMAGE_FORMAT_ARGB8888, MATERIAL_RT_DEPTH_NONE, false, "cathook_rt"); - } - return tr_cathook_rt.; -}*/ +namespace effect_glow { -IMaterial* GetGlowMaterial() { - return vfunc(g_IMaterialSystem, 73)(g_IMaterialSystem, "dev/glow_color", TEXTURE_GROUP_OTHER, true, 0); -} - -static CatVar glow_experimental(CV_SWITCH, "glow_experimental", "0", "Experimental Glow"); +CatVar enable(CV_SWITCH, "glow_experimental_enable", "0", "Enable"); +static CatVar health(CV_SWITCH, "glow_experimental_health", "0", "Health"); +static CatVar teammates(CV_SWITCH, "glow_experimental_teammates", "0", "Teammates"); +static CatVar players(CV_SWITCH, "glow_experimental_players", "1", "Players"); +static CatVar medkits(CV_SWITCH, "glow_experimental_medkits", "0", "Medkits"); +static CatVar ammobox(CV_SWITCH, "glow_experimental_ammo", "0", "Ammoboxes"); +static CatVar buildings(CV_SWITCH, "glow_experimental_buildings", "0", "Buildings"); +static CatVar stickies(CV_SWITCH, "glow_experimental_stickies", "0", "Stickies"); +static CatVar teammate_buildings(CV_SWITCH, "glow_experimental_teammate_buildings", "0", "Teammate Buildings"); void EffectGlow::Init() { - logging::Info("Init EffectGlow..."); - rt_A.InitRenderTarget(1920/2, 1080/2, RT_SIZE_DEFAULT, IMAGE_FORMAT_RGBA8888, MATERIAL_RT_DEPTH_SEPARATE, false, "__cathook_glow_rta"); - rt_B.InitRenderTarget(1920/2, 1080/2, RT_SIZE_DEFAULT, IMAGE_FORMAT_RGBA8888, MATERIAL_RT_DEPTH_SEPARATE, false, "__cathook_glow_rtb"); - logging::Info("Textures init!"); - //rt_B.InitRenderTarget(256, 256, RT_SIZE_FULL_FRAME_BUFFER, IMAGE_FORMAT_ARGB8888, MATERIAL_RT_DEPTH_NONE, false, "__cathook_glow_rtB"); - KeyValues *kv2 = new KeyValues( "VertexLitGeneric" ); - kv2->SetString( "$basetexture", "vgui/white" ); - kv2->SetInt( "$selfillum", 1 ); - kv2->SetString( "$selfillummask", "vgui/white" ); - kv2->SetInt( "$vertexalpha", 1 ); - kv2->SetInt( "$model", 1 ); - glow_material.Init( "__cathook_glow_mat_color", TEXTURE_GROUP_CLIENT_EFFECTS, kv2 ); - glow_material->Refresh(); - KeyValues* kv = new KeyValues("UnlitGeneric"); - kv->SetString("$basetexture", "_rt_FullFrameFB"); - kv->SetInt("$additive", 1); - result_material.Init("__cathook_glow_mat", TEXTURE_GROUP_CLIENT_EFFECTS, kv); - result_material->Refresh(); - logging::Info("Material init!"); - //dev_glow_color.Init(vfunc(g_IMaterialSystem, 73)(g_IMaterialSystem, "dev/glow_color", TEXTURE_GROUP_OTHER, true, 0)); - dev_bloomdadd.Init(vfunc(g_IMaterialSystem, 73)(g_IMaterialSystem, "dev/bloomadd", TEXTURE_GROUP_OTHER, true, 0)); - dev_blurfilterx.Init(vfunc(g_IMaterialSystem, 73)(g_IMaterialSystem, "dev/blurfilterx", TEXTURE_GROUP_OTHER, true, 0)); - dev_blurfiltery.Init(vfunc(g_IMaterialSystem, 73)(g_IMaterialSystem, "dev/blurfiltery", TEXTURE_GROUP_OTHER, true, 0)); - dev_halo_add_to_screen.Init(vfunc(g_IMaterialSystem, 73)(g_IMaterialSystem, "dev/halo_add_to_screen", TEXTURE_GROUP_OTHER, true, 0)); - + logging::Info("Init EffectChams..."); + { + KeyValues* kv = new KeyValues("UnlitGeneric"); + kv->SetString("$basetexture", "vgui/white_additive"); + kv->SetInt("$ignorez", 0); + mat_unlit.Init("__cathook_echams_unlit", kv); + } + { + KeyValues* kv = new KeyValues("UnlitGeneric"); + kv->SetString("$basetexture", "vgui/white_additive"); + kv->SetInt("$ignorez", 1); + mat_unlit_z.Init("__cathook_echams_unlit_z", kv); + } logging::Info("Init done!"); init = true; } -void EffectGlow::BeginRenderGlow() { +void EffectGlow::BeginRenderChams() { + drawing = true; CMatRenderContextPtr ptr(vfunc(g_IMaterialSystem, 100, 0)(g_IMaterialSystem)); - ptr->PushRenderTargetAndViewport(rt_A); - g_IVModelRender->SuppressEngineLighting(true); - g_IVRenderView->GetColorModulation(orig_modulation); - static Vector red(1.0f, 0.1f, 0.1f); - g_IVRenderView->SetColorModulation(red.Base()); - g_IStudioRender->ForcedMaterialOverride(glow_material); - g_IVRenderView->SetBlend(1.0f); } -void EffectGlow::EndRenderGlow() { +void EffectGlow::EndRenderChams() { + drawing = false; CMatRenderContextPtr ptr(vfunc(g_IMaterialSystem, 100, 0)(g_IMaterialSystem)); - g_IVRenderView->SetColorModulation(orig_modulation); - g_IStudioRender->ForcedMaterialOverride(nullptr); - g_IVModelRender->SuppressEngineLighting(false); - ptr->PopRenderTargetAndViewport(); + g_IVModelRender->ForcedMaterialOverride(nullptr); } -void EffectGlow::RenderGlow(int idx) { - CMatRenderContextPtr ptr(vfunc(g_IMaterialSystem, 100, 0)(g_IMaterialSystem)); - ptr->PushRenderTargetAndViewport( rt_A ); +int EffectGlow::ChamsColor(IClientEntity* entity) { + CachedEntity* ent = ENTITY(entity->entindex()); + if (CE_BAD(ent)) return colors::white; + if (vfunc(entity, 0xBE, 0)(entity)) { + IClientEntity* owner = vfunc(entity, 0x1C3, 0)(entity); + if (owner) { + return ChamsColor(owner); + } + } + switch (ent->m_Type) { + case ENTITY_BUILDING: + if (!ent->m_bEnemy && !(teammates || teammate_buildings)) { + return 0; + } + if (health) { + return colors::Health(ent->m_iHealth, ent->m_iMaxHealth); + } + break; + case ENTITY_PLAYER: + if (!players) return 0; + if (!ent->m_bEnemy && !teammates) return 0; + if (health) { + return colors::Health(ent->m_iHealth, ent->m_iMaxHealth); + } + break; + } + return colors::EntityF(ent); +} - g_IVModelRender->SuppressEngineLighting( true ); - - // Set the glow tint since selfillum trumps color modulation - IMaterialVar *var = glow_material->FindVar( "$selfillumtint", NULL, false ); - static float color[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; - var->SetVecValue( color, 4 ); // Fixed compilation error - var = glow_material->FindVar( "$alpha", NULL, false ); - var->SetFloatValue( color[3] ); // Fixed compilation error - - g_IVModelRender->ForcedMaterialOverride( glow_material ); - IClientEntity* ent = g_IEntityList->GetClientEntity(idx); - if (ent) ent->DrawModel(1); - g_IVModelRender->ForcedMaterialOverride( NULL ); - - g_IVModelRender->SuppressEngineLighting( false ); - - ptr->PopRenderTargetAndViewport(); - - - /*IClientEntity* ent = g_IEntityList->GetClientEntity(idx); - if (ent) { - ent->DrawModel(1); +bool EffectGlow::ShouldRenderChams(IClientEntity* entity) { + if (!enable) return false; + if (entity->entindex() < 0) return false; + CachedEntity* ent = ENTITY(entity->entindex()); + if (CE_BAD(ent)) return false; + /*if (weapons && vfunc(entity, 0xBE, 0)(entity)) { + IClientEntity* owner = vfunc(entity, 0x1C3, 0)(entity); + if (owner) { + return ShouldRenderChams(owner); + } }*/ + switch (ent->m_Type) { + case ENTITY_BUILDING: + if (!buildings) return false; + if (!ent->m_bEnemy && !(teammate_buildings || teammates)) return false; + return true; + case ENTITY_PLAYER: + if (!players) return false; + if (!teammates && !ent->m_bEnemy) return false; + if (CE_BYTE(ent, netvar.iLifeState) != LIFE_ALIVE) return false; + return true; + break; + case ENTITY_PROJECTILE: + if (!ent->m_bEnemy) return false; + if (stickies && ent->m_iClassID == g_pClassID->CTFGrenadePipebombProjectile) { + return true; + } + break; + case ENTITY_GENERIC: + switch (ent->m_ItemType) { + case ITEM_HEALTH_LARGE: + case ITEM_HEALTH_MEDIUM: + case ITEM_HEALTH_SMALL: + return medkits; + case ITEM_AMMO_LARGE: + case ITEM_AMMO_MEDIUM: + case ITEM_AMMO_SMALL: + return ammobox; + } + break; + } + return false; +} + +static CTextureReference buffers[4] {}; + +ITexture* GetBuffer(int i) { + if (!buffers[i]) { + ITexture* fullframe = g_IMaterialSystem->FindTexture("_rt_FullFrameFB", TEXTURE_GROUP_RENDER_TARGET); + char* newname = new char[32]; + std::string name = format("_cathook_buff", i); + strncpy(newname, name.c_str(), 30); + logging::Info("Creating new buffer %d with size %dx%d %s", i, fullframe->GetActualWidth(), fullframe->GetActualHeight(), newname); + + int textureFlags = TEXTUREFLAGS_CLAMPS | TEXTUREFLAGS_CLAMPT | TEXTUREFLAGS_EIGHTBITALPHA; + int renderTargetFlags = CREATERENDERTARGETFLAGS_HDR; + + ITexture* texture = g_IMaterialSystem->CreateNamedRenderTargetTextureEx( newname, fullframe->GetActualWidth(), fullframe->GetActualHeight(), RT_SIZE_LITERAL, IMAGE_FORMAT_RGBA8888, + MATERIAL_RT_DEPTH_SEPARATE, textureFlags, renderTargetFlags ); + buffers[i].Init(texture); + //buffers[i].InitRenderTarget(fullframe->GetActualWidth(), fullframe->GetActualHeight(), RenderTargetSizeMode_t::RT_SIZE_FULL_FRAME_BUFFER, IMAGE_FORMAT_ABGR8888, MaterialRenderTargetDepth_t::MATERIAL_RT_DEPTH_SEPARATE, true, newname); + } + return buffers[i]; +} + +IMaterial* GetBlurX() { + static CMaterialReference blur; + if (!blur) { + GetBuffer(1); + KeyValues* kv = new KeyValues("BlurFilterX"); + kv->SetString("$basetexture", "_cathook_buff1"); + kv->SetInt("$ignorez", 1); + kv->SetInt("$translucent", 1); + kv->SetInt("$alphatest", 1); + blur.Init("_cathook_blurx", kv); + blur->Refresh(); + } + return blur; +} + +IMaterial* GetBlurY() { + static CMaterialReference blur; + if (!blur) { + GetBuffer(2); + KeyValues* kv = new KeyValues("BlurFilterY"); + kv->SetString("$basetexture", "_cathook_buff2"); + kv->SetInt("$bloomamount", 5); + kv->SetInt("$ignorez", 1); + kv->SetInt("$translucent", 1); + kv->SetInt("$alphatest", 1); + blur.Init("_cathook_blury", kv); + blur->Refresh(); + } + return blur; +} + + + +void EffectGlow::RenderChams(int idx) { + CMatRenderContextPtr ptr(g_IMaterialSystem->GetRenderContext()); + IClientEntity* entity = g_IEntityList->GetClientEntity(idx); + if (entity && !entity->IsDormant()) { + if (ShouldRenderChams(entity)) { + int color = ChamsColor(entity); + unsigned char _b = (color >> 16) & 0xFF; + unsigned char _g = (color >> 8) & 0xFF; + unsigned char _r = (color) & 0xFF; + float color_1[] = { (float)_r / 255.0f, (float)_g / 255.0f, (float)_b / 255.0f }; + float color_2[] = { color_1[0] * 0.6f, color_1[1] * 0.6f, color_1[2] * 0.6f }; + mat_unlit_z->AlphaModulate(1.0f); + ptr->DepthRange(0.0f, 0.01f); + g_IVRenderView->SetColorModulation(color_1); + g_IVModelRender->ForcedMaterialOverride(mat_unlit_z); + entity->DrawModel(1); + //((DrawModelExecute_t)(hooks::hkIVModelRender->GetMethod(hooks::offDrawModelExecute)))(_this, state, info, matrix); + mat_unlit->AlphaModulate(1.0f); + g_IVRenderView->SetColorModulation(color_2); + ptr->DepthRange(0.0f, 1.0f); + g_IVModelRender->ForcedMaterialOverride(mat_unlit); + entity->DrawModel(1); + } + } } void EffectGlow::Render(int x, int y, int w, int h) { if (!init) Init(); - if (!glow_experimental) return; - CMatRenderContextPtr ptr(vfunc(g_IMaterialSystem, 100, 0)(g_IMaterialSystem)); + if (g_IEngine->IsTakingScreenshot() && clean_screenshots) return; + if (!enable) return; + CMatRenderContextPtr ptr(g_IMaterialSystem->GetRenderContext()); - //ptr->Viewport(x, y, w, h); - - ITexture* rt = ptr->GetRenderTarget(); - - IMaterialVar* var; - var = dev_blurfilterx->FindVar("$basetexture", nullptr); - var->SetTextureValue(rt_A); - var = dev_blurfiltery->FindVar("$basetexture", nullptr); - var->SetTextureValue(rt_B); - var = result_material->FindVar("$basetexture", nullptr); - var->SetTextureValue(rt_A); - //var = dev_blurfilterx->FindVar("$bloomamount", nullptr); - //var->SetFloatValue(10); - var = dev_blurfiltery->FindVar("$bloomamount", nullptr); - var->SetFloatValue(10); - - ptr->ClearColor4ub(0, 0, 0, 255); - ptr->PushRenderTargetAndViewport(rt_A); - ptr->ClearBuffers(true, true); - ptr->PopRenderTargetAndViewport(); - ptr->PushRenderTargetAndViewport(rt_B); - ptr->ClearBuffers(true, true); - ptr->PopRenderTargetAndViewport(); - - ptr->ClearStencilBufferRectangle( 0, 0, 1920, 1080, 0 ); - - BeginRenderGlow(); - for (int i = 1; i < 32; i++) { + ITexture* orig = ptr->GetRenderTarget(); + ptr->ClearColor4ub(0, 0, 0, 0); + ptr->PushRenderTargetAndViewport(); + ptr->SetRenderTarget(GetBuffer(1)); + ptr->Viewport(x, y, w, h); + ptr->OverrideAlphaWriteEnable( true, true ); + g_IVRenderView->SetBlend(0.99f); + ptr->ClearBuffers(true, false); + BeginRenderChams(); + for (int i = 1; i < HIGHEST_ENTITY; i++) { IClientEntity* ent = g_IEntityList->GetClientEntity(i); - if (ent && !ent->IsDormant() && NET_BYTE(ent, netvar.iLifeState) == LIFE_ALIVE) { - //BeginRenderGlow(); - RenderGlow(i); - //EndRenderGlow(); + if (ent && !ent->IsDormant()) { + RenderChams(i); } } - - ptr->PushRenderTargetAndViewport( rt_B ); + EndRenderChams(); + ptr->SetRenderTarget(GetBuffer(2)); + ptr->Viewport(x, y, w, h); + ptr->ClearBuffers(true, false); + ptr->DrawScreenSpaceRectangle(GetBlurX(), x, y, w, h, 0, 0, w - 1, h - 1, w, h); + static CMaterialReference blitmat; + if (!blitmat) { + KeyValues *kv = new KeyValues( "UnlitGeneric" ); + kv->SetString( "$basetexture", "_cathook_buff1" ); + kv->SetInt( "$additive", 1 ); + blitmat.Init( "_cathook_composite", TEXTURE_GROUP_CLIENT_EFFECTS, kv ); + blitmat->Refresh(); + } + ptr->SetRenderTarget(GetBuffer(1)); + ptr->DrawScreenSpaceRectangle(GetBlurY(), x, y, w, h, 0, 0, w - 1, h - 1, w, h); ptr->Viewport(x, y, w, h); - ptr->DrawScreenSpaceQuad( dev_blurfilterx ); ptr->PopRenderTargetAndViewport(); - - ptr->PushRenderTargetAndViewport( rt_A ); - ptr->Viewport(x, y, w, h); - ptr->DrawScreenSpaceQuad( dev_blurfiltery ); - ptr->PopRenderTargetAndViewport(); - - ptr->SetRenderTarget(rt); - ptr->Viewport(x, y, w, h); - - ptr->DrawScreenSpaceQuad(result_material); - var = result_material->FindVar("$basetexture", nullptr); - //var->SetTextureValue(rt_B); - //ptr->DrawScreenSpaceQuad(result_material); + ptr->DrawScreenSpaceRectangle(blitmat, x, y, w, h, 0, 0, w - 1, h - 1, w, h); } EffectGlow g_EffectGlow; CScreenSpaceEffectRegistration* g_pEffectGlow = nullptr; + +} diff --git a/src/EffectGlow.hpp b/src/EffectGlow.hpp index f76b3da7..5f531b5a 100644 --- a/src/EffectGlow.hpp +++ b/src/EffectGlow.hpp @@ -10,6 +10,8 @@ #include "common.h" +namespace effect_glow { + class EffectGlow : public IScreenSpaceEffect { public: virtual void Init( ); @@ -22,24 +24,23 @@ public: inline virtual void Enable( bool bEnable ) { enabled = bEnable; }; inline virtual bool IsEnabled( ) { return enabled; }; - void RenderGlow(int idx); - void BeginRenderGlow(); - void EndRenderGlow(); + int ChamsColor(IClientEntity* entity); + bool ShouldRenderChams(IClientEntity* entity); + void RenderChams(int idx); + void BeginRenderChams(); + void EndRenderChams(); public: bool init { false }; + bool drawing { false }; bool enabled; float orig_modulation[3]; - CTextureReference rt_A; - CTextureReference rt_B; - CMaterialReference result_material; - CMaterialReference glow_material; - CMaterialReference dev_bloomdadd; - CMaterialReference dev_halo_add_to_screen; - CMaterialReference dev_blurfilterx; - CMaterialReference dev_blurfiltery; + CMaterialReference mat_unlit; + CMaterialReference mat_unlit_z; }; extern EffectGlow g_EffectGlow; extern CScreenSpaceEffectRegistration* g_pEffectGlow; +} + #endif /* EFFECTGLOW_HPP_ */ diff --git a/src/hack.cpp b/src/hack.cpp index 4bcf40a8..9839a79a 100644 --- a/src/hack.cpp +++ b/src/hack.cpp @@ -183,15 +183,15 @@ void hack::Initialize() { hack::command_stack().push("cat_spam_reload"); logging::Info("Hooked!"); playerlist::Load(); - g_pEffectChams = new CScreenSpaceEffectRegistration("_cathook_chams", &g_EffectChams); + effect_chams::g_pEffectChams = new CScreenSpaceEffectRegistration("_cathook_chams", &effect_chams::g_EffectChams); g_pScreenSpaceEffects->EnableScreenSpaceEffect("_cathook_chams"); - g_EffectChams.Init(); - //g_pEffectGlow = new CScreenSpaceEffectRegistration("_cathook_glow", &g_EffectGlow); + effect_chams::g_EffectChams.Init(); + effect_glow::g_pEffectGlow = new CScreenSpaceEffectRegistration("_cathook_glow", &effect_glow::g_EffectGlow); //for (CScreenSpaceEffectRegistration* reg = *g_ppScreenSpaceRegistrationHead; reg; reg = reg->m_pNext) { // logging::Info("%s", reg->m_pEffectName); //} - //g_pScreenSpaceEffects->EnableScreenSpaceEffect("_cathook_glow"); - //g_EffectGlow.Init(); + g_pScreenSpaceEffects->EnableScreenSpaceEffect("_cathook_glow"); + effect_glow::g_EffectGlow.Init(); logging::Info("SSE enabled.."); } diff --git a/src/hooks/others.cpp b/src/hooks/others.cpp index 196f51ce..63c7d247 100644 --- a/src/hooks/others.cpp +++ b/src/hooks/others.cpp @@ -58,7 +58,7 @@ void DrawModelExecute_hook(IVModelRender* _this, const DrawModelState_t& state, IClientUnknown* unk = info.pRenderable->GetIClientUnknown(); if (unk) { IClientEntity* ent = unk->GetIClientEntity(); - if (ent && !g_EffectChams.drawing && g_EffectChams.ShouldRenderChams(ent)) { + if (ent && !effect_chams::g_EffectChams.drawing && effect_chams::g_EffectChams.ShouldRenderChams(ent)) { return; } } From 961f0a21b2330d73bda42cd557bb7de633a93ef0 Mon Sep 17 00:00:00 2001 From: nullifiedcat Date: Mon, 17 Apr 2017 22:01:47 +0300 Subject: [PATCH 2/3] started stencil stuff --- src/EffectGlow.cpp | 71 +++++++++++++++++++++++++++++++++++++++++----- src/EffectGlow.hpp | 3 ++ src/netvars.cpp | 1 + src/netvars.h | 1 + 4 files changed, 69 insertions(+), 7 deletions(-) diff --git a/src/EffectGlow.cpp b/src/EffectGlow.cpp index f9a20705..642a0f7f 100644 --- a/src/EffectGlow.cpp +++ b/src/EffectGlow.cpp @@ -137,6 +137,39 @@ bool EffectGlow::ShouldRenderChams(IClientEntity* entity) { return false; } +struct ShaderStencilState_t +{ + bool m_bEnable; + StencilOperation_t m_FailOp; + StencilOperation_t m_ZFailOp; + StencilOperation_t m_PassOp; + StencilComparisonFunction_t m_CompareFunc; + int m_nReferenceValue; + uint32 m_nTestMask; + uint32 m_nWriteMask; + + ShaderStencilState_t() + { + m_bEnable = false; + m_PassOp = m_FailOp = m_ZFailOp = STENCILOPERATION_KEEP; + m_CompareFunc = STENCILCOMPARISONFUNCTION_ALWAYS; + m_nReferenceValue = 0; + m_nTestMask = m_nWriteMask = 0xFFFFFFFF; + } + + void SetStencilState( CMatRenderContextPtr &pRenderContext ) + { + pRenderContext->SetStencilEnable( m_bEnable ); + pRenderContext->SetStencilFailOperation( m_FailOp ); + pRenderContext->SetStencilZFailOperation( m_ZFailOp ); + pRenderContext->SetStencilPassOperation( m_PassOp ); + pRenderContext->SetStencilCompareFunction( m_CompareFunc ); + pRenderContext->SetStencilReferenceValue( m_nReferenceValue ); + pRenderContext->SetStencilTestMask( m_nTestMask ); + pRenderContext->SetStencilWriteMask( m_nWriteMask ); + } +}; + static CTextureReference buffers[4] {}; ITexture* GetBuffer(int i) { @@ -189,7 +222,36 @@ IMaterial* GetBlurY() { return blur; } +static CatVar solid_when(CV_INT, "glow_experimental_solid_when", "0", "...", "Never, Always, Unoccluded, Occluded"); +void EffectGlow::DrawToStencil(IClientEntity* entity) { + ShaderStencilState_t state; + state.m_bEnable = true; + switch ((int)solid_when) { + case 0: + state.m_PassOp = STENCILOPERATION_REPLACE; + state.m_FailOp = STENCILOPERATION_KEEP; + state.m_ZFailOp = STENCILOPERATION_KEEP; + break; + case 2: + case 3: + } +} + +void EffectGlow::DrawToBuffer(IClientEntity* entity) { + +} + +void EffectGlow::DrawEntity(IClientEntity* entity) { + entity->DrawModel(1); + IClientEntity* attach = g_IEntityList->GetClientEntity(*(int*)((uintptr_t)entity + netvar.m_Collision - 24) & 0xFFF); + while (attach) { + if (attach->ShouldDraw()) { + attach->DrawModel(1); + } + attach = g_IEntityList->GetClientEntity(*(int*)((uintptr_t)entity + netvar.m_Collision - 20) & 0xFFF); + } +} void EffectGlow::RenderChams(int idx) { CMatRenderContextPtr ptr(g_IMaterialSystem->GetRenderContext()); @@ -206,13 +268,7 @@ void EffectGlow::RenderChams(int idx) { ptr->DepthRange(0.0f, 0.01f); g_IVRenderView->SetColorModulation(color_1); g_IVModelRender->ForcedMaterialOverride(mat_unlit_z); - entity->DrawModel(1); - //((DrawModelExecute_t)(hooks::hkIVModelRender->GetMethod(hooks::offDrawModelExecute)))(_this, state, info, matrix); - mat_unlit->AlphaModulate(1.0f); - g_IVRenderView->SetColorModulation(color_2); - ptr->DepthRange(0.0f, 1.0f); - g_IVModelRender->ForcedMaterialOverride(mat_unlit); - entity->DrawModel(1); + DrawEntity(entity); } } } @@ -255,6 +311,7 @@ void EffectGlow::Render(int x, int y, int w, int h) { ptr->DrawScreenSpaceRectangle(GetBlurY(), x, y, w, h, 0, 0, w - 1, h - 1, w, h); ptr->Viewport(x, y, w, h); ptr->PopRenderTargetAndViewport(); + g_IVRenderView->SetBlend(0.0f); ptr->DrawScreenSpaceRectangle(blitmat, x, y, w, h, 0, 0, w - 1, h - 1, w, h); } diff --git a/src/EffectGlow.hpp b/src/EffectGlow.hpp index 5f531b5a..f3cd001e 100644 --- a/src/EffectGlow.hpp +++ b/src/EffectGlow.hpp @@ -24,6 +24,9 @@ public: inline virtual void Enable( bool bEnable ) { enabled = bEnable; }; inline virtual bool IsEnabled( ) { return enabled; }; + void DrawEntity(IClientEntity* entity); + void DrawToStencil(IClientEntity* entity); + void DrawToBuffer(IClientEntity* entity); int ChamsColor(IClientEntity* entity); bool ShouldRenderChams(IClientEntity* entity); void RenderChams(int idx); diff --git a/src/netvars.cpp b/src/netvars.cpp index dd67b6ce..cf6c1705 100644 --- a/src/netvars.cpp +++ b/src/netvars.cpp @@ -20,6 +20,7 @@ void NetVars::Init() { this->iHitboxSet = gNetvars.get_offset("DT_BaseAnimating", "m_nHitboxSet"); this->vVelocity = gNetvars.get_offset("DT_BasePlayer", "localdata", "m_vecVelocity[0]"); this->movetype = gNetvars.get_offset("DT_BaseEntity", "movetype"); + this->m_Collision = gNetvars.get_offset("DT_BaseEntity", "m_Collision"); if (TF2) { m_angEyeAngles = gNetvars.get_offset("DT_TFPlayer", "tfnonlocaldata", "m_angEyeAngles[0]"); this->bGlowEnabled = gNetvars.get_offset("DT_TFPlayer", "m_bGlowEnabled"); diff --git a/src/netvars.h b/src/netvars.h index 66e5bf03..715f1e36 100644 --- a/src/netvars.h +++ b/src/netvars.h @@ -122,6 +122,7 @@ public: offset_t m_bDucked; offset_t m_angEyeAngles; offset_t m_bReadyToBackstab; + offset_t m_Collision; }; extern NetVars netvar; From 0d74d3c5b8485b2a04d907deb66c135234e15982 Mon Sep 17 00:00:00 2001 From: nullifiedcat Date: Tue, 18 Apr 2017 16:16:36 +0300 Subject: [PATCH 3/3] glow --- src/EffectGlow.cpp | 163 +++++++++++++++++++++++++++++-------------- src/EffectGlow.hpp | 4 +- src/glowobjects.cpp | 16 ++--- src/gui/ncc/Menu.cpp | 26 +++++-- src/hooks/others.cpp | 4 +- 5 files changed, 145 insertions(+), 68 deletions(-) diff --git a/src/EffectGlow.cpp b/src/EffectGlow.cpp index 642a0f7f..0c4f403e 100644 --- a/src/EffectGlow.cpp +++ b/src/EffectGlow.cpp @@ -25,15 +25,15 @@ CScreenSpaceEffectRegistration::CScreenSpaceEffectRegistration( const char *pNam namespace effect_glow { -CatVar enable(CV_SWITCH, "glow_experimental_enable", "0", "Enable"); -static CatVar health(CV_SWITCH, "glow_experimental_health", "0", "Health"); -static CatVar teammates(CV_SWITCH, "glow_experimental_teammates", "0", "Teammates"); -static CatVar players(CV_SWITCH, "glow_experimental_players", "1", "Players"); -static CatVar medkits(CV_SWITCH, "glow_experimental_medkits", "0", "Medkits"); -static CatVar ammobox(CV_SWITCH, "glow_experimental_ammo", "0", "Ammoboxes"); -static CatVar buildings(CV_SWITCH, "glow_experimental_buildings", "0", "Buildings"); -static CatVar stickies(CV_SWITCH, "glow_experimental_stickies", "0", "Stickies"); -static CatVar teammate_buildings(CV_SWITCH, "glow_experimental_teammate_buildings", "0", "Teammate Buildings"); +CatVar enable(CV_SWITCH, "glow_enable", "0", "Enable"); +static CatVar health(CV_SWITCH, "glow_health", "0", "Health"); +static CatVar teammates(CV_SWITCH, "glow_teammates", "0", "Teammates"); +static CatVar players(CV_SWITCH, "glow_players", "1", "Players"); +static CatVar medkits(CV_SWITCH, "glow_medkits", "0", "Medkits"); +static CatVar ammobox(CV_SWITCH, "glow_ammo", "0", "Ammoboxes"); +static CatVar buildings(CV_SWITCH, "glow_buildings", "0", "Buildings"); +static CatVar stickies(CV_SWITCH, "glow_stickies", "0", "Stickies"); +static CatVar teammate_buildings(CV_SWITCH, "glow_teammate_buildings", "0", "Teammate Buildings"); void EffectGlow::Init() { logging::Info("Init EffectChams..."); @@ -53,17 +53,6 @@ void EffectGlow::Init() { init = true; } -void EffectGlow::BeginRenderChams() { - drawing = true; - CMatRenderContextPtr ptr(vfunc(g_IMaterialSystem, 100, 0)(g_IMaterialSystem)); -} - -void EffectGlow::EndRenderChams() { - drawing = false; - CMatRenderContextPtr ptr(vfunc(g_IMaterialSystem, 100, 0)(g_IMaterialSystem)); - g_IVModelRender->ForcedMaterialOverride(nullptr); -} - int EffectGlow::ChamsColor(IClientEntity* entity) { CachedEntity* ent = ENTITY(entity->entindex()); if (CE_BAD(ent)) return colors::white; @@ -222,9 +211,32 @@ IMaterial* GetBlurY() { return blur; } -static CatVar solid_when(CV_INT, "glow_experimental_solid_when", "0", "...", "Never, Always, Unoccluded, Occluded"); -void EffectGlow::DrawToStencil(IClientEntity* entity) { +void EffectGlow::BeginRenderChams() { + drawing = true; + CMatRenderContextPtr ptr(vfunc(g_IMaterialSystem, 100, 0)(g_IMaterialSystem)); + ptr->ClearColor4ub(0, 0, 0, 0); + ptr->PushRenderTargetAndViewport(); + ptr->SetRenderTarget(GetBuffer(1)); + //ptr->Viewport(x, y, w, h); + ptr->OverrideAlphaWriteEnable( true, true ); + g_IVRenderView->SetBlend(0.99f); + ptr->ClearBuffers(true, false); +} + +void EffectGlow::EndRenderChams() { + drawing = false; + CMatRenderContextPtr ptr(vfunc(g_IMaterialSystem, 100, 0)(g_IMaterialSystem)); + g_IVModelRender->ForcedMaterialOverride(nullptr); + ptr->PopRenderTargetAndViewport(); +} + +// https://puu.sh/vobH4/5da8367aef.png +static CatEnum solid_when_enum({"Never", "Always", "Invisible"}); +static CatVar blur_scale(CV_INT, "glow_blur_scale", "5", "Blur amount"); +static CatVar solid_when(solid_when_enum, "glow_solid_when", "0", "Solid when", "Glow will be solid when entity is..."); + +void EffectGlow::StartStenciling() { ShaderStencilState_t state; state.m_bEnable = true; switch ((int)solid_when) { @@ -234,8 +246,41 @@ void EffectGlow::DrawToStencil(IClientEntity* entity) { state.m_ZFailOp = STENCILOPERATION_KEEP; break; case 2: - case 3: + state.m_PassOp = STENCILOPERATION_REPLACE; + state.m_FailOp = STENCILOPERATION_KEEP; + state.m_ZFailOp = STENCILOPERATION_KEEP; + break; + /*case 3: https://puu.sh/vobH4/5da8367aef.png + state.m_PassOp = STENCILOPERATION_KEEP; + state.m_FailOp = STENCILOPERATION_KEEP; + state.m_ZFailOp = STENCILOPERATION_REPLACE;*/ } + state.m_CompareFunc = STENCILCOMPARISONFUNCTION_ALWAYS; + state.m_nWriteMask = 1; + state.m_nReferenceValue = 1; + CMatRenderContextPtr ptr(g_IMaterialSystem->GetRenderContext()); + state.SetStencilState(ptr); + if (!solid_when) { + ptr->DepthRange(0.0f, 0.01f); + } else { + ptr->DepthRange(0.0f, 1.0f); + } + g_IVRenderView->SetBlend(0.0f); + mat_unlit->AlphaModulate(1.0f); + g_IVModelRender->ForcedMaterialOverride(solid_when ? mat_unlit : mat_unlit_z); +} + +void EffectGlow::EndStenciling() { + g_IVModelRender->ForcedMaterialOverride(nullptr); + CMatRenderContextPtr ptr(g_IMaterialSystem->GetRenderContext()); + ShaderStencilState_t state {}; + state.SetStencilState(ptr); + ptr->DepthRange(0.0f, 1.0f); + g_IVRenderView->SetBlend(1.0f); +} + +void EffectGlow::DrawToStencil(IClientEntity* entity) { + DrawEntity(entity); } void EffectGlow::DrawToBuffer(IClientEntity* entity) { @@ -249,28 +294,23 @@ void EffectGlow::DrawEntity(IClientEntity* entity) { if (attach->ShouldDraw()) { attach->DrawModel(1); } - attach = g_IEntityList->GetClientEntity(*(int*)((uintptr_t)entity + netvar.m_Collision - 20) & 0xFFF); + attach = g_IEntityList->GetClientEntity(*(int*)((uintptr_t)attach + netvar.m_Collision - 20) & 0xFFF); } } -void EffectGlow::RenderChams(int idx) { +void EffectGlow::RenderChams(IClientEntity* entity) { CMatRenderContextPtr ptr(g_IMaterialSystem->GetRenderContext()); - IClientEntity* entity = g_IEntityList->GetClientEntity(idx); - if (entity && !entity->IsDormant()) { - if (ShouldRenderChams(entity)) { - int color = ChamsColor(entity); - unsigned char _b = (color >> 16) & 0xFF; - unsigned char _g = (color >> 8) & 0xFF; - unsigned char _r = (color) & 0xFF; - float color_1[] = { (float)_r / 255.0f, (float)_g / 255.0f, (float)_b / 255.0f }; - float color_2[] = { color_1[0] * 0.6f, color_1[1] * 0.6f, color_1[2] * 0.6f }; - mat_unlit_z->AlphaModulate(1.0f); - ptr->DepthRange(0.0f, 0.01f); - g_IVRenderView->SetColorModulation(color_1); - g_IVModelRender->ForcedMaterialOverride(mat_unlit_z); - DrawEntity(entity); - } - } + int color = ChamsColor(entity); + unsigned char _b = (color >> 16) & 0xFF; + unsigned char _g = (color >> 8) & 0xFF; + unsigned char _r = (color) & 0xFF; + float color_1[] = { (float)_r / 255.0f, (float)_g / 255.0f, (float)_b / 255.0f }; + float color_2[] = { color_1[0] * 0.6f, color_1[1] * 0.6f, color_1[2] * 0.6f }; + mat_unlit_z->AlphaModulate(1.0f); + ptr->DepthRange(0.0f, 0.01f); + g_IVRenderView->SetColorModulation(color_1); + g_IVModelRender->ForcedMaterialOverride(mat_unlit_z); + DrawEntity(entity); } void EffectGlow::Render(int x, int y, int w, int h) { @@ -278,23 +318,26 @@ void EffectGlow::Render(int x, int y, int w, int h) { if (g_IEngine->IsTakingScreenshot() && clean_screenshots) return; if (!enable) return; CMatRenderContextPtr ptr(g_IMaterialSystem->GetRenderContext()); - ITexture* orig = ptr->GetRenderTarget(); - ptr->ClearColor4ub(0, 0, 0, 0); - ptr->PushRenderTargetAndViewport(); - ptr->SetRenderTarget(GetBuffer(1)); - ptr->Viewport(x, y, w, h); - ptr->OverrideAlphaWriteEnable( true, true ); - g_IVRenderView->SetBlend(0.99f); - ptr->ClearBuffers(true, false); BeginRenderChams(); for (int i = 1; i < HIGHEST_ENTITY; i++) { IClientEntity* ent = g_IEntityList->GetClientEntity(i); - if (ent && !ent->IsDormant()) { - RenderChams(i); + if (ent && !ent->IsDormant() && ShouldRenderChams(ent)) { + RenderChams(ent); } } EndRenderChams(); + if ((int)solid_when != 1) { + ptr->ClearStencilBufferRectangle(x, y, w, h, 0); + StartStenciling(); + for (int i = 1; i < HIGHEST_ENTITY; i++) { + IClientEntity* ent = g_IEntityList->GetClientEntity(i); + if (ent && !ent->IsDormant() && ShouldRenderChams(ent)) { + DrawToStencil(ent); + } + } + EndStenciling(); + } ptr->SetRenderTarget(GetBuffer(2)); ptr->Viewport(x, y, w, h); ptr->ClearBuffers(true, false); @@ -308,11 +351,27 @@ void EffectGlow::Render(int x, int y, int w, int h) { blitmat->Refresh(); } ptr->SetRenderTarget(GetBuffer(1)); + IMaterial* blury = GetBlurY(); + static IMaterialVar* blury_bloomamount = blury->FindVar("$bloomamount", nullptr); + blury_bloomamount->SetIntValue((int)blur_scale); ptr->DrawScreenSpaceRectangle(GetBlurY(), x, y, w, h, 0, 0, w - 1, h - 1, w, h); ptr->Viewport(x, y, w, h); - ptr->PopRenderTargetAndViewport(); + ptr->SetRenderTarget(orig); g_IVRenderView->SetBlend(0.0f); + if ((int)solid_when != 1) { + ShaderStencilState_t state {}; + state.m_bEnable = true; + state.m_nReferenceValue = 0; + state.m_nTestMask = 1; + state.m_CompareFunc = STENCILCOMPARISONFUNCTION_EQUAL; + state.m_PassOp = STENCILOPERATION_ZERO; + state.SetStencilState(ptr); + } ptr->DrawScreenSpaceRectangle(blitmat, x, y, w, h, 0, 0, w - 1, h - 1, w, h); + if ((int)solid_when != -1) { + static ShaderStencilState_t zeroStencil {}; + zeroStencil.SetStencilState(ptr); + } } EffectGlow g_EffectGlow; diff --git a/src/EffectGlow.hpp b/src/EffectGlow.hpp index f3cd001e..fca05613 100644 --- a/src/EffectGlow.hpp +++ b/src/EffectGlow.hpp @@ -24,12 +24,14 @@ public: inline virtual void Enable( bool bEnable ) { enabled = bEnable; }; inline virtual bool IsEnabled( ) { return enabled; }; + void StartStenciling(); + void EndStenciling(); void DrawEntity(IClientEntity* entity); void DrawToStencil(IClientEntity* entity); void DrawToBuffer(IClientEntity* entity); int ChamsColor(IClientEntity* entity); bool ShouldRenderChams(IClientEntity* entity); - void RenderChams(int idx); + void RenderChams(IClientEntity* entity); void BeginRenderChams(); void EndRenderChams(); public: diff --git a/src/glowobjects.cpp b/src/glowobjects.cpp index 1fa793f0..666c3e2c 100644 --- a/src/glowobjects.cpp +++ b/src/glowobjects.cpp @@ -9,15 +9,15 @@ #include "common.h" static CatEnum glow_color_scheme_enum({"ESP", "HEALTH"}); -static CatVar glow_color_scheme(glow_color_scheme_enum, "glow_color_scheme", "1", "Colors", "Doesn't need a description"); +static CatVar glow_color_scheme(glow_color_scheme_enum, "glow_old_color_scheme", "1", "Colors", "Doesn't need a description"); // Doesn't work - has to be registered manually. -static CatVar glow_ammo_boxes(CV_SWITCH, "glow_ammo_boxes", "0", "Ammo"); -static CatVar glow_health_packs(CV_SWITCH, "glow_health_packs", "0", "Health"); -static CatVar glow_teammates(CV_SWITCH, "glow_teammates", "0", "Teammates"); -static CatVar glow_teammate_buildings(CV_SWITCH, "glow_teammate_buildings", "0", "Teammate buildings"); -static CatVar glow_buildings(CV_SWITCH, "glow_buildings", "1", "Buildings"); -static CatVar glow_stickies(CV_SWITCH, "glow_stickies", "0", "Stickies"); -static CatVar glow_players(CV_SWITCH, "glow_players", "1", "Players"); +static CatVar glow_ammo_boxes(CV_SWITCH, "glow_old_ammo_boxes", "0", "Ammo"); +static CatVar glow_health_packs(CV_SWITCH, "glow_old_health_packs", "0", "Health"); +static CatVar glow_teammates(CV_SWITCH, "glow_old_teammates", "0", "Teammates"); +static CatVar glow_teammate_buildings(CV_SWITCH, "glow_old_teammate_buildings", "0", "Teammate buildings"); +static CatVar glow_buildings(CV_SWITCH, "glow_old_buildings", "1", "Buildings"); +static CatVar glow_stickies(CV_SWITCH, "glow_old_stickies", "0", "Stickies"); +static CatVar glow_players(CV_SWITCH, "glow_old_players", "1", "Players"); int CGlowObjectManager::EnableGlow(IClientEntity* entity, int color) { int idx = GlowHandle(entity); diff --git a/src/gui/ncc/Menu.cpp b/src/gui/ncc/Menu.cpp index 5647bb43..9e6a7694 100644 --- a/src/gui/ncc/Menu.cpp +++ b/src/gui/ncc/Menu.cpp @@ -172,17 +172,33 @@ List& MainList() { "Glow" [ "Glow Menu" - "glow_enabled" + "glow_enable" + "glow_solid_when" + "glow_blur_scale" + "glow_health" "glow_players" - "glow_color_scheme" - "glow_health_packs" - "glow_ammo_boxes" "glow_teammates" - "glow_teammate_buildings" "glow_buildings" + "glow_teammate_buildings" + "glow_medkits" + "glow_ammo" "glow_stickies" ] +"TF2 Glow Outline" [ + "TF2 Glow Menu" + "glow_old_enabled" + "glow_old_players" + "glow_old_color_scheme" + "glow_old_health_packs" + "glow_old_ammo_boxes" + "glow_old_alpha" + "glow_old_teammates" + "glow_old_teammate_buildings" + "glow_old_buildings" + "glow_old_stickies" +] + "ESP" [ "ESP Menu" "esp_enabled" diff --git a/src/hooks/others.cpp b/src/hooks/others.cpp index 63c7d247..88698aa0 100644 --- a/src/hooks/others.cpp +++ b/src/hooks/others.cpp @@ -152,8 +152,8 @@ void Shutdown_hook(void* thisptr, const char* reason) { SEGV_END; } -static CatVar glow_enabled(CV_SWITCH, "glow_enabled", "0", "Enable", "Make sure to enable glow_outline_effect_enable in tf2 settings"); -static CatVar glow_alpha(CV_FLOAT, "glow_alpha", "1", "Alpha", "Glow Transparency", 0.0f, 1.0f); +static CatVar glow_enabled(CV_SWITCH, "glow_old_enabled", "0", "Enable", "Make sure to enable glow_outline_effect_enable in tf2 settings"); +static CatVar glow_alpha(CV_FLOAT, "glow_old_alpha", "1", "Alpha", "Glow Transparency", 0.0f, 1.0f); void FrameStageNotify_hook(void* thisptr, int stage) { SEGV_BEGIN;