uniform sampler2D baseMap;
varying vec2 Texcoord;

uniform float screenX;
uniform float screenY;
uniform float invScreenX;
uniform float invScreenY;

uniform float blockEdge;
uniform int xOffset;
uniform int yOffset;

void main( void )
{  
   float px = Texcoord.x * screenX;
   float py = Texcoord.y * screenY;
   
   float remX;
   float remY;
   
   if (px < xOffset)
   {
		remX = px;	
	}
	else if (px < xOffset + blockEdge)
	{
		remX = px - xOffset;
	}
	else
	{
		remX = xOffset + mod(px, blockEdge);
	}
	
	if (py < yOffset)
   {
		remY = py;	
	}
	else if (py < yOffset + blockEdge)
	{
		remY = py - yOffset;
	}
	else
	{
		remY = yOffset + mod(py, blockEdge);
	}
		
   vec2 blockTopLeft = vec2(px - remX, py - remY);
   vec2 blockTopRight = vec2(px - remX + blockEdge - 1, py - remY);
   vec2 blockBottomRight = vec2(px - remX + blockEdge - 1, py - remY + blockEdge - 1);
   vec2 blockBottomLeft = vec2(px - remX, py - remY + blockEdge - 1);
     
   gl_FragColor = 0.2 * (texture2D(baseMap, Texcoord) +
						texture2D(baseMap, blockTopLeft * vec2(invScreenX, invScreenY)) +  
						texture2D(baseMap, blockTopRight * vec2(invScreenX, invScreenY)) +  
						texture2D(baseMap, blockBottomRight * vec2(invScreenX, invScreenY)) +  
						texture2D(baseMap, blockBottomLeft * vec2(invScreenX, invScreenY)));
}