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

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

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

float4x4 viewProj : ViewProjection;
float4x4 world : World;


#include "..\mrt_none.fx"

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;
}


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



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


// Fixed
vertexOutput VS_TransformFixed(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_TransformTexturedFixed(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;
}


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



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

//-----------------------------------
PS_OUTPUT PS_Untextured( vertexOutput IN)
{
    
    float4 colorSum;
    
    colorSum = IN.diffColor;
  
	PS_OUTPUT Out;	                                                               
	SetPSOutput( Out, colorSum, float4( 0,0,0,0 )/* float4( campos - IN.posWorld.xyz, 0 ) */ );
    return Out;
}


//-----------------------------------
PS_OUTPUT PS_Textured( vertexOutputTextured IN)
{
    
    float4 colorSum;
   	float4 diffuseTexture = tex2D( DiffuseTextureSampler, IN.textureCoords );
  
 	colorSum = diffuseTexture * IN.diffColor;
    
	PS_OUTPUT Out;
	SetPSOutput( Out, colorSum,  float4( 0,0,0,0 )/* float4( campos - IN.posWorld.xyz, 0 ) */ );
    return Out;
}


//-----------------------------------
#ifndef FixedPipe

//-----------------------------------
technique untextured
{
    pass p0 
    {
		VertexShader = compile vs_1_1 VS_Transform();
		PixelShader  = compile ps_1_1 PS_Untextured();
    }
}

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


//-----------------------------------
technique rhwUntextured
{
    pass p0 
    {
        VertexShader = compile vs_1_1 VS_TransformRHW();
		PixelShader  = compile ps_1_1 PS_Untextured();
    }
}


//-----------------------------------
technique rhwTextured
{
    pass p0 
    {
		VertexShader = compile vs_1_1 VS_TransformRHWTextured();
		PixelShader  = compile ps_1_1 PS_Textured();
    }
}
#else
//-----------------------------------
technique untextured
{
    pass p0 
    {
		VertexShader = compile vs_1_1 VS_TransformFixed();
		//PixelShader  = compile ps_1_1 PS_Untextured();
		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_TransformTexturedFixed();
		//PixelShader  = compile ps_1_1 PS_Textured();
		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;
    }
}


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


//-----------------------------------
technique rhwTextured
{
    pass p0 
    {
      	VertexShader = compile vs_1_1 VS_TransformRHWTexturedFixed();
		
		//PixelShader  = compile ps_1_1 PS_Textured();
		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

