//
// You are free to use this code in part or whole in any way you seem fit!
// If you find this code useful we would to know how,
// you can find us at www.spellofplay.com feel free to drop by!
//

texture g_texture0 : texture0;

float4x4 g_viewMat;
float4x4 g_projMat;
float4x4 g_worldMat;

float4x4 g_wvMatrix;
float4x4 g_wvpMatrix : WorldViewProjection; // World * View * Projection matrix

void CalcMatrices() {
	g_wvMatrix = mul(g_worldMat, g_viewMat);
	g_wvpMatrix = mul(g_wvMatrix, g_projMat);
}


sampler g_diffuseMap = sampler_state {
   Texture = (g_texture0);
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
   AddressU = CLAMP;
   AddressV = CLAMP;
};

struct PERPIXEL_VS_OUTPUT { 
	float4 m_pos   : POSITION;
	float2 m_uv		: TEXCOORD0;
	float4 m_color	: COLOR0;
};

PERPIXEL_VS_OUTPUT PerPixel_VS_Main(float4 a_pos  : POSITION,  float2 a_uv : TEXCOORD0, float4 a_color : COLOR0) {
   PERPIXEL_VS_OUTPUT Out = (PERPIXEL_VS_OUTPUT) 0;
   
   CalcMatrices();
   
   // Output transformed position:
   Out.m_pos = mul(a_pos, g_wvpMatrix);
	Out.m_uv = a_uv;

	Out.m_color.r = a_color.b;
	Out.m_color.g = a_color.g;
	Out.m_color.b = a_color.r;
	Out.m_color.a = a_color.a;
    
	return Out;
}

float4 PerPixel_PS_Main(float2 a_uv : TEXCOORD0, float4 a_color : COLOR0) : COLOR { 

   float4 finalColor = tex2D(g_diffuseMap, a_uv) * a_color;
   return finalColor;
}

technique PerPixel {

   pass Single_Pass {
		ZFunc = LESS;
   	ZEnable = TRUE;
   	ZWriteEnable = FALSE;
      SrcBlend = ONE;
      DestBlend = ONE;
      FogEnable = FALSE;
      Lighting = FALSE;
      AlphaBlendEnable = TRUE;
      CullMode = NONE;
      VertexShader = compile vs_1_1 PerPixel_VS_Main();
      PixelShader = compile ps_2_0 PerPixel_PS_Main();
   }
}


