#include "common.h"
#include "DX11\tess.h"

//if you use ccw then corresponding coefs are w, v, u
//if you use cw then corresponding coefs are u, v, w
[domain("tri")]
v2p_bumped main( HS_CONSTANT_DATA_OUTPUT input, 
             float3 uvw : SV_DomainLocation,
             const OutputPatch<p_bumped, 3> bp )
{
    v2p_bumped output;
	
	//interpolate in screen space
	//uvw = uvw*input.www/dot(uvw,input.www);
	
	float u = uvw.x;
	float v = uvw.y;
	float w = uvw.z;

	output.tcdh	    = bp[0].tcdh*w + bp[1].tcdh*v + bp[2].tcdh*u;
	output.position	= bp[0].position*w + bp[1].position*v + bp[2].position*u;
	float3 M1		= bp[0].M1*w + bp[1].M1*v + bp[2].M1*u; output.M1 = M1;
	float3 M2		= bp[0].M2*w + bp[1].M2*v + bp[2].M2*u; output.M2 = M2;
	float3 M3		= bp[0].M3*w + bp[1].M3*v + bp[2].M3*u; output.M3 = M3;
	float3	Normal	= normalize(float3(M1.z, M2.z, M3.z));

#ifdef USE_TDETAIL
	output.tcdbump	= bp[0].tcdbump*w + bp[1].tcdbump*v + bp[2].tcdbump*u;
#endif
#ifdef USE_LM_HEMI
	output.lmh		= bp[0].lmh*w + bp[1].lmh*v + bp[2].lmh*u;
#endif

#if TESS_PN
	float3 N[3] =
	{
		float3(bp[0].M1.z, bp[0].M2.z, bp[0].M3.z),
		float3(bp[1].M1.z, bp[1].M2.z, bp[1].M3.z),
		float3(bp[2].M1.z, bp[2].M2.z, bp[2].M3.z)
	};
	
	float3 P[3] = 
	{
		bp[0].position.xyz,
		bp[1].position.xyz,
		bp[2].position.xyz
	};

	ComputePatchVertex(P, N, uvw, input.patch, output.position.xyz, Normal);
#endif

#if TESS_HM
#	ifdef USE_TDETAIL
	ComputeDisplacedVertex(output.position.xyz, Normal, output.tcdh, output.tcdbump);
#	else
	ComputeDisplacedVertex(output.position.xyz, Normal, output.tcdh, 0);
#	endif
#endif
	
	//output.M1.z = Normal.x;
	//output.M2.z = Normal.y;
	//output.M3.z = Normal.z;
	output.hpos	= mul(m_P, float4(output.position.xyz,1));

    return output;
}