// WS30 FRAGMENT SHADER

// -*-C++-*-
#version 130
#extension GL_EXT_texture_array : enable

varying vec3 normal;

uniform sampler2D landclass;
uniform sampler2DArray atlas;
uniform sampler1D dimensionsArray;
uniform sampler1D diffuseArray;
uniform sampler1D specularArray;

// Passed from VPBTechnique, not the Effect
uniform float tile_width;
uniform float tile_height;

// See include_fog.frag
uniform int fogType;
vec3 fog_Func(vec3 color, int type);

void main()
{
	vec3 lightDir = gl_LightSource[0].position.xyz;
    vec3 halfVector = gl_LightSource[0].halfVector.xyz;
    vec4 texel;
    vec4 fragColor;

    // The Landclass for this particular fragment.  This can be used to
	// index into the atlas textures.
    int lc = int(texture2D(landclass, gl_TexCoord[0].st).g * 255.0 + 0.5);

    // If gl_Color.a == 0, this is a back-facing polygon and the
    // normal should be reversed.
    vec3 n = (2.0 * gl_Color.a - 1.0) * normal;
    n = normalize(n);
    float NdotL = dot(n, lightDir);
	float NdotHV = max(dot(n, halfVector), 0.0);

	// Different textures have different have different dimensions.
	// Dimensions array is scaled to fit in [0...1.0] in the texture1D, so has to be scaled back up here.
	vec4 color = texture(diffuseArray, float(lc)/512.0) * NdotL;	
	vec4 specular = texture(specularArray, float(lc)/512.0);
	vec2 atlas_dimensions = 10000.0 * texture(dimensionsArray, float(lc)/512.0).st;
	vec2 atlas_scale =  vec2(tile_width / atlas_dimensions.s, tile_height / atlas_dimensions.t );

	texel = texture(atlas, vec3(atlas_scale * gl_TexCoord[0].st, lc));

    fragColor = texel + pow(NdotHV, gl_FrontMaterial.shininess) * gl_LightSource[0].specular * specular;

	fragColor.rgb = fog_Func(fragColor.rgb, fogType);
	gl_FragColor = fragColor;
}