//====== Copyright © 1996-2005, Valve Corporation, All rights reserved. =======
//
// Purpose: 
//
//=============================================================================

//  STATIC: "MODEL"				"0..1"

//	DYNAMIC: "COMPRESSED_VERTS"	"0..1"
//	DYNAMIC: "DOWATERFOG"		"0..1"
//	DYNAMIC: "SKINNING"			"0..1"
//  DYNAMIC: "MORPHING"			"0..1" [vs30]

#include "common_vs_fxc.h"

static const bool g_bSkinning		= SKINNING ? true : false;
static const int g_FogType			= DOWATERFOG;

#ifdef SHADER_MODEL_VS_3_0
// NOTE: cMorphTargetTextureDim.xy = target dimensions,
//		 cMorphTargetTextureDim.z = 4tuples/morph
const float3 cMorphTargetTextureDim			: register( SHADER_SPECIFIC_CONST_6 );
const float4 cMorphSubrect					: register( SHADER_SPECIFIC_CONST_7 );

sampler2D morphSampler : register( D3DVERTEXTEXTURESAMPLER0, s0 );
#endif

struct VS_INPUT
{
	float4 vPos						: POSITION;
	float4 vBoneWeights				: BLENDWEIGHT;
	float4 vBoneIndices				: BLENDINDICES;
	float4 vNormal					: NORMAL;
	float4 vBaseTexCoord			: TEXCOORD0;
	float4 vUserData				: TANGENT;

	// Position and normal/tangent deltas
	float3 vPosFlex					: POSITION1;
	float3 vNormalFlex				: NORMAL1;

#ifdef SHADER_MODEL_VS_3_0
	float vVertexID					: POSITION2;
#endif
};

struct VS_OUTPUT
{
    float4 vSetupProjPos			: POSITION;	
	float  fFog						: FOG;
	float2 vBaseTexCoord			: TEXCOORD0;
	float3x3 tangentSpaceTranspose	: TEXCOORD1;
	//	     second row				: TEXCOORD2;
	//	     third row				: TEXCOORD3;
	float3 worldPos					: TEXCOORD4;
	float3 projPos					: TEXCOORD5;
	float4 lightAtten				: TEXCOORD6;
	float3 vRefract					: TEXCOORD7;
};

VS_OUTPUT main( const VS_INPUT v )
{
	VS_OUTPUT o = ( VS_OUTPUT )0;

	float4 vPosition = v.vPos;
	float3 vNormal;
	float4 vTangent;

	DecompressVertex_NormalTangent( v.vNormal, v.vUserData, vNormal, vTangent );

#if !defined( SHADER_MODEL_VS_3_0 ) || !MORPHING
	ApplyMorph( v.vPosFlex, v.vNormalFlex, vPosition.xyz, vNormal, vTangent.xyz );
#else
	ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect, v.vVertexID, float3( 0, 0, 0 ),
		vPosition.xyz, vNormal, vTangent.xyz );
#endif

	// Perform skinning
	float3 worldNormal, worldPos, worldTangentS, worldTangentT;
	SkinPositionNormalAndTangentSpace( g_bSkinning, vPosition, vNormal, vTangent,
										v.vBoneWeights, v.vBoneIndices, worldPos,
										worldNormal, worldTangentS, worldTangentT );

	// Always normalize since flex path is controlled by runtime
	// constant not a shader combo and will always generate the normalization
	worldNormal   = normalize( worldNormal );
	worldTangentS = normalize( worldTangentS );
	worldTangentT = normalize( worldTangentT );

	// Projected position
	float4 vProjPos = mul( float4( worldPos, 1 ), cViewProj );
	o.vSetupProjPos = vProjPos;

	// Map projected position to the refraction texture
	float2 vRefractPos;
	vRefractPos.x = vProjPos.x;
	vRefractPos.y = -vProjPos.y; // invert Y
	vRefractPos = (vRefractPos + vProjPos.w) * 0.5f;

	// Scalar light attenuation
	o.lightAtten.x = GetVertexAttenForLight( worldPos, 0 );
	o.lightAtten.y = GetVertexAttenForLight( worldPos, 1 );
	o.lightAtten.z = GetVertexAttenForLight( worldPos, 2 );
	o.lightAtten.w = GetVertexAttenForLight( worldPos, 3 );

	// Compute fog based on the position
	float3 vWorldPos = mul( v.vPos, cModel[0] );
#if !defined( _X360 )
	o.fFog = CalcFog( vWorldPos, vProjPos, FOGTYPE_RANGE );
#endif

	// World position
	o.worldPos = worldPos;

	// Refract position
	o.projPos = float3(vRefractPos.x, vRefractPos.y, vProjPos.w);

	// Tranform bump coordinates
	o.vBaseTexCoord = v.vBaseTexCoord;

	// Tangent space transform
	o.tangentSpaceTranspose[0] = float3( worldTangentS.x, worldTangentT.x, worldNormal.x );
	o.tangentSpaceTranspose[1] = float3( worldTangentS.y, worldTangentT.y, worldNormal.y );
	o.tangentSpaceTranspose[2] = float3( worldTangentS.z, worldTangentT.z, worldNormal.z );

	return o;
}
