锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
1錛屽皢effect婧愪唬鐮佹枃浠舵坊鍔犲埌欏圭洰涓紝姣斿鏄?Basic.fx
2錛屽湪Solution Explorer涓夋嫨Basic.fx錛?鍙抽敭錛岄夋嫨"Properties",
3錛屽湪General欏甸潰涓紝閫夋嫨Tool 涓?“Custom Build Tool”
4錛屽湪Custom Build Step欏甸潰涓紝緙栬緫Command Line涓?
fxc /Tfx_2_0 /Fo$(OutDir)/$(SafeInputName).fxo $(InputFileName)
濡傛灉緙栧啓鐨勬槸鐫鑹插櫒鑰屼笉鏄痚ffect錛屽垯瑕佹敞鎰忓湪fxc鍛戒護涓繕瑕佹寚瀹氱潃鑹插櫒鐨勫叆鍙e嚱鏁般俧xc鐨勫叿浣撶敤娉曞弬鑰僑DK鏂囨。銆?nbsp;
5錛岀紪杈慜utputs涓?br> $(OutDir)/$(SafeInputName).fxo
6錛岀紪璇戦」鐩紝榪欐椂IDE浼氬Basic.fx榪涜緙栬瘧錛屽鍚孋++鐨勭紪璇戜竴鏍鳳紝緙栬瘧鐨勭粨鏋滀篃浼氬湪IDE鐨刼utput紿楀彛鎵撳嵃鍑烘潵銆?br>
struct VS_INPUT
{
float4 Position : POSITION0;
float2 Texcoord : TEXCOORD0;
float3 Normal : NORMAL0;//閫氬父鍙渶瑕佷袱涓悜閲忥紝鍥犱負鍙︿竴涓彲浠ュ弶涔樺緱鍒?br> float3 Binormal : BINORMAL0;
float3 Tangent : TANGENT0;
};
struct VS_OUTPUT
{
float4 Position : POSITION0;
float2 Texcoord : TEXCOORD0;
float3 ViewDirection : TEXCOORD1;
float3 LightDirection: TEXCOORD2;
};
VS_OUTPUT vs_main( VS_INPUT Input )
{
VS_OUTPUT Output;
Output.Position = mul( Input.Position, matViewProjection );
Output.Texcoord = Input.Texcoord;
float3 fvObjectPosition = mul( Input.Position, matView );
float3 fvViewDirection = fvEyePosition - fvObjectPosition;
float3 fvLightDirection = fvLightPosition - fvObjectPosition;
float3 fvNormal = mul( Input.Normal, matView );
float3 fvTangent = mul( Input.Tangent, matView );
float3 fvBinormal = mul( Input.Binormal, matView );//閫氬父杈撳叆鍙渶瑕乶ornal鍜宼angent錛宐inormal鍙互鐢變袱鑰呭弶涔樺緱鍒?br> //fvBinormal = cross( fvNormal, fvTangent );
//灝嗚鏂瑰悜鍜屽厜綰挎柟鍚戦兘杞崲鍒版硶綰跨┖闂達紙鎴栬呯О鍒囩嚎絀洪棿錛?nbsp;
Output.ViewDirection.x = dot( fvTangent, fvViewDirection );
Output.ViewDirection.y = dot( fvBinormal, fvViewDirection );
Output.ViewDirection.z = dot( fvNormal, fvViewDirection );
Output.LightDirection.x = dot( fvTangent, fvLightDirection );
Output.LightDirection.y = dot( fvBinormal, fvLightDirection );
Output.LightDirection.z = dot( fvNormal, fvLightDirection );
return( Output );
}
//Pixel Shader
float4 fvAmbient;
float4 fvSpecular;
float4 fvDiffuse;
float fSpecularPower;
sampler2D baseMap;
sampler2D bumpMap;
struct PS_INPUT
{
float2 Texcoord : TEXCOORD0;
float3 ViewDirection : TEXCOORD1;
float3 LightDirection: TEXCOORD2;
};
float4 ps_main( PS_INPUT Input ) : COLOR0
{
float3 fvLightDirection = normalize( Input.LightDirection );
float3 fvNormal = normalize( ( tex2D( bumpMap, Input.Texcoord ).xyz * 2.0f ) - 1.0f );
float fNDotL = dot( fvNormal, fvLightDirection );
float3 fvReflection = normalize( ( ( 2.0f * fvNormal ) * ( fNDotL ) ) - fvLightDirection );
float3 fvViewDirection = normalize( Input.ViewDirection );
float fRDotV = max( 0.0f, dot( fvReflection, fvViewDirection ) );
float4 fvBaseColor = tex2D( baseMap, Input.Texcoord );
float4 fvTotalAmbient = fvAmbient * fvBaseColor;
float4 fvTotalDiffuse = fvDiffuse * fNDotL * fvBaseColor;
float4 fvTotalSpecular = fvSpecular * pow( fRDotV, fSpecularPower );
return( saturate( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular ) );
}
//let P = v1 - v0
D3DXVECTOR3 P = v1.pos - v0.pos;
//let Q = v2 - v0
D3DXVECTOR3 Q = v2.pos - v0.pos;
float s1 = v1.s - v0.s;
float t1 = v1.t - v0.t;
float s2 = v2.s - v0.s;
float t2 = v2.t - v0.t; 
//we need to solve the equation
// P = s1*T + t1*B
// Q = s2*T + t2*B
// for T and B

//this is a linear system with six unknowns and six equatinos, for TxTyTz BxByBz
//[px,py,pz] = [s1,t1] * [Tx,Ty,Tz]
// qx,qy,qz s2,t2 Bx,By,Bz
//multiplying both sides by the inverse of the s,t matrix gives
//[Tx,Ty,Tz] = 1/(s1t2-s2t1) * [t2,-t1] * [px,py,pz]
// Bx,By,Bz -s2,s1 qx,qy,qz 
//solve this for the unormalized T and B to get from tangent to object space

float tmp = 0.0f;
if(fabsf(s1*t2 - s2*t1) <= 0.0001f)

{
tmp = 1.0f;
}
else

{
tmp = 1.0f/(s1*t2 - s2*t1 );
}
tangent.x = (t2*P.x - t1*Q.x);
tangent.y = (t2*P.y - t1*Q.y);
tangent.z = (t2*P.z - t1*Q.z);
tangent = tmp * tangent;
binormal.x = (s1*Q.x - s2*P.x);
binormal.y = (s1*Q.y - s2*P.y);
binormal.z = (s1*Q.z - s2*P.z);
binormal = tmp * binormal;
inline bool floatEqual(float a, float b)

{
return abs(a-b) < 0.00001f;
}
HRESULT ComputerTangent(D3DXVECTOR3 position[3], D3DXVECTOR3 normal[3], D3DXVECTOR2 texcoord[3],D3DXVECTOR3 oTangent[3])

{
D3DXVECTOR3 edge1;
D3DXVECTOR3 edge2;
D3DXVECTOR3 crossP;
//==============================================
// x, s, t
// S & T vectors get used several times in this vector,
// but are only computed once.
//==============================================
edge1.x = position[1].x - position[0].x;
edge1.y = texcoord[1].x - texcoord[0].x;// s-vector - don't need to compute this multiple times
edge1.z = texcoord[1].y - texcoord[0].y;// t-vector
edge2.x = position[2].x - position[0].x;
edge2.y = texcoord[2].x - texcoord[0].x;// another s-vector
edge2.z = texcoord[2].y - texcoord[0].y;// another t-vector
D3DXVec3Cross(&crossP,&edge1,&edge2);
D3DXVec3Normalize(&crossP,&crossP);
bool degnerateUVTangentPlane = floatEqual(crossP.x, 0.0f);
if(degnerateUVTangentPlane)
crossP.x = 1.0f;
float tanX = -crossP.y / crossP.x;
oTangent[0].x = tanX;
oTangent[1].x = tanX;
oTangent[2].x = tanX;
//--------------------------------------------------------
// y, s, t
//--------------------------------------------------------
edge1.x = position[1].y - position[0].y;
edge2.x = position[2].y - position[0].y;
edge2.y = texcoord[2].x - texcoord[0].x;// another s-vector
edge2.z = texcoord[2].y - texcoord[0].y;// another t-vector
D3DXVec3Cross(&crossP,&edge1,&edge2);
D3DXVec3Normalize(&crossP,&crossP);
degnerateUVTangentPlane = floatEqual(crossP.x, 0.0f);
if(degnerateUVTangentPlane)
crossP.x = 1.0f;
float tanY = -crossP.y / crossP.x;
oTangent[0].y = tanY;
oTangent[1].y = tanY;
oTangent[2].y = tanY;
//------------------------------------------------------
// z, s, t
//------------------------------------------------------
edge1.x = position[1].z - position[0].z;
edge2.x = position[2].z - position[0].z;
edge2.y = texcoord[2].x - texcoord[0].x;// another s-vector
edge2.z = texcoord[2].y - texcoord[0].y;// another t-vector
D3DXVec3Cross(&crossP,&edge1,&edge2);
D3DXVec3Normalize(&crossP,&crossP);
degnerateUVTangentPlane = floatEqual(crossP.x, 0.0f);
if(degnerateUVTangentPlane)
crossP.x = 1.0f;
float tanZ = -crossP.y / crossP.x;
oTangent[0].z = tanZ;
oTangent[1].z = tanZ;
oTangent[2].z = tanZ;
//------------------------------------------------------
for( int i = 0; i < 3; i++)
{
// Ortho-normalize to normal
float dot = D3DXVec3Dot(&oTangent[i],&normal[i]);
oTangent[i] -= normal[i] * dot;
// Normalize tangents
D3DXVec3Normalize(&oTangent[i],&oTangent[i]);
}
return S_OK;
}