From 05f26a92f2dc84a16f64e83a57e6ce622ecc4dd3 Mon Sep 17 00:00:00 2001
From: Latapostrophe <hyperclassic3@gmail.com>
Date: Sun, 1 Sep 2019 03:10:30 +0200
Subject: [PATCH] fake software fog

---
 src/hardware/hw_main.c           |  4 ++--
 src/hardware/r_opengl/r_opengl.c | 24 +++++++++++++++++++++---
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c
index 5228befc6..1afb35c7a 100644
--- a/src/hardware/hw_main.c
+++ b/src/hardware/hw_main.c
@@ -139,7 +139,7 @@ void HWR_Lighting(FSurfaceInfo *Surface, INT32 light_level, UINT32 mixcolor, UIN
 	mix_color.rgba = mixcolor;
 	fog_color.rgba = fadecolor;
 
-	mix = mix_color.s.alpha*10/4;
+	mix = mix_color.s.alpha*10/5;
 	if (mix > 25) mix = 25;
 	mix *= 255;
 	mix /= 25;
@@ -166,7 +166,7 @@ void HWR_Lighting(FSurfaceInfo *Surface, INT32 light_level, UINT32 mixcolor, UIN
 	if (cv_grfog.value)
 	{
 		// be careful, this may get negative for high lightlevel values.
-		float fog = (fog_alpha - (light_level/255.0f))*3/2;
+		float fog = (fog_alpha - (light_level/255.0f))*115/100;
 		if (fog < 0)
 			fog = 0;
 
diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c
index 85c28e130..fb184ebe5 100644
--- a/src/hardware/r_opengl/r_opengl.c
+++ b/src/hardware/r_opengl/r_opengl.c
@@ -579,17 +579,35 @@ static gl_shaderprogram_t gl_shaderprograms[MAXSHADERPROGRAMS];
 // GLSL Software fragment shader
 //
 
+// dist is gl_FragCoord.z / gl_FragCoord.w
+// lighting is the polygon's light (0.0 to 255.0)
+// globaldensity is the fog density
+
+/*
+	1.0 -> no fog
+	0.0 -> pitch black
+
+	fog should at minimum be at the sector's lightlevel and then fade to that lightlevel + another value
+	GL lightlevels kinda suck though so everything needs to be darker somewhat.
+
+*/
+
 #define GLSL_INTERNAL_FOG_FUNCTION \
-	"float fog(const float dist, const float density,  const float globaldensity) {\n" \
+	"float fog(const float dist, const float lighting,  const float globaldensity) {\n" \
 		"const float LOG2 = -1.442695;\n" \
+		"const float brightness_coeff = 1.5;\n" \
+		"const float brightness_add = 64.0;\n" \
+		"float density = 0.0001 * ((256.0-lighting)/24.0);\n" \
 		"float d = density * dist;\n" \
-		"return 1.0 - clamp(exp2(d * d * globaldensity * LOG2), 0.0, 1.0);\n" \
+		"float startbrightness = clamp(lighting / brightness_coeff, 0.0, 255.0);\n" \
+		"float endbrightness = clamp(startbrightness - brightness_add, 0.0, 255.0);\n" \
+		"return 1.0 - clamp( startbrightness/255.0 + exp2(d*4*globaldensity * LOG2)/4.0, endbrightness/255.0, lighting/255.0);\n" \
 	"}\n"
 
 // https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_gpu_shader_fp64.txt
 #define GLSL_INTERNAL_FOG_MIX \
 	"float fog_distance = gl_FragCoord.z / gl_FragCoord.w;\n" \
-	"float fog_attenuation = floor(fog(fog_distance, 0.0001 * ((256.0-lighting)/24.0), fog_density)*10.0)/10.0;\n" \
+	"float fog_attenuation = floor(fog(fog_distance, lighting, fog_density)*10.0)/10.0;\n" \
 	"vec4 fog_color = vec4(fade_color[0], fade_color[1], fade_color[2], 1.0);\n" \
 	"vec4 mixed_color = texel * mix_color;\n" \
 	"vec4 fog_mix = mix(mixed_color, fog_color, fog_attenuation);\n" \
-- 
GitLab