port R_PointToPseudoAngle for Opengl sector clipping from PrBoom+ #98

Closed
alufolie91 wants to merge 8 commits from pseudoangle into Saturn-Next
pull from: pseudoangle
5 changed files with 102 additions and 31 deletions

No files matched your search

+2 -2
View File
@@ -344,9 +344,9 @@ angle_t gld_FrustumAngle(angle_t tiltangle)
// ok, this is a gross hack that barely works...
// but at least it doesn't overestimate too much...
floatangle = 2.0f + (45.0f + (tilt / 1.9f)) * (float)render_fov * 48.0f / render_multiplier / 90.0f;
a1 = ANG1 * (int)floatangle;
if (a1 >= ANGLE_180)
if (floatangle >= ANGLE_180)
return 0xffffffff;
a1 = (angle_t)xs_CRoundToInt(ANG1 * floatangle);
return a1;
}
+6 -6
View File
@@ -2859,8 +2859,8 @@ void HWR_AddLine(seg_t *line)
}
// OPTIMIZE: quickly reject orthogonal back sides.
angle1 = R_PointToAngle64(v1x, v1y);
angle2 = R_PointToAngle64(v2x, v2y);
angle1 = R_PointToPseudoAngle(v1x, v1y);
angle2 = R_PointToPseudoAngle(v2x, v2y);
// do an extra culling check when rendering portals
// check if any line vertex is on the viewable side of the portal target line
@@ -3053,8 +3053,8 @@ boolean HWR_CheckBBox(fixed_t *bspcoord)
if (mindist > current_bsp_culling_distance) return false;
}
angle1 = R_PointToAngle64(px1, py1);
angle2 = R_PointToAngle64(px2, py2);
angle1 = R_PointToPseudoAngle(px1, py1);
angle2 = R_PointToPseudoAngle(px2, py2);
return gld_clipper_SafeCheckRange(angle2, angle1);
}
@@ -5913,8 +5913,8 @@ static void HWR_PortalClipping(gl_portal_t *portal)
line_t *line = &lines[portal->clipline];
angle1 = R_PointToAngleEx(viewx, viewy, line->v1->x, line->v1->y);
angle2 = R_PointToAngleEx(viewx, viewy, line->v2->x, line->v2->y);
angle1 = R_PointToPseudoAngle(line->v1->x, line->v1->y);
angle2 = R_PointToPseudoAngle(line->v2->x, line->v2->y);
// clip things that are not inside the portal window from our viewpoint
gld_clipper_SafeAddClipRange(angle2, angle1);
+36
View File
@@ -265,6 +265,42 @@ 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 faster OpenGL culling / R_PointToPseudoAngle
#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);
+57 -23
View File
@@ -428,6 +428,63 @@ angle_t R_PointToAngle2(fixed_t pviewx, fixed_t pviewy, fixed_t x, fixed_t y)
0;
}
angle_t R_PointToAngleEx(INT32 x2, INT32 y2, INT32 x1, INT32 y1)
{
INT64 dx = x1-x2;
INT64 dy = y1-y2;
if (dx < INT32_MIN || dx > INT32_MAX || dy < INT32_MIN || dy > INT32_MAX)
{
x1 = (int)(dx / 2 + x2);
y1 = (int)(dy / 2 + y2);
}
return (y1 -= y2, (x1 -= x2) || y1) ?
x1 >= 0 ?
y1 >= 0 ?
(x1 > y1) ? tantoangle[SlopeDivEx(y1,x1)] : // octant 0
ANGLE_90-tantoangle[SlopeDivEx(x1,y1)] : // octant 1
x1 > (y1 = -y1) ? 0-tantoangle[SlopeDivEx(y1,x1)] : // octant 8
ANGLE_270+tantoangle[SlopeDivEx(x1,y1)] : // octant 7
y1 >= 0 ? (x1 = -x1) > y1 ? ANGLE_180-tantoangle[SlopeDivEx(y1,x1)] : // octant 3
ANGLE_90 + tantoangle[SlopeDivEx(x1,y1)] : // octant 2
(x1 = -x1) > (y1 = -y1) ? ANGLE_180+tantoangle[SlopeDivEx(y1,x1)] : // octant 4
ANGLE_270-tantoangle[SlopeDivEx(x1,y1)] : // octant 5
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)
alufolie91 commented 2024-02-12 07:35:55 +01:00 (Migrated from github.com)
Review

well im just a little unsure, it used to check if both vec are zero, which the compiler didnt like, but comparing if its smaller than 1e-9 epsilon should be fine?

well im just a little unsure, it used to check if both vec are zero, which the compiler didnt like, but comparing if its smaller than 1e-9 epsilon should be fine?
Indev450 commented 2024-02-12 11:15:45 +01:00 (Migrated from github.com)
Review

Compiler complains because even though double is pretty precise, it is still not perfect so two numbers that are logically same might give you result that is very close to zero but not equals zero, so "double equals zero" will be false almost always. Tho here you can just change type from double to INT64 and check if it equals 0 (it will be faster and still correct since both y and viewy are fixed_t, which is integer)

Didn't pay attention to this part at first, my bad

Ok i need to always look at full code my bad again ><

Compiler complains because even though double is pretty precise, it is still not perfect so two numbers that are logically same might give you result that is very close to zero but not equals zero, so "double equals zero" will be false almost always. ~~Tho here you can just change type from double to INT64 and check if it equals 0 (it will be faster and still correct since both y and viewy are fixed_t, which is integer)~~ ~~Didn't pay attention to this part at first, my bad~~ Ok i need to always look at full code my bad again ><
alufolie91 commented 2024-02-12 14:21:24 +01:00 (Migrated from github.com)
Review

oki changed it to INT64
1186294931891953768

oki changed it to INT64 ![1186294931891953768](https://github.com/Indev450/SRB2Kart-Saturn/assets/136046486/9e31f7b9-bd04-42d8-864c-3b0406c77a9a)
alufolie91 commented 2024-02-12 14:34:53 +01:00 (Migrated from github.com)
Review

lmao nvm it broke everything

lmao nvm it broke everything
{
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);
@@ -483,29 +540,6 @@ fixed_t R_PointToDist(fixed_t x, fixed_t y)
return R_PointToDist2(viewx, viewy, x, y);
}
angle_t R_PointToAngleEx(INT32 x2, INT32 y2, INT32 x1, INT32 y1)
{
INT64 dx = x1-x2;
INT64 dy = y1-y2;
if (dx < INT32_MIN || dx > INT32_MAX || dy < INT32_MIN || dy > INT32_MAX)
{
x1 = (int)(dx / 2 + x2);
y1 = (int)(dy / 2 + y2);
}
return (y1 -= y2, (x1 -= x2) || y1) ?
x1 >= 0 ?
y1 >= 0 ?
(x1 > y1) ? tantoangle[SlopeDivEx(y1,x1)] : // octant 0
ANGLE_90-tantoangle[SlopeDivEx(x1,y1)] : // octant 1
x1 > (y1 = -y1) ? 0-tantoangle[SlopeDivEx(y1,x1)] : // octant 8
ANGLE_270+tantoangle[SlopeDivEx(x1,y1)] : // octant 7
y1 >= 0 ? (x1 = -x1) > y1 ? ANGLE_180-tantoangle[SlopeDivEx(y1,x1)] : // octant 3
ANGLE_90 + tantoangle[SlopeDivEx(x1,y1)] : // octant 2
(x1 = -x1) > (y1 = -y1) ? ANGLE_180+tantoangle[SlopeDivEx(y1,x1)] : // octant 4
ANGLE_270-tantoangle[SlopeDivEx(x1,y1)] : // octant 5
0;
}
//
// R_ScaleFromGlobalAngle
// Returns the texture mapping scale for the current line (horizontal span)
+1
View File
@@ -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_PointToAngle2(fixed_t px2, fixed_t py2, fixed_t px1, fixed_t py1);
angle_t R_PointToAngleEx(INT32 x2, INT32 y2, INT32 x1, INT32 y1);
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);