Index: GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/MetalTeapotNew.hlsl
===================================================================
--- GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/MetalTeapotNew.hlsl	(revision 1870)
+++ GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/MetalTeapotNew.hlsl	(revision 1885)
@@ -70,4 +70,5 @@
 
 void linearSearch(  float3 x, float3 R, float3 N, samplerCUBE mp,
+					float min,					
                     out float3 p,
                     out float dl,
@@ -87,6 +88,22 @@
  dt = max(dt, MIN_LIN_ITERATIONCOUNT);
  dt = 1.0 / dt;
-    
+  
  float t = 0.01;//dt;
+ if(min > length(x))
+ {
+	float a = 1;
+	float b = 2 * dot(R, x);
+	float c = dot(x,x) - min * min; 
+	
+	float dis = b*b - 4*a*c;
+	float t1 = (-b + sqrt(dis))/ 2 * a;
+	float t2 = (-b - sqrt(dis))/ 2 * a;
+	
+	if(t1 > 0 && (t2 < 0 || t1 < t2))
+	 t =  t1 / ( a + t1);
+	else if(t2 > 0)
+	 t =  t2 / ( a + t2);	
+  }
+ 
  float pa;
  
@@ -156,12 +173,14 @@
            samplerCUBE mp3Color,
            samplerCUBE mp3Dist,
+           float min1,
+		   float min2,		   
            out float4 Il, out float3 Nl)
 {
  float dl1 = 0, dp1, llp1, ppp1;
  float3 p1;
- linearSearch(x, R, N, mp1, p1, dl1, dp1, llp1, ppp1);
+ linearSearch(x, R, N, mp1, min1, p1, dl1, dp1, llp1, ppp1);
  float dl2 = 0, dp2, llp2, ppp2;
  float3 p2;
- linearSearch(x, R, N, mp2, p2, dl2, dp2, llp2, ppp2);
+ linearSearch(x, R, N, mp2, min2, p2, dl2, dp2, llp2, ppp2);
  
  bool valid1 = dot(p1,p1) != 0;
@@ -173,5 +192,5 @@
  if(!valid1 && ! valid2)
  {
-    linearSearch(x, R, N, mp3Dist, p, dl, dp, llp, ppp);
+    linearSearch(x, R, N, mp3Dist, 0, p, dl, dp, llp, ppp);
     Il.a = 1; 
     secantSearch(x, R, mp3Dist, dl, dp, llp, ppp, p);
@@ -232,5 +251,8 @@
 							uniform samplerCUBE NormDistMap2 : register(s3),
 							uniform float3 lastCenter,
-							uniform float refIndex)
+							uniform float refIndex,
+							float min1,
+							float min2
+							)
 {
  float4 Color = float4(1,1,1,1);
@@ -254,5 +276,5 @@
  float3 Nl;
  float4 Il;
- float3 l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, Il, Nl);
+ float3 l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, min1, min2, Il, Nl);
  if(Il.a == 0)
   Il = readCubeMap(CubeMap, l);
@@ -261,4 +283,5 @@
 }
 
+#define REFRACTION 0
 
 float4 MultipleReflectionPS(VertOut IN,
@@ -269,5 +292,7 @@
 							uniform samplerCUBE NormDistMap2 : register(s3),
 							uniform float3 lastCenter,
-							uniform float refIndex)
+							uniform float refIndex,
+							float min1,
+							float min2)
 {
 	 float4 I = float4(0,0,0,0);
@@ -282,56 +307,16 @@
 	 V = normalize(V);
 	 float3 l;
-	 float4 Irefr;
+	 float4 Irefl;
 	 int depth = 0;
+	
+	
 	 while(depth < MAX_RAY_DEPTH)
 	 {
-	   float3 R;  
-	   //R = normalize(reflect( V, N));
-	     
-	  
-	  float ri = refIndex;
-	  
-	  if(dot(V,N) > 0)
-	  {
-	    ri = 1.0 / ri;
-	    N = -N;	    
-	  }
-	  R = refract( V, N, ri);
-	  if(dot(R,R) == 0) R = reflect( V, N); 
-	  
-				
-	   float3 Nl;
-	   float4 Il;
-	   l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, Il, Nl);
-	   if(Il.a == 0)
-	   {
-		   depth += 1;		 
-	   }
-	   else
-	   {
-		   I = Il;
-		   depth = MAX_RAY_DEPTH;
-	   }
-	   x = l;	   
-	   N = normalize(Nl);
-	   V = R;	   	   
-	}
-	if(I.a == 0)
-       I = readCubeMap(CubeMap, l);
-	Irefr = I;
-	
-	float4 Irefl;
-	N = normalize(IN.mNormal);
-	x = IN.wPos - lastCenter;
-	V  = (IN.wPos - cameraPos);
-	depth = 0;
-	while(depth < MAX_RAY_DEPTH)
-	 {
-	   float3 R;  
+	    float3 R;  
 	   R = normalize(reflect( V, N));	  
 	  	
 	   float3 Nl;
 	   float4 Il;
-	   l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, Il, Nl);
+	   l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, min1, min2, Il, Nl);
 	   if(Il.a == 0)
 	   {
@@ -346,11 +331,57 @@
 	   N = Nl;
 	   V = R;   
+	   
 	}
+	if(I.a == 0)
+	   I = readCubeMap(CubeMap, l);
+	Irefl = I;
 	
-	if(I.a == 0)
-       I = readCubeMap(CubeMap, l);
-    Irefl = I;
-    
-    I = Irefl * F + (1.0 - F) * Irefr;
+	if(REFRACTION)
+	{
+		float4 Irefr;
+		N = normalize(IN.mNormal);
+		x = IN.wPos - lastCenter;
+		V  = (IN.wPos - cameraPos);
+		depth = 0;
+		while(depth < MAX_RAY_DEPTH)
+		{
+		  float3 R;  
+		   //R = normalize(reflect( V, N));
+		     
+		  
+		  float ri = refIndex;
+		  
+		  if(dot(V,N) > 0)
+		  {
+			ri = 1.0 / ri;
+			N = -N;	    
+		  }
+		  R = refract( V, N, ri);
+		  if(dot(R,R) == 0) R = reflect( V, N); 
+		  
+					
+		   float3 Nl;
+		   float4 Il;
+		   l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, min1, min2, Il, Nl);
+		   if(Il.a == 0)
+		   {
+			   depth += 1;		 
+		   }
+		   else
+		   {
+			   I = Il;
+			   depth = MAX_RAY_DEPTH;
+		   }
+		   x = l;	   
+		   N = normalize(Nl);
+		   V = R;	   	   
+		}
+		
+		if(I.a == 0)
+		   I = readCubeMap(CubeMap, l);
+		Irefr = I;
+	    
+		I = Irefl * F + (1.0 - F) * Irefr;
+    }
  	return I;
 }
@@ -364,11 +395,16 @@
 							uniform float3 lastCenter,
 							uniform float SingleBounce,
-							uniform float refIndex):COLOR
-{
+							uniform float refIndex,
+							uniform float4 min1,
+							uniform float4 min2
+							):COLOR
+{
+    min1.x = 1.0 / min1.x;
+    min2.x = 1.0 / min2.x;
 	float4 Color = 1.0;
 	if(SingleBounce == 1)
-	 Color = SingleReflectionPS(IN,cameraPos, CubeMap, DistanceMap, NormDistMap1,NormDistMap2,lastCenter,refIndex);
+	 Color = SingleReflectionPS(IN,cameraPos, CubeMap, DistanceMap, NormDistMap1,NormDistMap2,lastCenter,refIndex, min1.x, min2.x);
 	else
-	 Color = MultipleReflectionPS(IN,cameraPos, CubeMap, DistanceMap, NormDistMap1,NormDistMap2,lastCenter,refIndex);
+	 Color = MultipleReflectionPS(IN,cameraPos, CubeMap, DistanceMap, NormDistMap1,NormDistMap2,lastCenter,refIndex, min1.x, min2.x);
 	 
 	return Color;
