diff --git a/src/Components/Modules/Renderer.cpp b/src/Components/Modules/Renderer.cpp index 28feacf3..dd18f02a 100644 --- a/src/Components/Modules/Renderer.cpp +++ b/src/Components/Modules/Renderer.cpp @@ -284,7 +284,7 @@ namespace Components if (!scene->sceneModel[i].model) continue; - if (Utils::Vec3SqrDistance(playerPosition, scene->sceneModel[i].placement.base.origin) < sqrDist) + if (Utils::Maths::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]; @@ -302,7 +302,7 @@ namespace Components for (auto i = 0; i < scene->sceneDObjCount; i++) { - if (Utils::Vec3SqrDistance(playerPosition, scene->sceneDObj[i].cull.bounds.midPoint) < sqrDist) + if (Utils::Maths::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]); @@ -327,7 +327,7 @@ namespace Components { auto staticModel = world->dpvs.smodelDrawInsts[i]; - if (Utils::Vec3SqrDistance(playerPosition, staticModel.placement.origin) < sqrDist) + if (Utils::Maths::Vec3SqrDistance(playerPosition, staticModel.placement.origin) < sqrDist) { if (staticModel.model) { @@ -381,7 +381,7 @@ namespace Components if (!scene->sceneModel[i].model) continue; - if (Utils::Vec3SqrDistance(playerPosition, scene->sceneModel[i].placement.base.origin) < sqrDist) + if (Utils::Maths::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); } @@ -395,7 +395,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::Maths::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); } @@ -411,7 +411,7 @@ namespace Components auto staticModel = world->dpvs.smodelDrawInsts[i]; if (staticModel.model) { - auto dist = Utils::Vec3SqrDistance(playerPosition, staticModel.placement.origin); + const auto dist = Utils::Maths::Vec3SqrDistance(playerPosition, staticModel.placement.origin); if (dist < sqrDist) { Game::R_AddDebugString(staticModelsColor, staticModel.placement.origin, 1.0, staticModel.model->name); diff --git a/src/Game/Functions.cpp b/src/Game/Functions.cpp index f9b17b3e..33917332 100644 --- a/src/Game/Functions.cpp +++ b/src/Game/Functions.cpp @@ -453,6 +453,11 @@ namespace Game player_die_t player_die = player_die_t(0x42BC70); + Vec3Normalize_t Vec3Normalize = Vec3Normalize_t(0x453500); + Vec3NormalizeFast_t Vec3NormalizeFast = Vec3NormalizeFast_t(0x478F80); + Vec2Normalize_t Vec2Normalize = Vec2Normalize_t(0x416F70); + Vec2NormalizeFast_t Vec2NormalizeFast = Vec2NormalizeFast_t(0x5FC830); + XAssetHeader* DB_XAssetPool = reinterpret_cast(0x7998A8); unsigned int* g_poolSize = reinterpret_cast(0x7995E8); @@ -920,33 +925,6 @@ namespace Game return atoi(StringTable_Lookup(rankTable, 0, maxrank, 7)); } - float Vec2Normalize(vec2_t& vec) - { - const auto length = std::sqrt(vec[0] * vec[0] + vec[1] * vec[1]); - - if (length > 0.0f) - { - vec[0] /= length; - vec[1] /= length; - } - - return length; - } - - float Vec3Normalize(vec3_t& vec) - { - const auto length = std::sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]); - - if (length > 0.0f) - { - vec[0] /= length; - vec[1] /= length; - vec[2] /= length; - } - - return length; - } - void Vec2UnpackTexCoords(const PackedTexCoords in, vec2_t* out) { unsigned int v3; // xmm1_4 @@ -1684,11 +1662,11 @@ namespace Game pushad mov eax, [esp + 0x4 + 0x20] // dvar - push[esp + 0x18 + 0x20] // source - push[esp + 0x18 + 0x20] // value - push[esp + 0x18 + 0x20] // value - push[esp + 0x18 + 0x20] // value - push[esp + 0x18 + 0x20] // value + push [esp + 0x18 + 0x20] // source + push [esp + 0x18 + 0x20] // value + push [esp + 0x18 + 0x20] // value + push [esp + 0x18 + 0x20] // value + push [esp + 0x18 + 0x20] // value mov ebx, 0x647400 call ebx diff --git a/src/Game/Functions.hpp b/src/Game/Functions.hpp index 49c4eae9..f043bc0d 100644 --- a/src/Game/Functions.hpp +++ b/src/Game/Functions.hpp @@ -1086,6 +1086,18 @@ namespace Game typedef void(__cdecl * player_die_t)(gentity_s* self, const gentity_s* inflictor, gentity_s* attacker, int damage, int meansOfDeath, int iWeapon, const float* vDir, const hitLocation_t hitLoc, int psTimeOffset); extern player_die_t player_die; + typedef float(__cdecl * Vec3Normalize_t)(float* v); + extern Vec3Normalize_t Vec3Normalize; + + typedef void(__cdecl * Vec3NormalizeFast_t)(float* v); + extern Vec3NormalizeFast_t Vec3NormalizeFast; + + typedef float(__cdecl * Vec2Normalize_t)(float* v); + extern Vec2Normalize_t Vec2Normalize; + + typedef void(__cdecl * Vec2NormalizeFast_t)(float* v); + extern Vec2NormalizeFast_t Vec2NormalizeFast; + extern XAssetHeader* DB_XAssetPool; extern unsigned int* g_poolSize; @@ -1301,8 +1313,6 @@ namespace Game void Image_Setup(GfxImage* image, unsigned int width, unsigned int height, unsigned int depth, unsigned int flags, _D3DFORMAT format); - float Vec2Normalize(vec2_t& vec); - float Vec3Normalize(vec3_t& vec); void Vec2UnpackTexCoords(const PackedTexCoords in, vec2_t* out); void MatrixVecMultiply(const float(&mulMat)[3][3], const vec3_t& mulVec, vec3_t& solution); void QuatRot(vec3_t* vec, const vec4_t* quat); diff --git a/src/STDInclude.hpp b/src/STDInclude.hpp index 63e5d202..950c5248 100644 --- a/src/STDInclude.hpp +++ b/src/STDInclude.hpp @@ -128,6 +128,7 @@ using namespace std::literals; #include "Utils/IO.hpp" #include "Utils/Json.hpp" #include "Utils/Library.hpp" +#include "Utils/Maths.hpp" #include "Utils/String.hpp" #include "Utils/Thread.hpp" #include "Utils/Time.hpp" diff --git a/src/Utils/Maths.cpp b/src/Utils/Maths.cpp new file mode 100644 index 00000000..2b840002 --- /dev/null +++ b/src/Utils/Maths.cpp @@ -0,0 +1,46 @@ +#include + +namespace Utils::Maths +{ + float DotProduct(float v1[3], float v2[3]) + { + return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]; + } + + void VectorSubtract(float va[3], float vb[3], float out[3]) + { + out[0] = va[0] - vb[0]; + out[1] = va[1] - vb[1]; + out[2] = va[2] - vb[2]; + } + + void VectorAdd(float va[3], float vb[3], float out[3]) + { + out[0] = va[0] + vb[0]; + out[1] = va[1] + vb[1]; + out[2] = va[2] + vb[2]; + } + + void VectorCopy(float in[3], float out[3]) + { + out[0] = in[0]; + out[1] = in[1]; + out[2] = in[2]; + } + + void VectorScale(float v[3], float scale, float out[3]) + { + out[0] = v[0] * scale; + out[1] = v[1] * scale; + out[2] = v[2] * scale; + } + + float Vec3SqrDistance(float v1[3], float v2[3]) + { + float out[3]; + + VectorSubtract(v2, v1, out); + + return (out[0] * out[0]) + (out[1] * out[1]) + (out[2] * out[2]); + } +} diff --git a/src/Utils/Maths.hpp b/src/Utils/Maths.hpp new file mode 100644 index 00000000..47f37ed5 --- /dev/null +++ b/src/Utils/Maths.hpp @@ -0,0 +1,15 @@ +#pragma once + +namespace Utils::Maths +{ + // Macros in Quake-III + constexpr auto VectorClear(float x[3]) { x[0] = x[1] = x[2] = 0; } + constexpr auto VectorNegate(float x[3]) { x[0] = -x[0]; x[1] = -x[1]; x[2] = -x[2]; } + + float DotProduct(float v1[3], float v2[3]); + void VectorSubtract(float va[3], float vb[3], float out[3]); + void VectorAdd(float va[3], float vb[3], float out[3]); + void VectorCopy(float in[3], float out[3]); + void VectorScale(float v[3], float scale, float out[3]); + float Vec3SqrDistance(float v1[3], float v2[3]); +} diff --git a/src/Utils/Utils.cpp b/src/Utils/Utils.cpp index 5166b84f..a214c963 100644 --- a/src/Utils/Utils.cpp +++ b/src/Utils/Utils.cpp @@ -145,15 +145,4 @@ namespace Utils { return !(base1 + len1 <= base2 || base2 + len2 <= base1); } - - 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]; - - return x * x + y * y + z * z; - } - - } diff --git a/src/Utils/Utils.hpp b/src/Utils/Utils.hpp index cca25872..79c6b706 100644 --- a/src/Utils/Utils.hpp +++ b/src/Utils/Utils.hpp @@ -24,9 +24,8 @@ namespace Utils void OpenUrl(const std::string& url); bool HasIntercection(unsigned int base1, unsigned int len1, unsigned int base2, unsigned int len2); - float Vec3SqrDistance(const float v1[3], const float v2[3]); - template inline void RotLeft(T& object, size_t bits) + template void RotLeft(T& object, size_t bits) { bits %= sizeof(T) * 8; @@ -39,13 +38,13 @@ namespace Utils object |= T(negative) << ((sizeof(T) * 8 - 1 + bits) % (sizeof(T) * 8)); } - template inline void RotRight(T& object, size_t bits) + template void RotRight(T& object, size_t bits) { bits %= (sizeof(T) * 8); RotLeft(object, ((sizeof(T) * 8) - bits)); } - template inline void Merge(std::vector* target, T* source, size_t length) + template void Merge(std::vector* target, T* source, size_t length) { if (source) { @@ -56,7 +55,7 @@ namespace Utils } } - template inline void Merge(std::vector* target, std::vector source) + template void Merge(std::vector* target, std::vector source) { for (auto &entry : source) { @@ -122,7 +121,4 @@ namespace Utils mutable std::recursive_mutex mutex; std::vector> slots; }; - - template - constexpr auto VectorCopy(T a, T b) { b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; } }