From 834350b4cc9aa18cf73d813d0a4b5dc1fd44773f Mon Sep 17 00:00:00 2001
From: Thorsten Renk <thorsten.i.renk@jyu.fi>
Date: Thu, 6 Aug 2015 12:26:47 +0300
Subject: [PATCH] Lightmap for reflections in ALS glass effect

---
 Effects/glass.eff      | 83 ++++++++++++++++++++++++++++++++++++++++++
 Shaders/glass-ALS.frag | 58 +++++++++++++++++++++++++++--
 Shaders/glass-ALS.vert |  2 +-
 3 files changed, 138 insertions(+), 5 deletions(-)

diff --git a/Effects/glass.eff b/Effects/glass.eff
index 2e58bc221..95e8519a0 100644
--- a/Effects/glass.eff
+++ b/Effects/glass.eff
@@ -17,6 +17,7 @@
 	<texture n="2">
 		<type>white</type>
 	</texture>	
+	<!-- texture for reflections in the glass -->
 	<texture n="3">
 		<type>cubemap</type>
 		<images>
@@ -28,6 +29,18 @@
 			<negative-z>Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_nz.png</negative-z>
 		</images>
 		</texture>
+	<!-- placeholder lightmap texture for reflections in the glass -->
+	<texture n="4">
+		<type>cubemap</type>
+		<images>
+			<positive-x>Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_px.png</positive-x>
+			<negative-x>Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_nx.png</negative-x>
+			<positive-y>Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_py.png</positive-y>
+			<negative-y>Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_ny.png</negative-y>
+			<positive-z>Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_pz.png</positive-z>
+			<negative-z>Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_nz.png</negative-z>
+		</images>
+		</texture>
 	<glass-tint type="vec4d" n="0"> 1.0 1.0 1.0 1.0</glass-tint>
 	<overlay-color type="vec3d" n="0"> 1.0 1.0 1.0</overlay-color>
 	<splash-x><use>/environment/aircraft-effects/splash-vector-x</use></splash-x>
@@ -45,6 +58,16 @@
 	<use-reflection type="int">0</use-reflection>
 	<reflection-strength type="float">1.0</reflection-strength>
 	<use-mask type="int">0</use-mask>
+	<use-reflection-lightmap type="int">0</use-reflection-lightmap>
+	<lightmap-multi type="int">0</lightmap-multi>
+	<lightmap-factor type="float" n="0">1.0</lightmap-factor>
+	<lightmap-color type="vec3d" n="0"> 1.0 1.0 1.0 </lightmap-color>
+	<lightmap-factor type="float" n="1">1.0</lightmap-factor>
+	<lightmap-color type="vec3d" n="1"> 1.0 1.0 1.0 </lightmap-color>
+	<lightmap-factor type="float" n="2">1.0</lightmap-factor>
+	<lightmap-color type="vec3d" n="2"> 1.0 1.0 1.0 </lightmap-color>
+	<lightmap-factor type="float" n="3">1.0</lightmap-factor>
+	<lightmap-color type="vec3d" n="3"> 1.0 1.0 1.0 </lightmap-color>
 </parameters>
 
 <technique n="4">
@@ -122,6 +145,11 @@
 	<type><use>texture[3]/type</use></type>
 	<images><use>texture[3]/images</use></images>
       </texture-unit>
+      <texture-unit>
+	<unit>4</unit>
+	<type><use>texture[4]/type</use></type>
+	<images><use>texture[4]/images</use></images>
+      </texture-unit>
       <vertex-program-two-side>
         <use>vertex-program-two-side</use>
       </vertex-program-two-side>
@@ -141,6 +169,46 @@
         <type>float-vec3</type>
         <value><use>overlay-color</use></value>
      </uniform>
+     <uniform>
+	<name>lightmap_r_factor</name>
+	<type>float</type>
+	<value><use>lightmap-factor[0]</use></value>
+     </uniform>	   
+     <uniform>
+	<name>lightmap_r_color</name>
+	<type>float-vec3</type>
+	<value><use>lightmap-color[0]</use></value>
+     </uniform>
+    <uniform>
+	<name>lightmap_g_factor</name>
+	<type>float</type>
+	<value><use>lightmap-factor[1]</use></value>
+     </uniform>	   
+     <uniform>
+	<name>lightmap_g_color</name>
+	<type>float-vec3</type>
+	<value><use>lightmap-color[1]</use></value>
+     </uniform>
+    <uniform>
+	<name>lightmap_b_factor</name>
+	<type>float</type>
+	<value><use>lightmap-factor[2]</use></value>
+     </uniform>	   
+     <uniform>
+	<name>lightmap_b_color</name>
+	<type>float-vec3</type>
+	<value><use>lightmap-color[2]</use></value>
+     </uniform>
+    <uniform>
+	<name>lightmap_a_factor</name>
+	<type>float</type>
+	<value><use>lightmap-factor[3]</use></value>
+     </uniform>	   
+     <uniform>
+	<name>lightmap_a_color</name>
+	<type>float-vec3</type>
+	<value><use>lightmap-color[3]</use></value>
+     </uniform>
      <uniform>
         <name>splash_x</name>
         <type>float</type>
@@ -261,6 +329,11 @@
         <type>sampler-cube</type>
 	<value type="int">3</value>
       </uniform>
+      <uniform>
+	<name>cube_light_texture</name>
+        <type>sampler-cube</type>
+	<value type="int">4</value>
+      </uniform>
       <uniform>
 	<name>use_reflection</name>
 	<type>int</type>
@@ -281,6 +354,16 @@
 	<type>int</type>
 	<value><use>use-overlay</use></value>
       </uniform>
+      <uniform>
+	<name>use_reflection_lightmap</name>
+	<type>int</type>
+	<value><use>use-reflection-lightmap</use></value>
+      </uniform>
+      <uniform>
+	<name>lightmap_multi</name>
+	<type>int</type>
+	<value><use>lightmap-multi</use></value>
+      </uniform>
       <uniform>
 	<name>adaptive_mapping</name>
 	<type>int</type>
diff --git a/Shaders/glass-ALS.frag b/Shaders/glass-ALS.frag
index deffd0b5a..165cdc438 100644
--- a/Shaders/glass-ALS.frag
+++ b/Shaders/glass-ALS.frag
@@ -14,6 +14,7 @@ uniform sampler2D texture;
 uniform sampler2D frost_texture;
 uniform sampler2D func_texture;
 uniform samplerCube cube_texture;
+uniform samplerCube cube_light_texture;
 
 uniform vec4 tint;
 uniform vec3 overlay_color;
@@ -29,13 +30,24 @@ uniform float overlay_glare;
 uniform float splash_x;
 uniform float splash_y;
 uniform float splash_z;
+uniform float lightmap_r_factor;
+uniform float lightmap_g_factor;
+uniform float lightmap_b_factor;
+uniform float lightmap_a_factor;
 uniform float osg_SimulationTime;
 
 uniform int use_reflection;
+uniform int use_reflection_lightmap;
 uniform int use_mask;
 uniform int use_wipers;
 uniform int use_overlay;
 uniform int adaptive_mapping;
+uniform int lightmap_multi;
+
+uniform vec3 lightmap_r_color;
+uniform vec3 lightmap_g_color;
+uniform vec3 lightmap_b_color;
+uniform vec3 lightmap_a_color;
 
 float DotNoise2D(in vec2 coord, in float wavelength, in float fractionalMaxDotSize, in float dot_density);
 float DropletNoise2D(in vec2 coord, in float wavelength, in float fractionalMaxDotSize, in float dot_density);
@@ -66,9 +78,37 @@ float noise_003m = Noise2D(vertPos.xy, 0.03);
 float noise_0003m = Noise2D(vertPos.xy, 0.003);
 
 
-// environment reflection
+// environment reflection, including a lightmap for the reflections
 
 vec4 reflection = textureCube(cube_texture, refl_vec);
+vec4 reflection_lighting = textureCube(cube_light_texture, refl_vec);
+
+vec3 lightmapcolor = vec3(0.0, 0.0, 0.0);
+
+
+if (use_reflection_lightmap == 1)
+	{
+	vec4 lightmapFactor = vec4(lightmap_r_factor, lightmap_g_factor, lightmap_b_factor, lightmap_a_factor);
+        lightmapFactor = lightmapFactor * reflection_lighting;
+        if (lightmap_multi > 0 )
+		{
+	        lightmapcolor = lightmap_r_color * lightmapFactor.r +
+                lightmap_g_color * lightmapFactor.g +
+                lightmap_b_color * lightmapFactor.b +
+                lightmap_a_color * lightmapFactor.a ;
+            	}
+	 else 
+		{
+                lightmapcolor = reflection_lighting.rgb * lightmap_r_color * lightmapFactor.r;
+            	}
+
+	}
+
+float lightmap_intensity = length(lightmapcolor);
+float light_fraction = clamp(lightmap_intensity / (length(light_diffuse.rgb) + 0.01), 0.0, 5.0);
+
+if (light_fraction < 1.0) {light_fraction = smoothstep(0.7, 1.0, light_fraction);}
+
 
 if (use_reflection ==1)
 	{
@@ -76,10 +116,13 @@ if (use_reflection ==1)
 	// assumption that its normal will be opposite to the glass normal
 	// (which is mostly truish in a normal cockpit)
 	float reflection_shade = ambient_fraction + (1.0-ambient_fraction) * max(0.0, dot (normalize(normal),  normalize(gl_LightSource[0].position.xyz)));
-	texel.rgb = mix(texel.rgb, reflection.rgb, reflection_strength *  reflection_shade * (1.0-Mie));
+
+	texel.rgb = mix(texel.rgb, reflection.rgb, (reflection_strength *  reflection_shade  * (1.0-Mie)));
 
 	}
 
+//texel.rgb = mix(texel.rgb, lightmapcolor.rgb, lightmap_intensity);
+
 // overlay pattern
 
 if ((use_mask == 1) && (use_overlay==1))
@@ -174,9 +217,18 @@ rainColor.rgb *= length(light_diffuse)/1.73;
 
 
 vec4 outerColor = mix(texel, rainColor, rain_factor);
+// now mix illuminated reflections in
+
+vec3 reflLitColor = reflection.rgb * lightmapcolor.rgb;
+
+outerColor.rgb = mix(outerColor.rgb, reflLitColor, clamp(reflection_strength * light_fraction,0.0,1.0));
+outerColor.a = max(outerColor.a, 0.1 * light_fraction * reflection_strength);
+
 outerColor  *= tint;
 
 
+
+
 // fogging - this is inside the glass
 
 vec4 fog_texel = vec4 (0.6,0.6,0.6, fog_level);
@@ -196,7 +248,5 @@ fragColor.a = max(outerColor.a, fog_texel.a);
 
 gl_FragColor = clamp(fragColor,0.0,1.0);
 
-//gl_FragColor = vec4(normal,1.0);
-
 
 }
diff --git a/Shaders/glass-ALS.vert b/Shaders/glass-ALS.vert
index 0ea1fd6be..12c89fb8d 100644
--- a/Shaders/glass-ALS.vert
+++ b/Shaders/glass-ALS.vert
@@ -121,7 +121,7 @@ vertPos = gl_Vertex.xyz;
 
 splash_angle = dot(gl_Normal, corrected_splash);
 
-ambient_fraction = length(light_ambient.rgb)/length(light_diffuse.rgb +light_ambient.rgb );
+ambient_fraction = length(light_ambient.rgb)/(length(light_diffuse.rgb +light_ambient.rgb ) + 0.01);
 
 
 gl_Position = ftransform();