DXSDK里光照模型的讲解依然停留在DX9的固定管线时代; DX10及DX11的文档里只是API文档,居然一点基于Shader的光照模型讲解文章都未见,失望.
无奈, 打开NV官网,找到这样一篇文章, 基于Cg语言的Shader标准光照模型讲解, Bingo!
了解标准光照基础知识后,就不会面对MAX的材质系统, Maya的Node Based Material System感到陌生.
这里贴下这组光照模型的Cg代码
float4x4 matViewProjection;
float3 GlobalAmbient;
float3 LightColor;
float3 LightPosition;
float4 EyePosition;
float3 Ke;
float3 Ka;
float3 Kd;
float3 Ks;
float Shininess;
void vs_main(
in float4 InPosition : POSITION,
in float3 InNormal : NORMAL,
out float4 OutPosition : POSITION,
out float4 OutColor : COLOR0
)
{
OutPosition = mul( InPosition, matViewProjection );
float3 P = InPosition.xyz;
float3 N = InNormal;
// Compute the emissive term
float3 Emissive = Ke;
// Compute the ambient term
float3 Ambient = Ka * GlobalAmbient;
// Compute the diffuse term
float3 L = normalize( LightPosition - P );
float DiffuseLight = max( dot( N, L ), 0 );
float3 Diffuse = Kd * LightColor * DiffuseLight;
// Compute the specular term
float3 V = normalize( EyePosition - P );
float3 H = normalize( L + V );
float SpecularLight = pow( max( dot( N, H ), 0 ), Shininess );
if ( DiffuseLight <= 0 ) SpecularLight = 0;
float3 Specular = Ks * LightColor * SpecularLight;
OutColor.xyz = Emissive + Ambient + Diffuse + Specular;
OutColor.w = 1;
}
float4 ps_main( float4 OutColor : COLOR0 ) : COLOR0
{
return( OutColor );
}