Index: GTP/trunk/App/Demos/Illum/Ogre/Media/materials/scripts/MetalTeapot.material
===================================================================
--- GTP/trunk/App/Demos/Illum/Ogre/Media/materials/scripts/MetalTeapot.material	(revision 1897)
+++ GTP/trunk/App/Demos/Illum/Ogre/Media/materials/scripts/MetalTeapot.material	(revision 1929)
@@ -133,4 +133,5 @@
     scene_blend max
     depth_test off
+    polygon_mode points
     
 	vertex_program_ref DefaultVS
@@ -157,5 +158,6 @@
     scene_blend max
     depth_test off
-    
+    polygon_mode points
+        
 	vertex_program_ref DefaultVS
     {       
@@ -204,6 +206,4 @@
 				resolution 1024
 				update_interval		0
-				get_minmax true
-				min_var_name min
 				distance_calc false
 				face_angle_calc false
@@ -215,6 +215,4 @@
 				layer 1
 				texture_unit_id 2
-				get_minmax true
-				min_var_name min1
 				update_interval		0
 				distance_calc false
@@ -230,6 +228,4 @@
 				layer 2
 				texture_unit_id 3
-				get_minmax true
-				min_var_name min2
 				update_interval		0
 				distance_calc false
@@ -239,5 +235,50 @@
 				render_self true
 				self_material NormalDistanceCW
-			}		
+			}
+			RenderTechnique ColorCubeMap
+			{
+				resolution 1
+				layer 3				
+				attach_to_texture_unit false
+				get_minmax true
+				max_var_name minmax
+				update_interval		0
+				distance_calc false
+				face_angle_calc false
+				update_all_face true
+				render_env true
+				render_self false
+				env_material DistanceMinMaxCW
+			}
+			RenderTechnique ColorCubeMap
+			{
+				resolution 1
+				layer 4
+				attach_to_texture_unit false
+				get_minmax true
+				max_var_name minmax1
+				update_interval		0
+				distance_calc false
+				face_angle_calc false
+				update_all_face true
+				render_env false
+				render_self true
+				self_material DistanceMinMaxCCW
+			}
+			RenderTechnique ColorCubeMap
+			{
+				resolution 1
+				layer 5
+				attach_to_texture_unit false
+				get_minmax true
+				max_var_name minmax2
+				update_interval		0
+				distance_calc false
+				face_angle_calc false
+				update_all_face true
+				render_env false
+				render_self true
+				self_material DistanceMinMaxCW
+			}			
 		 }
 		 vertex_program_ref DefaultVS
@@ -274,6 +315,5 @@
 		{
 			filtering none
-		}
-		
+		}		
 		//Cube map of reflective object's normals and distances CW
 		texture_unit
Index: GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/MetalTeapot.hlsl
===================================================================
--- GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/MetalTeapot.hlsl	(revision 1885)
+++ GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/MetalTeapot.hlsl	(revision 1929)
@@ -333,5 +333,6 @@
  
  float4 Color = float4(0, 0, 0, 0);
- Color = float4(1.0/length(IN.cPos), length(IN.cPos),0,0);
+ Color = float4(1.0/length(IN.cPos), length(IN.cPos),0,1);
+ //return 1;
  return Color;
 }
Index: GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/MetalTeapotNew.hlsl
===================================================================
--- GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/MetalTeapotNew.hlsl	(revision 1897)
+++ GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/MetalTeapotNew.hlsl	(revision 1929)
@@ -13,6 +13,6 @@
 }
 
-#define MAX_LIN_ITERATIONCOUNT 80  //80
-#define MIN_LIN_ITERATIONCOUNT 60  //60
+#define MAX_LIN_ITERATIONCOUNT 20  //80
+#define MIN_LIN_ITERATIONCOUNT 30  //60
 #define SECANT_ITERATIONCOUNT 2
 #define MAX_RAY_DEPTH 2
@@ -70,5 +70,5 @@
 
 void linearSearch(  float3 x, float3 R, float3 N, samplerCUBE mp,
-					float min,					
+					float2 minMax,					
                     out float3 p,
                     out float dl,
@@ -77,42 +77,67 @@
                     out float ppp)
 {
- float3 Ra = abs(R), xa = abs(x);
- float xm =  max(max(xa.x,xa.y),xa.z);
- float Rm = max(max(Ra.x,Ra.y),Ra.z);
- float a = xm / Rm;
  
  int shootL = 0, shootP = 0;          
  bool found = false; 
  
- float dt =  length(x / xm - R / Rm) * MAX_LIN_ITERATIONCOUNT;
- dt = max(dt, MIN_LIN_ITERATIONCOUNT);
- dt = 1.0 / dt;
-  
- float t = 0.01;
- if(min > length(x))
+ float min = minMax.x;
+ float max = minMax.y;
+ 
+ float minT = 0;
+ float maxT = 0;	
  {
- 
-	float a = 1;
+  	float a = dot(R,R);
 	float b = 2 * dot(R, x);
 	float c = dot(x,x) - min * min; 
+	float c2 = dot(x,x) - max * max; 
 	
 	float dis = b*b - 4*a*c;
+	float dis2 = b*b - 4*a*c2;
+	
 	float t1 = (-b + sqrt(dis))/ 2.0 * a;
 	float t2 = (-b - sqrt(dis))/ 2.0 * a;
-	
-	if(t1 > 0 && (t2 < 0 || t1 < t2))
-	 t =  1.0;//t1 / ( a + t1);
-	else if(t2 > 0)
-	 t =  1.0;//t2 / ( a + t2);
-	 //t = 0.011;
-  }
+    float t3 = (-b + sqrt(dis2))/ 2.0 * a;
+	float t4 = (-b - sqrt(dis2))/ 2.0 * a;
+	
+	int numvalids = 0;
+	if(t1 > 0)
+	{
+	 numvalids++;
+	 if(t1 > maxT)maxT = t1;
+	 if(t1 < minT || minT == 0)minT = t1;
+	}
+	if(t2 > 0)
+	{
+	 numvalids++;
+	 if(t2 > maxT)maxT = t2;
+	 if(t2 < minT || minT == 0)minT = t2;
+	}
+	if(t3 > 0)
+	{
+	 numvalids++;
+	 if(t3 > maxT)maxT = t3;
+	 if(t3 < minT || minT == 0)minT = t3;
+	}
+	if(t4 > 0)
+	{
+	 numvalids++;
+	 if(t4 > maxT)maxT = t4;
+	 if(t4 < minT || minT == 0)minT = t4;
+	}
+	
+	if(numvalids % 2 == 1)
+	 minT = 0.01;
+	if(numvalids == 0)
+	 minT = maxT + 1.0;	
+ }
  
  float pa;
- 
+ float dt = (maxT - minT) / MAX_LIN_ITERATIONCOUNT;
+ float t = minT;
  //Linear iteration
- while(t <= 1.0 && !found)
+ while(t <= maxT && !found)
  {
-   dp = a * t / (1 - t);
-   p = x + R * dp;
+   dp = t;
+   p = x + R * t;
    pa = readDistanceCubeMap(mp, p);
       
@@ -175,15 +200,15 @@
            samplerCUBE mp3Color,
            samplerCUBE mp3Dist,
-           float min,
-           float min1,
-		   float min2,		   
+           float2 minMax,
+           float2 minMax1,
+		   float2 minMax2,		   
            out float4 Il, out float3 Nl)
 {
  float dl1 = 0, dp1, llp1, ppp1;
  float3 p1;
- linearSearch(x, R, N, mp1, min1, p1, dl1, dp1, llp1, ppp1);
+ linearSearch(x, R, N, mp1, minMax1, p1, dl1, dp1, llp1, ppp1);
  float dl2 = 0, dp2, llp2, ppp2;
  float3 p2;
- linearSearch(x, R, N, mp2, min2, p2, dl2, dp2, llp2, ppp2);
+ linearSearch(x, R, N, mp2, minMax2, p2, dl2, dp2, llp2, ppp2);
  
  bool valid1 = dot(p1,p1) != 0;
@@ -195,8 +220,13 @@
  if(!valid1 && ! valid2)
  {
-    linearSearch(x, R, N, mp3Dist, min, p, dl, dp, llp, ppp);
-    Il.a = 1; 
-    secantSearch(x, R, mp3Dist, dl, dp, llp, ppp, p);
-    Il.rgb =  Nl.rgb = readCubeMap(mp3Color, p).rgb;  
+    linearSearch(x, R, N, mp3Dist, minMax, p, dl, dp, llp, ppp);
+    if(dot(p,p) != 0)
+    {
+     Il.a = 1;    
+     secantSearch(x, R, mp3Dist, dl, dp, llp, ppp, p);
+     Il.rgb =  Nl.rgb = readCubeMap(mp3Color, p).rgb;  
+    }
+    else
+     Il = float4(0,0,0,1);
  }
  else
@@ -255,7 +285,7 @@
 							uniform float3 lastCenter,
 							uniform float refIndex,
-							float min,
-							float min1,
-							float min2
+							float2 minMax,
+							float2 minMax1,
+							float2 minMax2
 							)
 {
@@ -270,15 +300,10 @@
 
 float3 R;	   
-//if(refIndex > 100.0)
-    R = normalize(reflect( V, N));
-//else
-//{
-//	refIndex = 1.0 / refIndex;
-//    R = normalize(refract( V, N, refIndex));
-//}
+
+R = normalize(reflect( V, N));
 			
  float3 Nl;
  float4 Il;
- float3 l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, min, min1, min2, Il, Nl);
+ float3 l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, minMax, minMax1, minMax2, Il, Nl);
  if(Il.a == 0)
   Il = readCubeMap(CubeMap, l);
@@ -297,7 +322,7 @@
 							uniform float3 lastCenter,
 							uniform float refIndex,
-							float min,
-							float min1,
-							float min2)
+							float2 minMax,
+							float2 minMax1,
+							float2 minMax2)
 {
 	 float4 I = float4(0,0,0,0);
@@ -323,5 +348,5 @@
 	   float3 Nl;
 	   float4 Il;
-	   l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, min, min1, min2, Il, Nl);
+	   l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, minMax, minMax1, minMax2, Il, Nl);
 	   if(Il.a == 0)
 	   {
@@ -368,5 +393,5 @@
 		   float3 Nl;
 		   float4 Il;
-		   l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, min, min1, min2, Il, Nl);
+		   l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, minMax, minMax1, minMax2, Il, Nl);
 		   if(Il.a == 0)
 		   {
@@ -401,16 +426,26 @@
 							uniform float SingleBounce,
 							uniform float refIndex,
-							uniform float4 min,
-							uniform float4 min1,
-							uniform float4 min2
+							uniform float4 minmax,
+							uniform float4 minmax1,
+							uniform float4 minmax2
 							):COLOR
 {
-   // min1.x = 1.0 / min1.x;
-   // min2.x = 1.0 / min2.x;
+   float2 miniMaxi1;
+   miniMaxi1.x = 1.0 / minmax.x / sqrt(2.0);
+   miniMaxi1.y = minmax.y;
+   
+   float2 miniMaxi2;
+   miniMaxi2.x = 1.0 / minmax1.x / sqrt(2.0);
+   miniMaxi2.y = minmax1.y;
+   
+   float2 miniMaxi3;
+   miniMaxi3.x = 1.0 / minmax2.x / sqrt(2.0);
+   miniMaxi3.y = minmax2.y;
+
 	float4 Color = 1.0;
 	if(SingleBounce == 1)
-	 Color = SingleReflectionPS(IN,cameraPos, CubeMap, DistanceMap, NormDistMap1,NormDistMap2,lastCenter,refIndex, min.w, min1.w, min2.w);
+	 Color = SingleReflectionPS(IN,cameraPos, CubeMap, DistanceMap, NormDistMap1,NormDistMap2,lastCenter,refIndex, miniMaxi1, miniMaxi2, miniMaxi3);
 	else
-	 Color = MultipleReflectionPS(IN,cameraPos, CubeMap, DistanceMap, NormDistMap1,NormDistMap2,lastCenter,refIndex, min.w, min1.w, min2.w);
+	 Color = MultipleReflectionPS(IN,cameraPos, CubeMap, DistanceMap, NormDistMap1,NormDistMap2,lastCenter,refIndex, miniMaxi1, miniMaxi2, miniMaxi3);
 	 
 	return Color;
Index: GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/GameTools_Diffuse.hlsl
===================================================================
--- GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/GameTools_Diffuse.hlsl	(revision 1735)
+++ GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/GameTools_Diffuse.hlsl	(revision 1929)
@@ -140,5 +140,5 @@
 			
 	}																			
-	
+	/*
 	for (float x = 1; x < M; x++)			
 	 for (float y = 1; y < M; y++)											
@@ -239,5 +239,5 @@
 		
 		I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);				
-	 }	
+	 }	*/
 	 float kd = 0.5;																		
 	return kd * I;
