Merge pull request #316 from diamante0018/maths

[General] Quick Maths
This commit is contained in:
Edo 2022-06-27 18:59:48 +02:00 committed by GitHub
commit 2daaee8358
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 94 additions and 59 deletions

View File

@ -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);

View File

@ -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<XAssetHeader*>(0x7998A8);
unsigned int* g_poolSize = reinterpret_cast<unsigned int*>(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

View File

@ -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);

View File

@ -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"

46
src/Utils/Maths.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <STDInclude.hpp>
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]);
}
}

15
src/Utils/Maths.hpp Normal file
View File

@ -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]);
}

View File

@ -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;
}
}

View File

@ -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 <typename T> inline void RotLeft(T& object, size_t bits)
template <typename T> 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 <typename T> inline void RotRight(T& object, size_t bits)
template <typename T> void RotRight(T& object, size_t bits)
{
bits %= (sizeof(T) * 8);
RotLeft<T>(object, ((sizeof(T) * 8) - bits));
}
template <typename T> inline void Merge(std::vector<T>* target, T* source, size_t length)
template <typename T> void Merge(std::vector<T>* target, T* source, size_t length)
{
if (source)
{
@ -56,7 +55,7 @@ namespace Utils
}
}
template <typename T> inline void Merge(std::vector<T>* target, std::vector<T> source)
template <typename T> void Merge(std::vector<T>* target, std::vector<T> source)
{
for (auto &entry : source)
{
@ -122,7 +121,4 @@ namespace Utils
mutable std::recursive_mutex mutex;
std::vector<Slot<T>> slots;
};
template <typename T>
constexpr auto VectorCopy(T a, T b) { b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; }
}