From 9d1b5cf6043a2dff66326d282ab53b7d72a2f515 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 7 Nov 2021 22:48:52 +0100 Subject: [PATCH 01/12] Better r_draw debug functions, and relocated in renderer --- src/Components/Modules/QuickPatch.cpp | 198 ---------------------- src/Components/Modules/Renderer.cpp | 227 ++++++++++++++++++++++++++ src/Components/Modules/Renderer.hpp | 12 ++ 3 files changed, 239 insertions(+), 198 deletions(-) diff --git a/src/Components/Modules/QuickPatch.cpp b/src/Components/Modules/QuickPatch.cpp index 1ab8479b..87c906da 100644 --- a/src/Components/Modules/QuickPatch.cpp +++ b/src/Components/Modules/QuickPatch.cpp @@ -964,204 +964,6 @@ namespace Components } }); - Scheduler::OnFrame([]() - { - if (!Game::CL_IsCgameInitialized() || !Dvar::Var("r_drawAabbTrees").get()) return; - - float cyan[4] = { 0.0f, 0.5f, 0.5f, 1.0f }; - float red[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; - - Game::clipMap_t* clipMap = *reinterpret_cast(0x7998E0); - //Game::GfxWorld* gameWorld = *reinterpret_cast(0x66DEE94); - if (!clipMap) return; - - for (unsigned short i = 0; i < clipMap->smodelNodeCount; ++i) - { - Game::R_AddDebugBounds(cyan, &clipMap->smodelNodes[i].bounds); - } - - for (unsigned int i = 0; i < clipMap->numStaticModels; i += 2) - { - Game::R_AddDebugBounds(red, &clipMap->staticModelList[i].absBounds); - } - }); - - Dvar::OnInit([] - { - Dvar::Register("r_drawSceneModelBoundingBoxes", false, Game::DVAR_FLAG_CHEAT, "Draw scene model bounding boxes"); - Dvar::Register("r_drawSceneModelCollisions", false, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); - Dvar::Register("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); - Dvar::Register("r_drawModelNames", false, Game::DVAR_FLAG_CHEAT, "Draw all model names"); - Dvar::Register("r_drawAabbTrees", false, Game::DVAR_FLAG_USERCREATED, "Draw aabb trees"); - }); - - Scheduler::OnFrame([]() - { - if (!Game::CL_IsCgameInitialized() || !Dvar::Var("r_drawModelNames").get()) return; - - float sceneModelsColor[4] = { 1.0f, 1.0f, 0.0f, 1.0f }; - float dobjsColor[4] = { 0.0f, 1.0f, 1.0f, 1.0f }; - float staticModelsColor[4] = { 1.0f, 0.0f, 1.0f, 1.0f }; - - auto mapName = Dvar::Var("mapname").get(); - auto* scene = Game::scene; - auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; - - for (auto i = 0; i < scene->sceneModelCount; i++) - { - if (!scene->sceneModel[i].model) - continue; - - Game::R_AddDebugString(sceneModelsColor, scene->sceneModel[i].placement.base.origin, 1.0, scene->sceneModel[i].model->name); - } - - for (auto i = 0; i < scene->sceneDObjCount; i++) - { - if (scene->sceneDObj[i].obj) { - for (int j = 0; j < scene->sceneDObj[i].obj->numModels; j++) - { - Game::R_AddDebugString(dobjsColor, scene->sceneDObj[i].placement.origin, 1.0, scene->sceneDObj[i].obj->models[j]->name); - } - } - } - - // Static models - for (size_t i = 0; i < world->dpvs.smodelCount; i++) - { - auto staticModel = world->dpvs.smodelDrawInsts[i]; - if (staticModel.model) { - Game::R_AddDebugString(staticModelsColor, staticModel.placement.origin, 1.0, staticModel.model->name); - } - } - }); - - Scheduler::OnFrame([]() - { - if (!Game::CL_IsCgameInitialized() || !Dvar::Var("r_drawSceneModelBoundingBoxes").get()) return; - - float red[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; - float blue[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; - - auto* scene = Game::scene; - - for(auto i = 0; i < scene->sceneModelCount; i++) - { - if(!scene->sceneModel[i].model) - continue; - - auto b = scene->sceneModel[i].model->bounds; - b.midPoint[0] += scene->sceneModel[i].placement.base.origin[0]; - b.midPoint[1] += scene->sceneModel[i].placement.base.origin[1]; - b.midPoint[2] += scene->sceneModel[i].placement.base.origin[2]; - b.halfSize[0] *= scene->sceneModel[i].placement.scale; - b.halfSize[1] *= scene->sceneModel[i].placement.scale; - b.halfSize[2] *= scene->sceneModel[i].placement.scale; - Game::R_AddDebugBounds(red, &b, &scene->sceneModel[i].placement.base.quat); - } - - for(auto i = 0; i < scene->sceneDObjCount; i++) - { - scene->sceneDObj[i].cull.bounds.halfSize[0] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[0]); - scene->sceneDObj[i].cull.bounds.halfSize[1] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[1]); - scene->sceneDObj[i].cull.bounds.halfSize[2] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[2]); - - if (scene->sceneDObj[i].cull.bounds.halfSize[0] < 0 || - scene->sceneDObj[i].cull.bounds.halfSize[1] < 0 || - scene->sceneDObj[i].cull.bounds.halfSize[2] < 0) { - - Components::Logger::Print("WARNING: Negative half size for DOBJ %s, this will cause culling issues!", scene->sceneDObj[i].obj->models[0]->name); - } - - Game::R_AddDebugBounds(blue, &scene->sceneDObj[i].cull.bounds); - } - }); - - Scheduler::OnFrame([]() - { - if (!Game::CL_IsCgameInitialized()) return; - if (!Dvar::Var("r_drawSceneModelCollisions").get()) return; - - float green[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; - - auto* scene = Game::scene; - - for (auto i = 0; i < scene->sceneModelCount; i++) - { - if (!scene->sceneModel[i].model) - continue; - - for (auto j = 0; j < scene->sceneModel[i].model->numCollSurfs; j++) { - auto b = scene->sceneModel[i].model->collSurfs[j].bounds; - b.midPoint[0] += scene->sceneModel[i].placement.base.origin[0]; - b.midPoint[1] += scene->sceneModel[i].placement.base.origin[1]; - b.midPoint[2] += scene->sceneModel[i].placement.base.origin[2]; - b.halfSize[0] *= scene->sceneModel[i].placement.scale; - b.halfSize[1] *= scene->sceneModel[i].placement.scale; - b.halfSize[2] *= scene->sceneModel[i].placement.scale; - - Game::R_AddDebugBounds(green, &b, &scene->sceneModel[i].placement.base.quat); - } - } - }); - - Scheduler::OnFrame([]() - { - if (!Game::CL_IsCgameInitialized() || !Dvar::Var("r_drawTriggers").get()) return; - - float hurt[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; - float hurtTouch[4] = { 0.75f, 0.0f, 0.0f, 1.0f }; - float damage[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; - float once[4] = { 0.0f, 1.0f, 1.0f, 1.0f }; - float multiple[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; - - auto* entities = Game::g_entities; - for(auto i = 0u; i < 0x800; i++) - { - auto* ent = &entities[i]; - - if(ent->r.isInUse) - { - Game::Bounds b = ent->r.box; - b.midPoint[0] += ent->r.currentOrigin[0]; - b.midPoint[1] += ent->r.currentOrigin[1]; - b.midPoint[2] += ent->r.currentOrigin[2]; - - switch(ent->handler) - { - case Game::ENT_HANDLER_TRIGGER_HURT: - Game::R_AddDebugBounds(hurt, &b); - break; - - case Game::ENT_HANDLER_TRIGGER_HURT_TOUCH: - Game::R_AddDebugBounds(hurtTouch, &b); - break; - - case Game::ENT_HANDLER_TRIGGER_DAMAGE: - Game::R_AddDebugBounds(damage, &b); - break; - - case Game::ENT_HANDLER_TRIGGER_MULTIPLE: - if(ent->spawnflags & 0x40) - Game::R_AddDebugBounds(once, &b); - else - Game::R_AddDebugBounds(multiple, &b); - break; - - default: - float rv = std::min((float)ent->handler, (float)5) / 5; - float gv = std::clamp((float)ent->handler-5, (float)0, (float)5) / 5; - float bv = std::clamp((float)ent->handler - 10, (float)0, (float)5) / 5; - - float color[4] = { rv, gv, bv, 1.0f }; - - Game::R_AddDebugBounds(color, &b); - break; - } - } - } - }); - - // Dvars Dvar::Register("ui_streamFriendly", false, Game::DVAR_FLAG_SAVED, "Stream friendly UI"); diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index 2422fde2..be2d8240 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -8,6 +8,12 @@ namespace Components Utils::Signal Renderer::EndRecoverDeviceSignal; Utils::Signal Renderer::BeginRecoverDeviceSignal; + Dvar::Var Renderer::DrawTriggers; + Dvar::Var Renderer::DrawSceneModelCollisions; + Dvar::Var Renderer::DrawSceneModelBoundingBoxes; + Dvar::Var Renderer::DrawModelNames; + Dvar::Var Renderer::DrawAABBTrees; + __declspec(naked) void Renderer::FrameStub() { __asm @@ -166,6 +172,209 @@ namespace Components return Utils::Hook::Call(0x005033E0)(a1, a2, a3, Utils::String::VA("%s (^3%s^7)", mat->info.name, mat->techniqueSet->name), color, a6); } + void Renderer::DebugDrawTriggers() + { + if (!DrawTriggers.get()) return; + + float hurt[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; + float hurtTouch[4] = { 0.75f, 0.0f, 0.0f, 1.0f }; + float damage[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; + float once[4] = { 0.0f, 1.0f, 1.0f, 1.0f }; + float multiple[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; + + auto* entities = Game::g_entities; + + for (auto i = 0u; i < 0x800; i++) + { + auto* ent = &entities[i]; + + if (ent->r.isInUse) + { + Game::Bounds b = ent->r.box; + b.midPoint[0] += ent->r.currentOrigin[0]; + b.midPoint[1] += ent->r.currentOrigin[1]; + b.midPoint[2] += ent->r.currentOrigin[2]; + + switch (ent->handler) + { + case Game::ENT_HANDLER_TRIGGER_HURT: + Game::R_AddDebugBounds(hurt, &b); + break; + + case Game::ENT_HANDLER_TRIGGER_HURT_TOUCH: + Game::R_AddDebugBounds(hurtTouch, &b); + break; + + case Game::ENT_HANDLER_TRIGGER_DAMAGE: + Game::R_AddDebugBounds(damage, &b); + break; + + case Game::ENT_HANDLER_TRIGGER_MULTIPLE: + if (ent->spawnflags & 0x40) + Game::R_AddDebugBounds(once, &b); + else + Game::R_AddDebugBounds(multiple, &b); + break; + + default: + float rv = std::min((float)ent->handler, (float)5) / 5; + float gv = std::clamp((float)ent->handler - 5, (float)0, (float)5) / 5; + float bv = std::clamp((float)ent->handler - 10, (float)0, (float)5) / 5; + + float color[4] = { rv, gv, bv, 1.0f }; + + Game::R_AddDebugBounds(color, &b); + break; + } + } + } + } + + void Renderer::DebugDrawSceneModelCollisions() + { + if (!DrawSceneModelCollisions.get()) return; + + float green[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; + + auto* scene = Game::scene; + + for (auto i = 0; i < scene->sceneModelCount; i++) + { + if (!scene->sceneModel[i].model) + continue; + + for (auto j = 0; j < scene->sceneModel[i].model->numCollSurfs; j++) + { + auto b = scene->sceneModel[i].model->collSurfs[j].bounds; + b.midPoint[0] += scene->sceneModel[i].placement.base.origin[0]; + b.midPoint[1] += scene->sceneModel[i].placement.base.origin[1]; + b.midPoint[2] += scene->sceneModel[i].placement.base.origin[2]; + b.halfSize[0] *= scene->sceneModel[i].placement.scale; + b.halfSize[1] *= scene->sceneModel[i].placement.scale; + b.halfSize[2] *= scene->sceneModel[i].placement.scale; + + Game::R_AddDebugBounds(green, &b, &scene->sceneModel[i].placement.base.quat); + } + } + } + + void Renderer::DebugDrawSceneModelBoundingBoxes() + { + if (!DrawSceneModelBoundingBoxes.get()) return; + + float red[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; + float blue[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; + + auto* scene = Game::scene; + + for (auto i = 0; i < scene->sceneModelCount; i++) + { + if (!scene->sceneModel[i].model) + continue; + + auto b = scene->sceneModel[i].model->bounds; + b.midPoint[0] += scene->sceneModel[i].placement.base.origin[0]; + b.midPoint[1] += scene->sceneModel[i].placement.base.origin[1]; + b.midPoint[2] += scene->sceneModel[i].placement.base.origin[2]; + b.halfSize[0] *= scene->sceneModel[i].placement.scale; + b.halfSize[1] *= scene->sceneModel[i].placement.scale; + b.halfSize[2] *= scene->sceneModel[i].placement.scale; + Game::R_AddDebugBounds(red, &b, &scene->sceneModel[i].placement.base.quat); + } + + for (auto i = 0; i < scene->sceneDObjCount; i++) + { + scene->sceneDObj[i].cull.bounds.halfSize[0] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[0]); + scene->sceneDObj[i].cull.bounds.halfSize[1] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[1]); + scene->sceneDObj[i].cull.bounds.halfSize[2] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[2]); + + if (scene->sceneDObj[i].cull.bounds.halfSize[0] < 0 || + scene->sceneDObj[i].cull.bounds.halfSize[1] < 0 || + scene->sceneDObj[i].cull.bounds.halfSize[2] < 0) + { + + Components::Logger::Print("WARNING: Negative half size for DOBJ %s, this will cause culling issues!", scene->sceneDObj[i].obj->models[0]->name); + } + + Game::R_AddDebugBounds(blue, &scene->sceneDObj[i].cull.bounds); + } + } + + void Renderer::DebugDrawModelNames() + { + auto val = DrawModelNames.get()->current.integer; + + if (!val) return; + + float sceneModelsColor[4] = { 1.0f, 1.0f, 0.0f, 1.0f }; + float dobjsColor[4] = { 0.0f, 1.0f, 1.0f, 1.0f }; + float staticModelsColor[4] = { 1.0f, 0.0f, 1.0f, 1.0f }; + + auto mapName = Dvar::Var("mapname").get(); + auto* scene = Game::scene; + auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; + + if (val == 1) + { + for (auto i = 0; i < scene->sceneModelCount; i++) + { + if (!scene->sceneModel[i].model) + continue; + + Game::R_AddDebugString(sceneModelsColor, scene->sceneModel[i].placement.base.origin, 1.0, scene->sceneModel[i].model->name); + } + } + + else if (val == 2) + { + for (auto i = 0; i < scene->sceneDObjCount; i++) + { + if (scene->sceneDObj[i].obj) + { + for (int j = 0; j < scene->sceneDObj[i].obj->numModels; j++) + { + Game::R_AddDebugString(dobjsColor, scene->sceneDObj[i].placement.origin, 1.0, scene->sceneDObj[i].obj->models[j]->name); + } + } + } + } + + else if (val == 3) + { + // Static models + for (size_t i = 0; i < world->dpvs.smodelCount; i++) + { + auto staticModel = world->dpvs.smodelDrawInsts[i]; + if (staticModel.model) + { + Game::R_AddDebugString(staticModelsColor, staticModel.placement.origin, 1.0, staticModel.model->name); + } + } + } + } + + void Renderer::DebugDrawAABBTrees() + { + if (!DrawAABBTrees.get()) return; + + float cyan[4] = { 0.0f, 0.5f, 0.5f, 1.0f }; + float red[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; + + Game::clipMap_t* clipMap = *reinterpret_cast(0x7998E0); + //Game::GfxWorld* gameWorld = *reinterpret_cast(0x66DEE94); + if (!clipMap) return; + + for (unsigned short i = 0; i < clipMap->smodelNodeCount; ++i) + { + Game::R_AddDebugBounds(cyan, &clipMap->smodelNodes[i].bounds); + } + + for (unsigned int i = 0; i < clipMap->numStaticModels; i += 2) + { + Game::R_AddDebugBounds(red, &clipMap->staticModelList[i].absBounds); + } + } + Renderer::Renderer() { if (Dedicated::IsEnabled()) return; @@ -230,6 +439,24 @@ namespace Components // End vid_restart Utils::Hook(0x4CA3A7, Renderer::PostVidRestartStub, HOOK_CALL).install()->quick(); + + Dvar::OnInit([] + { + static std::vector < char* > values = + { + const_cast("Disabled"), + const_cast("Scene Models"), + const_cast("Scene Dynamic Objects"), + const_cast("GfxWorld Static Models"), + nullptr + }; + + Renderer::DrawSceneModelBoundingBoxes = Game::Dvar_RegisterBool("r_drawSceneModelBoundingBoxes", false, Game::DVAR_FLAG_CHEAT, "Draw scene model bounding boxes"); + Renderer::DrawSceneModelCollisions = Game::Dvar_RegisterBool("r_drawSceneModelCollisions", false, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); + Renderer::DrawTriggers = Game::Dvar_RegisterBool("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); + Renderer::DrawModelNames = Game::Dvar_RegisterEnum("r_drawModelNames", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw all model names"); + Renderer::DrawAABBTrees = Game::Dvar_RegisterBool("r_drawAabbTrees", false, Game::DVAR_FLAG_CHEAT, "Draw aabb trees"); + }); } Renderer::~Renderer() diff --git a/src/Components/Modules/Renderer.hpp b/src/Components/Modules/Renderer.hpp index 040eb6ba..48b5a0e6 100644 --- a/src/Components/Modules/Renderer.hpp +++ b/src/Components/Modules/Renderer.hpp @@ -35,10 +35,22 @@ namespace Components static int DrawTechsetForMaterial(int a1, float a2, float a3, const char* material, Game::vec4_t* color, int a6); + static void DebugDrawTriggers(); + static void DebugDrawSceneModelCollisions(); + static void DebugDrawSceneModelBoundingBoxes(); + static void DebugDrawModelNames(); + static void DebugDrawAABBTrees(); + static Utils::Signal EndRecoverDeviceSignal; static Utils::Signal BeginRecoverDeviceSignal; static Utils::Signal BackendFrameSignal; static Utils::Signal SingleBackendFrameSignal; + + static Dvar::Var DrawTriggers; + static Dvar::Var DrawSceneModelCollisions; + static Dvar::Var DrawSceneModelBoundingBoxes; + static Dvar::Var DrawModelNames; + static Dvar::Var DrawAABBTrees; }; } From 15a3f0fe040b1ec73ea02325c4fc487c21a45aca Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Sun, 7 Nov 2021 22:57:35 +0100 Subject: [PATCH 02/12] Actually call the functions --- src/Components/Modules/Renderer.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index be2d8240..f789e02a 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -379,6 +379,17 @@ namespace Components { if (Dedicated::IsEnabled()) return; + Scheduler::OnFrame([]() { + if (Game::CL_IsCgameInitialized()) + { + DebugDrawAABBTrees(); + DebugDrawModelNames(); + DebugDrawSceneModelBoundingBoxes(); + DebugDrawSceneModelCollisions(); + DebugDrawTriggers(); + } + }); + // Renderer::OnBackendFrame([] (IDirect3DDevice9* device) // { // if (Game::Sys_Milliseconds() % 2) From 0c87243519e7adbe10d7868886b89fb4db76f9b9 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Mon, 8 Nov 2021 17:55:48 +0100 Subject: [PATCH 03/12] Vec3 Square distance function --- src/Utils/Utils.cpp | 7 +++++++ src/Utils/Utils.hpp | 1 + 2 files changed, 8 insertions(+) diff --git a/src/Utils/Utils.cpp b/src/Utils/Utils.cpp index 3a16ee58..299bfa6c 100644 --- a/src/Utils/Utils.cpp +++ b/src/Utils/Utils.cpp @@ -149,4 +149,11 @@ namespace Utils { return !(base1 + len1 <= base2 || base2 + len2 <= base1); } + + float Vec3SqrDistance(float(*v1)[3], float(*v2)[3]) + { + return (std::pow((*v2)[0] - (*v1)[0], 2) + std::pow((*v2)[1] - (*v1)[1], 2) + std::pow((*v2)[2] - (*v1)[2], 2)); + } + + } diff --git a/src/Utils/Utils.hpp b/src/Utils/Utils.hpp index f296f0e1..257ec5bf 100644 --- a/src/Utils/Utils.hpp +++ b/src/Utils/Utils.hpp @@ -24,6 +24,7 @@ namespace Utils void OpenUrl(const std::string& url); bool HasIntercection(unsigned int base1, unsigned int len1, unsigned int base2, unsigned int len2); + float Vec3SqrDistance(float (*v1)[3], float (*v2)[3]); template inline void RotLeft(T& object, size_t bits) { From b63cdc6632c5fa7c69a460418f37ddeddcfad7ec Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Mon, 8 Nov 2021 17:56:19 +0100 Subject: [PATCH 04/12] Much better r_drawmodelnames and r_drawboundingboxes for every model category --- src/Components/Modules/Renderer.cpp | 277 +++++++++++++++++----------- src/Components/Modules/Renderer.hpp | 4 +- 2 files changed, 172 insertions(+), 109 deletions(-) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index f789e02a..e4463488 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -10,10 +10,16 @@ namespace Components Dvar::Var Renderer::DrawTriggers; Dvar::Var Renderer::DrawSceneModelCollisions; - Dvar::Var Renderer::DrawSceneModelBoundingBoxes; + Dvar::Var Renderer::DrawModelBoundingBoxes; Dvar::Var Renderer::DrawModelNames; Dvar::Var Renderer::DrawAABBTrees; + float sceneModelsColor[4] = { 1.0f, 1.0f, 0.0f, 1.0f }; + float dobjsColor[4] = { 0.0f, 1.0f, 1.0f, 1.0f }; + float staticModelsColor[4] = { 1.0f, 0.0f, 1.0f, 1.0f }; + float gentitiesColor[4] = { 1.0f, 0.5f, 0.5f, 1.0f }; + int drawDistance = 80000; + __declspec(naked) void Renderer::FrameStub() { __asm @@ -133,7 +139,7 @@ namespace Components // show error pushad; - push [esp + 24h + 20h]; + push[esp + 24h + 20h]; push eax; call R_TextureFromCodeError; add esp, 8; @@ -258,45 +264,90 @@ namespace Components } } - void Renderer::DebugDrawSceneModelBoundingBoxes() + void Renderer::DebugDrawModelBoundingBoxes() { - if (!DrawSceneModelBoundingBoxes.get()) return; + auto val = DrawModelBoundingBoxes.get()->current.integer; - float red[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; - float blue[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; + if (!val) return; + int clientNum = Game::CG_GetClientNum(); + if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client) return; + + Game::gentity_t* clientEntity = &Game::g_entities[clientNum]; + + float(*playerPosition)[3] = &clientEntity->r.currentOrigin; + + auto mapName = Dvar::Var("mapname").get(); auto* scene = Game::scene; + auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; - for (auto i = 0; i < scene->sceneModelCount; i++) - { - if (!scene->sceneModel[i].model) - continue; + switch (val) { + case 1: + for (auto i = 0; i < scene->sceneModelCount; i++) + { + if (!scene->sceneModel[i].model) + continue; - auto b = scene->sceneModel[i].model->bounds; - b.midPoint[0] += scene->sceneModel[i].placement.base.origin[0]; - b.midPoint[1] += scene->sceneModel[i].placement.base.origin[1]; - b.midPoint[2] += scene->sceneModel[i].placement.base.origin[2]; - b.halfSize[0] *= scene->sceneModel[i].placement.scale; - b.halfSize[1] *= scene->sceneModel[i].placement.scale; - b.halfSize[2] *= scene->sceneModel[i].placement.scale; - Game::R_AddDebugBounds(red, &b, &scene->sceneModel[i].placement.base.quat); - } + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < drawDistance) + { + auto b = scene->sceneModel[i].model->bounds; + b.midPoint[0] += scene->sceneModel[i].placement.base.origin[0]; + b.midPoint[1] += scene->sceneModel[i].placement.base.origin[1]; + b.midPoint[2] += scene->sceneModel[i].placement.base.origin[2]; + b.halfSize[0] *= scene->sceneModel[i].placement.scale; + b.halfSize[1] *= scene->sceneModel[i].placement.scale; + b.halfSize[2] *= scene->sceneModel[i].placement.scale; + Game::R_AddDebugBounds(sceneModelsColor, &b, &scene->sceneModel[i].placement.base.quat); + } + } + break; - for (auto i = 0; i < scene->sceneDObjCount; i++) - { - scene->sceneDObj[i].cull.bounds.halfSize[0] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[0]); - scene->sceneDObj[i].cull.bounds.halfSize[1] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[1]); - scene->sceneDObj[i].cull.bounds.halfSize[2] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[2]); - - if (scene->sceneDObj[i].cull.bounds.halfSize[0] < 0 || - scene->sceneDObj[i].cull.bounds.halfSize[1] < 0 || - scene->sceneDObj[i].cull.bounds.halfSize[2] < 0) + case 2: + for (auto i = 0; i < scene->sceneDObjCount; i++) { - Components::Logger::Print("WARNING: Negative half size for DOBJ %s, this will cause culling issues!", scene->sceneDObj[i].obj->models[0]->name); - } + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].cull.bounds.midPoint) < drawDistance) + { + scene->sceneDObj[i].cull.bounds.halfSize[0] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[0]); + scene->sceneDObj[i].cull.bounds.halfSize[1] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[1]); + scene->sceneDObj[i].cull.bounds.halfSize[2] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[2]); - Game::R_AddDebugBounds(blue, &scene->sceneDObj[i].cull.bounds); + if (scene->sceneDObj[i].cull.bounds.halfSize[0] < 0 || + scene->sceneDObj[i].cull.bounds.halfSize[1] < 0 || + scene->sceneDObj[i].cull.bounds.halfSize[2] < 0) + { + + Components::Logger::Print("WARNING: Negative half size for DOBJ %s, this will cause culling issues!", scene->sceneDObj[i].obj->models[0]->name); + } + + Game::R_AddDebugBounds(dobjsColor, &scene->sceneDObj[i].cull.bounds); + } + } + break; + + case 3: + // Static models + for (size_t i = 0; i < world->dpvs.smodelCount; i++) + { + auto staticModel = world->dpvs.smodelDrawInsts[i]; + + if (Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin) < drawDistance) + { + if (staticModel.model) + { + Game::Bounds b = staticModel.model->bounds; + b.midPoint[0] += staticModel.placement.origin[0]; + b.midPoint[1] += staticModel.placement.origin[1]; + b.midPoint[2] += staticModel.placement.origin[2]; + b.halfSize[0] *= staticModel.placement.scale; + b.halfSize[1] *= staticModel.placement.scale; + b.halfSize[2] *= staticModel.placement.scale; + + Game::R_AddDebugBounds(staticModelsColor, &b); + } + } + } + break; } } @@ -306,50 +357,62 @@ namespace Components if (!val) return; - float sceneModelsColor[4] = { 1.0f, 1.0f, 0.0f, 1.0f }; - float dobjsColor[4] = { 0.0f, 1.0f, 1.0f, 1.0f }; - float staticModelsColor[4] = { 1.0f, 0.0f, 1.0f, 1.0f }; + int clientNum = Game::CG_GetClientNum(); + if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client) return; + + Game::gentity_t* clientEntity = &Game::g_entities[clientNum]; + + float (*playerPosition)[3] = &clientEntity->r.currentOrigin; auto mapName = Dvar::Var("mapname").get(); auto* scene = Game::scene; auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; - if (val == 1) - { + switch (val) { + case 1: for (auto i = 0; i < scene->sceneModelCount; i++) { if (!scene->sceneModel[i].model) continue; - Game::R_AddDebugString(sceneModelsColor, scene->sceneModel[i].placement.base.origin, 1.0, scene->sceneModel[i].model->name); + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < drawDistance) + { + Game::R_AddDebugString(sceneModelsColor, scene->sceneModel[i].placement.base.origin, 1.0, scene->sceneModel[i].model->name); + } } - } + break; - else if (val == 2) - { + case 2: for (auto i = 0; i < scene->sceneDObjCount; i++) { if (scene->sceneDObj[i].obj) { for (int j = 0; j < scene->sceneDObj[i].obj->numModels; j++) { - Game::R_AddDebugString(dobjsColor, scene->sceneDObj[i].placement.origin, 1.0, scene->sceneDObj[i].obj->models[j]->name); + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].placement.origin) < drawDistance) + { + Game::R_AddDebugString(dobjsColor, scene->sceneDObj[i].placement.origin, 1.0, scene->sceneDObj[i].obj->models[j]->name); + } } } } - } + break; - else if (val == 3) - { + case 3: // Static models for (size_t i = 0; i < world->dpvs.smodelCount; i++) { auto staticModel = world->dpvs.smodelDrawInsts[i]; if (staticModel.model) { - Game::R_AddDebugString(staticModelsColor, staticModel.placement.origin, 1.0, staticModel.model->name); + auto dist = Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin); + if (dist < drawDistance) + { + Game::R_AddDebugString(staticModelsColor, staticModel.placement.origin, 1.0, staticModel.model->name); + } } } + break; } } @@ -384,41 +447,41 @@ namespace Components { DebugDrawAABBTrees(); DebugDrawModelNames(); - DebugDrawSceneModelBoundingBoxes(); + DebugDrawModelBoundingBoxes(); DebugDrawSceneModelCollisions(); DebugDrawTriggers(); } }); -// Renderer::OnBackendFrame([] (IDirect3DDevice9* device) -// { -// if (Game::Sys_Milliseconds() % 2) -// { -// device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, 0, 0, 0); -// } -// -// return; -// -// IDirect3DSurface9* buffer = nullptr; -// -// device->CreateOffscreenPlainSurface(Renderer::Width(), Renderer::Height(), D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &buffer, nullptr); -// device->GetFrontBufferData(0, buffer); -// -// if (buffer) -// { -// D3DSURFACE_DESC desc; -// D3DLOCKED_RECT lockedRect; -// -// buffer->GetDesc(&desc); -// -// HRESULT res = buffer->LockRect(&lockedRect, NULL, D3DLOCK_READONLY); -// -// -// buffer->UnlockRect(); -// } -// }); + // Renderer::OnBackendFrame([] (IDirect3DDevice9* device) + // { + // if (Game::Sys_Milliseconds() % 2) + // { + // device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, 0, 0, 0); + // } + // + // return; + // + // IDirect3DSurface9* buffer = nullptr; + // + // device->CreateOffscreenPlainSurface(Renderer::Width(), Renderer::Height(), D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &buffer, nullptr); + // device->GetFrontBufferData(0, buffer); + // + // if (buffer) + // { + // D3DSURFACE_DESC desc; + // D3DLOCKED_RECT lockedRect; + // + // buffer->GetDesc(&desc); + // + // HRESULT res = buffer->LockRect(&lockedRect, NULL, D3DLOCK_READONLY); + // + // + // buffer->UnlockRect(); + // } + // }); - // Log broken materials + // Log broken materials Utils::Hook(0x0054CAAA, Renderer::StoreGfxBufContextPtrStub1, HOOK_JUMP).install()->quick(); Utils::Hook(0x0054CF8D, Renderer::StoreGfxBufContextPtrStub2, HOOK_JUMP).install()->quick(); @@ -433,41 +496,41 @@ namespace Components // Begin device recovery (not D3D9Ex) Utils::Hook(0x508298, []() - { - Game::DB_BeginRecoverLostDevice(); - Renderer::BeginRecoverDeviceSignal(); - }, HOOK_CALL).install()->quick(); - - // End device recovery (not D3D9Ex) - Utils::Hook(0x508355, []() - { - Renderer::EndRecoverDeviceSignal(); - Game::DB_EndRecoverLostDevice(); - }, HOOK_CALL).install()->quick(); - - // Begin vid_restart - Utils::Hook(0x4CA2FD, Renderer::PreVidRestart, HOOK_CALL).install()->quick(); - - // End vid_restart - Utils::Hook(0x4CA3A7, Renderer::PostVidRestartStub, HOOK_CALL).install()->quick(); - - Dvar::OnInit([] { - static std::vector < char* > values = - { - const_cast("Disabled"), - const_cast("Scene Models"), - const_cast("Scene Dynamic Objects"), - const_cast("GfxWorld Static Models"), - nullptr - }; + Game::DB_BeginRecoverLostDevice(); + Renderer::BeginRecoverDeviceSignal(); + }, HOOK_CALL).install()->quick(); - Renderer::DrawSceneModelBoundingBoxes = Game::Dvar_RegisterBool("r_drawSceneModelBoundingBoxes", false, Game::DVAR_FLAG_CHEAT, "Draw scene model bounding boxes"); - Renderer::DrawSceneModelCollisions = Game::Dvar_RegisterBool("r_drawSceneModelCollisions", false, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); - Renderer::DrawTriggers = Game::Dvar_RegisterBool("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); - Renderer::DrawModelNames = Game::Dvar_RegisterEnum("r_drawModelNames", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw all model names"); - Renderer::DrawAABBTrees = Game::Dvar_RegisterBool("r_drawAabbTrees", false, Game::DVAR_FLAG_CHEAT, "Draw aabb trees"); - }); + // End device recovery (not D3D9Ex) + Utils::Hook(0x508355, []() + { + Renderer::EndRecoverDeviceSignal(); + Game::DB_EndRecoverLostDevice(); + }, HOOK_CALL).install()->quick(); + + // Begin vid_restart + Utils::Hook(0x4CA2FD, Renderer::PreVidRestart, HOOK_CALL).install()->quick(); + + // End vid_restart + Utils::Hook(0x4CA3A7, Renderer::PostVidRestartStub, HOOK_CALL).install()->quick(); + + Dvar::OnInit([] + { + static std::vector < char* > values = + { + const_cast("Disabled"), + const_cast("Scene Models"), + const_cast("Scene Dynamic Objects"), + const_cast("GfxWorld Static Models") + nullptr + }; + + Renderer::DrawModelBoundingBoxes = Game::Dvar_RegisterEnum("r_drawModelBoundingBoxes", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw scene model bounding boxes"); + Renderer::DrawSceneModelCollisions = Game::Dvar_RegisterEnum("r_drawSceneModelCollisions", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); + Renderer::DrawTriggers = Game::Dvar_RegisterBool("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); + Renderer::DrawModelNames = Game::Dvar_RegisterEnum("r_drawModelNames", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw all model names"); + Renderer::DrawAABBTrees = Game::Dvar_RegisterBool("r_drawAabbTrees", false, Game::DVAR_FLAG_CHEAT, "Draw aabb trees"); + }); } Renderer::~Renderer() diff --git a/src/Components/Modules/Renderer.hpp b/src/Components/Modules/Renderer.hpp index 48b5a0e6..8773fd3a 100644 --- a/src/Components/Modules/Renderer.hpp +++ b/src/Components/Modules/Renderer.hpp @@ -37,7 +37,7 @@ namespace Components static void DebugDrawTriggers(); static void DebugDrawSceneModelCollisions(); - static void DebugDrawSceneModelBoundingBoxes(); + static void DebugDrawModelBoundingBoxes(); static void DebugDrawModelNames(); static void DebugDrawAABBTrees(); @@ -49,7 +49,7 @@ namespace Components static Dvar::Var DrawTriggers; static Dvar::Var DrawSceneModelCollisions; - static Dvar::Var DrawSceneModelBoundingBoxes; + static Dvar::Var DrawModelBoundingBoxes; static Dvar::Var DrawModelNames; static Dvar::Var DrawAABBTrees; }; From 08054af027d07236537e5817d8d53733837994c7 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Mon, 8 Nov 2021 18:05:49 +0100 Subject: [PATCH 05/12] Whoopsies --- src/Components/Modules/Renderer.cpp | 2 +- src/Utils/Utils.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index e4463488..7ea81832 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -521,7 +521,7 @@ namespace Components const_cast("Disabled"), const_cast("Scene Models"), const_cast("Scene Dynamic Objects"), - const_cast("GfxWorld Static Models") + const_cast("GfxWorld Static Models"), nullptr }; diff --git a/src/Utils/Utils.cpp b/src/Utils/Utils.cpp index 299bfa6c..8a7a7cb8 100644 --- a/src/Utils/Utils.cpp +++ b/src/Utils/Utils.cpp @@ -152,7 +152,7 @@ namespace Utils float Vec3SqrDistance(float(*v1)[3], float(*v2)[3]) { - return (std::pow((*v2)[0] - (*v1)[0], 2) + std::pow((*v2)[1] - (*v1)[1], 2) + std::pow((*v2)[2] - (*v1)[2], 2)); + return static_cast(std::pow((*v2)[0] - (*v1)[0], 2) + std::pow((*v2)[1] - (*v1)[1], 2) + std::pow((*v2)[2] - (*v1)[2], 2)); } From b57a7800511ba6245b4970d2e0f5383501fff9f9 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Mon, 8 Nov 2021 19:00:51 +0100 Subject: [PATCH 06/12] Fixed formatting a little bit --- src/Components/Modules/Renderer.cpp | 56 ++++++++++++++--------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index 7ea81832..815521b5 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -362,7 +362,7 @@ namespace Components Game::gentity_t* clientEntity = &Game::g_entities[clientNum]; - float (*playerPosition)[3] = &clientEntity->r.currentOrigin; + float(*playerPosition)[3] = &clientEntity->r.currentOrigin; auto mapName = Dvar::Var("mapname").get(); auto* scene = Game::scene; @@ -451,7 +451,7 @@ namespace Components DebugDrawSceneModelCollisions(); DebugDrawTriggers(); } - }); + }); // Renderer::OnBackendFrame([] (IDirect3DDevice9* device) // { @@ -501,36 +501,36 @@ namespace Components Renderer::BeginRecoverDeviceSignal(); }, HOOK_CALL).install()->quick(); - // End device recovery (not D3D9Ex) - Utils::Hook(0x508355, []() - { - Renderer::EndRecoverDeviceSignal(); - Game::DB_EndRecoverLostDevice(); - }, HOOK_CALL).install()->quick(); + // End device recovery (not D3D9Ex) + Utils::Hook(0x508355, []() + { + Renderer::EndRecoverDeviceSignal(); + Game::DB_EndRecoverLostDevice(); + }, HOOK_CALL).install()->quick(); - // Begin vid_restart - Utils::Hook(0x4CA2FD, Renderer::PreVidRestart, HOOK_CALL).install()->quick(); + // Begin vid_restart + Utils::Hook(0x4CA2FD, Renderer::PreVidRestart, HOOK_CALL).install()->quick(); - // End vid_restart - Utils::Hook(0x4CA3A7, Renderer::PostVidRestartStub, HOOK_CALL).install()->quick(); + // End vid_restart + Utils::Hook(0x4CA3A7, Renderer::PostVidRestartStub, HOOK_CALL).install()->quick(); - Dvar::OnInit([] - { - static std::vector < char* > values = - { - const_cast("Disabled"), - const_cast("Scene Models"), - const_cast("Scene Dynamic Objects"), - const_cast("GfxWorld Static Models"), - nullptr - }; + Dvar::OnInit([] + { + static std::vector < char* > values = + { + const_cast("Disabled"), + const_cast("Scene Models"), + const_cast("Scene Dynamic Objects"), + const_cast("GfxWorld Static Models"), + nullptr + }; - Renderer::DrawModelBoundingBoxes = Game::Dvar_RegisterEnum("r_drawModelBoundingBoxes", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw scene model bounding boxes"); - Renderer::DrawSceneModelCollisions = Game::Dvar_RegisterEnum("r_drawSceneModelCollisions", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); - Renderer::DrawTriggers = Game::Dvar_RegisterBool("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); - Renderer::DrawModelNames = Game::Dvar_RegisterEnum("r_drawModelNames", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw all model names"); - Renderer::DrawAABBTrees = Game::Dvar_RegisterBool("r_drawAabbTrees", false, Game::DVAR_FLAG_CHEAT, "Draw aabb trees"); - }); + Renderer::DrawModelBoundingBoxes = Game::Dvar_RegisterEnum("r_drawModelBoundingBoxes", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw scene model bounding boxes"); + Renderer::DrawSceneModelCollisions = Game::Dvar_RegisterEnum("r_drawSceneModelCollisions", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); + Renderer::DrawTriggers = Game::Dvar_RegisterBool("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); + Renderer::DrawModelNames = Game::Dvar_RegisterEnum("r_drawModelNames", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw all model names"); + Renderer::DrawAABBTrees = Game::Dvar_RegisterBool("r_drawAabbTrees", false, Game::DVAR_FLAG_CHEAT, "Draw aabb trees"); + }); } Renderer::~Renderer() From 892f521728b7dad6641820cae0c2937811617c6c Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Thu, 11 Nov 2021 14:00:46 +0100 Subject: [PATCH 07/12] Added a dvar for player draw debug distance, use array for dvar enums, a bit of formatting --- src/Components/Modules/Renderer.cpp | 104 ++++++++++++++++------------ src/Components/Modules/Renderer.hpp | 11 +-- 2 files changed, 66 insertions(+), 49 deletions(-) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index 815521b5..a1d01f90 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -8,17 +8,29 @@ namespace Components Utils::Signal Renderer::EndRecoverDeviceSignal; Utils::Signal Renderer::BeginRecoverDeviceSignal; - Dvar::Var Renderer::DrawTriggers; - Dvar::Var Renderer::DrawSceneModelCollisions; - Dvar::Var Renderer::DrawModelBoundingBoxes; - Dvar::Var Renderer::DrawModelNames; - Dvar::Var Renderer::DrawAABBTrees; + Dvar::Var Renderer::r_drawTriggers; + Dvar::Var Renderer::r_drawSceneModelCollisions; + Dvar::Var Renderer::r_drawModelBoundingBoxes; + Dvar::Var Renderer::r_drawModelNames; + Dvar::Var Renderer::r_drawAABBTrees; + Dvar::Var Renderer::r_playerDrawDebugDistance; + float cyan[4] = { 0.0f, 0.5f, 0.5f, 1.0f }; + float red[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; + float green[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; + + // R_draw model names & collisions colors float sceneModelsColor[4] = { 1.0f, 1.0f, 0.0f, 1.0f }; float dobjsColor[4] = { 0.0f, 1.0f, 1.0f, 1.0f }; float staticModelsColor[4] = { 1.0f, 0.0f, 1.0f, 1.0f }; float gentitiesColor[4] = { 1.0f, 0.5f, 0.5f, 1.0f }; - int drawDistance = 80000; + + // Trigger colors + float hurt[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; + float hurtTouch[4] = { 0.75f, 0.0f, 0.0f, 1.0f }; + float damage[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; + float once[4] = { 0.0f, 1.0f, 1.0f, 1.0f }; + float multiple[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; __declspec(naked) void Renderer::FrameStub() { @@ -180,17 +192,11 @@ namespace Components void Renderer::DebugDrawTriggers() { - if (!DrawTriggers.get()) return; - - float hurt[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; - float hurtTouch[4] = { 0.75f, 0.0f, 0.0f, 1.0f }; - float damage[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; - float once[4] = { 0.0f, 1.0f, 1.0f, 1.0f }; - float multiple[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; + if (!r_drawTriggers.get()) return; auto* entities = Game::g_entities; - for (auto i = 0u; i < 0x800; i++) + for (auto i = 0u; i < 0x800u; i++) { auto* ent = &entities[i]; @@ -223,9 +229,9 @@ namespace Components break; default: - float rv = std::min((float)ent->handler, (float)5) / 5; - float gv = std::clamp((float)ent->handler - 5, (float)0, (float)5) / 5; - float bv = std::clamp((float)ent->handler - 10, (float)0, (float)5) / 5; + auto rv = std::min(static_cast(ent->handler), 5.0f) / 5.0f; + auto gv = std::clamp(static_cast(ent->handler - 5), 0.f, 5.f) / 5.f; + auto bv = std::clamp(static_cast(ent->handler - 10), 0.f, 5.f) / 5.f; float color[4] = { rv, gv, bv, 1.0f }; @@ -238,9 +244,7 @@ namespace Components void Renderer::DebugDrawSceneModelCollisions() { - if (!DrawSceneModelCollisions.get()) return; - - float green[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; + if (!r_drawSceneModelCollisions.get()) return; auto* scene = Game::scene; @@ -266,12 +270,19 @@ namespace Components void Renderer::DebugDrawModelBoundingBoxes() { - auto val = DrawModelBoundingBoxes.get()->current.integer; + auto val = r_drawModelBoundingBoxes.get(); if (!val) return; + // Ingame only int clientNum = Game::CG_GetClientNum(); - if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client) return; + if (!Game::CL_IsCgameInitialized() || + clientNum >= 18 || + clientNum < 0 || + !Game::g_entities[clientNum].client) { + + return; + } Game::gentity_t* clientEntity = &Game::g_entities[clientNum]; @@ -288,7 +299,7 @@ namespace Components if (!scene->sceneModel[i].model) continue; - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < drawDistance) + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < r_playerDrawDebugDistance.get()) { auto b = scene->sceneModel[i].model->bounds; b.midPoint[0] += scene->sceneModel[i].placement.base.origin[0]; @@ -306,7 +317,7 @@ namespace Components for (auto i = 0; i < scene->sceneDObjCount; i++) { - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].cull.bounds.midPoint) < drawDistance) + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].cull.bounds.midPoint) < r_playerDrawDebugDistance.get()) { scene->sceneDObj[i].cull.bounds.halfSize[0] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[0]); scene->sceneDObj[i].cull.bounds.halfSize[1] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[1]); @@ -331,7 +342,7 @@ namespace Components { auto staticModel = world->dpvs.smodelDrawInsts[i]; - if (Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin) < drawDistance) + if (Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin) < r_playerDrawDebugDistance.get()) { if (staticModel.model) { @@ -353,12 +364,19 @@ namespace Components void Renderer::DebugDrawModelNames() { - auto val = DrawModelNames.get()->current.integer; + auto val = r_drawModelNames.get(); if (!val) return; + // Ingame only int clientNum = Game::CG_GetClientNum(); - if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || !Game::g_entities[clientNum].client) return; + if (!Game::CL_IsCgameInitialized() || + clientNum >= 18 || + clientNum < 0 || + !Game::g_entities[clientNum].client) { + + return; + } Game::gentity_t* clientEntity = &Game::g_entities[clientNum]; @@ -375,7 +393,7 @@ namespace Components if (!scene->sceneModel[i].model) continue; - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < drawDistance) + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < r_playerDrawDebugDistance.get()) { Game::R_AddDebugString(sceneModelsColor, scene->sceneModel[i].placement.base.origin, 1.0, scene->sceneModel[i].model->name); } @@ -389,7 +407,7 @@ namespace Components { for (int j = 0; j < scene->sceneDObj[i].obj->numModels; j++) { - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].placement.origin) < drawDistance) + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].placement.origin) < r_playerDrawDebugDistance.get()) { Game::R_AddDebugString(dobjsColor, scene->sceneDObj[i].placement.origin, 1.0, scene->sceneDObj[i].obj->models[j]->name); } @@ -406,7 +424,7 @@ namespace Components if (staticModel.model) { auto dist = Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin); - if (dist < drawDistance) + if (dist < r_playerDrawDebugDistance.get()) { Game::R_AddDebugString(staticModelsColor, staticModel.placement.origin, 1.0, staticModel.model->name); } @@ -418,10 +436,7 @@ namespace Components void Renderer::DebugDrawAABBTrees() { - if (!DrawAABBTrees.get()) return; - - float cyan[4] = { 0.0f, 0.5f, 0.5f, 1.0f }; - float red[4] = { 1.0f, 0.0f, 0.0f, 1.0f }; + if (!r_drawAABBTrees.get()) return; Game::clipMap_t* clipMap = *reinterpret_cast(0x7998E0); //Game::GfxWorld* gameWorld = *reinterpret_cast(0x66DEE94); @@ -516,20 +531,21 @@ namespace Components Dvar::OnInit([] { - static std::vector < char* > values = + char* values[5] = { - const_cast("Disabled"), - const_cast("Scene Models"), - const_cast("Scene Dynamic Objects"), - const_cast("GfxWorld Static Models"), + "Disabled", + "Scene Models", + "Scene Dynamic Objects", + "GfxWorld Static Models", nullptr }; - Renderer::DrawModelBoundingBoxes = Game::Dvar_RegisterEnum("r_drawModelBoundingBoxes", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw scene model bounding boxes"); - Renderer::DrawSceneModelCollisions = Game::Dvar_RegisterEnum("r_drawSceneModelCollisions", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); - Renderer::DrawTriggers = Game::Dvar_RegisterBool("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); - Renderer::DrawModelNames = Game::Dvar_RegisterEnum("r_drawModelNames", values.data(), 0, Game::DVAR_FLAG_CHEAT, "Draw all model names"); - Renderer::DrawAABBTrees = Game::Dvar_RegisterBool("r_drawAabbTrees", false, Game::DVAR_FLAG_CHEAT, "Draw aabb trees"); + Renderer::r_drawModelBoundingBoxes = Game::Dvar_RegisterEnum("r_drawModelBoundingBoxes", values, 0, Game::DVAR_FLAG_CHEAT, "Draw scene model bounding boxes"); + Renderer::r_drawSceneModelCollisions = Game::Dvar_RegisterEnum("r_drawSceneModelCollisions", values, 0, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); + Renderer::r_drawTriggers = Game::Dvar_RegisterBool("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); + Renderer::r_drawModelNames = Game::Dvar_RegisterEnum("r_drawModelNames", values, 0, Game::DVAR_FLAG_CHEAT, "Draw all model names"); + Renderer::r_drawAABBTrees = Game::Dvar_RegisterBool("r_drawAabbTrees", false, Game::DVAR_FLAG_CHEAT, "Draw aabb trees"); + Renderer::r_playerDrawDebugDistance = Game::Dvar_RegisterInt("r_drawDebugProximity", 100000, 0, 500000, Game::DVAR_FLAG_SAVED, "r_draw debug functions draw distance, relative to the player"); }); } diff --git a/src/Components/Modules/Renderer.hpp b/src/Components/Modules/Renderer.hpp index 8773fd3a..553dd336 100644 --- a/src/Components/Modules/Renderer.hpp +++ b/src/Components/Modules/Renderer.hpp @@ -47,10 +47,11 @@ namespace Components static Utils::Signal BackendFrameSignal; static Utils::Signal SingleBackendFrameSignal; - static Dvar::Var DrawTriggers; - static Dvar::Var DrawSceneModelCollisions; - static Dvar::Var DrawModelBoundingBoxes; - static Dvar::Var DrawModelNames; - static Dvar::Var DrawAABBTrees; + static Dvar::Var r_drawTriggers; + static Dvar::Var r_drawSceneModelCollisions; + static Dvar::Var r_drawModelBoundingBoxes; + static Dvar::Var r_drawModelNames; + static Dvar::Var r_drawAABBTrees; + static Dvar::Var r_playerDrawDebugDistance; }; } From 6d70cd3e743456530a2b645400a7d81b771bc9b6 Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Thu, 11 Nov 2021 16:44:54 +0100 Subject: [PATCH 08/12] Get int now works on enum dvars --- src/Components/Modules/Dvar.cpp | 2 +- src/Components/Modules/Renderer.cpp | 22 ++++++++++++++-------- src/Utils/Utils.cpp | 6 +++++- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Components/Modules/Dvar.cpp b/src/Components/Modules/Dvar.cpp index d0892d2e..c8628e4d 100644 --- a/src/Components/Modules/Dvar.cpp +++ b/src/Components/Modules/Dvar.cpp @@ -38,7 +38,7 @@ namespace Components template <> int Dvar::Var::get() { - if (this->dvar && this->dvar->type == Game::dvar_type::DVAR_TYPE_INT) + if (this->dvar && this->dvar->type == Game::dvar_type::DVAR_TYPE_INT || this->dvar->type == Game::dvar_type::DVAR_TYPE_ENUM) { return this->dvar->current.integer; } diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index a1d01f90..2aa68b2d 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -292,6 +292,9 @@ namespace Components auto* scene = Game::scene; auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; + auto drawDistance = r_playerDrawDebugDistance.get(); + auto sqrDist = drawDistance * drawDistance; + switch (val) { case 1: for (auto i = 0; i < scene->sceneModelCount; i++) @@ -299,7 +302,7 @@ namespace Components if (!scene->sceneModel[i].model) continue; - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < r_playerDrawDebugDistance.get()) + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < sqrDist) { auto b = scene->sceneModel[i].model->bounds; b.midPoint[0] += scene->sceneModel[i].placement.base.origin[0]; @@ -317,7 +320,7 @@ namespace Components for (auto i = 0; i < scene->sceneDObjCount; i++) { - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].cull.bounds.midPoint) < r_playerDrawDebugDistance.get()) + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].cull.bounds.midPoint) < sqrDist) { scene->sceneDObj[i].cull.bounds.halfSize[0] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[0]); scene->sceneDObj[i].cull.bounds.halfSize[1] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[1]); @@ -342,7 +345,7 @@ namespace Components { auto staticModel = world->dpvs.smodelDrawInsts[i]; - if (Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin) < r_playerDrawDebugDistance.get()) + if (Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin) < sqrDist) { if (staticModel.model) { @@ -386,6 +389,9 @@ namespace Components auto* scene = Game::scene; auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; + auto drawDistance = r_playerDrawDebugDistance.get(); + auto sqrDist = drawDistance * drawDistance; + switch (val) { case 1: for (auto i = 0; i < scene->sceneModelCount; i++) @@ -393,7 +399,7 @@ namespace Components if (!scene->sceneModel[i].model) continue; - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < r_playerDrawDebugDistance.get()) + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < sqrDist) { Game::R_AddDebugString(sceneModelsColor, scene->sceneModel[i].placement.base.origin, 1.0, scene->sceneModel[i].model->name); } @@ -407,7 +413,7 @@ namespace Components { for (int j = 0; j < scene->sceneDObj[i].obj->numModels; j++) { - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].placement.origin) < r_playerDrawDebugDistance.get()) + if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].placement.origin) < sqrDist) { Game::R_AddDebugString(dobjsColor, scene->sceneDObj[i].placement.origin, 1.0, scene->sceneDObj[i].obj->models[j]->name); } @@ -424,7 +430,7 @@ namespace Components if (staticModel.model) { auto dist = Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin); - if (dist < r_playerDrawDebugDistance.get()) + if (dist < sqrDist) { Game::R_AddDebugString(staticModelsColor, staticModel.placement.origin, 1.0, staticModel.model->name); } @@ -531,7 +537,7 @@ namespace Components Dvar::OnInit([] { - char* values[5] = + static char* values[5] = { "Disabled", "Scene Models", @@ -545,7 +551,7 @@ namespace Components Renderer::r_drawTriggers = Game::Dvar_RegisterBool("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); Renderer::r_drawModelNames = Game::Dvar_RegisterEnum("r_drawModelNames", values, 0, Game::DVAR_FLAG_CHEAT, "Draw all model names"); Renderer::r_drawAABBTrees = Game::Dvar_RegisterBool("r_drawAabbTrees", false, Game::DVAR_FLAG_CHEAT, "Draw aabb trees"); - Renderer::r_playerDrawDebugDistance = Game::Dvar_RegisterInt("r_drawDebugProximity", 100000, 0, 500000, Game::DVAR_FLAG_SAVED, "r_draw debug functions draw distance, relative to the player"); + Renderer::r_playerDrawDebugDistance = Game::Dvar_RegisterInt("r_drawDebugProximity", 1000, 0, 50000, Game::DVAR_FLAG_SAVED, "r_draw debug functions draw distance, relative to the player"); }); } diff --git a/src/Utils/Utils.cpp b/src/Utils/Utils.cpp index 8a7a7cb8..5feb2980 100644 --- a/src/Utils/Utils.cpp +++ b/src/Utils/Utils.cpp @@ -152,7 +152,11 @@ namespace Utils float Vec3SqrDistance(float(*v1)[3], float(*v2)[3]) { - return static_cast(std::pow((*v2)[0] - (*v1)[0], 2) + std::pow((*v2)[1] - (*v1)[1], 2) + std::pow((*v2)[2] - (*v1)[2], 2)); + auto x = (*v2)[0] - (*v1)[0]; + auto y = (*v2)[1] - (*v1)[1]; + auto z = (*v2)[2] - (*v1)[2]; + + return x * x + y * y + z * z; } From d726015ba24ad2d045c2052935808a4add5071de Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Mon, 13 Dec 2021 18:46:55 +0100 Subject: [PATCH 09/12] Fix some declarations and pointer madness in Vec3SqrDist --- src/Components/Modules/Renderer.cpp | 35 +++++++++++++++-------------- src/Utils/Utils.cpp | 8 +++---- src/Utils/Utils.hpp | 2 +- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index 2aa68b2d..ef957924 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -194,7 +194,7 @@ namespace Components { if (!r_drawTriggers.get()) return; - auto* entities = Game::g_entities; + auto entities = Game::g_entities; for (auto i = 0u; i < 0x800u; i++) { @@ -230,8 +230,8 @@ namespace Components default: auto rv = std::min(static_cast(ent->handler), 5.0f) / 5.0f; - auto gv = std::clamp(static_cast(ent->handler - 5), 0.f, 5.f) / 5.f; - auto bv = std::clamp(static_cast(ent->handler - 10), 0.f, 5.f) / 5.f; + auto gv = std::clamp(static_cast(ent->handler - 5), 0.f, 5.0f) / 5.0f; + auto bv = std::clamp(static_cast(ent->handler - 10), 0.f, 5.0f) / 5.0f; float color[4] = { rv, gv, bv, 1.0f }; @@ -246,7 +246,7 @@ namespace Components { if (!r_drawSceneModelCollisions.get()) return; - auto* scene = Game::scene; + auto scene = Game::scene; for (auto i = 0; i < scene->sceneModelCount; i++) { @@ -279,30 +279,31 @@ namespace Components if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || - !Game::g_entities[clientNum].client) { + Game::g_entities[clientNum].client != nullptr) { return; } Game::gentity_t* clientEntity = &Game::g_entities[clientNum]; - float(*playerPosition)[3] = &clientEntity->r.currentOrigin; + float playerPosition[3]{ clientEntity->r.currentOrigin[0], clientEntity->r.currentOrigin[1], clientEntity->r.currentOrigin[2] }; auto mapName = Dvar::Var("mapname").get(); - auto* scene = Game::scene; + auto scene = Game::scene; auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; auto drawDistance = r_playerDrawDebugDistance.get(); auto sqrDist = drawDistance * drawDistance; - switch (val) { + switch (val) + { case 1: for (auto i = 0; i < scene->sceneModelCount; i++) { if (!scene->sceneModel[i].model) continue; - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < sqrDist) + if (Utils::Vec3SqrDistance(playerPosition, scene->sceneModel[i].placement.base.origin) < sqrDist) { auto b = scene->sceneModel[i].model->bounds; b.midPoint[0] += scene->sceneModel[i].placement.base.origin[0]; @@ -320,7 +321,7 @@ namespace Components for (auto i = 0; i < scene->sceneDObjCount; i++) { - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].cull.bounds.midPoint) < sqrDist) + if (Utils::Vec3SqrDistance(playerPosition, scene->sceneDObj[i].cull.bounds.midPoint) < sqrDist) { scene->sceneDObj[i].cull.bounds.halfSize[0] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[0]); scene->sceneDObj[i].cull.bounds.halfSize[1] = std::abs(scene->sceneDObj[i].cull.bounds.halfSize[1]); @@ -345,7 +346,7 @@ namespace Components { auto staticModel = world->dpvs.smodelDrawInsts[i]; - if (Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin) < sqrDist) + if (Utils::Vec3SqrDistance(playerPosition, staticModel.placement.origin) < sqrDist) { if (staticModel.model) { @@ -376,17 +377,17 @@ namespace Components if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || - !Game::g_entities[clientNum].client) { + Game::g_entities[clientNum].client != nullptr) { return; } Game::gentity_t* clientEntity = &Game::g_entities[clientNum]; - float(*playerPosition)[3] = &clientEntity->r.currentOrigin; + float playerPosition[3]{ clientEntity->r.currentOrigin[0], clientEntity->r.currentOrigin[1], clientEntity->r.currentOrigin[2] }; auto mapName = Dvar::Var("mapname").get(); - auto* scene = Game::scene; + auto scene = Game::scene; auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; auto drawDistance = r_playerDrawDebugDistance.get(); @@ -399,7 +400,7 @@ namespace Components if (!scene->sceneModel[i].model) continue; - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneModel[i].placement.base.origin) < sqrDist) + if (Utils::Vec3SqrDistance(playerPosition, scene->sceneModel[i].placement.base.origin) < sqrDist) { Game::R_AddDebugString(sceneModelsColor, scene->sceneModel[i].placement.base.origin, 1.0, scene->sceneModel[i].model->name); } @@ -413,7 +414,7 @@ namespace Components { for (int j = 0; j < scene->sceneDObj[i].obj->numModels; j++) { - if (Utils::Vec3SqrDistance(playerPosition, &scene->sceneDObj[i].placement.origin) < sqrDist) + if (Utils::Vec3SqrDistance(playerPosition, scene->sceneDObj[i].placement.origin) < sqrDist) { Game::R_AddDebugString(dobjsColor, scene->sceneDObj[i].placement.origin, 1.0, scene->sceneDObj[i].obj->models[j]->name); } @@ -429,7 +430,7 @@ namespace Components auto staticModel = world->dpvs.smodelDrawInsts[i]; if (staticModel.model) { - auto dist = Utils::Vec3SqrDistance(playerPosition, &staticModel.placement.origin); + auto dist = Utils::Vec3SqrDistance(playerPosition, staticModel.placement.origin); if (dist < sqrDist) { Game::R_AddDebugString(staticModelsColor, staticModel.placement.origin, 1.0, staticModel.model->name); diff --git a/src/Utils/Utils.cpp b/src/Utils/Utils.cpp index 5feb2980..71d0af14 100644 --- a/src/Utils/Utils.cpp +++ b/src/Utils/Utils.cpp @@ -150,11 +150,11 @@ namespace Utils return !(base1 + len1 <= base2 || base2 + len2 <= base1); } - float Vec3SqrDistance(float(*v1)[3], float(*v2)[3]) + float Vec3SqrDistance(const float v1[3], const float v2[3]) { - auto x = (*v2)[0] - (*v1)[0]; - auto y = (*v2)[1] - (*v1)[1]; - auto z = (*v2)[2] - (*v1)[2]; + auto x = v2[0] - v1[0]; + auto y = v2[1] - v1[1]; + auto z = v2[2] - v1[2]; return x * x + y * y + z * z; } diff --git a/src/Utils/Utils.hpp b/src/Utils/Utils.hpp index 257ec5bf..c33bbda3 100644 --- a/src/Utils/Utils.hpp +++ b/src/Utils/Utils.hpp @@ -24,7 +24,7 @@ namespace Utils void OpenUrl(const std::string& url); bool HasIntercection(unsigned int base1, unsigned int len1, unsigned int base2, unsigned int len2); - float Vec3SqrDistance(float (*v1)[3], float (*v2)[3]); + float Vec3SqrDistance(const float v1[3], const float v2[3]); template inline void RotLeft(T& object, size_t bits) { From 6ec5cac5cbd5eb355aaf76760e4dc7e36272f65d Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Mon, 13 Dec 2021 18:50:52 +0100 Subject: [PATCH 10/12] Fix const cast on enum dvar registration --- src/Components/Modules/Renderer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index 321ab9fa..f7104751 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -538,7 +538,7 @@ namespace Components Dvar::OnInit([] { - static char* values[5] = + static const char* values[5] = { "Disabled", "Scene Models", From c0f95d3fc3be3a8cfeccbdc350d222627ff5d72d Mon Sep 17 00:00:00 2001 From: Louvenarde Date: Mon, 13 Dec 2021 19:06:04 +0100 Subject: [PATCH 11/12] Address review --- src/Components/Modules/Renderer.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index f7104751..3d4b8f86 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -279,7 +279,7 @@ namespace Components if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || - Game::g_entities[clientNum].client != nullptr) { + Game::g_entities[clientNum].client == nullptr) { return; } @@ -288,7 +288,7 @@ namespace Components float playerPosition[3]{ clientEntity->r.currentOrigin[0], clientEntity->r.currentOrigin[1], clientEntity->r.currentOrigin[2] }; - auto mapName = Dvar::Var("mapname").get(); + const auto mapName = Dvar::Var("mapname").get(); auto scene = Game::scene; auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; @@ -377,7 +377,7 @@ namespace Components if (!Game::CL_IsCgameInitialized() || clientNum >= 18 || clientNum < 0 || - Game::g_entities[clientNum].client != nullptr) { + Game::g_entities[clientNum].client == nullptr) { return; } @@ -386,7 +386,7 @@ namespace Components float playerPosition[3]{ clientEntity->r.currentOrigin[0], clientEntity->r.currentOrigin[1], clientEntity->r.currentOrigin[2] }; - auto mapName = Dvar::Var("mapname").get(); + const auto mapName = Dvar::Var("mapname").get(); auto scene = Game::scene; auto world = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_GFXWORLD, Utils::String::VA("maps/mp/%s.d3dbsp", mapName))->asset.header.gfxWorld; @@ -518,10 +518,10 @@ namespace Components // Begin device recovery (not D3D9Ex) Utils::Hook(0x508298, []() - { - Game::DB_BeginRecoverLostDevice(); - Renderer::BeginRecoverDeviceSignal(); - }, HOOK_CALL).install()->quick(); + { + Game::DB_BeginRecoverLostDevice(); + Renderer::BeginRecoverDeviceSignal(); + }, HOOK_CALL).install()->quick(); // End device recovery (not D3D9Ex) Utils::Hook(0x508355, []() @@ -538,7 +538,7 @@ namespace Components Dvar::OnInit([] { - static const char* values[5] = + static const char* values[] = { "Disabled", "Scene Models", From a6f0d78481cc7e8f140898833d97be1b5c43ce0b Mon Sep 17 00:00:00 2001 From: "rack.over" Date: Fri, 17 Dec 2021 09:55:50 +0100 Subject: [PATCH 12/12] address review --- src/Components/Modules/Renderer.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index 3d4b8f86..07cc2a65 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -446,7 +446,6 @@ namespace Components if (!r_drawAABBTrees.get()) return; Game::clipMap_t* clipMap = *reinterpret_cast(0x7998E0); - //Game::GfxWorld* gameWorld = *reinterpret_cast(0x66DEE94); if (!clipMap) return; for (unsigned short i = 0; i < clipMap->smodelNodeCount; ++i) @@ -548,11 +547,11 @@ namespace Components }; Renderer::r_drawModelBoundingBoxes = Game::Dvar_RegisterEnum("r_drawModelBoundingBoxes", values, 0, Game::DVAR_FLAG_CHEAT, "Draw scene model bounding boxes"); - Renderer::r_drawSceneModelCollisions = Game::Dvar_RegisterEnum("r_drawSceneModelCollisions", values, 0, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); + Renderer::r_drawSceneModelCollisions = Game::Dvar_RegisterBool("r_drawSceneModelCollisions", false, Game::DVAR_FLAG_CHEAT, "Draw scene model collisions"); Renderer::r_drawTriggers = Game::Dvar_RegisterBool("r_drawTriggers", false, Game::DVAR_FLAG_CHEAT, "Draw triggers"); Renderer::r_drawModelNames = Game::Dvar_RegisterEnum("r_drawModelNames", values, 0, Game::DVAR_FLAG_CHEAT, "Draw all model names"); Renderer::r_drawAABBTrees = Game::Dvar_RegisterBool("r_drawAabbTrees", false, Game::DVAR_FLAG_CHEAT, "Draw aabb trees"); - Renderer::r_playerDrawDebugDistance = Game::Dvar_RegisterInt("r_drawDebugProximity", 1000, 0, 50000, Game::DVAR_FLAG_SAVED, "r_draw debug functions draw distance, relative to the player"); + Renderer::r_playerDrawDebugDistance = Game::Dvar_RegisterInt("r_drawDebugDistance", 1000, 0, 50000, Game::DVAR_FLAG_SAVED, "r_draw debug functions draw distance, relative to the player"); }); }