# 漫反射与Lambert模型

A=K*I(环境光强)

K为漫反射物体材质的反射系数,取值范围为(0,1)

I1为环境光光强

A=K*I2*Cos(α)(点光源)

K为漫反射物体材质的反射系数,取值范围为(0,1)

I2为点光源光强

α为点光源向量与表面法向量的夹角

A=K*I2*Cos(α)等价于A=K*I2*(n*l)/(|n|*|l|)

struct VertexIn
{
float4 position:POSITION;
float4 normal:NORMAL;
};
struct VertexScreen
{
float4 oPosition:POSITION;
float4 color:COLOR;
};
void main_v(
VertexIn posIn,
out VertexScreen posOut,

uniform float4x4 modelViewProj,
uniform float4x4 worldMatrix,
uniform float4x4 worldMatrix_IT,
uniform float3 globalAmbient,
uniform float3 lightPosition,
uniform float3 lightColor,
uniform float3 Kd)
{
posOut.oPosition=mul(modelViewProj,posIn.position);

float3 worldpos=mul(worldMatrix,posIn.position).xyz;
float3 N=mul(worldMatrix_IT,posIn.normal).xyz;
N=normalize(N);

float3 L=lightPosition-worldpos;//计算入射光方向
L=normalize(L);//归一化

float3 diffuseColor=Kd*lightColor*max(dot(N,L),0);//计算方向光漫反射光强

float3 ambientColor=Kd*globalAmbient;//计算环境光漫反射光强

posOut.color.xyz=diffuseColor+ambientColor;//RGB

posOut.color.w=1;//Alpha
}
(代码源于GPU编程+Cg语言之阳春白雪下里巴人)