Index: /GTP/trunk/App/Demos/Illum/EnvMap/.matrix
===================================================================
--- /GTP/trunk/App/Demos/Illum/EnvMap/.matrix	(revision 1574)
+++ /GTP/trunk/App/Demos/Illum/EnvMap/.matrix	(revision 1672)
@@ -1,12 +1,12 @@
 
 World matrix:
-    0.9135     0.2775    -0.2975          0
-   -0.2198      0.952     0.2132          0
-    0.3424    -0.1293     0.9306          0
-         0  5.96e-008      2.399          1
+    0.9997   -0.01646   -0.01694          0
+   0.01642     0.9999    -0.0024          0
+   0.01698   0.002121     0.9999          0
+         0          0      5.524          1
 
 Camera position:
-  0.713843   -0.51149   -2.23277
+ 0.0935908  0.0132558   -5.52339
 
 Mesh position:
- -0.331426   0.385354    1.32215
+ -0.166667          0          0
Index: /GTP/trunk/App/Demos/Illum/EnvMap/Parameters.h
===================================================================
--- /GTP/trunk/App/Demos/Illum/EnvMap/Parameters.h	(revision 1573)
+++ /GTP/trunk/App/Demos/Illum/EnvMap/Parameters.h	(revision 1672)
@@ -14,9 +14,10 @@
 /// Possible values for #iWhichMethod.
 typedef enum { 
-	DIFFUSE_SPECULAR_CLASSIC,		///< Classic environment mapping technique.
-	DIFFUSE_SPECULAR_LOCALIZED,		///< Our proposal.
+	DIFFUSE_SPECULAR_CLASSIC,			///< Classic environment mapping technique.
+	DIFFUSE_SPECULAR_LOCALIZED,			///< Our proposal.
 	DIFFUSE_SPECULAR_LOCALIZED_5TEX,	///< Our proposal, using 5 texel only.
 	DIFFUSE_SPECULAR_LOCALIZED_NEW,		///< Our proposal.	
-	DIFFUSE_SPECULAR_LOCALIZED_P2P		///< Point to point form factor
+	DIFFUSE_SPECULAR_LOCALIZED_P2P,		///< Point to point form factor
+	DIFFUSE_SPECULAR_LOCALIZED_ADAPT	///< Point to point form factor
 } method_t;
 
Index: /GTP/trunk/App/Demos/Illum/EnvMap/.params
===================================================================
--- /GTP/trunk/App/Demos/Illum/EnvMap/.params	(revision 1574)
+++ /GTP/trunk/App/Demos/Illum/EnvMap/.params	(revision 1672)
@@ -1,10 +1,10 @@
 47 version ----- RayTraceEffects: Saved parameters of the algorithm -----
 Bool values:
-0001
+0011
 (Help [F1]; [F]ireballs; Multiple [O]bjects; Between [W]alls only; )
 Float values:
 3 	(WhichMethod [TAB,Q], 0..4)
-7 	(WhichMesh [Home,End], 0..9)
-30 	(Mesh size [Ins,Del], 0..100)
+8 	(WhichMesh [Home,End], 0..9)
+27 	(Mesh size [Ins,Del], 0..100)
 1 	(Resolution [M], 0..3)
 0 	(ShowCubeMap [C], 0..3)
Index: /GTP/trunk/App/Demos/Illum/EnvMap/EnvMap.fx
===================================================================
--- /GTP/trunk/App/Demos/Illum/EnvMap/EnvMap.fx	(revision 1575)
+++ /GTP/trunk/App/Demos/Illum/EnvMap/EnvMap.fx	(revision 1672)
@@ -532,6 +532,6 @@
 	float d; 
 	
-	for (float x = 0.5; x < M; x++)			
-	 for (float y = 0.5; y < M; y++)											
+	for (float x = 0; x < M; x++)			
+	 for (float y = 0; y < M; y++)											
 	 {																
 		float2 p, tpos; 
@@ -542,9 +542,9 @@
 	    p = 2.0 * p - 1.0; //-1..1
 	    	    	    
-		L1 = float3(p.x - width, p.y - width, 1);	
-		L2 = float3(p.x + width, p.y - width, 1);	
-		L3 = float3(p.x + width, p.y + width, 1);	
-		L4 = float3(p.x - width, p.y + width, 1);
-		L = float3(p.x, p.y, 1);
+		L1 = float3(p.x, p.y, 1);	
+		L2 = float3(p.x + width2, p.y, 1);	
+		L3 = float3(p.x + width2, p.y + width2, 1);	
+		L4 = float3(p.x, p.y + width2, 1);
+		L = float3(p.x + width, p.y + width, 1);
 		Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
 		
@@ -553,6 +553,6 @@
 	}																			
 	
-	for (float x = 0.5; x < M; x++)			
-	 for (float y = 0.5; y < M; y++)											
+	for (float x = 0; x < M; x++)			
+	 for (float y = 0; y < M; y++)											
 	 {																
 		float2 p, tpos; 
@@ -563,9 +563,9 @@
 	    p = 2.0 * p - 1.0; //-1..1
 	    	    	    
-		L4 = float3(p.x - width, p.y - width, -1);	
-		L3 = float3(p.x + width, p.y - width, -1);	
-		L2 = float3(p.x + width, p.y + width, -1);	
-		L1 = float3(p.x - width, p.y + width, -1);
-		L = float3(p.x, p.y, -1);
+		L4 = float3(p.x, p.y, -1);	
+		L3 = float3(p.x + width2, p.y, -1);	
+		L2 = float3(p.x + width2, p.y + width2, -1);	
+		L1 = float3(p.x, p.y + width2, -1);
+		L = float3(p.x + width, p.y + width, -1);
 		Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
 		
@@ -573,6 +573,6 @@
 	 }	
 	 
-	for (float x = 0.5; x < M; x++)			
-	 for (float y = 0.5; y < M; y++)											
+	for (float x = 0; x < M; x++)			
+	 for (float y = 0; y < M; y++)											
 	 {																
 		float2 p, tpos; 
@@ -583,9 +583,9 @@
 	    p = 2.0 * p - 1.0; //-1..1
 	    	    	    
-		L4 = float3(p.x - width, 1, p.y - width);
-		L3 = float3(p.x + width, 1, p.y - width);	
-		L2 = float3(p.x + width, 1, p.y + width);	
-		L1 = float3(p.x - width, 1, p.y + width);			
-		L = float3(p.x, 1, p.y);
+		L4 = float3(p.x, 1, p.y);
+		L3 = float3(p.x + width2, 1, p.y);	
+		L2 = float3(p.x + width2, 1, p.y + width2);	
+		L1 = float3(p.x, 1, p.y + width2);			
+		L = float3(p.x + width, 1, p.y + width);
 		Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
 		
@@ -593,6 +593,6 @@
 	 }		
 	 
-	for (float x = 0.5; x < M; x++)			
-	 for (float y = 0.5; y < M; y++)											
+	for (float x = 0; x < M; x++)			
+	 for (float y = 0; y < M; y++)											
 	 {																
 		float2 p, tpos; 
@@ -603,9 +603,9 @@
 	    p = 2.0 * p - 1.0; //-1..1
 	    	    	    
-		L1 = float3(p.x - width, -1, p.y - width);
-		L2 = float3(p.x + width, -1, p.y - width);	
-		L3 = float3(p.x + width, -1, p.y + width);	
-		L4 = float3(p.x - width, -1, p.y + width);			
-		L = float3(p.x, -1, p.y);
+		L1 = float3(p.x, -1, p.y);
+		L2 = float3(p.x + width2, -1, p.y);	
+		L3 = float3(p.x + width2, -1, p.y + width2);	
+		L4 = float3(p.x, -1, p.y + width2);			
+		L = float3(p.x + width, -1, p.y + width);
 		Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
 		
@@ -613,6 +613,6 @@
 	 }
 	 
-	 for (float x = 0.5; x < M; x++)			
-		for (float y = 0.5; y < M; y++)											
+	 for (float x = 0; x < M; x++)			
+		for (float y = 0; y < M; y++)											
 		{																
 		float2 p, tpos; 
@@ -623,9 +623,9 @@
 	    p = 2.0 * p - 1.0; //-1..1
 	    	    	    
-		L1 = float3(1, p.x - width, p.y - width);
-		L2 = float3(1, p.x + width, p.y - width);	
-		L3 = float3(1, p.x + width, p.y + width);	
-		L4 = float3(1, p.x - width, p.y + width);	
-		L = float3(1, p.x, p.y);
+		L1 = float3(1, p.x, p.y);
+		L2 = float3(1, p.x + width2, p.y);	
+		L3 = float3(1, p.x + width2, p.y + width2);	
+		L4 = float3(1, p.x, p.y + width2);	
+		L = float3(1, p.x + width, p.y + width);
 		Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
 		
@@ -633,6 +633,6 @@
 	}
 	 
-	for (float x = 0.5; x < M; x++)			
-	 for (float y = 0.5; y < M; y++)											
+	for (float x = 0; x < M; x++)			
+	 for (float y = 0; y < M; y++)											
 	 {																
 		float2 p, tpos; 
@@ -643,9 +643,9 @@
 	    p = 2.0 * p - 1.0; //-1..1
 	    	    	    
-		L4 = float3(-1, p.x - width, p.y - width);
-		L3 = float3(-1, p.x + width, p.y - width);	
-		L2 = float3(-1, p.x + width, p.y + width);	
-		L1 = float3(-1, p.x - width, p.y + width);	
-		L = float3(-1, p.x, p.y);
+		L4 = float3(-1, p.x, p.y);
+		L3 = float3(-1, p.x + width2, p.y);	
+		L2 = float3(-1, p.x + width2, p.y + width2);	
+		L1 = float3(-1, p.x, p.y + width2);	
+		L = float3(-1, p.x + width, p.y + width);
 		Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
 		
@@ -706,4 +706,199 @@
 	}
 																		
+	return intensity * I;															
+}
+
+float4 EnvMapDiffuseAdaptPS( _EnvMapVS_output IN ) : COLOR			
+{		
+	float M = 4.0;																			
+	IN.View = -normalize( IN.View );												
+	IN.Normal = normalize( IN.Normal );												
+	IN.Position -= reference_pos.xyz;				
+	float3 pos = IN.Position.xyz;	
+	
+	//return	reference_pos;
+	//return  texCUBE(SmallEnvironmentMapSampler, pos);	
+												
+	float3 N =IN.Normal;																				
+	float3 R = -reflect( IN.View, IN.Normal );		
+													
+    float4 I = 0;									
+	float3 L1, L2, L3, L4, L;						
+	float4 Le;
+	float Ld;										
+	float width = 1.0 / M;							
+	float width2 = width * 2;
+	float d; 
+	
+	for (float x = 0; x < M; x++)			
+	 for (float y = 0; y < M; y++)											
+	 {																
+		float2 p, tpos; 
+	    tpos.x = x * width;
+	    tpos.y = y * width;
+	    
+	    p = tpos.xy;    
+	    p = 2.0 * p - 1.0; //-1..1
+	    
+	    L = float3(p.x + width, p.y + width, 1);
+		Ld = texCUBE(SmallEnvironmentMapSampler, L).a;
+		
+		float dist = length(normalize(L) * Ld - pos);
+		if(dist < 1.0)
+		{	    	    
+			L1 = float3(p.x, p.y, 1);	
+			L2 = float3(p.x + width2, p.y, 1);	
+			L3 = float3(p.x + width2, p.y + width2, 1);	
+			L4 = float3(p.x, p.y + width2, 1);
+			Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
+		
+			I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);
+		}
+		else
+			I += P2PContr(N, float3(0,0,-1), pos, L, SmallEnvironmentMapSampler);				
+			
+	}																			
+	
+	for (float x = 0; x < M; x++)			
+	 for (float y = 0; y < M; y++)											
+	 {																
+		float2 p, tpos; 
+	    tpos.x = x * width;	// 0..1
+	    tpos.y = y * width;	// 0..1
+	    
+	    p = tpos.xy;    
+	    p = 2.0 * p - 1.0; //-1..1
+	    
+	    L = float3(p.x + width, p.y + width, -1);
+		Ld = texCUBE(SmallEnvironmentMapSampler, L).a;
+		
+		float dist = length(normalize(L) * Ld - pos);
+		if(dist < 1.0)
+		{	   	    	    
+			L4 = float3(p.x, p.y, -1);	
+			L3 = float3(p.x + width2, p.y, -1);	
+			L2 = float3(p.x + width2, p.y + width2, -1);	
+			L1 = float3(p.x, p.y + width2, -1);
+			Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
+			
+			I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);			
+		}
+		else
+			I += P2PContr(N, float3(0,0,1), pos, L, SmallEnvironmentMapSampler);		
+	 }	
+	 
+	for (float x = 0; x < M; x++)			
+	 for (float y = 0; y < M; y++)											
+	 {																
+		float2 p, tpos; 
+	    tpos.x = x * width;	// 0..1
+	    tpos.y = y * width;	// 0..1
+	    
+	    p = tpos.xy;    
+	    p = 2.0 * p - 1.0; //-1..1
+	    
+	    L = float3(p.x + width, 1, p.y + width);
+		Ld = texCUBE(SmallEnvironmentMapSampler, L).a;
+		
+		float dist = length(normalize(L) * Ld - pos);
+		if(dist < 1.0)
+		{	   	    	    
+			L4 = float3(p.x, 1, p.y);
+			L3 = float3(p.x + width2, 1, p.y);	
+			L2 = float3(p.x + width2, 1, p.y + width2);	
+			L1 = float3(p.x, 1, p.y + width2);			
+			Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
+			
+			I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);			
+		}
+		else
+			I += P2PContr(N, float3(0,-1,0), pos, L, SmallEnvironmentMapSampler);
+		
+	 }		
+	 
+	for (float x = 0; x < M; x++)			
+	 for (float y = 0; y < M; y++)											
+	 {																
+		float2 p, tpos; 
+	    tpos.x = x * width;	// 0..1
+	    tpos.y = y * width;	// 0..1
+	    
+	    p = tpos.xy;    
+	    p = 2.0 * p - 1.0; //-1..1
+	    
+	    L = float3(p.x + width, -1, p.y + width);
+		Ld = texCUBE(SmallEnvironmentMapSampler, L).a;
+		
+		float dist = length(normalize(L) * Ld - pos);
+		if(dist < 1.0)
+		{	    	    
+			L1 = float3(p.x, -1, p.y);
+			L2 = float3(p.x + width2, -1, p.y);	
+			L3 = float3(p.x + width2, -1, p.y + width2);	
+			L4 = float3(p.x, -1, p.y + width2);			
+			Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
+			
+			I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);			
+		}
+		else
+			I += P2PContr(N, float3(0,1,0), pos, L, SmallEnvironmentMapSampler);
+		
+	 }
+	 
+	 for (float x = 0; x < M; x++)			
+		for (float y = 0; y < M; y++)											
+		{																
+		float2 p, tpos; 
+	    tpos.x = x * width;	// 0..1
+	    tpos.y = y * width;	// 0..1
+	    
+	    p = tpos.xy;    
+	    p = 2.0 * p - 1.0; //-1..1
+	    
+		L = float3(1, p.x + width, p.y + width);
+		Ld = texCUBE(SmallEnvironmentMapSampler, L).a;
+		float dist = length(normalize(L) * Ld - pos);
+		if(dist < 1.0)
+		{		    	    
+			L1 = float3(1, p.x, p.y);
+			L2 = float3(1, p.x + width2, p.y);	
+			L3 = float3(1, p.x + width2, p.y + width2);	
+			L4 = float3(1, p.x, p.y + width2);	
+			Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
+			
+			I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);			
+		}
+		else
+			I += P2PContr(N, float3(-1,0,0), pos, L, SmallEnvironmentMapSampler);
+		
+	}
+
+	for (float x = 0; x < M; x++)			
+	 for (float y = 0; y < M; y++)											
+	 {																
+		float2 p, tpos; 
+	    tpos.x = x * width;	// 0..1
+	    tpos.y = y * width;	// 0..1
+	    
+	    p = tpos.xy;    
+	    p = 2.0 * p - 1.0; //-1..1
+	    
+	    L = float3(-1, p.x + width, p.y + width);
+		Ld = texCUBE(SmallEnvironmentMapSampler, L).a;
+		float dist = length(normalize(L) * Ld - pos);
+		if(dist < 1.0)
+		{	    	    
+			L4 = float3(-1, p.x, p.y);
+			L3 = float3(-1, p.x + width2, p.y);	
+			L2 = float3(-1, p.x + width2, p.y + width2);	
+			L1 = float3(-1, p.x, p.y + width2);	
+			Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
+			
+			I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);				
+			
+		}
+		else
+			I += P2PContr(N, float3(1,0,0), pos, L, SmallEnvironmentMapSampler);		
+	 }																				
 	return intensity * I;															
 }
@@ -806,7 +1001,5 @@
 TechniqueUsingCommonVS( EnvMapDiffuseLocalizedNew );
 TechniqueUsingCommonVS( EnvMapDiffuseP2P );
-//TechniqueUsingCommonVS( EnvMapDiffuseLocalizedNew4 );
-//TechniqueUsingCommonVS( EnvMapDiffuseLocalizedNew8 );
-//TechniqueUsingCommonVS( EnvMapDiffuseLocalizedNew16 );
+TechniqueUsingCommonVS( EnvMapDiffuseAdapt );
 
 #define ReduceTextureTechnique(M);									\
Index: /GTP/trunk/App/Demos/Illum/EnvMap/Main.cpp
===================================================================
--- /GTP/trunk/App/Demos/Illum/EnvMap/Main.cpp	(revision 1573)
+++ /GTP/trunk/App/Demos/Illum/EnvMap/Main.cpp	(revision 1672)
@@ -199,5 +199,5 @@
 
 	// SLIDERS
-	pp.Add( iWhichMethod, "WhichMethod [TAB,Q]", 4, VK_TAB, 'Q', noconvert, OnChangeCubeMap );	
+	pp.Add( iWhichMethod, "WhichMethod [TAB,Q]", 5, VK_TAB, 'Q', noconvert, OnChangeCubeMap );	
 	pp.Add( iWhichMesh, "WhichMesh [Home,End]", 9, VK_HOME, VK_END, noconvert, OnChangeMesh );	
 	pp.Add( fMeshScale, "Mesh size [Ins,Del]", 100, VK_DELETE, VK_INSERT, noconvert, OnChangeMeshSize );	
@@ -305,5 +305,5 @@
     #endif
 
-	dwShaderFlags |= D3DXSHADER_SKIPOPTIMIZATION;
+	//dwShaderFlags |= D3DXSHADER_SKIPOPTIMIZATION;
 
 	// ----------- compiling fx file -----------
Index: /GTP/trunk/App/Demos/Illum/EnvMap/EnvMap.cpp
===================================================================
--- /GTP/trunk/App/Demos/Illum/EnvMap/EnvMap.cpp	(revision 1574)
+++ /GTP/trunk/App/Demos/Illum/EnvMap/EnvMap.cpp	(revision 1672)
@@ -297,4 +297,7 @@
 		case DIFFUSE_SPECULAR_LOCALIZED_5TEX:
 			V( g_pEffect->SetTechnique( "EnvMapDiffuseLocalized5Tex" ) );
+			break;
+		case DIFFUSE_SPECULAR_LOCALIZED_ADAPT:
+			V( g_pEffect->SetTechnique( "EnvMapDiffuseAdapt" ) );
 			break;
 	}
Index: /GTP/trunk/App/Demos/Illum/EnvMap/Mesh.cpp
===================================================================
--- /GTP/trunk/App/Demos/Illum/EnvMap/Mesh.cpp	(revision 1488)
+++ /GTP/trunk/App/Demos/Illum/EnvMap/Mesh.cpp	(revision 1672)
@@ -110,4 +110,6 @@
 		exit(-1);
 	}
+//	if(fileName == L"Media\\Objects\\bunny.x")
+//		D3DXSaveMeshToX(L"bunnyasc.x",pMesh,NULL,NULL,NULL,0,D3DXF_FILEFORMAT_TEXT);
 	
 	if ( FAILED( CalculateMeshSize() ))
