From 87ed67fd3dd0da2b5e25a148b512faddb221fc40 Mon Sep 17 00:00:00 2001 From: Yamagi Burmeister Date: Fri, 16 Oct 2015 12:29:22 +0200 Subject: [PATCH] Revert "Cleanup vector math" This reverts commit 123e409a2e5a223336ab4ad61dd582fef7074d0b. This commit breaks several float calculations in subtiles ways. For example grenates drift to the left. In fact, it's another example why I'm so hesitant to merge anything that's not a fix for a clearly reproducable bug. This fixes #99. --- CHANGELOG | 3 - src/common/header/shared.h | 26 ++-- src/common/shared/shared.c | 149 ++++++++++++++++++++++- src/game/g_trigger.c | 2 +- src/game/savegame/tables/gamefunc_decs.h | 12 ++ src/game/savegame/tables/gamefunc_list.h | 12 ++ 6 files changed, 192 insertions(+), 12 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index ad47eaab..be524196 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,3 @@ -Quake II 5.31 to 5.32: -- Cleanup vector math. (by Dmitry Antipov) - Quake II 5.30 to 5.31: - Enabled hud scaling by default and added an option to the video menu to switch scaling off. diff --git a/src/common/header/shared.h b/src/common/header/shared.h index 58c8556c..df8dbd03 100644 --- a/src/common/header/shared.h +++ b/src/common/header/shared.h @@ -129,6 +129,11 @@ typedef enum typedef float vec_t; typedef vec_t vec3_t[3]; +typedef vec_t vec5_t[5]; + +typedef int fixed4_t; +typedef int fixed8_t; +typedef int fixed16_t; #ifndef M_PI #define M_PI 3.14159265358979323846 /* matches value in gcc v2 math.h */ @@ -155,21 +160,28 @@ extern vec3_t vec3_origin; #define VectorClear(a) (a[0] = a[1] = a[2] = 0) #define VectorNegate(a, b) (b[0] = -a[0], b[1] = -a[1], b[2] = -a[2]) #define VectorSet(v, x, y, z) (v[0] = (x), v[1] = (y), v[2] = (z)) -#define VectorLength(x) sqrtf(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]) -#define VectorScale(x, scale, y) (y[0] = scale * x[0], \ - y[1] = scale * x[1], \ - y[2] = scale * x[2]) -#define VectorMA(x, scale, y, z) (z[0] = x[0] + scale * y[0], \ - z[1] = x[1] + scale * y[1], \ - z[2] = x[2] + scale * y[2]) + +void VectorMA(vec3_t veca, float scale, vec3_t vecb, vec3_t vecc); + +/* just in case you do't want to use the macros */ +vec_t _DotProduct(vec3_t v1, vec3_t v2); +void _VectorSubtract(vec3_t veca, vec3_t vecb, vec3_t out); +void _VectorAdd(vec3_t veca, vec3_t vecb, vec3_t out); +void _VectorCopy(vec3_t in, vec3_t out); void ClearBounds(vec3_t mins, vec3_t maxs); void AddPointToBounds(vec3_t v, vec3_t mins, vec3_t maxs); int VectorCompare(vec3_t v1, vec3_t v2); +vec_t VectorLength(vec3_t v); void CrossProduct(vec3_t v1, vec3_t v2, vec3_t cross); vec_t VectorNormalize(vec3_t v); /* returns vector length */ +vec_t VectorNormalize2(vec3_t v, vec3_t out); +void VectorInverse(vec3_t v); +void VectorScale(vec3_t in, vec_t scale, vec3_t out); +int Q_log2(int val); void R_ConcatRotations(float in1[3][3], float in2[3][3], float out[3][3]); +void R_ConcatTransforms(float in1[3][4], float in2[3][4], float out[3][4]); void AngleVectors(vec3_t angles, vec3_t forward, vec3_t right, vec3_t up); void AngleVectors2(vec3_t value1, vec3_t angles); diff --git a/src/common/shared/shared.c b/src/common/shared/shared.c index f43199bd..f9f84794 100644 --- a/src/common/shared/shared.c +++ b/src/common/shared/shared.c @@ -258,8 +258,46 @@ R_ConcatRotations(float in1[3][3], float in2[3][3], float out[3][3]) in1[2][2] * in2[2][2]; } +void +R_ConcatTransforms(float in1[3][4], float in2[3][4], float out[3][4]) +{ + out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] + + in1[0][2] * in2[2][0]; + out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] + + in1[0][2] * in2[2][1]; + out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] + + in1[0][2] * in2[2][2]; + out[0][3] = in1[0][0] * in2[0][3] + in1[0][1] * in2[1][3] + + in1[0][2] * in2[2][3] + in1[0][3]; + out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] + + in1[1][2] * in2[2][0]; + out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] + + in1[1][2] * in2[2][1]; + out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] + + in1[1][2] * in2[2][2]; + out[1][3] = in1[1][0] * in2[0][3] + in1[1][1] * in2[1][3] + + in1[1][2] * in2[2][3] + in1[1][3]; + out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] + + in1[2][2] * in2[2][0]; + out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] + + in1[2][2] * in2[2][1]; + out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] + + in1[2][2] * in2[2][2]; + out[2][3] = in1[2][0] * in2[0][3] + in1[2][1] * in2[1][3] + + in1[2][2] * in2[2][3] + in1[2][3]; +} + /* ============================================================================ */ +float +Q_fabs(float f) +{ + int tmp = *(int *)&f; + + tmp &= 0x7FFFFFFF; + return *(float *)&tmp; +} + float LerpAngle(float a2, float a1, float frac) { @@ -464,7 +502,10 @@ VectorCompare(vec3_t v1, vec3_t v2) vec_t VectorNormalize(vec3_t v) { - float ilength, length = VectorLength(v); + float length, ilength; + + length = v[0] * v[0] + v[1] * v[1] + v[2] * v[2]; + length = (float)sqrt(length); if (length) { @@ -477,6 +518,63 @@ VectorNormalize(vec3_t v) return length; } +vec_t +VectorNormalize2(vec3_t v, vec3_t out) +{ + float length, ilength; + + length = v[0] * v[0] + v[1] * v[1] + v[2] * v[2]; + length = (float)sqrt(length); + + if (length) + { + ilength = 1 / length; + out[0] = v[0] * ilength; + out[1] = v[1] * ilength; + out[2] = v[2] * ilength; + } + + return length; +} + +void +VectorMA(vec3_t veca, float scale, vec3_t vecb, vec3_t vecc) +{ + vecc[0] = veca[0] + scale * vecb[0]; + vecc[1] = veca[1] + scale * vecb[1]; + vecc[2] = veca[2] + scale * vecb[2]; +} + +vec_t +_DotProduct(vec3_t v1, vec3_t v2) +{ + return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]; +} + +void +_VectorSubtract(vec3_t veca, vec3_t vecb, vec3_t out) +{ + out[0] = veca[0] - vecb[0]; + out[1] = veca[1] - vecb[1]; + out[2] = veca[2] - vecb[2]; +} + +void +_VectorAdd(vec3_t veca, vec3_t vecb, vec3_t out) +{ + out[0] = veca[0] + vecb[0]; + out[1] = veca[1] + vecb[1]; + out[2] = veca[2] + vecb[2]; +} + +void +_VectorCopy(vec3_t in, vec3_t out) +{ + out[0] = in[0]; + out[1] = in[1]; + out[2] = in[2]; +} + void CrossProduct(vec3_t v1, vec3_t v2, vec3_t cross) { @@ -485,6 +583,55 @@ CrossProduct(vec3_t v1, vec3_t v2, vec3_t cross) cross[2] = v1[0] * v2[1] - v1[1] * v2[0]; } +double sqrt(double x); + +vec_t +VectorLength(vec3_t v) +{ + int i; + float length; + + length = 0; + + for (i = 0; i < 3; i++) + { + length += v[i] * v[i]; + } + + length = (float)sqrt(length); + + return length; +} + +void +VectorInverse(vec3_t v) +{ + v[0] = -v[0]; + v[1] = -v[1]; + v[2] = -v[2]; +} + +void +VectorScale(vec3_t in, vec_t scale, vec3_t out) +{ + out[0] = in[0] * scale; + out[1] = in[1] * scale; + out[2] = in[2] * scale; +} + +int +Q_log2(int val) +{ + int answer = 0; + + while (val >>= 1) + { + answer++; + } + + return answer; +} + /* ==================================================================================== */ char * diff --git a/src/game/g_trigger.c b/src/game/g_trigger.c index 2f81322b..5cee9cee 100644 --- a/src/game/g_trigger.c +++ b/src/game/g_trigger.c @@ -149,7 +149,7 @@ Touch_Multi(edict_t *self, edict_t *other, cplane_t *plane /* unused */, AngleVectors(other->s.angles, forward, NULL, NULL); - if (DotProduct(forward, self->movedir) < 0) + if (_DotProduct(forward, self->movedir) < 0) { return; } diff --git a/src/game/savegame/tables/gamefunc_decs.h b/src/game/savegame/tables/gamefunc_decs.h index a68a654a..a30232f7 100644 --- a/src/game/savegame/tables/gamefunc_decs.h +++ b/src/game/savegame/tables/gamefunc_decs.h @@ -76,7 +76,17 @@ extern void COM_FileBase ( char * in , char * out ) ; extern const char * COM_FileExtension ( const char * in ) ; extern void COM_StripExtension ( char * in , char * out ) ; extern char * COM_SkipPath ( char * pathname ) ; +extern int Q_log2 ( int val ) ; +extern void VectorScale ( vec3_t in , vec_t scale , vec3_t out ) ; +extern void VectorInverse ( vec3_t v ) ; +extern vec_t VectorLength ( vec3_t v ) ; extern void CrossProduct ( vec3_t v1 , vec3_t v2 , vec3_t cross ) ; +extern void _VectorCopy ( vec3_t in , vec3_t out ) ; +extern void _VectorAdd ( vec3_t veca , vec3_t vecb , vec3_t out ) ; +extern void _VectorSubtract ( vec3_t veca , vec3_t vecb , vec3_t out ) ; +extern vec_t _DotProduct ( vec3_t v1 , vec3_t v2 ) ; +extern void VectorMA ( vec3_t veca , float scale , vec3_t vecb , vec3_t vecc ) ; +extern vec_t VectorNormalize2 ( vec3_t v , vec3_t out ) ; extern vec_t VectorNormalize ( vec3_t v ) ; extern int VectorCompare ( vec3_t v1 , vec3_t v2 ) ; extern void AddPointToBounds ( vec3_t v , vec3_t mins , vec3_t maxs ) ; @@ -84,6 +94,8 @@ extern void ClearBounds ( vec3_t mins , vec3_t maxs ) ; extern int BoxOnPlaneSide2 ( vec3_t emins , vec3_t emaxs , struct cplane_s * p ) ; extern float anglemod ( float a ) ; extern float LerpAngle ( float a2 , float a1 , float frac ) ; +extern float Q_fabs ( float f ) ; +extern void R_ConcatTransforms ( float in1 [ 3 ] [ 4 ] , float in2 [ 3 ] [ 4 ] , float out [ 3 ] [ 4 ] ) ; extern void R_ConcatRotations ( float in1 [ 3 ] [ 3 ] , float in2 [ 3 ] [ 3 ] , float out [ 3 ] [ 3 ] ) ; extern void PerpendicularVector ( vec3_t dst , const vec3_t src ) ; extern void ProjectPointOnPlane ( vec3_t dst , const vec3_t p , const vec3_t normal ) ; diff --git a/src/game/savegame/tables/gamefunc_list.h b/src/game/savegame/tables/gamefunc_list.h index 51ee3465..6a61b77c 100644 --- a/src/game/savegame/tables/gamefunc_list.h +++ b/src/game/savegame/tables/gamefunc_list.h @@ -76,7 +76,17 @@ {"COM_FileExtension", (byte *)COM_FileExtension}, {"COM_StripExtension", (byte *)COM_StripExtension}, {"COM_SkipPath", (byte *)COM_SkipPath}, +{"Q_log2", (byte *)Q_log2}, +{"VectorScale", (byte *)VectorScale}, +{"VectorInverse", (byte *)VectorInverse}, +{"VectorLength", (byte *)VectorLength}, {"CrossProduct", (byte *)CrossProduct}, +{"_VectorCopy", (byte *)_VectorCopy}, +{"_VectorAdd", (byte *)_VectorAdd}, +{"_VectorSubtract", (byte *)_VectorSubtract}, +{"_DotProduct", (byte *)_DotProduct}, +{"VectorMA", (byte *)VectorMA}, +{"VectorNormalize2", (byte *)VectorNormalize2}, {"VectorNormalize", (byte *)VectorNormalize}, {"VectorCompare", (byte *)VectorCompare}, {"AddPointToBounds", (byte *)AddPointToBounds}, @@ -84,6 +94,8 @@ {"BoxOnPlaneSide2", (byte *)BoxOnPlaneSide2}, {"anglemod", (byte *)anglemod}, {"LerpAngle", (byte *)LerpAngle}, +{"Q_fabs", (byte *)Q_fabs}, +{"R_ConcatTransforms", (byte *)R_ConcatTransforms}, {"R_ConcatRotations", (byte *)R_ConcatRotations}, {"PerpendicularVector", (byte *)PerpendicularVector}, {"ProjectPointOnPlane", (byte *)ProjectPointOnPlane},