#include "ShaderConstantsGNM.h"

struct VS_Input
{
    float3 position : POSITION;
    float4 color : COLOR;
    float2 texCoords : TEXCOORD_0;
};
 
struct PS_Input
{
    float4 position : S_Position;
    float4 color : COLOR;
    float2 texCoords : TEXCOORD_0;

#ifdef ENABLE_FOG
	float4 fogColor : FOG_COLOR;
#endif

#ifdef GLINT
	float2 layer1UV : TEXCOORD_1;
	float2 layer2UV : TEXCOORD_2;
#endif
};

#ifdef GLINT
float2 calculateLayerUV(float2 origUV, float offset, float rotation) {
	float2 uv = origUV;
	uv -= 0.5;
	float rsin = 0.f;
	float rcos = 0.f;
	sincos(rotation, rsin, rcos);
	uv = mul(uv, float2x2(rcos, -rsin, rsin, rcos));
	uv.x += offset;
	uv += 0.5;

	return uv * GLINT_UV_SCALE;
}
#endif

PS_Input main( in VS_Input VSInput )
{
	PS_Input PSInput = (PS_Input)0;

    PSInput.color = VSInput.color;

	PSInput.position = mul(WORLDVIEWPROJ, float4(VSInput.position, 1));
    PSInput.texCoords = VSInput.texCoords;

#ifdef ENABLE_FOG
	//fog
	PSInput.fogColor.rgb = FOG_COLOR.rgb;
	PSInput.fogColor.a = clamp(((PSInput.position.z / RENDER_DISTANCE) - FOG_CONTROL.x) / (FOG_CONTROL.y - FOG_CONTROL.x), 0.0, 1.0);
#endif

#ifdef USE_LIGHTING
	PSInput.color *= float4(TILE_LIGHT_COLOR.rgb, 1.0);
#endif

#ifdef GLINT
	PSInput.layer1UV = calculateLayerUV(VSInput.texCoords, UV_OFFSET.x, UV_ROTATION.x);
	PSInput.layer2UV = calculateLayerUV(VSInput.texCoords, UV_OFFSET.y, UV_ROTATION.y);
#endif

	return PSInput;
}
