Index: /GTP/trunk/App/Demos/Illum/EnvMap/EnvMap.fx
===================================================================
--- /GTP/trunk/App/Demos/Illum/EnvMap/EnvMap.fx	(revision 1672)
+++ /GTP/trunk/App/Demos/Illum/EnvMap/EnvMap.fx	(revision 2062)
@@ -37,5 +37,8 @@
 float intensity, shininess, brightness;
 
-
+float4 readCubeMap(samplerCUBE cm, float3 dir)
+{
+ return texCUBElod(cm, float4(dir, 0));
+}
 //--------------------------------------------------------------------------------------
 // Textures & texture samplers
@@ -165,10 +168,10 @@
 	// diffuse
 	if (shininess <= 0)	
-		return 0.2 * fcos * texCUBE( SmallEnvironmentMapSampler, L);
+		return 0.2 * fcos * readCubeMap( SmallEnvironmentMapSampler, L);
 	else
 	{
 		// some ad-hoc formula to avoid darkening
 		float brightness = (pow(shininess,0.8)*0.2);
-		return brightness * pow(fcos, shininess) * texCUBE( SmallEnvironmentMapSampler, L);
+		return brightness * pow(fcos, shininess) * readCubeMap( SmallEnvironmentMapSampler, L);
 	}
 }
@@ -289,10 +292,11 @@
 	IN.Normal = normalize( IN.Normal ); 
 	
-	float3 R = reflect(IN.View, IN.Normal);
+	//float3 R = reflect(IN.View, IN.Normal);
+	float3 R = refract(IN.View, IN.Normal, 1);
 
 	if (shininess <= 0)	// diffuse
-		return intensity * texCUBE(PreconvolvedEnvironmentMapSampler, IN.Normal) *2; 		
+		return intensity * readCubeMap(PreconvolvedEnvironmentMapSampler, IN.Normal) *2; 		
 	else				// specular
-		return intensity * texCUBE(PreconvolvedEnvironmentMapSampler, R) *2;	
+		return intensity * readCubeMap(PreconvolvedEnvironmentMapSampler, R) *2;	
 }
 
@@ -322,5 +326,5 @@
 
 	//Lin
-	float4 Lin = texCUBE(SmallEnvironmentMapSampler, L);
+	float4 Lin = readCubeMap(SmallEnvironmentMapSampler, L);
 
 	//dw
@@ -330,5 +334,5 @@
 
 	//r
-	float doy = texCUBE(SmallEnvironmentMapSampler, L).a;
+	float doy = readCubeMap(SmallEnvironmentMapSampler, L).a;
 	float dxy = length(pos - L * doy);
 
@@ -413,5 +417,5 @@
 /// \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 EnvMapDiffuseLocalized5TexPS( _EnvMapVS_output IN ) : COLOR
 {
@@ -443,4 +447,5 @@
 		offset2 = float3(0,0,1);						// select x,z
 	} 
+	
 
 	I += GetContr( LR_CUBEMAP_SIZE, q, IN.Position, IN.Normal, IN.View );
@@ -451,7 +456,8 @@
 	
 	// since only 5 texels are considered, the result gets darker.
-	// LR_CUBEMAP_SIZE is present to compensate this.
+	// LR_CUBEMAP_SIZE is present to compensate this.	
 	return intensity * I * LR_CUBEMAP_SIZE / 2;		
-}
+	
+}*/
 
 // Method #3
@@ -462,12 +468,12 @@
 {
 	float d;
-	//d = texCUBE(cubemap, L).a;	
-	d = texCUBE(cubemap, L1).a;	
+	//d = readCubeMap(cubemap, L).a;	
+	d = readCubeMap(cubemap, L1).a;	
 	L1 = d * normalize(L1);
-	d = texCUBE(cubemap, L2).a;	
+	d = readCubeMap(cubemap, L2).a;	
 	L2 = d * normalize(L2);
-	d = texCUBE(cubemap, L3).a;	
+	d = readCubeMap(cubemap, L3).a;	
 	L3 = d * normalize(L3);
-	d = texCUBE(cubemap, L4).a;	
+	d = readCubeMap(cubemap, L4).a;	
 	L4 = d * normalize(L4);
 		
@@ -520,5 +526,5 @@
 	
 	//return	reference_pos;
-	//return  texCUBE(SmallEnvironmentMapSampler, pos);	
+	//return  readCubeMap(SmallEnvironmentMapSampler, pos);	
 												
 	float3 N =IN.Normal;																				
@@ -547,5 +553,5 @@
 		L4 = float3(p.x, p.y + width2, 1);
 		L = float3(p.x + width, p.y + width, 1);
-		Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
+		Le = float4(readCubeMap(SmallEnvironmentMapSampler, L).rgb, 1);
 		
 		I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);			
@@ -568,5 +574,5 @@
 		L1 = float3(p.x, p.y + width2, -1);
 		L = float3(p.x + width, p.y + width, -1);
-		Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
+		Le = float4(readCubeMap(SmallEnvironmentMapSampler, L).rgb, 1);
 		
 		I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);			
@@ -588,5 +594,5 @@
 		L1 = float3(p.x, 1, p.y + width2);			
 		L = float3(p.x + width, 1, p.y + width);
-		Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
+		Le = float4(readCubeMap(SmallEnvironmentMapSampler, L).rgb, 1);
 		
 		I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);			
@@ -608,5 +614,5 @@
 		L4 = float3(p.x, -1, p.y + width2);			
 		L = float3(p.x + width, -1, p.y + width);
-		Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
+		Le = float4(readCubeMap(SmallEnvironmentMapSampler, L).rgb, 1);
 		
 		I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);			
@@ -628,5 +634,5 @@
 		L4 = float3(1, p.x, p.y + width2);	
 		L = float3(1, p.x + width, p.y + width);
-		Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
+		Le = float4(readCubeMap(SmallEnvironmentMapSampler, L).rgb, 1);
 		
 		I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);			
@@ -648,5 +654,5 @@
 		L1 = float3(-1, p.x, p.y + width2);	
 		L = float3(-1, p.x + width, p.y + width);
-		Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
+		Le = float4(readCubeMap(SmallEnvironmentMapSampler, L).rgb, 1);
 		
 		I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);				
@@ -659,6 +665,6 @@
 	Nl = normalize(Nl);
 	L = normalize(L);
-	float4 Le = float4(texCUBE(cubemap, L).rgb, 1);
-	float d = texCUBE(cubemap, L).a;
+	float4 Le = float4(readCubeMap(cubemap, L).rgb, 1);
+	float d = readCubeMap(cubemap, L).a;
 	float3 Lpos = L * d;
 	float3 Ldir = Lpos - pos;
@@ -678,5 +684,5 @@
 	
 	//return	reference_pos;
-	//return  texCUBE(SmallEnvironmentMapSampler, pos);	
+	//return  readCubeMap(SmallEnvironmentMapSampler, pos);	
 												
 	float3 N =IN.Normal;																				
@@ -709,4 +715,47 @@
 }
 
+float4 EnvMapDiffuseLocalized5TexPS( _EnvMapVS_output IN ) : COLOR
+{
+	IN.View = -normalize( IN.View );
+	IN.Normal = normalize( IN.Normal ); 
+	// translate reference point to the origin
+	IN.Position -= reference_pos.xyz;		
+	
+	float3 R = -reflect( IN.View, IN.Normal );		// reflection direction
+	
+    float4 I = 0;
+
+	float3 q;
+	if ( shininess <= 0 )
+		q = IN.Normal;					// diffuse 
+	else
+		q = R;
+		
+    float rr = max( max(abs(q.x), abs(q.y)), abs(q.z) );	// select the largest component
+    q /= rr;	// scale the largest component to value +/-1
+    
+    float3 offset1 = float3(1,0,0);						// default: largest: z
+    float3 offset2 = float3(0,1,0);						// select: x,y
+    
+    if (abs(q.x) > abs(q.y) && abs(q.x) > abs(q.z)) {	// largest: x
+		offset1 = float3(0,0,1);						// select y,z
+	} 
+    if (abs(q.y) > abs(q.x) && abs(q.y) > abs(q.z)) {	// largest: y
+		offset2 = float3(0,0,1);						// select x,z
+	} 
+	
+
+	I += GetContr( LR_CUBEMAP_SIZE, q, IN.Position, IN.Normal, IN.View );
+	I += GetContr( LR_CUBEMAP_SIZE, q + offset1*(2.0/LR_CUBEMAP_SIZE), IN.Position, IN.Normal, IN.View );
+	I += GetContr( LR_CUBEMAP_SIZE, q - offset1*(2.0/LR_CUBEMAP_SIZE), IN.Position, IN.Normal, IN.View );
+	I += GetContr( LR_CUBEMAP_SIZE, q + offset2*(2.0/LR_CUBEMAP_SIZE), IN.Position, IN.Normal, IN.View );
+	I += GetContr( LR_CUBEMAP_SIZE, q - offset2*(2.0/LR_CUBEMAP_SIZE), IN.Position, IN.Normal, IN.View );
+	
+	// since only 5 texels are considered, the result gets darker.
+	// LR_CUBEMAP_SIZE is present to compensate this.	
+	return intensity * I * LR_CUBEMAP_SIZE / 2;		
+	
+}
+/*
 float4 EnvMapDiffuseAdaptPS( _EnvMapVS_output IN ) : COLOR			
 {		
@@ -718,5 +767,5 @@
 	
 	//return	reference_pos;
-	//return  texCUBE(SmallEnvironmentMapSampler, pos);	
+	//return  readCubeMap(SmallEnvironmentMapSampler, pos);	
 												
 	float3 N =IN.Normal;																				
@@ -742,5 +791,5 @@
 	    
 	    L = float3(p.x + width, p.y + width, 1);
-		Ld = texCUBE(SmallEnvironmentMapSampler, L).a;
+		Ld = readCubeMap(SmallEnvironmentMapSampler, L).a;
 		
 		float dist = length(normalize(L) * Ld - pos);
@@ -751,5 +800,5 @@
 			L3 = float3(p.x + width2, p.y + width2, 1);	
 			L4 = float3(p.x, p.y + width2, 1);
-			Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
+			Le = float4(readCubeMap(SmallEnvironmentMapSampler, L).rgb, 1);
 		
 			I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);
@@ -771,5 +820,5 @@
 	    
 	    L = float3(p.x + width, p.y + width, -1);
-		Ld = texCUBE(SmallEnvironmentMapSampler, L).a;
+		Ld = readCubeMap(SmallEnvironmentMapSampler, L).a;
 		
 		float dist = length(normalize(L) * Ld - pos);
@@ -780,5 +829,5 @@
 			L2 = float3(p.x + width2, p.y + width2, -1);	
 			L1 = float3(p.x, p.y + width2, -1);
-			Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
+			Le = float4(readCubeMap(SmallEnvironmentMapSampler, L).rgb, 1);
 			
 			I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);			
@@ -799,5 +848,5 @@
 	    
 	    L = float3(p.x + width, 1, p.y + width);
-		Ld = texCUBE(SmallEnvironmentMapSampler, L).a;
+		Ld = readCubeMap(SmallEnvironmentMapSampler, L).a;
 		
 		float dist = length(normalize(L) * Ld - pos);
@@ -808,5 +857,5 @@
 			L2 = float3(p.x + width2, 1, p.y + width2);	
 			L1 = float3(p.x, 1, p.y + width2);			
-			Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
+			Le = float4(readCubeMap(SmallEnvironmentMapSampler, L).rgb, 1);
 			
 			I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);			
@@ -828,5 +877,5 @@
 	    
 	    L = float3(p.x + width, -1, p.y + width);
-		Ld = texCUBE(SmallEnvironmentMapSampler, L).a;
+		Ld = readCubeMap(SmallEnvironmentMapSampler, L).a;
 		
 		float dist = length(normalize(L) * Ld - pos);
@@ -837,5 +886,5 @@
 			L3 = float3(p.x + width2, -1, p.y + width2);	
 			L4 = float3(p.x, -1, p.y + width2);			
-			Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
+			Le = float4(readCubeMap(SmallEnvironmentMapSampler, L).rgb, 1);
 			
 			I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);			
@@ -857,5 +906,5 @@
 	    
 		L = float3(1, p.x + width, p.y + width);
-		Ld = texCUBE(SmallEnvironmentMapSampler, L).a;
+		Ld = readCubeMap(SmallEnvironmentMapSampler, L).a;
 		float dist = length(normalize(L) * Ld - pos);
 		if(dist < 1.0)
@@ -865,5 +914,5 @@
 			L3 = float3(1, p.x + width2, p.y + width2);	
 			L4 = float3(1, p.x, p.y + width2);	
-			Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
+			Le = float4(readCubeMap(SmallEnvironmentMapSampler, L).rgb, 1);
 			
 			I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);			
@@ -885,5 +934,5 @@
 	    
 	    L = float3(-1, p.x + width, p.y + width);
-		Ld = texCUBE(SmallEnvironmentMapSampler, L).a;
+		Ld = readCubeMap(SmallEnvironmentMapSampler, L).a;
 		float dist = length(normalize(L) * Ld - pos);
 		if(dist < 1.0)
@@ -893,5 +942,5 @@
 			L2 = float3(-1, p.x + width2, p.y + width2);	
 			L1 = float3(-1, p.x, p.y + width2);	
-			Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
+			Le = float4(readCubeMap(SmallEnvironmentMapSampler, L).rgb, 1);
 			
 			I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);				
@@ -902,5 +951,5 @@
 	 }																				
 	return intensity * I;															
-}
+}*/
 //--------------------------------------------------------------------------------------
 // Shading the environment
@@ -955,5 +1004,5 @@
 		// if one of the cube maps should be displayed on the walls,
 		// display it
-	    color = texCUBE(EnvironmentMapSampler, IN.Position) * intensity;
+	    color = readCubeMap(EnvironmentMapSampler, IN.Position) * intensity;
     }
     else if (brightness>0)
@@ -1001,5 +1050,5 @@
 TechniqueUsingCommonVS( EnvMapDiffuseLocalizedNew );
 TechniqueUsingCommonVS( EnvMapDiffuseP2P );
-TechniqueUsingCommonVS( EnvMapDiffuseAdapt );
+//TechniqueUsingCommonVS( EnvMapDiffuseAdapt );
 
 #define ReduceTextureTechnique(M);									\
