Index: GTP/trunk/App/Demos/Illum/EnvMap/EnvMap.fx
===================================================================
--- GTP/trunk/App/Demos/Illum/EnvMap/EnvMap.fx	(revision 1556)
+++ GTP/trunk/App/Demos/Illum/EnvMap/EnvMap.fx	(revision 1573)
@@ -459,10 +459,17 @@
 /// \brief Calculates diffuse or specular contributions of the 5 "most important" texels of #SmallEnvironmentMap to the current point.
 /// For these texels, function GetContr(int,float3,float3,float3,float3) is called.
-float4 GetContibution(float3 L1, float3 L2, float3 L3, float3 L4, float3 pos, float3 N, float d)
-{
+float4 GetContibution(float3 L, float3 L1, float3 L2, float3 L3, float3 L4, float3 pos, float3 N, samplerCUBE cubemap)
+{
+	float d;
+	d = texCUBE(cubemap, L).a;	
+	//d = texCUBE(cubemap, L1).a;	
 	L1 = d * normalize(L1);
+	//d = texCUBE(cubemap, L2).a;	
 	L2 = d * normalize(L2);
-	L3 = d *  normalize(L3);
-	L4 = d * normalize(L4);	
+	//d = texCUBE(cubemap, L3).a;	
+	L3 = d * normalize(L3);
+	//d = texCUBE(cubemap, L4).a;	
+	L4 = d * normalize(L4);
+		
 	
     float3 r1 = normalize(L1 - pos);    
@@ -470,19 +477,5 @@
     float3 r3 = normalize(L3 - pos);
     float3 r4 = normalize(L4 - pos);
-   
-    
-	float kd = 0.3;	// 0.3
-	/*
-	float3 R;
-	R = cross(r1, r2);
-	float tri1 = asin(length(R)) * dot(R, N);
-	R = cross(r2, r3);
-	float tri2 = asin(length(R)) * dot(R, N);
-	R = cross(r3, r4);
-	float tri3 = asin(length(R)) * dot(R, N);
-	R = cross(r4, r1);
-	float tri4 = asin(length(R)) * dot(R, N);
-	*/
-	
+       	
 	float tri1 = acos(dot(r1, r2)) * dot(cross(r1, r2), N);
 	float tri2 = acos(dot(r2, r3)) * dot(cross(r2, r3), N);
@@ -493,4 +486,7 @@
 	//return tri1 + tri2 + tri3 + tri4;	
 }
+
+
+
 
 float4 EnvMapDiffuseLocalizedNewPS( _EnvMapVS_output IN ) : COLOR			
@@ -511,13 +507,14 @@
 	float3 L1, L2, L3, L4, L;						
 	float4 Le;										
-	float d;										
 	float width = 1.0 / M;							
-	
-	for (int x = 0; x < M; x++)			
-	 for (int y = 0; y < M; y++)											
+	float width2 = width * 2;
+	float d; 
+	
+	for (float x = 0.5; x < M; x++)			
+	 for (float y = 0.5; y < M; y++)											
 	 {																
 		float2 p, tpos; 
-	    tpos.x = (x + 0.5) * width;	// 0..1
-	    tpos.y = (y + 0.5) * width;	// 0..1
+	    tpos.x = x * width;
+	    tpos.y = y * width;
 	    
 	    p = tpos.xy;    
@@ -530,16 +527,15 @@
 		L = float3(p.x, p.y, 1);
 		Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
-		d = texCUBE(SmallEnvironmentMapSampler, L).a;	
 		
-		I += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, d);			
-		//I += Le / 16.0;
-	 }																			
+		I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);			
 			
-	for (int x = 0; x < M; x++)			
-	 for (int y = 0; y < M; y++)											
+	}																			
+	
+	for (float x = 0; x < M; x++)			
+	 for (float y = 0; y < M; y++)											
 	 {																
 		float2 p, tpos; 
-	    tpos.x = (x + 0.5) * width;	// 0..1
-	    tpos.y = (y + 0.5) * width;	// 0..1
+	    tpos.x = x * width;	// 0..1
+	    tpos.y = y * width;	// 0..1
 	    
 	    p = tpos.xy;    
@@ -552,16 +548,14 @@
 		L = float3(p.x, p.y, -1);
 		Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
-		d = texCUBE(SmallEnvironmentMapSampler, L).a;	
 		
-		I += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, d);			
-		//I += Le / 16.0;
+		I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);			
 	 }	
 	 
-	for (int x = 0; x < M; x++)			
-	 for (int y = 0; y < M; y++)											
+	for (float x = 0; x < M; x++)			
+	 for (float y = 0; y < M; y++)											
 	 {																
 		float2 p, tpos; 
-	    tpos.x = (x + 0.5) * width;	// 0..1
-	    tpos.y = (y + 0.5) * width;	// 0..1
+	    tpos.x = x * width;	// 0..1
+	    tpos.y = y * width;	// 0..1
 	    
 	    p = tpos.xy;    
@@ -574,16 +568,14 @@
 		L = float3(p.x, 1, p.y);
 		Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
-		d = texCUBE(SmallEnvironmentMapSampler, L).a;	
 		
-		I += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, d);			
-		//I += Le / 16.0;
+		I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);			
 	 }		
 	 
-	for (int x = 0; x < M; x++)			
-	 for (int y = 0; y < M; y++)											
+	for (float x = 0; x < M; x++)			
+	 for (float y = 0; y < M; y++)											
 	 {																
 		float2 p, tpos; 
-	    tpos.x = (x + 0.5) * width;	// 0..1
-	    tpos.y = (y + 0.5) * width;	// 0..1
+	    tpos.x = x * width;	// 0..1
+	    tpos.y = y * width;	// 0..1
 	    
 	    p = tpos.xy;    
@@ -596,15 +588,14 @@
 		L = float3(p.x, -1, p.y);
 		Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
-		d = texCUBE(SmallEnvironmentMapSampler, L).a;	
 		
-		I += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, d);			
-		//I += Le / 16.0;
+		I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);			
 	 }
-	 for (int x = 0; x < M; x++)			
-	 for (int y = 0; y < M; y++)											
-	 {																
+	 
+	 for (float x = 0; x < M; x++)			
+		for (float y = 0; y < M; y++)											
+		{																
 		float2 p, tpos; 
-	    tpos.x = (x + 0.5) * width;	// 0..1
-	    tpos.y = (y + 0.5) * width;	// 0..1
+	    tpos.x = x * width;	// 0..1
+	    tpos.y = y * width;	// 0..1
 	    
 	    p = tpos.xy;    
@@ -617,16 +608,14 @@
 		L = float3(1, p.x, p.y);
 		Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
-		d = texCUBE(SmallEnvironmentMapSampler, L).a;	
 		
-		I += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, d);			
-		//I += Le / 16.0;
-	 }
+		I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);			
+	}
 	 
-	 for (int x = 0; x < M; x++)			
-	 for (int y = 0; y < M; y++)											
+	for (float x = 0; x < M; x++)			
+	 for (float y = 0; y < M; y++)											
 	 {																
 		float2 p, tpos; 
-	    tpos.x = (x + 0.5) * width;	// 0..1
-	    tpos.y = (y + 0.5) * width;	// 0..1
+	    tpos.x = x * width;	// 0..1
+	    tpos.y = y * width;	// 0..1
 	    
 	    p = tpos.xy;    
@@ -639,13 +628,63 @@
 		L = float3(-1, p.x, p.y);
 		Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
-		d = texCUBE(SmallEnvironmentMapSampler, L).a;	
 		
-		I += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, d);			
-		//I += Le / 16.0;
-	 }																					
+		I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);				
+	 }																				
 	return intensity * I;															
 }
 
-
+float4 P2PContr(float3 N, float3 Nl, float3 pos, float3 L, samplerCUBE cubemap)
+{
+	Nl = normalize(Nl);
+	L = normalize(L);
+	float4 Le = float4(texCUBE(cubemap, L).rgb, 1);
+	float d = texCUBE(cubemap, L).a;
+	float3 Lpos = L * d;
+	float3 Ldir = Lpos - pos;
+	float dist = dot(Ldir, Ldir);
+	Ldir = normalize(Ldir);
+	
+	return Le * max(dot(N, Ldir),0) * dot(Nl, -1 * Ldir) / dist;	
+}
+
+float4 EnvMapDiffuseP2PPS( _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;																				
+													
+    float4 I = 0;									
+	float3 L;						
+	float4 Le;										
+	float width = 1.0 / M;
+	float d;							
+	
+	for (float x = 0.5; x < M; x++)			
+	 for (float y = 0.5; 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
+	    	    	    
+		I += P2PContr(N, float3(0,0,-1), pos, float3(p.x, p.y, 1), SmallEnvironmentMapSampler);
+		I += P2PContr(N, float3(0,0,1), pos, float3(-p.x, p.y, -1), SmallEnvironmentMapSampler);
+		I += P2PContr(N, float3(-1,0,0), pos, float3(1, p.y, -p.x), SmallEnvironmentMapSampler);
+		I += P2PContr(N, float3(1,0,0), pos, float3(-1, p.y, p.x), SmallEnvironmentMapSampler);
+		I += P2PContr(N, float3(0,-1,0), pos, float3(p.x, 1, -p.y), SmallEnvironmentMapSampler);
+		I += P2PContr(N, float3(0,1,0), pos, float3(p.x, -1, p.y), SmallEnvironmentMapSampler);
+	}
+																		
+	return intensity * I;															
+}
 //--------------------------------------------------------------------------------------
 // Shading the environment
@@ -694,6 +733,6 @@
 float4 IlluminatedScenePS( _IlluminatedSceneVS_output IN ) : COLOR0
 {
-    float3 color = objColor;// * tex2D(DecorationSampler, IN.TexCoord);
-    /*
+    float3 color = objColor * tex2D(DecorationSampler, IN.TexCoord);
+   /* 
     if (iShowCubeMap > 0) 
     {
@@ -710,6 +749,6 @@
 		color *= abs(pow(dot(L,N), 4)) * brightness;
 	}
-	else color *= 0.7;
-	*/	
+	else color *= 0.7;*/
+		
 	float dist = length( IN.Position );
     return float4(color, dist);	
@@ -745,4 +784,5 @@
 
 TechniqueUsingCommonVS( EnvMapDiffuseLocalizedNew );
+TechniqueUsingCommonVS( EnvMapDiffuseP2P );
 //TechniqueUsingCommonVS( EnvMapDiffuseLocalizedNew4 );
 //TechniqueUsingCommonVS( EnvMapDiffuseLocalizedNew8 );
