From 48a30657b5e66a201e7379229cfebbfe689624a0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gustaf=20Alh=C3=A4ll?= <gustaf@hanicef.me>
Date: Thu, 20 Mar 2025 16:48:33 +0100
Subject: [PATCH] Refactor OpenGL drop shadow code

---
 src/hardware/hw_main.c | 39 +++++++++++++++++++--------------------
 1 file changed, 19 insertions(+), 20 deletions(-)

diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c
index 15bd12c08..074573324 100644
--- a/src/hardware/hw_main.c
+++ b/src/hardware/hw_main.c
@@ -2807,7 +2807,6 @@ static void HWR_DrawDropShadow(mobj_t *thing, gl_vissprite_t *spr, fixed_t scale
 	UINT16 alpha;
 	fixed_t floordiff;
 	fixed_t groundz;
-	fixed_t slopez;
 	pslope_t *groundslope;
 
 	// uncapped/interpolation
@@ -2881,15 +2880,22 @@ static void HWR_DrawDropShadow(mobj_t *thing, gl_vissprite_t *spr, fixed_t scale
 		shadowVerts[0].z = shadowVerts[3].z = spr->z1;
 		shadowVerts[2].z = shadowVerts[1].z = spr->z2;
 
-		if (thing && fabsf(fscale - 1.0f) > 1.0E-36f)
+		// Always a pixel above the floor, perfectly flat.
+		for (i = 0; i < 4; i++)
 		{
-			// Always a pixel above the floor, perfectly flat.
-			for (i = 0; i < 4; i++)
+			if (groundslope)
 			{
-				if (groundslope)
-					slopez = P_GetSlopeZAt(groundslope, FLOAT_TO_FIXED(shadowVerts[i].x), FLOAT_TO_FIXED(shadowVerts[i].z));
-				shadowVerts[i].y = (groundslope ? FIXED_TO_FLOAT(slopez) : FIXED_TO_FLOAT(groundz))/2 + flip * 0.05f;
+				fixed_t slopez = P_GetSlopeZAt(groundslope, FLOAT_TO_FIXED(shadowVerts[i].x), FLOAT_TO_FIXED(shadowVerts[i].z));
+				shadowVerts[i].y = FIXED_TO_FLOAT(slopez)/2 + flip * 0.05f;
 			}
+			else
+			{
+				shadowVerts[i].y = FIXED_TO_FLOAT(groundz)/2 + flip * 0.05f;
+			}
+		}
+
+		if (thing && fabsf(fscale - 1.0f) > 1.0E-36f)
+		{
 
 			// Now transform the TOP vertices along the floor in the direction of the camera
 			shadowVerts[3].x = spr->x1 + (gpatch->height + fscale + offset) * gl_viewcos;
@@ -2899,14 +2905,6 @@ static void HWR_DrawDropShadow(mobj_t *thing, gl_vissprite_t *spr, fixed_t scale
 		}
 		else
 		{
-			// Always a pixel above the floor, perfectly flat.
-			for (i = 0; i < 4; i++)
-			{
-				if (groundslope)
-					slopez = P_GetSlopeZAt(groundslope, FLOAT_TO_FIXED(shadowVerts[i].x), FLOAT_TO_FIXED(shadowVerts[i].z));
-				shadowVerts[i].y = (groundslope ? FIXED_TO_FLOAT(slopez) : FIXED_TO_FLOAT(groundz))/2 + flip * 0.05f;
-			}
-
 			// Now transform the TOP vertices along the floor in the direction of the camera
 			shadowVerts[3].x = spr->x1 + (gpatch->height + offset) * gl_viewcos;
 			shadowVerts[2].x = spr->x2 + (gpatch->height + offset) * gl_viewcos;
@@ -2921,8 +2919,8 @@ static void HWR_DrawDropShadow(mobj_t *thing, gl_vissprite_t *spr, fixed_t scale
 		float oldy = shadowVerts[i].z;
 		if(!(cv_shadow.value == 2))
 		{
-		shadowVerts[i].x = fx + ((oldx - fx) * gl_viewcos) - ((oldy - fy) * gl_viewsin);
-		shadowVerts[i].z = fy + ((oldx - fx) * gl_viewsin) + ((oldy - fy) * gl_viewcos);
+			shadowVerts[i].x = fx + ((oldx - fx) * gl_viewcos) - ((oldy - fy) * gl_viewsin);
+			shadowVerts[i].z = fy + ((oldx - fx) * gl_viewsin) + ((oldy - fy) * gl_viewcos);
 		}
 	}
 
@@ -2930,7 +2928,7 @@ static void HWR_DrawDropShadow(mobj_t *thing, gl_vissprite_t *spr, fixed_t scale
 	{
 		for (i = 0; i < 4; i++)
 		{
-			slopez = P_GetSlopeZAt(groundslope, FLOAT_TO_FIXED(shadowVerts[i].x), FLOAT_TO_FIXED(shadowVerts[i].z));
+			fixed_t slopez = P_GetSlopeZAt(groundslope, FLOAT_TO_FIXED(shadowVerts[i].x), FLOAT_TO_FIXED(shadowVerts[i].z));
 			shadowVerts[i].y = FIXED_TO_FLOAT(slopez) + flip * 0.05f;
 		}
 	}
@@ -4250,10 +4248,11 @@ static void HWR_DrawSprites(void)
 				// the linkdraw sprite because the linkdraw sprite does not modify the z-buffer.
 				// The !skipshadow check is there in case there are multiple linkdraw sprites connected
 				// to the same tracer, so the tracer's shadow only gets drawn once.
-				if (cv_shadow.value && !skipshadow && spr->dispoffset < 0 && spr->mobj->tracer->shadowscale)
+				if (cv_shadow.value && !skipshadow && (spr->dispoffset < 0 || cv_shadow.value == 2) && spr->mobj->tracer->shadowscale)
 				{
 					HWR_DrawDropShadow(spr->mobj->tracer, spr, spr->mobj->tracer->shadowscale);
-					skipshadow = true;
+					if (cv_shadow.value == 1)
+						skipshadow = true;
 					// The next sprite in this loop should be either another linkdraw sprite or the tracer.
 					// When the tracer is inevitably encountered, skipshadow will cause it's shadow
 					// to get skipped and skipshadow will get set to false by the 'else' clause below.
-- 
GitLab