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 1525)
+++ GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/GameTools_Diffuse.hlsl	(revision 1535)
@@ -103,5 +103,5 @@
 }
 
-
+/*
 float4 DiffusePS( vertOUT IN,
            uniform float3 cameraPos,
@@ -112,4 +112,185 @@
             ) : COLOR0
 {
+    REDUCED_CUBEMAP_SIZE = 4;
+	float width = 1.0 /REDUCED_CUBEMAP_SIZE;
+   
+    normalize( V );
+    float3 V = normalize(IN.wPos - cameraPos);	// 
+	float3 N = mul( float4(IN.mNormal,1), world_IT).xyz;
+    //float3 N = IN.mNormal;
+    N = normalize( N ); 
+	float3 R = reflect(V, N);
+    float3 pos = IN.wPos - lastCenter;
+     
+    float4 intens = 0;
+    //intens = readCubeMap(SmallEnvMapSampler, pos);
+ 	
+ 	float3 L1, L2, L3, L4, L;
+	float4 Le;
+	float d;
+	  
+ 	for (int x = 0; x < REDUCED_CUBEMAP_SIZE - 1; x++)			// az envmap minden texelére
+	 for (int y = 0; y < REDUCED_CUBEMAP_SIZE - 1; y++)
+	 {
+		float2 p, tpos; 
+	    tpos.x = (x + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
+	    tpos.y = (y + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
+	    
+	    p.x = tpos.x; 
+	    p.y = 1-tpos.y;
+	    p.xy = 2 * p.xy - 1;	// -1..1	// az adott texel középpont pozíciója
+	    	    
+		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);
+		Le = readCubeMap(SmallEnvMapSampler, L);
+		d = readDistanceCubeMap(DistanceEnvMapSampler, L);	
+		intens += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, SmallEnvMapSampler, d);
+	}	
+	
+	for (int x = 0; x < REDUCED_CUBEMAP_SIZE - 1; x++)			// az envmap minden texelére
+	 for (int y = 0; y < REDUCED_CUBEMAP_SIZE - 1; y++)
+	 {
+		float2 p, tpos; 
+	    tpos.x = (x + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
+	    tpos.y = (y + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
+	    
+	    p.x = tpos.x; 
+	    p.y = 1-tpos.y;
+	    p.xy = 2 * p.xy - 1;	// -1..1	// az adott texel középpont pozíciója
+	   	
+		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);
+		Le = readCubeMap(SmallEnvMapSampler, L);
+		d = readDistanceCubeMap(DistanceEnvMapSampler, L);	
+		intens += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, SmallEnvMapSampler, d);
+	}
+	
+	for (int x = 0; x < REDUCED_CUBEMAP_SIZE - 1; x++)			// az envmap minden texelére
+	 for (int y = 0; y < REDUCED_CUBEMAP_SIZE - 1; y++)
+	 {
+		float2 p, tpos; 
+	    tpos.x = (x + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
+	    tpos.y = (y + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
+	    
+	    p.x = tpos.x; 
+	    p.y = 1-tpos.y;
+	    p.xy = 2 * p.xy - 1;	// -1..1	// az adott texel középpont pozíciója
+	   	
+		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);
+		Le = readCubeMap(SmallEnvMapSampler, L);
+		d = readDistanceCubeMap(DistanceEnvMapSampler, L);	
+		intens += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, SmallEnvMapSampler, d);
+	}
+	
+	for (int x = 0; x < REDUCED_CUBEMAP_SIZE - 1; x++)			// az envmap minden texelére
+	 for (int y = 0; y < REDUCED_CUBEMAP_SIZE - 1; y++)
+	 {
+		float2 p, tpos; 
+	    tpos.x = (x + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
+	    tpos.y = (y + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
+	    
+	    p.x = tpos.x; 
+	    p.y = 1-tpos.y;
+	    p.xy = 2 * p.xy - 1;	// -1..1	// az adott texel középpont pozíciója
+	   	
+		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);
+		Le = readCubeMap(SmallEnvMapSampler, L);
+		d = readDistanceCubeMap(DistanceEnvMapSampler, L);	
+		intens += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, SmallEnvMapSampler, d);
+	}
+	
+	for (int x = 0; x < REDUCED_CUBEMAP_SIZE - 1; x++)			// az envmap minden texelére
+	 for (int y = 0; y < REDUCED_CUBEMAP_SIZE - 1; y++)
+	 {
+		float2 p, tpos; 
+	    tpos.x = (x + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
+	    tpos.y = (y + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
+	    
+	    p.x = tpos.x; 
+	    p.y = 1-tpos.y;
+	    p.xy = 2 * p.xy - 1;	// -1..1	// az adott texel középpont pozíciója
+	   	
+		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);
+		Le = readCubeMap(SmallEnvMapSampler, L);
+		d = readDistanceCubeMap(DistanceEnvMapSampler, L);	
+		intens += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, SmallEnvMapSampler, d);
+	}
+	
+	for (int x = 0; x < REDUCED_CUBEMAP_SIZE - 1; x++)			// az envmap minden texelére
+	 for (int y = 0; y < REDUCED_CUBEMAP_SIZE - 1; y++)
+	 {
+		float2 p, tpos; 
+	    tpos.x = (x + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
+	    tpos.y = (y + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
+	    
+	    p.x = tpos.x; 
+	    p.y = 1-tpos.y;
+	    p.xy = 2 * p.xy - 1;	// -1..1	// az adott texel középpont pozíciója
+	   	
+		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);
+		Le = readCubeMap(SmallEnvMapSampler, L);
+		d = readDistanceCubeMap(DistanceEnvMapSampler, L);	
+		intens += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, SmallEnvMapSampler, d);
+	}	
+	
+	
+	//return float4(pos, 1);
+	return intens;
+}
+
+*/
+
+
+float4 DiffusePS( vertOUT IN,
+           uniform float3 cameraPos,
+           uniform float3 lastCenter,	//LI//
+           uniform samplerCUBE SmallEnvMapSampler : register(s0),
+           uniform samplerCUBE DistanceEnvMapSampler : register(s1),
+           uniform float4x4 world_IT
+            ) : COLOR0
+{
+	float3 corner[] = {float3(1,-1,1),//  float3(0,0,1),
+					float3(-1,-1,-1),  // 0,0,-1
+					float3(-1,1,-1),   // 0,1,0
+					float3(-1,-1, 1),  // 0,-1,0
+					float3(1,-1,-1),   // 1,0,0
+					float3(-1,-1, 1)}; // -1,0,0
+					 
+	float3 right[] = {float3(-1,0,0),
+					float3(1,0,0),
+					float3(1,0,0),
+					float3(-1,0,0),
+					float3(0,0,1),
+					float3(0,0,-1)};
+					 
+	float3 up[] = {float3(0,1,0),
+					float3(0,1,0),
+					float3(0,0,1),
+					float3(0,0,1),
+					float3(0,1,0),
+					float3(0,1,0)};
+				 
     REDUCED_CUBEMAP_SIZE = 4;
 	float width = 1.0 /REDUCED_CUBEMAP_SIZE;
@@ -129,133 +310,43 @@
 	float4 Le;
 	float d;
+	  
+ 	for (int x = 0; x < REDUCED_CUBEMAP_SIZE; x++)
+	 for (int y = 0; y < REDUCED_CUBEMAP_SIZE; y++)
+		for (int f = 0; f < 6; f++)
+		{
+		
+			float2 p, tpos; 
+			tpos.x = (x + 0.5) * 2 * width;	// 0..2
+			tpos.y = (y + 0.5) * 2 * width;	// 0..2
+		    
+			p = tpos;			
+		    	    
+			L = corner[0] + p.x * right[0] + p.y * up[0];			
+			L1 = normalize(L - width * right[f] + width * up[f]);
+			L2 = normalize(L + width * right[f] + width * up[f]);	
+			L3 = normalize(L + width * right[f] - width * up[f]);	
+			L4 = normalize(L - width * right[f] - width * up[f]);
+			Le = texCUBE(SmallEnvMapSampler, L);
+			d = texCUBE(DistanceEnvMapSampler, L).r;
+			
+			L1 = d * L1;
+			L2 = d * L2;
+			L3 = d * L3;
+			L4 = d * L4;	
+		
+			float3 r1 = normalize(L1 - pos);    
+			float3 r2 = normalize(L2 - pos);
+			float3 r3 = normalize(L3 - pos);
+			float3 r4 = normalize(L4 - pos); 
 	   
- 	for (int x = 0; x < REDUCED_CUBEMAP_SIZE - 1; x++)			// az envmap minden texelére
-	 for (int y = 0; y < REDUCED_CUBEMAP_SIZE - 1; y++)
-	 {
-		float2 p, tpos; 
-	    tpos.x = (x + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
-	    tpos.y = (y + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
-	    
-	    p.x = tpos.x; 
-	    p.y = 1-tpos.y;
-	    p.xy = 2 * p.xy - 1;	// -1..1	// az adott texel középpont pozíciója
-	    	    
-		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);
-		Le = readCubeMap(SmallEnvMapSampler, L);
-		d = readDistanceCubeMap(DistanceEnvMapSampler, L);	
-		intens += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, SmallEnvMapSampler, d);
-	}	
-	
-	for (int x = 0; x < REDUCED_CUBEMAP_SIZE - 1; x++)			// az envmap minden texelére
-	 for (int y = 0; y < REDUCED_CUBEMAP_SIZE - 1; y++)
-	 {
-		float2 p, tpos; 
-	    tpos.x = (x + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
-	    tpos.y = (y + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
-	    
-	    p.x = tpos.x; 
-	    p.y = 1-tpos.y;
-	    p.xy = 2 * p.xy - 1;	// -1..1	// az adott texel középpont pozíciója
-	   	
-		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);
-		Le = readCubeMap(SmallEnvMapSampler, L);
-		d = readDistanceCubeMap(DistanceEnvMapSampler, L);	
-		intens += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, SmallEnvMapSampler, d);
-	}
-	
-	for (int x = 0; x < REDUCED_CUBEMAP_SIZE - 1; x++)			// az envmap minden texelére
-	 for (int y = 0; y < REDUCED_CUBEMAP_SIZE - 1; y++)
-	 {
-		float2 p, tpos; 
-	    tpos.x = (x + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
-	    tpos.y = (y + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
-	    
-	    p.x = tpos.x; 
-	    p.y = 1-tpos.y;
-	    p.xy = 2 * p.xy - 1;	// -1..1	// az adott texel középpont pozíciója
-	   	
-		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);
-		Le = readCubeMap(SmallEnvMapSampler, L);
-		d = readDistanceCubeMap(DistanceEnvMapSampler, L);	
-		intens += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, SmallEnvMapSampler, d);
-	}
-	
-	for (int x = 0; x < REDUCED_CUBEMAP_SIZE - 1; x++)			// az envmap minden texelére
-	 for (int y = 0; y < REDUCED_CUBEMAP_SIZE - 1; y++)
-	 {
-		float2 p, tpos; 
-	    tpos.x = (x + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
-	    tpos.y = (y + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
-	    
-	    p.x = tpos.x; 
-	    p.y = 1-tpos.y;
-	    p.xy = 2 * p.xy - 1;	// -1..1	// az adott texel középpont pozíciója
-	   	
-		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);
-		Le = readCubeMap(SmallEnvMapSampler, L);
-		d = readDistanceCubeMap(DistanceEnvMapSampler, L);	
-		intens += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, SmallEnvMapSampler, d);
-	}
-	
-	for (int x = 0; x < REDUCED_CUBEMAP_SIZE - 1; x++)			// az envmap minden texelére
-	 for (int y = 0; y < REDUCED_CUBEMAP_SIZE - 1; y++)
-	 {
-		float2 p, tpos; 
-	    tpos.x = (x + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
-	    tpos.y = (y + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
-	    
-	    p.x = tpos.x; 
-	    p.y = 1-tpos.y;
-	    p.xy = 2 * p.xy - 1;	// -1..1	// az adott texel középpont pozíciója
-	   	
-		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);
-		Le = readCubeMap(SmallEnvMapSampler, L);
-		d = readDistanceCubeMap(DistanceEnvMapSampler, L);	
-		intens += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, SmallEnvMapSampler, d);
-	}
-	
-	for (int x = 0; x < REDUCED_CUBEMAP_SIZE - 1; x++)			// az envmap minden texelére
-	 for (int y = 0; y < REDUCED_CUBEMAP_SIZE - 1; y++)
-	 {
-		float2 p, tpos; 
-	    tpos.x = (x + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
-	    tpos.y = (y + 0.5) /(float)REDUCED_CUBEMAP_SIZE;	// 0..1
-	    
-	    p.x = tpos.x; 
-	    p.y = 1-tpos.y;
-	    p.xy = 2 * p.xy - 1;	// -1..1	// az adott texel középpont pozíciója
-	   	
-		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);
-		Le = readCubeMap(SmallEnvMapSampler, L);
-		d = readDistanceCubeMap(DistanceEnvMapSampler, L);	
-		intens += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, SmallEnvMapSampler, d);
-	}	
-	
-	
-	//return float4(pos, 1);
+			float tri1 = acos(dot(r1, r2)) * dot(cross(r1, r2), N);
+			float tri2 = acos(dot(r2, r3)) * dot(cross(r2, r3), N);
+			float tri3 = acos(dot(r3, r4)) * dot(cross(r3, r4), N);
+			float tri4 = acos(dot(r4, r1)) * dot(cross(r4, r1), N);
+		
+		//	intens += Le / 16.0 + pos.x*0.000000000001;
+			intens += 0.5 * Le * max(tri1 + tri2 + tri3 + tri4, 0);
+		}	
+
 	return intens;
 }
