Fixed-function Lighting Pipeline
ColorsOutput CalcLighting( float3 worldNormal, float3 worldPos, float3 cameraPos )
{
ColorsOutput output = (ColorsOutput)0.0;
for(int i=0; i<8; i++)
{
//光线方向
float3 toLight = g_lights[i].Position.xyz - worldPos;
//离光源距离
float lightDist = length( toLight );
//atten=(1/(a[2]*d*d+a[1]*d+a[1])
float fAtten = 1.0/dot( g_lights[i].Atten, float4(1,lightDist,lightDist*lightDist,0) );
float3 lightDir = normalize( toLight );
//H
float3 halfAngle = normalize( normalize(-cameraPos) + lightDir );
//Phong方程,逐顶点光照
output.Diffuse += max(0,dot( lightDir, worldNormal ) * g_lights[i].Diffuse * fAtten) + g_lights[i].Ambient;
output.Specular += max(0,pow( dot( halfAngle, worldNormal ), 64 ) * g_lights[i].Specular * fAtten );
}
return output;
}
简单ALPHA测试
//
// PS for rendering with alpha test
//
float4 PSAlphaTestmain(PSSceneIn input) : COLOR0
{
float4 color = tex2D( g_samLinear, g_txDiffuse, input.tex ) * input.colorD;
if( color.a < 0.5 )
discard;
return color;
}
雾化:
//
// Calculates fog factor based upon distance
//
// E is defined as the base of the natural logarithm (2.71828)
float CalcFogFactor( float d )
{
float fogCoeff = 1.0;
if( FOGMODE_LINEAR == g_fogMode )
{
fogCoeff = (g_fogEnd - d)/(g_fogEnd - g_fogStart);
}
else if( FOGMODE_EXP == g_fogMode )
{
fogCoeff = 1.0 / pow( E, d*g_fogDensity );
}
else if( FOGMODE_EXP2 == g_fogMode )
{
fogCoeff = 1.0 / pow( E, d*d*g_fogDensity*g_fogDensity );
}
return clamp( fogCoeff, 0, 1 );
}
Finally, the pixel shader uses the fog factor to determine how much of the original color and how much of the fog color to output to the pixel.
最后,PS使用雾化参数确定雾颜色和纹理颜色的混合程度。
return fog * normalColor + (1.0 - fog)*g_fogColor;