Index: /GTP/trunk/App/Demos/Illum/Ogre/Media/materials/scripts/GlassHead.material
===================================================================
--- /GTP/trunk/App/Demos/Illum/Ogre/Media/materials/scripts/GlassHead.material	(revision 1691)
+++ /GTP/trunk/App/Demos/Illum/Ogre/Media/materials/scripts/GlassHead.material	(revision 1699)
@@ -144,5 +144,5 @@
 			{
 				attenuation	0
-				photonmap_resolution 128
+				photonmap_resolution 32
 				caustic_cubemap_resolution 256	
 				caustic_map_material	GameTools/CauTri
Index: /GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/GameTools_Localized_EnvMap.hlsl
===================================================================
--- /GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/GameTools_Localized_EnvMap.hlsl	(revision 1691)
+++ /GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/GameTools_Localized_EnvMap.hlsl	(revision 1699)
@@ -47,6 +47,6 @@
 }*/
 
-#define LIN_ITERATIONCOUNT 8
-#define SECANT_ITERATIONCOUNT 3
+#define LIN_ITERATIONCOUNT 5
+#define SECANT_ITERATIONCOUNT 0
 
 float3 Hit(float3 x, float3 R, samplerCUBE mp)
@@ -55,54 +55,44 @@
 	float3 xNorm = normalize(x);
 	float3 dt = (R - xNorm) / (float) LIN_ITERATIONCOUNT;
-	//float3 pN = normalize(cross(R, xNorm));
-	//float3 N = normalize(cross(pN,R));
-	//float d = dot(x, N);
-	
-	float3 under = x;
-	float3 over = R;
-	float dUnder = readDistanceCubeMap( mp, x);
-	float dOver = readDistanceCubeMap( mp, R);
-	
-	for(int i = 0; i < LIN_ITERATIONCOUNT; i++)
+	float dx = length(cross(R,x));
+	
+	float3 pN = normalize(cross(R, xNorm));
+	float3 N = normalize(cross(pN,R));
+	float d = dot(x, N);	
+	
+	float3 under = xNorm * readDistanceCubeMap(mp, x);
+	float3 over = R * readDistanceCubeMap(mp, R);
+	
+	//linear iteration	
+	for(int i = 1; i < LIN_ITERATIONCOUNT; i++)
 	{
-		float3 dir = normalize(x + dt * i);
+		float3 dir = normalize(xNorm + dt * i);
 		float dist = readDistanceCubeMap( mp, dir);
 		float3 point = dir * dist;
-		
-		float t = length(x) / dot(dir,xNorm);
-		
-		if(dist > t) //undershooting
-		{
-			under = normalize(point);
-			dUnder = dist;
+				
+		if( dot(N, point) > d)//undershooting
+		{
+			under = point;			
 		}
 		else				//overshooting
 		{
-			over = normalize(point);
-			dOver = dist;
+			over = point;			
 			i = LIN_ITERATIONCOUNT;
-		}
-		
-	/*
-		
-		if( dot(N, point) > d)//undershooting
-			under = normalize(point);
-		else				//overshooting
-		{
-			over = normalize(point);
-			i = LIN_ITERATIONCOUNT;
-		}
-	*/
+		}	
 	}
 	
-	float r1 = length(x) * ctan(acos(dot(under, xNorm)));
-	float r2 = length(x) * ctan(acos(dot(over, xNorm)));
-	float rl = dUnder;
-	
-	float ppp = dOver /  readDistanceCubeMap( mp, x);			// |p|/|p|
-	float dun = 0, pun = ppp, dov = 0, pov = 0;
-	float dl = rl * ( 1 - ppp );							// eq. 2
-	float3 l = x + R * dl;									// ray equation
-	
+	float3 dirUn = normalize(under);
+	float3 dirOv = normalize(over);
+	float tun = d / dot(N, dirUn);
+	float pun = tun / length(under);
+	float dun = length(tun * dirUn - x);
+	float tov = d / dot(N, dirOv);
+	float pov = tov / length(over);
+	float dov = length(tov * dirOv - x);
+	float tl = (d - dot(N, over)) / dot(N, under - over);
+	float3 l = over + tl * (under - over);
+	float dl = length(l - x);
+		
+	//secant iteration
 	for( int i = 0; i < SECANT_ITERATIONCOUNT; i++ )
 	{
@@ -111,16 +101,18 @@
 		{
 			dun = dl; pun = llp;							// last undershooting
-			dl += ( dov == 0 ) ? rl * ( 1 - llp ) :			// eq. 2
-				( dl - dov ) * ( 1 - llp ) / ( llp - pov );	// eq. 3
+			dl += ( dl - dov ) * ( 1 - llp ) / ( llp - pov );	// eq. 3
 		} else if ( llp > 1.001f )							// overshooting
 		{
 			dov = dl; pov = llp;							// last overshooting
-			dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
+			dl += ( dl - dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
 		}
 		l = x + R * dl;										// ray equation
 	}
 	
+	return l;		
+	return over;	
 	return (under + over)/2.0;
 }
+
 
 /*
