/*********************************************************************NVMH3****

Basic shader
Supports untextured and textured techniques
Used as a replacement for fixed-pipeline draws

******************************************************************************/


float4x4 worldViewProj : WorldViewProjection;
float4x4 viewProj : ViewProjection;
float4x4 world : World;

#include "..\mrt_none.fx"

float ColorModulate : ColorModulate;

struct DirectionalLight
{
    float3 Direction    : DIRECTION;
    float3 Diffuse      : DIFFUSE;
    float3 Specular     : SPECULAR;
};

struct PointLight
{
    float4 Position_Att0     : POSITION_ATT0;
    float4 Diffuse_Att1      : DIFFUSE_ATT1;
    float4 Specular_Att2     : SPECULAR_ATT2;
};

struct Material
{
    float4 Diffuse_Opacity  : DIFFUSE_OPACITY;
    float4 Specular_Power : SPECULAR_POWER;
    float3 Ambient  : AMBIENT;
    float3 Emissive : EMISSIVE;
};

DirectionalLight    dirLights[3]    : DIRLIGHT;
PointLight          pointLights[3]  : POINTLIGHT;
Material            matSettings     : MATERIAL;

float3 ambientLevel : AMBIENT = {0.2, 0.2, 0.2};
float3 campos : CAMERAPOS;

struct vertexInput {
    float4 position				: POSITION;
    float4 diffuse				: COLOR0;
};

struct vertexOutput {
    float4 hPosition		: POSITION;
    float4 diffColor		: COLOR0;
};

struct vertexInputTextured {
    float4 position				: POSITION;
    float4 diffuse				: COLOR0;
    float2 textureCoords	    : TEXCOORD0;
};

struct vertexOutputTextured {
    float4 hPosition		: POSITION;
    float2 textureCoords	: TEXCOORD0;
    float4 diffColor		: COLOR0;    
};



texture diffuseTexture : DiffuseTexture;

sampler DiffuseTextureSampler = sampler_state 
{
    texture = <diffuseTexture>;
    AddressU  = WRAP;        
    AddressV  = WRAP;
    AddressW  = WRAP;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};

vertexOutput VS_Transform(vertexInput IN) 
{
    float4 pos;
    float3 norm;
    float3 tang;
    
    pos = mul(IN.position, world);
    
    vertexOutput OUT;
    OUT.hPosition = mul( pos , viewProj);
    OUT.diffColor = IN.diffuse;
    
    return OUT;
}



vertexOutputTextured VS_TransformTextured(vertexInputTextured IN) 
{
    float4 pos;
    float3 norm;
    float3 tang;
    
    pos = mul(IN.position, world);
    
    vertexOutputTextured OUT;
    OUT.hPosition = mul( pos , viewProj);
  
    OUT.diffColor = IN.diffuse;
    OUT.textureCoords = IN.textureCoords;
    
    return OUT;
}




//-----------------------------------
PS_OUTPUT PS_Untextured( vertexOutput IN)
{
    
    float4 colorSum;
    
    colorSum = IN.diffColor;

//	colorSum.xyz += matSettings.Ambient * ambientLevel;
    colorSum.xyz += ambientLevel;
    colorSum.xyz *= ColorModulate;
  
	PS_OUTPUT Out;	                                                               
	SetPSOutput( Out, colorSum, float4( 0,0,0, 0 ) );
    return Out;
}


//-----------------------------------
PS_OUTPUT PS_Textured( vertexOutputTextured IN)
{
    
    float4 colorSum;
   	float4 diffuseTexture = tex2D( DiffuseTextureSampler, IN.textureCoords );
  
 	colorSum = diffuseTexture * IN.diffColor;

	colorSum.xyz += matSettings.Ambient * ambientLevel;
    colorSum.xyz += ambientLevel;
    colorSum.xyz *= ColorModulate;
    
	PS_OUTPUT Out;
	SetPSOutput( Out, colorSum, float4( 0,0,0, 0 ) );
    return Out;
}
//#define FixedPipe

#ifndef FixedPipe
//-----------------------------------
technique untextured
{
    pass p0 
    {
        VertexShader = compile vs_1_1 VS_Transform();
		PixelShader  = compile ps_2_0 PS_Untextured();
    }
}

//-----------------------------------
technique textured
{
    pass p0 
    {
       	VertexShader = compile vs_1_1 VS_TransformTextured();
		PixelShader  = compile ps_2_0 PS_Textured();
    }
}
#else

//-----------------------------------
technique untextured
{
    pass p0 
    {
      	VertexShader = compile vs_1_1 VS_Transform();
		
		PixelShader  = 0;
		
		ColorOp[0] = SelectArg1; ColorArg1[0] = Diffuse; 
		AlphaOp[0] = SelectArg1; AlphaArg1[0] = Diffuse;
		SpecularEnable = false;
		
		ColorOp[1] = Disable;
		AlphaOp[1] = Disable;
    }
}

//-----------------------------------
technique textured
{
    pass p0 
    {
     	VertexShader = compile vs_1_1 VS_TransformTextured();
		
		
		PixelShader  = 0;
		
		ColorOp[0] = Modulate; ColorArg1[0] = Texture; ColorArg2[0] = Diffuse;
		AlphaOp[0] = Modulate; AlphaArg1[0] = Texture; AlphaArg2[0] = Diffuse;
		SpecularEnable = false;
		Sampler[0] = <DiffuseTextureSampler>;
		
		ColorOp[1] = Disable;
		AlphaOp[1] = Disable;
    }
}


#endif




