From 605f0e3c742ae4e68455a86378792ebb4e656a60 Mon Sep 17 00:00:00 2001
From: Stuart Buchanan <stuart_d_buchanan@yahoo.co.uk>
Date: Thu, 12 Nov 2020 20:47:08 +0000
Subject: [PATCH] WS3.0: Add forest texture to shader

Interm shader update until texture array support available.
---
 Compositor/Effects/ws30.eff      | 53 ++++++++++++++++++++++++++++----
 Compositor/Shaders/ALS/ws30.frag | 35 ++++++++++-----------
 2 files changed, 63 insertions(+), 25 deletions(-)

diff --git a/Compositor/Effects/ws30.eff b/Compositor/Effects/ws30.eff
index 3471da921..acece4aa5 100644
--- a/Compositor/Effects/ws30.eff
+++ b/Compositor/Effects/ws30.eff
@@ -46,6 +46,19 @@
       <internal-format>normalized</internal-format>
     </texture>
     <texture n="3">
+      <image>Textures/Terrain/forest.png</image>
+      <type>2d</type>
+      <filter>linear-mipmap-linear</filter>
+      <!-- also repeat -->
+      <wrap-s>repeat</wrap-s>
+      <wrap-t>repeat</wrap-t>
+      <!--
+           <wrap-r>clamp-to-border</wrap-r>
+           -->
+      <!-- float, signed-integer, integer -->
+      <internal-format>normalized</internal-format>
+    </texture>
+    <texture n="4">
       <image>Textures/Terrain/water.png</image>
       <type>2d</type>
       <filter>linear-mipmap-linear</filter>
@@ -492,9 +505,8 @@
         <image>
           <use>texture[0]/image</use>
         </image>
-        <filter>
-          <use>texture[0]/filter</use>
-        </filter>
+        <filter>nearest-mipmap-nearest</filter>
+        <mag-filter>nearest-mipmap-nearest</mag-filter>
         <wrap-s>
           <use>texture[0]/wrap-s</use>
         </wrap-s>
@@ -504,6 +516,11 @@
         <internal-format>
           <use>texture[0]/internal-format</use>
         </internal-format>
+        <mipmap-control>
+          <r>max</r>
+          <g>max</g>
+          <b>max</b>
+        </mipmap-control>
       </texture-unit>
 
       <texture-unit>
@@ -562,6 +579,25 @@
         </internal-format>
       </texture-unit>
 
+      <texture-unit>
+        <unit>4</unit>
+        <image>
+          <use>texture[4]/image</use>
+        </image>
+        <filter>
+          <use>texture[4]/filter</use>
+        </filter>
+        <wrap-s>
+          <use>texture[4]/wrap-s</use>
+        </wrap-s>
+        <wrap-t>
+          <use>texture[4]/wrap-t</use>
+        </wrap-t>
+        <internal-format>
+          <use>texture[4]/internal-format</use>
+        </internal-format>
+      </texture-unit>
+
       <program>
         <vertex-shader>Shaders/ALS/generic-base.vert</vertex-shader>
         <vertex-shader>Shaders/ALS/shadows-include.vert</vertex-shader>
@@ -732,20 +768,25 @@
         <value type="int">0</value>
       </uniform>
       <uniform>
-        <name>texture1</name>
+        <name>grass</name>
         <type>sampler-2d</type>
         <value type="int">1</value>
       </uniform>
       <uniform>
-        <name>texture2</name>
+        <name>city</name>
         <type>sampler-2d</type>
         <value type="int">2</value>
       </uniform>
       <uniform>
-        <name>texture3</name>
+        <name>forest</name>
         <type>sampler-2d</type>
         <value type="int">3</value>
       </uniform>
+      <uniform>
+        <name>water</name>
+        <type>sampler-2d</type>
+        <value type="int">4</value>
+      </uniform>
       <uniform>
         <name>colorMode</name>
         <type>int</type>
diff --git a/Compositor/Shaders/ALS/ws30.frag b/Compositor/Shaders/ALS/ws30.frag
index f227d516a..c794b508c 100644
--- a/Compositor/Shaders/ALS/ws30.frag
+++ b/Compositor/Shaders/ALS/ws30.frag
@@ -13,9 +13,10 @@ varying vec3 relPos;
 uniform float fg_Fcoef;
 
 uniform sampler2D landclass;
-uniform sampler2D texture1;
-uniform sampler2D texture2;
-uniform sampler2D texture3;
+uniform sampler2D grass;
+uniform sampler2D city;
+uniform sampler2D forest;
+uniform sampler2D water;
 
 
 varying float yprime_alt;
@@ -64,7 +65,6 @@ void main()
     vec3 lightDir = gl_LightSource[0].position.xyz;
     vec3 halfVector = gl_LightSource[0].halfVector.xyz;
     vec4 texel;
-	vec4 lc;
     vec4 fragColor;
     vec4 specular = vec4(0.0);
     float intensity;
@@ -96,27 +96,24 @@ void main()
     // is closer to what the OpenGL fixed function pipeline does.
     color = clamp(color, 0.0, 1.0);
 
-/*
-	landclass = texture2D(texture, gl_TexCoord[0].st);
-	texel =  (landclass.r > 0.148) * texture2D(texture, gl_TexCoord[3].st) + 
-	        (landclass.g < 0.02) * texture2D(texture, gl_TexCoord[2].st) +
-			(1 - ((landclass.r > 0.148) || (landclass.g < 0.02))) * texture2D(texture, gl_TexCoord[1].st);
-*/
-	lc = texture2D(landclass, gl_TexCoord[0].st);
-	if (lc.r > 0.148) {
-		// Water
-		texel = texture2D(texture3, gl_TexCoord[0].st);
-	} else if (lc.g > 0.02) {
-		texel = texture2D(texture1, gl_TexCoord[0].st);
+	int lc = int(texture2D(landclass, gl_TexCoord[0].st).r * 256.0 + 0.5);
+
+	if ((lc == 1) || (lc == 2) || (lc == 4) || (lc == 5))
+	{
+		texel = texture2D(city, gl_TexCoord[0].st);
+	} else if ((lc > 21) && (lc < 25))
+	{
+		texel = texture2D(forest, gl_TexCoord[0].st);
+	} else if (lc > 38)
+	{
+		texel = texture2D(water, gl_TexCoord[0].st);
 	} else {
-		texel = texture2D(texture2, gl_TexCoord[0].st);
+		texel = texture2D(grass, gl_TexCoord[0].st);
 	}
 
     //texel = texture2D(texture, gl_TexCoord[0].st);
     fragColor = color * texel + specular;
 
-
-
 // here comes the terrain haze model