From f71d794866eb0d5a8b7bc3c3416b89faaec42118 Mon Sep 17 00:00:00 2001
From: vmmeazza <vmmeazza>
Date: Wed, 31 Mar 2010 09:11:44 +0000
Subject: [PATCH] Tie reflectivity to material shininess, and provide more user
 control via parameters.

Perpare for cube cross textures.
---
 Effects/reflect.eff  | 89 +++++++++++++++++++++++++++++++++++++-------
 Shaders/reflect.frag | 36 ++++++++++++------
 Shaders/reflect.vert |  4 ++
 3 files changed, 105 insertions(+), 24 deletions(-)

diff --git a/Effects/reflect.eff b/Effects/reflect.eff
index 59d45e743..25349d905 100644
--- a/Effects/reflect.eff
+++ b/Effects/reflect.eff
@@ -1,4 +1,46 @@
 <?xml version="1.0" encoding="utf-8"?>
+<!-- Reflections
+
+PARAMETERS: There are two forms of cube map texture:- vertical cross and 6 images.
+Either can be used - alternative forms are shown in <parameters> and in <texture-unit>
+
+The reflection is set proportional to the shininess of the material. Thus by 
+varying the material shininess value over or between objects the amount of 
+refection can be controlled. The overall amount of reflection may be 
+adjusted by the use of <refl_correction> -1.0 (fully transparent)- 1.0 (fully opaque).
+
+The overall values of the coloured fringing or fresnel effect may be adjusted
+by the use of <rainbowiness> and <fresneliness>.
+
+If your result is too dark/too light the overall ambient light value can be adjusted
+by the use of <ambient_correction>. This correction also takes out some of the blueness
+added as default to compensate for the lack of reflection.
+
+USE: 	To use the default reflection effect (controlled by material shininess values) use 
+	
+	<effect>
+			<inherits-from>Effects/reflect</inherits-from>
+			<object-name>Fuselage</object-name>
+	</<effect>
+	
+			To use your own reflection effectuse
+			
+	<effect>
+		<inherits-from>Aircraft/Lightning/Models/Effects/lightningreflect</inherits-from>
+		<object-name>Fuselage</object-name>
+	</<effect>
+	
+in your model file.
+
+To use your own effect place your efffect file containing something like this:
+
+	<name>Effects/lightningreflect</name>
+	<inherits-from>Effects/reflect</inherits-from>
+	
+	and the the modified tags in the path as above
+
+EXAMPLES: You can find examples of both usages in the Hunter and Lightning models.
+-->
 <PropertyList>
 	<name>Effects/reflect</name>
 	<inherits-from>Effects/model-default</inherits-from>
@@ -6,6 +48,10 @@
 	<parameters>
 		<texture n="5">
 			<type>cubemap</type>
+			<!-- use this form for a cube cross -->
+			<!--<image>Aircraft/Generic/Effects/CubeCrosses/blue_sky.jpg</image>-->
+
+			<!-- use this form for a 6 image cube map -->
 			<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>
@@ -14,6 +60,7 @@
 				<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>
 		<texture n="6">
 			<image>Aircraft/Generic/Effects/Rainbow.png</image>
@@ -31,10 +78,14 @@
 		</texture>
 		<rendering-hint>transparent</rendering-hint>
 		<shade-model>smooth</shade-model>
+		<rainbowiness>0.075</rainbowiness>
+		<fresneliness>0.075</fresneliness>
+		<refl_correction>0.0</refl_correction>
+		<ambient_correction>0.1</ambient_correction>
 	</parameters>
 
 	<technique n="9">
-		
+
 		<predicate>
 			<and>
 				<property>/sim/rendering/shader-effects</property>
@@ -52,7 +103,7 @@
 				</or>
 			</and>
 		</predicate>
-		
+
 		<pass>
 			<lighting>true</lighting>
 			<material>
@@ -98,7 +149,7 @@
 			<rendering-hint>
 				<use>rendering-hint</use>
 			</rendering-hint>
-			
+
 			<texture-unit>
 				<unit>0</unit>
 				<image>
@@ -117,12 +168,19 @@
 					<use>texture[0]/internal-format</use>
 				</internal-format>
 			</texture-unit>
-			
+
 			<texture-unit>
 				<unit>5</unit>
 				<type>
 					<use>texture[5]/type</use>
 				</type>
+
+				<!-- use this form for a cube cross -->
+				<!--<image>
+					<use>texture[5]/image</use>
+				</image>-->
+
+				<!-- use this form for a 6 image cube map -->
 				<images>
 					<use>texture[5]/images</use>
 				</images>
@@ -212,29 +270,34 @@
 			<uniform>
 				<name>rainbowiness</name>
 				<type>float</type>
-				<value type="float">0.1</value>
+				<value><use>rainbowiness</use></value>
 			</uniform>
 
 			<!-- set the amount of fresnel effect colour 0.0 - 1.0 -->
 			<uniform>
 				<name>fresneliness</name>
 				<type>float</type>
-				<value type="float">0.1</value>
+				<value><use>fresneliness</use></value>
 			</uniform>
 
-			<!-- set the amount of tranparency 0.0 (fully tranaparent)
-				- 1.0 (fully opaque) -->
+			<!-- The reflection is set proportional to the shininess of the material. 
+			The amount of reflection may be adjusted by the use of this correction
+			-1.0 (fully transparent)- 1.0 (fully opaque) -->
 			<uniform>
-				<name>transparency</name>
+				<name>refl_correction</name>
 				<type>float</type>
-				<value type="float">0.4</value>
+				<value>
+					<use>refl_correction</use>
+				</value>
 			</uniform>
 
-			<!-- set the amount of ambient light correction 0.0 -1.0  -->
+			<!-- set the amount of ambient light correction 0.0 - 1.0  -->
 			<uniform>
-				<name>correction</name>
+				<name>ambient_correction</name>
 				<type>float</type>
-				<value type="float">0.2</value>
+				<value>
+					<use>ambient_correction</use>
+				</value>
 			</uniform>
 		</pass>
 	</technique>
diff --git a/Shaders/reflect.frag b/Shaders/reflect.frag
index 652effd70..37900d415 100644
--- a/Shaders/reflect.frag
+++ b/Shaders/reflect.frag
@@ -1,4 +1,8 @@
-#version 120
+// -*- mode: C; -*-
+// Licence: GPL v2
+// Author: Vivian Meazza. 
+
+#version 120
 
 varying vec4  ecPosition;
 varying vec3  VNormal;
@@ -16,10 +20,10 @@ uniform sampler2D Rainbow;
 uniform sampler2D BaseTex;
 uniform sampler2D Fresnel;
 
-uniform float transparency;
+uniform float refl_correction;
 uniform float rainbowiness;
 uniform float fresneliness;
-uniform float correction;
+uniform float ambient_correction;
 
 
 void main (void)
@@ -33,7 +37,7 @@ void main (void)
     n = VNormal;
     NdotL = max(0.0, dot(n, lightDir));
 
-    //calculate the specular light
+    // calculate the specular light
     if (NdotL > 0.0) {
         color += Diffuse * NdotL;
         halfV = normalize(halfVector);
@@ -68,18 +72,28 @@ void main (void)
     // Map a fresnel effect
     vec4 fresnel = texture2D(Fresnel, vec2(v, 0.0));
 
-    //map the refection of the environment
+    // map the refection of the environment
     vec4 reflection = textureCube(Environment, reflVec);
 
-    //add fringing fresnel and rainbow effects and modulate by transparency
+    // set the reflectivity proportional to shininess with user 
+    // input ambient
+    float transparency_offset = clamp(refl_correction, -1.0, 1.0);
+    float reflFactor = (gl_FrontMaterial.shininess / 128) + transparency_offset;
+    reflFactor = clamp(reflFactor, 0.0, 1.0);
+
+    // set adjust ambient ambient
+    float ambient_offset = clamp(ambient_correction, -1.0, 1.0);
+    vec4 ambient_Correction = mix(gl_LightSource[0].ambient, vec4(1.0, 1.0, 0.9, 1.0), 0.5) 
+        * ambient_offset;
+
+    // add fringing fresnel and rainbow effects and modulate by reflection
     vec4 reflcolor = mix(reflection, rainbow, rainbowiness * v);
     vec4 reflfrescolor = mix(reflcolor, fresnel, fresneliness * v);
-    vec4 raincolor = vec4(reflfrescolor.rgb, 1.0) * transparency;
-    vec4 mixedcolor = mix(texel, raincolor, transparency);
+    vec4 raincolor = vec4(reflfrescolor.rgb, 1.0) * reflFactor;
+    vec4 mixedcolor = mix(texel, raincolor, reflFactor);
 
-    //the final reflection
-    vec4 ambient_correction = mix(gl_LightSource[0].ambient, vec4(1.0, 1.0, 0.9, 1.0), 0.5) * correction;
-    vec4 reflColor = color * mixedcolor + specular + ambient_correction ;
+    // the final reflection
+    vec4 reflColor = color * mixedcolor + specular + ambient_Correction ;
     reflColor = clamp(reflColor, 0.0, 1.0);
 
     gl_FragColor = mix(gl_Fog.color, reflColor, fogFactor);
diff --git a/Shaders/reflect.vert b/Shaders/reflect.vert
index ea2c651b6..6c3e8fb37 100644
--- a/Shaders/reflect.vert
+++ b/Shaders/reflect.vert
@@ -1,3 +1,7 @@
+// -*- mode: C; -*-
+// Licence: GPL v2
+// Author: Vivian Meazza. 
+
 varying vec4  ecPosition;
 varying vec3  VNormal;
 varying vec3  Normal;