From 05827ffcda0b33b04dda80fad33a053b02a028fd Mon Sep 17 00:00:00 2001
From: Magnus Norddahl <dpjudas@users.noreply.github.com>
Date: Wed, 21 Feb 2018 23:12:46 +0100
Subject: [PATCH] - Implement auto textures for materials

---
 src/gl/data/gl_data.cpp        |  4 ++--
 src/gl/textures/gl_texture.cpp | 44 ++++++++++++++++++++++++++--------
 2 files changed, 36 insertions(+), 12 deletions(-)

diff --git a/src/gl/data/gl_data.cpp b/src/gl/data/gl_data.cpp
index 42fc6e96d..ca3e25ab9 100644
--- a/src/gl/data/gl_data.cpp
+++ b/src/gl/data/gl_data.cpp
@@ -68,7 +68,7 @@ CUSTOM_CVAR(Bool, gl_notexturefill, false, 0)
 
 
 void gl_CreateSections();
-void AddAutoBrightmaps();
+void AddAutoMaterials();
 
 //-----------------------------------------------------------------------------
 //
@@ -364,7 +364,7 @@ void gl_RecalcVertexHeights(vertex_t * v)
 void gl_InitData()
 {
 	AdjustSpriteOffsets();
-	AddAutoBrightmaps();
+	AddAutoMaterials();
 }
 
 //==========================================================================
diff --git a/src/gl/textures/gl_texture.cpp b/src/gl/textures/gl_texture.cpp
index 7e39fd9f8..fae65d980 100644
--- a/src/gl/textures/gl_texture.cpp
+++ b/src/gl/textures/gl_texture.cpp
@@ -763,26 +763,50 @@ void gl_ParseBrightmap(FScanner &sc, int deflump)
 
 //==========================================================================
 //
-//
+// Search auto paths for extra material textures
 //
 //==========================================================================
 
-void AddAutoBrightmaps()
+struct AutoTextureSearchPath
+{
+	const char *path;
+	ptrdiff_t offset;
+
+	void SetTexture(FTexture *material, FTexture *texture) const
+	{
+		*reinterpret_cast<FTexture**>(reinterpret_cast<uint8_t*>(&material->gl_info) + offset) = texture;
+	}
+};
+
+static AutoTextureSearchPath autosearchpaths[] =
+{
+	{ "brightmaps/auto/", offsetof(FTexture::MiscGLInfo, Brightmap) },
+	{ "normalmaps/auto/", offsetof(FTexture::MiscGLInfo, Normal) },
+	{ "specular/auto/", offsetof(FTexture::MiscGLInfo, Specular) },
+	{ "metallic/auto/", offsetof(FTexture::MiscGLInfo, Metallic) },
+	{ "roughness/auto/", offsetof(FTexture::MiscGLInfo, Roughness) },
+	{ "ao/auto/", offsetof(FTexture::MiscGLInfo, AmbientOcclusion) }
+};
+
+void AddAutoMaterials()
 {
 	int num = Wads.GetNumLumps();
 	for (int i = 0; i < num; i++)
 	{
 		const char *name = Wads.GetLumpFullName(i);
-		if (strstr(name, "brightmaps/auto/") == name)
+		for (const AutoTextureSearchPath &searchpath : autosearchpaths)
 		{
-			TArray<FTextureID> list;
-			FString texname = ExtractFileBase(name, false);
-			TexMan.ListTextures(texname, list);
-			auto bmtex = TexMan.FindTexture(name, FTexture::TEX_Any, FTextureManager::TEXMAN_TryAny);
-			for (auto texid : list)
+			if (strstr(name, searchpath.path) == name)
 			{
-				bmtex->bMasked = false;
-				TexMan[texid]->gl_info.Brightmap = bmtex;
+				TArray<FTextureID> list;
+				FString texname = ExtractFileBase(name, false);
+				TexMan.ListTextures(texname, list);
+				auto bmtex = TexMan.FindTexture(name, FTexture::TEX_Any, FTextureManager::TEXMAN_TryAny);
+				for (auto texid : list)
+				{
+					bmtex->bMasked = false;
+					searchpath.SetTexture(TexMan[texid], bmtex);
+				}
 			}
 		}
 	}