uniform sampler3D SampleTex;

void main(void)
{
	vec3
	  cPos = gl_TexCoord[0].xyz;
	  
	vec3
	  cDeltaX = vec3(gl_TexCoord[0].w * 0.5, 0.0, 0.0),
	  cDeltaY = vec3(0.0, gl_TexCoord[0].w * 0.5, 0.0),  
	  cDeltaZ = vec3(0.0, 0.0, gl_TexCoord[0].w * 0.5);
	  
	vec3 
	  cPos0 = cPos,
	  cPos1 = cPos + cDeltaX * 2.0,
	  cPos2 = cPos + cDeltaX * 4.0,
	  cPos3 = cPos + cDeltaX * 6.0;
	  
	float r = texture3D(SampleTex, cPos0).x;
	float g = texture3D(SampleTex, cPos1).x;
	float b = texture3D(SampleTex, cPos2).x;
	float a = texture3D(SampleTex, cPos3).x;
	
	if (r == 0.0) r = texture3D(SampleTex, cPos0 + cDeltaX).x;
	if (r == 0.0) r = texture3D(SampleTex, cPos0 + cDeltaY).x;
	if (r == 0.0) r = texture3D(SampleTex, cPos0 + cDeltaX + cDeltaY).x;
	if (r == 0.0) r = texture3D(SampleTex, cPos0 + cDeltaZ).x;
	if (r == 0.0) r = texture3D(SampleTex, cPos0 + cDeltaX + cDeltaZ).x;
	if (r == 0.0) r = texture3D(SampleTex, cPos0 + cDeltaY + cDeltaZ).x;
	if (r == 0.0) r = texture3D(SampleTex, cPos0 + cDeltaX + cDeltaY + cDeltaZ).x;

	if (g == 0.0) g = texture3D(SampleTex, cPos1 + cDeltaX).x;
	if (g == 0.0) g = texture3D(SampleTex, cPos1 + cDeltaY).x;
	if (g == 0.0) g = texture3D(SampleTex, cPos1 + cDeltaX + cDeltaY).x;
	if (g == 0.0) g = texture3D(SampleTex, cPos1 + cDeltaZ).x;
	if (g == 0.0) g = texture3D(SampleTex, cPos1 + cDeltaX + cDeltaZ).x;
	if (g == 0.0) g = texture3D(SampleTex, cPos1 + cDeltaY + cDeltaZ).x;
	if (g == 0.0) g = texture3D(SampleTex, cPos1 + cDeltaX + cDeltaY + cDeltaZ).x;

	if (b == 0.0) b = texture3D(SampleTex, cPos2 + cDeltaX).x;
	if (b == 0.0) b = texture3D(SampleTex, cPos2 + cDeltaY).x;
	if (b == 0.0) b = texture3D(SampleTex, cPos2 + cDeltaX + cDeltaY).x;
	if (b == 0.0) b = texture3D(SampleTex, cPos2 + cDeltaZ).x;
	if (b == 0.0) b = texture3D(SampleTex, cPos2 + cDeltaX + cDeltaZ).x;
	if (b == 0.0) b = texture3D(SampleTex, cPos2 + cDeltaY + cDeltaZ).x;
	if (b == 0.0) b = texture3D(SampleTex, cPos2 + cDeltaX + cDeltaY + cDeltaZ).x;

	if (a == 0.0) a = texture3D(SampleTex, cPos3 + cDeltaX).x;
	if (a == 0.0) a = texture3D(SampleTex, cPos3 + cDeltaY).x;
	if (a == 0.0) a = texture3D(SampleTex, cPos3 + cDeltaX + cDeltaY).x;
	if (a == 0.0) a = texture3D(SampleTex, cPos3 + cDeltaZ).x;
	if (a == 0.0) a = texture3D(SampleTex, cPos3 + cDeltaX + cDeltaZ).x;
	if (a == 0.0) a = texture3D(SampleTex, cPos3 + cDeltaY + cDeltaZ).x;
	if (a == 0.0) a = texture3D(SampleTex, cPos3 + cDeltaX + cDeltaY + cDeltaZ).x;

	gl_FragColor = vec4(b, g, r, a);
}

