string description = "LS3D Diffuse | Specular | Normal | Lightmap | Incendescense | 1 Point Light | Shader";

//------------------------------------
/*
float4x4	Model					: World;
float4x4	InvModel				: WorldInverse;
float4x4	ModelViewProjection		: WorldViewProjection;
float4x4	InvView					: ViewInverse;
*/

shared float4x4	ModelViewProjection;
shared float4x4	Model;
shared float4x4	InvModel;
shared float4x4	InvView;

int			CullMode = 1;

texture DiffMap0 : Diffuse
<
	string ResourceName = "C:\\projects\\boxer\\coding\\metal2_colAlpha.dds";
>;

texture DiffMap1 : Diffuse
<
	string ResourceName = "C:\\projects\\boxer\\coding\\lightmap3.dds";
>;

texture DiffMap2 : Diffuse
<
	string ResourceName = "C:\\projects\\boxer\\coding\\lightmap3.dds";
>;

texture DiffMap3 : Diffuse
<
	string ResourceName = "C:\\projects\\boxer\\coding\\metal2_specnormal.dds";
>;

float4 MatSpecular : Specular
<
	string UIName = "Material Specular Color";
	string Space = "material";
> = {1.0f, 1.0f, 1.0f, 1.0f};

float MatSpecularPower : SpecularPower
<
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 128.0;
    string UIName = "Shinyness";
> = 30.0;

float Intensity0
<
	string UIWidget = "slider";
	string UIName = "Lightmap Ambient";
    float UIMin = 0.0;
    float UIMax = 4.0;
> = 0.0f;

float Intensity1
<
	string UIWidget = "slider";
	string UIName = "Specular Intensity";
    float UIMin = 0.0;
    float UIMax = 4.0;
> = 1.0f;

float BumpScale
<
	string UIWidget = "slider";
	string UIName = "Bump Scale";
    float UIMin = -20.0;
    float UIMax = 20.0;
> = 2.0f;

float4 lightPosition1 : Position
<
    string UIName = "Light Position 1";
    string Object = "PointLight";    
    string Space = "World";
> = { 0.0f, 10.0f, 0.0f, 0.0f};

float4 lightColor1 : Diffuse
<
    string UIName = "Light Color 1";
> = { 1.0f, 1.0f, 1.0f, 1.0f };

float lightIntensity1
<
    string UIName = "Light Intensity 1";
> = { 1.0f };

float lightFalloffRange1
<
    string UIName = "Light Falloff Range 1";
    float UIMin = 0.001;
> = { 100.0f };

float4   LightAmbient;
/*
float4 LightAmbient : Ambient
<
    string UIName = "Ambient Light";
    string Object = "AmbientLight";
> = {0.0f, 0.0f, 0.0f, 1.0f};
*/

//------------------------------------
sampler diffuseMapSampler = sampler_state 
{
    texture = <DiffMap0>;
    AddressU  = Wrap;        
    AddressV  = Wrap;
    AddressW  = Wrap;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};

sampler lightMapSampler = sampler_state 
{
    texture = <DiffMap1>;
    AddressU  = Wrap;        
    AddressV  = Wrap;
    AddressW  = Wrap;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};

sampler incendescenseMapSampler = sampler_state 
{
    texture = <DiffMap2>;
    AddressU  = Wrap;        
    AddressV  = Wrap;
    AddressW  = Wrap;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};

sampler normalSpecMapSampler = sampler_state 
{
    texture = <DiffMap3>;
    AddressU  = Wrap;        
    AddressV  = Wrap;
    AddressW  = Wrap;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};

//------------------------------------
struct vertexInput {
    float4 position			: POSITION;
    float3 normal			: NORMAL;
	float3 tangent			: TANGENT; 
    float2 uv0				: TEXCOORD0;
    float2 uv1				: TEXCOORD1;
};

struct vertexOutput {
    float4	position     	: POSITION;
    float2	uv0          	: TEXCOORD0;
    float2	uv1          	: TEXCOORD1;
    float3	primLightVec 	: TEXCOORD2;
    float3	primHalfVec  	: TEXCOORD3;
    float	lightDistance	: TEXCOORD4;
   	float3	worldNormal		: TEXCOORD5;
    float3	worldTangent	: TEXCOORD6;
    float3	worldBinormal	: TEXCOORD7;    
 };

//------------------------------------
vertexOutput VS_TransformAndTexture( vertexInput IN ) 
{
    vertexOutput OUT;
  
  	float4x4 worldInverseTranspose = transpose( InvModel );
  
	//calculate our vectors N, E, L, and H (world space)
	float3 worldEyePos = InvView[3].xyz;
	float3 worldVertPos = mul( IN.position, Model ).xyz;

	float4 N = normalize( mul( IN.normal, worldInverseTranspose ) );
	float4 T = normalize( mul( IN.tangent, worldInverseTranspose ) );
	float3 E = normalize( worldEyePos - worldVertPos ); //eye vector
	float3 L = lightPosition1.xyz - worldVertPos; //light vector;
   	OUT.lightDistance = saturate( ( lightFalloffRange1 - length( L ) ) / lightFalloffRange1 );
   	L = normalize( L );
	float3 H = normalize( E + L ); //half angle vector

    // compute the binormal
    float3 binormal = normalize( cross( N, T ) );
    float3x3 tangentMatrix = float3x3( IN.tangent, binormal, N.rgb );

	OUT.primLightVec = L;
	OUT.primHalfVec  = H;

    OUT.position = mul( float4( IN.position.xyz , 1.0 ), ModelViewProjection );
    OUT.uv0 = IN.uv0;
    OUT.uv1 = IN.uv1;

	OUT.worldNormal = N;
	OUT.worldBinormal = binormal;
	OUT.worldTangent = T;

    return OUT;
}

//-----------------------------------
float4 PS_Textured( vertexOutput IN ): COLOR
{
	float4 diffAlphaVal = tex2D( diffuseMapSampler, IN.uv0 );
	float4 lightMapVal = tex2D( lightMapSampler, IN.uv1 );
	float4 incendescenseMapVal = tex2D( incendescenseMapSampler, IN.uv0 );
	float4 normSpecVal = tex2D( normalSpecMapSampler, IN.uv0 );
	float specularVal = normSpecVal.a;

	float3 normalVal = ( normSpecVal.rgb - 0.5 ) * BumpScale;
    normalVal = IN.worldNormal + ( normalVal.x * IN.worldTangent + normalVal.y * IN.worldBinormal );
    normalVal = normalize( normalVal );
    

    // compute light intensities
    float lightIntensity = lightIntensity1 * IN.lightDistance;
    
    float dotLight = dot( normalVal, IN.primLightVec );
    float primDiffIntensity = saturate( dotLight ) * lightIntensity;
    float primSpecIntensity = pow( saturate ( dot( normalVal, IN.primHalfVec ) ), MatSpecularPower ) *
    						Intensity1 * lightIntensity;
	
    // compute light colors
    float4 diffColor = diffAlphaVal * primDiffIntensity * lightColor1;
    float4 specColor = MatSpecular * lightColor1 * primSpecIntensity * specularVal;
 
    float4 resultColor;
    resultColor =
    	diffColor * lightMapVal * 2 +				// diffuse (including lightmap)
		specColor +									// specular
		incendescenseMapVal;						// incendescense

    resultColor.a = diffAlphaVal.a;

    return resultColor;
}

//-----------------------------------
technique textured
{
    pass p0
    {
/*
		Zenable = true;
		ZWriteEnable = true;
		AlphaBlendEnable = true;
		SrcBlend  = 5;
		DestBlend = 6;
*/
		CullMode = <CullMode>;
		VertexShader = compile vs_2_0 VS_TransformAndTexture();
		PixelShader  = compile ps_2_0 PS_Textured();
    }
}