diff --git a/src/m_fixed.h b/src/m_fixed.h index 4e4c79c65..9fe35c91d 100644 --- a/src/m_fixed.h +++ b/src/m_fixed.h @@ -260,6 +260,41 @@ typedef struct fixed_t y; } vector2_t; +// Source: "Know Your FPU: Fixing Floating Fast" +// http://www.stereopsis.com/sree/fpu2006.html +// +// xs_CRoundToInt: Round toward nearest, but ties round toward even (just like FISTP) +// taken from prboom+ for OpenGL big room fix +#ifndef _xs_DEFAULT_CONVERSION +#define _xs_DEFAULT_CONVERSION 0 +#endif //_xs_DEFAULT_CONVERSION + +#ifdef WORDS_BIGENDIAN + #define _xs_iexp_ 0 + #define _xs_iman_ 1 +#else + #define _xs_iexp_ 1 //intel is little endian + #define _xs_iman_ 0 +#endif //BigEndian_ + +typedef double real64; +typedef union _xs_doubleints +{ + real64 val; + unsigned ival[2]; +} _xs_doubleints; + +FUNCMATH FUNCINLINE ATTRINLINE int xs_CRoundToInt(real64 val) +{ +#if _xs_DEFAULT_CONVERSION==0 + _xs_doubleints uval; + uval.val = val + 6755399441055744.0; + return uval.ival[_xs_iman_]; +#else + return int(floor(val+.5)); +#endif +} + vector2_t *FV2_Load(vector2_t *vec, fixed_t x, fixed_t y); vector2_t *FV2_UnLoad(vector2_t *vec, fixed_t *x, fixed_t *y); vector2_t *FV2_Copy(vector2_t *a_o, const vector2_t *a_i); diff --git a/src/r_main.c b/src/r_main.c index 8a975498d..f2609fb5c 100644 --- a/src/r_main.c +++ b/src/r_main.c @@ -457,6 +457,40 @@ angle_t R_PointToAngle2(fixed_t pviewx, fixed_t pviewy, fixed_t x, fixed_t y) 0; } +// +// R_PointToPseudoAngle +// ! Returns the pseudoangle between the line p1 to (infinity, p1.y) and the +// line from p1 to p2. The pseudoangle has the property that the ordering of +// points by true angle anround p1 and ordering of points by pseudoangle are the +// same. +// +// For clipping exact angles are not needed. Only the ordering matters. +// This is about as fast as the fixed point R_PointToAngle2 but without +// the precision issues associated with that function. +// + +angle_t R_PointToPseudoAngle (fixed_t x, fixed_t y) +{ + // Note: float won't work here as it's less precise than the BAM values being passed as parameters + double vecx = (double)x - viewx; + double vecy = (double)y - viewy; + double epsilon = 1e-9; + + if (fabs(vecx) < epsilon && fabs(vecy) < epsilon) + { + return 0; + } + else + { + double result = vecy / (fabs(vecx) + fabs(vecy)); + if (vecx < 0) + { + result = 2.0 - result; + } + return (angle_t)xs_CRoundToInt(result * (1 << 30)); + } +} + fixed_t R_PointToDist2(fixed_t px2, fixed_t py2, fixed_t px1, fixed_t py1) { return FixedHypot(px1 - px2, py1 - py2); diff --git a/src/r_main.h b/src/r_main.h index 63c7c3ab5..85bb9e827 100644 --- a/src/r_main.h +++ b/src/r_main.h @@ -71,6 +71,7 @@ angle_t R_PointToAngle(fixed_t x, fixed_t y); angle_t R_PointToAngle64(INT64 x, INT64 y); angle_t R_PointToAngleEx(INT32 x2, INT32 y2, INT32 x1, INT32 y1); angle_t R_PointToAngle2(fixed_t px2, fixed_t py2, fixed_t px1, fixed_t py1); +angle_t R_PointToPseudoAngle (fixed_t x, fixed_t y); fixed_t R_PointToDist(fixed_t x, fixed_t y); fixed_t R_PointToDist2(fixed_t px2, fixed_t py2, fixed_t px1, fixed_t py1); angle_t R_PlayerSliptideAngle(player_t *player);