From 85d377647a75c7af6d4d49f34dd498385af0efa6 Mon Sep 17 00:00:00 2001
From: Christoph Oelckers <coelckers@users.noreply.github.com>
Date: Sun, 20 Oct 2019 01:14:48 +0200
Subject: [PATCH] - tweaked the fade ramp a bit.

For Duke Nukem and its direct offspring (Nam, WW2GI and Redneck Rampage) the ramp is not a linear fade from 0 to 1, it needs to be a little darker than that.
Unfortunately the proper factor needed here must be set manually, this cannot really be calculated from the lookup tables.
---
 source/build/src/sdlayer.cpp                  | 27 +++++++++++++++----
 source/duke3d/src/premap.cpp                  |  4 ---
 source/glbackend/gl_renderstate.h             |  3 ++-
 source/glbackend/gl_shader.cpp                |  3 ++-
 source/glbackend/gl_shader.h                  |  3 ++-
 source/glbackend/glbackend.cpp                |  1 +
 source/glbackend/glbackend.h                  |  7 ++++-
 .../demolition/shaders/glsl/polymost.fp       |  3 ++-
 8 files changed, 37 insertions(+), 14 deletions(-)

diff --git a/source/build/src/sdlayer.cpp b/source/build/src/sdlayer.cpp
index fac287b5b..a73046291 100644
--- a/source/build/src/sdlayer.cpp
+++ b/source/build/src/sdlayer.cpp
@@ -354,6 +354,8 @@ static void sighandler(int signum)
 
 #ifdef _WIN32
 
+FString currentGame;	// Currently there is no global state for the current game. This is a temporary workaround because the video init code needs to do a few things based on the active game.
+
 namespace Duke
 {
 	extern GameInterface Interface;
@@ -376,6 +378,7 @@ GameInterface *CheckFrontend()
 	FILE* f = fopen("blood.rff", "rb");
 	if (f)
 	{
+		currentGame = "Blood";
 		fclose(f);
 		return &Blood::Interface;
 	}
@@ -384,13 +387,26 @@ GameInterface *CheckFrontend()
 		f = fopen("redneck.grp", "rb");
 		if (f)
 		{
+			currentGame = "Redneck";	// RRRA is not separately covered
 			fclose(f);
 			return &Redneck::Interface;
 		}
 		else
 		{
 			f = fopen("sw.grp", "rb");
-			if (f) return &ShadowWarrior::Interface;
+			if (f)
+			{
+				currentGame = "ShadowWarrior";
+				fclose(f);
+				return &ShadowWarrior::Interface;
+			}
+			f = fopen("fury.grp", "rb");
+			if (f)
+			{
+				currentGame = "Fury";
+				fclose(f);
+			}
+			else currentGame = "Duke"; // also covers Nam and WW2GI.
 			return &Duke::Interface;
 		}
 	}
@@ -1341,11 +1357,12 @@ void sdlayer_setvideomode_opengl(void)
 	GLInterface.Deinit();
 	GLInterface.Init();
 	GLInterface.InitGLState(4, glmultisample);
-#if 1
-	GLInterface.mSamplers->SetTextureFilterMode(0, 1);
-#else
+	// I have no idea how to get this info from the lookup tables. Fortunately it is consistent per game.
+	if (!currentGame.Compare("Blood")) GLInterface.SetShadeDiv(62);
+	else if (!currentGame.Compare("Fury")) GLInterface.SetShadeDiv(30);
+	else GLInterface.SetShadeDiv(26);
+
 	GLInterface.mSamplers->SetTextureFilterMode(gltexfiltermode, glanisotropy);
-#endif
 
 }
 
diff --git a/source/duke3d/src/premap.cpp b/source/duke3d/src/premap.cpp
index 1d7fa3189..f6855c554 100644
--- a/source/duke3d/src/premap.cpp
+++ b/source/duke3d/src/premap.cpp
@@ -421,10 +421,6 @@ void G_CacheMapData(void)
 
     S_TryPlaySpecialMusic(MUS_LOADING);
 
-#if defined EDUKE32_TOUCH_DEVICES && defined USE_OPENGL
-    polymost_glreset();
-#endif
-
     uint32_t const cacheStartTime = timerGetTicks();
 
     cacheFlaggedTiles();
diff --git a/source/glbackend/gl_renderstate.h b/source/glbackend/gl_renderstate.h
index bd91abc2c..b860e27a7 100644
--- a/source/glbackend/gl_renderstate.h
+++ b/source/glbackend/gl_renderstate.h
@@ -27,7 +27,8 @@ struct PolymostRenderState
 {
     float Shade;
     float NumShades = 64.f;
-    float VisFactor = 128.f;
+	float ShadeDiv = 62.f;
+	float VisFactor = 128.f;
 	int Flags = 0;
     float NPOTEmulationFactor = 1.f;
     float NPOTEmulationXOffset;
diff --git a/source/glbackend/gl_shader.cpp b/source/glbackend/gl_shader.cpp
index d78796576..3b30c7d21 100644
--- a/source/glbackend/gl_shader.cpp
+++ b/source/glbackend/gl_shader.cpp
@@ -137,7 +137,8 @@ bool PolymostShader::Load(const char * name, const char * vert_prog, const char
 	
 	Flags.Init(hShader, "u_flags");
 	Shade.Init(hShader, "u_shade");
-    NumShades.Init(hShader, "u_numShades");
+	ShadeDiv.Init(hShader, "u_shadeDiv");
+	NumShades.Init(hShader, "u_numShades");
     VisFactor.Init(hShader, "u_visFactor");
     NPOTEmulationFactor.Init(hShader, "u_npotEmulationFactor");
     NPOTEmulationXOffset.Init(hShader, "u_npotEmulationXOffset");
diff --git a/source/glbackend/gl_shader.h b/source/glbackend/gl_shader.h
index 13f7b89d6..a86afc804 100644
--- a/source/glbackend/gl_shader.h
+++ b/source/glbackend/gl_shader.h
@@ -38,7 +38,8 @@ public:
 	FBufferedUniform1i Flags;
     FBufferedUniform1f Shade;
     FBufferedUniform1f NumShades;
-    FBufferedUniform1f VisFactor;
+	FBufferedUniform1f ShadeDiv;
+	FBufferedUniform1f VisFactor;
     FBufferedUniform1f NPOTEmulationFactor;
     FBufferedUniform1f NPOTEmulationXOffset;
     FBufferedUniform1f Brightness;
diff --git a/source/glbackend/glbackend.cpp b/source/glbackend/glbackend.cpp
index 10f309454..2f6b4d687 100644
--- a/source/glbackend/glbackend.cpp
+++ b/source/glbackend/glbackend.cpp
@@ -484,6 +484,7 @@ void PolymostRenderState::Apply(PolymostShader* shader)
 	shader->Flags.Set(Flags);
 	shader->Shade.Set(Shade);
 	shader->NumShades.Set(NumShades);
+	shader->ShadeDiv.Set(ShadeDiv);
 	shader->VisFactor.Set(VisFactor);
 	shader->Flags.Set(Flags);
 	shader->NPOTEmulationFactor.Set(NPOTEmulationFactor);
diff --git a/source/glbackend/glbackend.h b/source/glbackend/glbackend.h
index b5301bf4c..8767adefb 100644
--- a/source/glbackend/glbackend.h
+++ b/source/glbackend/glbackend.h
@@ -320,9 +320,14 @@ public:
 		renderState.NumShades = numshades;
 	}
 
+	void SetShadeDiv(int value)
+	{
+		renderState.ShadeDiv = 1.f / value;	// There's 3 values here: Blood uses 62 with numShades = 64, Ion Fury uses 30 with NumShades = 32, the other games use 26 with NumShades = 32.
+	}
+
 	void SetVisibility(float visibility, float fviewingrange)
 	{
-		renderState.VisFactor = visibility * fviewingrange * (1.f / (64.f * 65536.f));
+		renderState.VisFactor = visibility* fviewingrange* (1.f / (64.f * 65536.f));
 	}
 
 	void UseColorOnly(bool yes)
diff --git a/wadsrc/static/demolition/shaders/glsl/polymost.fp b/wadsrc/static/demolition/shaders/glsl/polymost.fp
index fed1f0c61..cff8da7d0 100644
--- a/wadsrc/static/demolition/shaders/glsl/polymost.fp
+++ b/wadsrc/static/demolition/shaders/glsl/polymost.fp
@@ -30,6 +30,7 @@ uniform sampler2D s_glow;
 
 uniform float u_shade;
 uniform float u_numShades;
+uniform float u_shadeDiv;
 uniform float u_visFactor;
 uniform int u_flags;
 
@@ -192,7 +193,7 @@ void main()
 			color.rgb *= detailColor.rgb;
 			if ((u_flags & RF_FogDisabled) == 0)
 			{
-				shade = clamp(shade / (u_numShades-2), 0.0, 1.0);
+				shade = clamp(shade * u_shadeDiv, 0.0, 1.0);	// u_shadeDiv is really 1/shadeDiv.
 				// Apply the shade as a linear depth fade ramp.
 				color.rgb = mix(color.rgb, u_fogColor.rgb, shade);
 			}