From 77af507b7aa1dcdc0be33fc64fafeff23f69fdbf Mon Sep 17 00:00:00 2001 From: Alug Date: Thu, 27 Nov 2025 22:03:05 +0100 Subject: [PATCH] OpenGL: fixup spherefrustum calling and use it for sprites clips sprites that are out of our view, especially with precip this may lead to significant speedup in certain cases huge thanks to indev for helping with this <3 huge thanks to dsda-doom for the maths needed! --- src/hardware/hw_clip.c | 29 +++---- src/hardware/hw_clip.h | 2 +- src/hardware/hw_main.cpp | 129 ++++++++++++++++++++++++------- src/hardware/r_opengl/r_opengl.c | 2 +- src/hardware/r_opengl/r_opengl.h | 1 + 5 files changed, 115 insertions(+), 48 deletions(-) diff --git a/src/hardware/hw_clip.c b/src/hardware/hw_clip.c index 3365136b3..caa19f565 100644 --- a/src/hardware/hw_clip.c +++ b/src/hardware/hw_clip.c @@ -348,34 +348,29 @@ angle_t gld_FrustumAngle(angle_t tiltangle) // gld_FrustumSetup // -static GLdouble viewMatrix[16]; -static GLdouble projMatrix[16]; float frustum[6][4]; #define CALCMATRIX(a, b, c, d, e, f, g, h)\ -(float)(viewMatrix[a] * projMatrix[b] + \ -viewMatrix[c] * projMatrix[d] + \ -viewMatrix[e] * projMatrix[f] + \ -viewMatrix[g] * projMatrix[h]) + (modelMatrix[a] * projMatrix[b] + \ + modelMatrix[c] * projMatrix[d] + \ + modelMatrix[e] * projMatrix[f] + \ + modelMatrix[g] * projMatrix[h]) #define NORMALIZE_PLANE(i)\ -t = (float)sqrt(\ -frustum[i][0] * frustum[i][0] + \ -frustum[i][1] * frustum[i][1] + \ -frustum[i][2] * frustum[i][2]); \ -frustum[i][0] /= t; \ -frustum[i][1] /= t; \ -frustum[i][2] /= t; \ -frustum[i][3] /= t + t = sqrtf(\ + frustum[i][0] * frustum[i][0] + \ + frustum[i][1] * frustum[i][1] + \ + frustum[i][2] * frustum[i][2]); \ + frustum[i][0] /= t; \ + frustum[i][1] /= t; \ + frustum[i][2] /= t; \ + frustum[i][3] /= t void gld_FrustumSetup(void) { float t; float clip[16]; - pglGetFloatv(GL_PROJECTION_MATRIX, projMatrix); - pglGetFloatv(GL_MODELVIEW_MATRIX, viewMatrix); - clip[0] = CALCMATRIX(0, 0, 1, 4, 2, 8, 3, 12); clip[1] = CALCMATRIX(0, 1, 1, 5, 2, 9, 3, 13); clip[2] = CALCMATRIX(0, 2, 1, 6, 2, 10, 3, 14); diff --git a/src/hardware/hw_clip.h b/src/hardware/hw_clip.h index 4299fbaea..232d09c06 100644 --- a/src/hardware/hw_clip.h +++ b/src/hardware/hw_clip.h @@ -16,7 +16,7 @@ extern "C" { #include "../tables.h" #include "../doomtype.h" -//#define HAVE_SPHEREFRUSTRUM // enable if you want gld_SphereInFrustum and related code +#define HAVE_SPHEREFRUSTRUM // enable if you want gld_SphereInFrustum and related code boolean gld_clipper_SafeCheckRange(angle_t startAngle, angle_t endAngle); void gld_clipper_SafeAddClipRange(angle_t startangle, angle_t endangle); diff --git a/src/hardware/hw_main.cpp b/src/hardware/hw_main.cpp index 8dbda4d87..434ff5252 100644 --- a/src/hardware/hw_main.cpp +++ b/src/hardware/hw_main.cpp @@ -84,6 +84,13 @@ float gl_viewsin = 0.0f, gl_viewcos = 0.0f; static float gl_viewludsin, gl_viewludcos; static angle_t gl_aimingangle; +static float yaw = 0.0f; +static float inv_yaw = 0.0f; +static float cos_inv_yaw = 0.0f; +static float sin_inv_yaw = 0.0f; + +static constexpr float deg2radians = (M_PIf / 180.0f); + seg_t *gl_curline = NULL; side_t *gl_sidedef = NULL; line_t *gl_linedef = NULL; @@ -4659,6 +4666,7 @@ static void HWR_ProjectSprite(mobj_t *thing) float tr_x, tr_y; float tz; float x1, x2; + float y1, y2; float z1, z2; float rightsin, rightcos; float this_scale; @@ -4889,17 +4897,6 @@ static void HWR_ProjectSprite(mobj_t *thing) spr_offset += interp.spritexoffset; spr_topoffset += interp.spriteyoffset; - if (papersprite) - { - rightsin = FixedToFloat(FINESINE(interp.angle >> ANGLETOFINESHIFT)); - rightcos = FixedToFloat(FINECOSINE(interp.angle >> ANGLETOFINESHIFT)); - } - else - { - rightsin = FixedToFloat(FINESINE((viewangle + ANGLE_90)>>ANGLETOFINESHIFT)); - rightcos = FixedToFloat(FINECOSINE((viewangle + ANGLE_90)>>ANGLETOFINESHIFT)); - } - spritexscale *= this_scale; spriteyscale *= this_scale; @@ -4916,19 +4913,17 @@ static void HWR_ProjectSprite(mobj_t *thing) x2 = (FixedToFloat(spr_width - spr_offset) * spritexscale); } - z1 = tr_y + x1 * rightsin; - z2 = tr_y - x2 * rightsin; - x1 = tr_x + x1 * rightcos; - x2 = tr_x - x2 * rightcos; + y1 = (FixedToFloat(spr_topoffset) * spriteyscale); + y2 = (FixedToFloat(spr_height - spr_topoffset) * spriteyscale); if (vflip) { - gz = FixedToFloat(interp.z + thing->height) - (FixedToFloat(spr_topoffset) * spriteyscale); + gz = FixedToFloat(interp.z + thing->height) - y1; gzt = gz + (FixedToFloat(spr_height) * spriteyscale); } else { - gzt = FixedToFloat(interp.z) + (FixedToFloat(spr_topoffset) * spriteyscale); + gzt = FixedToFloat(interp.z) + y1; gz = gzt - (FixedToFloat(spr_height) * spriteyscale); } @@ -4938,6 +4933,32 @@ static void HWR_ProjectSprite(mobj_t *thing) return; } + // rest in piece sweet prince + /*if (cv_glshearing.value && !papersprite) + { + // killough 4/9/98: clip things which are out of view due to height + // e6y: fix of hanging decoration disappearing in Batman Doom MAP02 + // centeryfrac -> viewheightfrac + // [kb] add +1 so sprites are shown even with the extended freelook + // lug: attempt to account for freelook properly + const fixed_t fixedtz = FloatToFixed(tz); + if (interp.z > viewz + FixedMul(centeryfrac, fixedtz) || // view center accounted for aspect (i hope) + FloatToFixed(gzt) < viewz + FixedMul((centeryfrac - (viewheight << FRACBITS)), fixedtz)) // view center accounted for aspect but the bottom (OwO) + { + return; + } + }*/ + + if (!gld_SphereInFrustum( + (FixedToFloat(interp.x)) + cos_inv_yaw * (x1 + x2) / 2.0f, + FixedToFloat(interp.z) + (y1 + y2) / 2.0f, + FixedToFloat(interp.y) - sin_inv_yaw * (x1 + x2) / 2.0f, + //1.5 == sqrt(2) + small delta for MF_FOREGROUND + std::max(FixedToFloat(spr_width), FixedToFloat(spr_height)) / 2.0f * 1.5f)) + { + return; + } + heightsec = thing->subsector->sector->heightsec; if (viewplayer && viewplayer->mo && viewplayer->mo->subsector) phs = viewplayer->mo->subsector->sector->heightsec; @@ -4962,6 +4983,24 @@ static void HWR_ProjectSprite(mobj_t *thing) return; } + if (papersprite) + { + rightsin = FixedToFloat(FINESINE(interp.angle >> ANGLETOFINESHIFT)); + rightcos = FixedToFloat(FINECOSINE(interp.angle >> ANGLETOFINESHIFT)); + } + else + { + rightsin = FixedToFloat(FINESINE((viewangle + ANGLE_90)>>ANGLETOFINESHIFT)); + rightcos = FixedToFloat(FINECOSINE((viewangle + ANGLE_90)>>ANGLETOFINESHIFT)); + } + + z1 = tr_y + x1 * rightsin; + z2 = tr_y - x2 * rightsin; + x1 = tr_x + x1 * rightcos; + x2 = tr_x - x2 * rightcos; + //y1 = tr_y + y1 * rightcos; + //y2 = tr_y - y2 * rightcos; + // store information in a vissprite vis = HWR_NewVisSprite(); vis->x1 = x1; @@ -5037,12 +5076,16 @@ static void HWR_ProjectPrecipitationSprite(precipmobj_t *thing) float tr_x, tr_y; float tz; float x1, x2; + float y1, y2; float z1, z2; + float gz, gzt; float rightsin, rightcos; float this_scale; spritedef_t *sprdef; spriteframe_t *sprframe; size_t lumpoff; + fixed_t spr_width, spr_height; + fixed_t spr_offset, spr_topoffset; unsigned rot = 0; UINT8 flip; @@ -5103,22 +5146,46 @@ static void HWR_ProjectPrecipitationSprite(precipmobj_t *thing) lumpoff = sprframe->lumpid[0]; flip = sprframe->flip; // Will only be 0x00 or 0xFF - rightsin = FixedToFloat(FINESINE((viewangle + ANGLE_90)>>ANGLETOFINESHIFT)); - rightcos = FixedToFloat(FINECOSINE((viewangle + ANGLE_90)>>ANGLETOFINESHIFT)); + spr_width = spritecachedinfo[lumpoff].width; + spr_height = spritecachedinfo[lumpoff].height; + spr_offset = spritecachedinfo[lumpoff].offset; + spr_topoffset = spritecachedinfo[lumpoff].topoffset; if (flip) { - x1 = FixedToFloat(spritecachedinfo[lumpoff].width - spritecachedinfo[lumpoff].offset); - x2 = FixedToFloat(spritecachedinfo[lumpoff].offset); + x1 = FixedToFloat(spr_width - spr_offset); + x2 = FixedToFloat(spr_offset); } else { - x1 = FixedToFloat(spritecachedinfo[lumpoff].offset); - x2 = FixedToFloat(spritecachedinfo[lumpoff].width - spritecachedinfo[lumpoff].offset); + x1 = FixedToFloat(spr_offset); + x2 = FixedToFloat(spr_offset); } + y1 = (FixedToFloat(spr_topoffset)); + y2 = (FixedToFloat(spr_height - spr_topoffset)); + x1 *= this_scale; x2 *= this_scale; + y1 *= this_scale; + y2 *= this_scale; + + if (!gld_SphereInFrustum( + (FixedToFloat(interp.x)) + cos_inv_yaw * (x1 + x2) / 2.0f, + FixedToFloat(interp.z) + (y1 + y2) / 2.0f, + FixedToFloat(interp.y) - sin_inv_yaw * (x1 + x2) / 2.0f, + //1.5 == sqrt(2) + small delta for MF_FOREGROUND + std::max(FixedToFloat(spr_width), FixedToFloat(spr_height)) / 2.0f * 1.5f)) + { + return; + } + + // set top/bottom coords + gzt = FixedToFloat(interp.z) + y1; + gz = gzt - (FixedToFloat(spr_height) * this_scale); + + rightsin = FixedToFloat(FINESINE((viewangle + ANGLE_90)>>ANGLETOFINESHIFT)); + rightcos = FixedToFloat(FINECOSINE((viewangle + ANGLE_90)>>ANGLETOFINESHIFT)); z1 = tr_y + x1 * rightsin; z2 = tr_y - x2 * rightsin; @@ -5147,8 +5214,8 @@ static void HWR_ProjectPrecipitationSprite(precipmobj_t *thing) #endif // set top/bottom coords - vis->gzt = FixedToFloat(interp.z) + (FixedToFloat(spritecachedinfo[lumpoff].topoffset) * this_scale); - vis->gz = vis->gzt - (FixedToFloat(spritecachedinfo[lumpoff].height) * this_scale); + vis->gzt = gzt; + vis->gz = gz; vis->precip = thing; } @@ -5162,11 +5229,10 @@ static gl_sky_t gl_sky; static void HWR_SkyDomeVertex(gl_sky_t *sky, gl_skyvertex_t *vbo, int r, int c, signed char yflip, float delta, boolean foglayer) { - static constexpr float radians = (M_PIf / 180.0f); static constexpr float scale = 10000.0f; - static constexpr float maxSideAngle = 60.0f * radians; + static constexpr float maxSideAngle = 60.0f * deg2radians; - float topAngle = ((float)c / (float)sky->columns * 360.0f) * radians; + float topAngle = ((float)c / (float)sky->columns * 360.0f) * deg2radians; float sideAngle = (maxSideAngle * (float)(sky->rows - r) / (float)sky->rows); float height = sinf(sideAngle); float realRadius = (scale * cosf(sideAngle)); @@ -5405,6 +5471,12 @@ void HWR_SetTransform(float fpov) HWR_SetTransformAiming(&atransform); atransform.angley = (float)(viewangle>>ANGLETOFINESHIFT)*(FINEDEGREE); + yaw = 270.0f - atransform.angley; + inv_yaw = 180.0f - yaw; + const float inv_yaw_radians = inv_yaw * deg2radians; + cos_inv_yaw = cosf(inv_yaw_radians); + sin_inv_yaw = sinf(inv_yaw_radians); + // only needed for sprite billboarding if (cv_glspritebillboarding.value && !cv_glshearing.value) { @@ -5512,7 +5584,6 @@ void HWR_RenderViewpoint(gl_portal_t *rootportal, player_t *player, int stencil_ HWR_SetStencilState(HWR_STENCIL_NORMAL, stencil_level); HWR_SetTransform(fpov); - HWR_ClearSprites(); HWR_ClearClipper(); diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c index a78eefb04..e2495bcb9 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -127,7 +127,7 @@ GLuint gl_num_extensions; int majorGL = 0, minorGL = 0; //Hurdler: 04/10/2000: added for the kick ass coronas as Boris wanted;-) -static GLfloat modelMatrix[16]; +GLfloat modelMatrix[16]; GLfloat projMatrix[16] = {0}; static GLint viewport[4]; diff --git a/src/hardware/r_opengl/r_opengl.h b/src/hardware/r_opengl/r_opengl.h index da97168fb..308d69f92 100644 --- a/src/hardware/r_opengl/r_opengl.h +++ b/src/hardware/r_opengl/r_opengl.h @@ -125,6 +125,7 @@ extern GLint maximumAnisotropy; extern boolean supportNPO2tex; extern boolean supportMipMap; +extern GLfloat modelMatrix[16]; extern GLfloat projMatrix[16]; #ifdef __cplusplus