转载请注明出处: http://blog.csdn.net/tianhai110
改良后的Phong光照模型:
上一节实现的phong镜面光照模型,如果固定光源,移动视点(及matView 关联为matWorld, vecEye关联viewPosition)得到的效果会有问题,就是到背面的时候,模型会突然变白;
图形学的前辈们,解决了这个问题,采用了一个改进的光照公式:
I = A + saturate(4*N.L)*(D*N.L + (R.V)n) saturate表示让参数所得结果位于0,1之间
在上一节例子的基础上,做如下步骤:
1.将vecEye关联到viewPosition上;
2. 将matView关联到World上
3. 修改pixel Shader如下:
- sampler2D baseMap;
- float4 ps_main( float2 Texc:TEXCOORD0, float3 Light:TEXCOORD1,
- float3 Norm:TEXCOORD2, float3 View:TEXCOORD3) : COLOR0
- {
- float4 ambient = { 0.3686f, 0.3686f, 0.3686f, 1.0f};
- float4 diffuse = { 0.88f, 0.88f, 0.88f, 1.0f};
- float3 Normal = normalize( Norm);
- float3 LightDir = normalize( Light);
- float3 ViewDir = normalize( View);
- float4 diff = saturate( dot( Normal, LightDir));
- float3 Reflect = normalize( 2 * diff * Normal - LightDir);
- float4 specular = pow(saturate(dot(Reflect, ViewDir)), 8);
- float4 shadow = saturate(4*diff);
- float4 fvBaseColor = tex2D( baseMap, Texc );
- float4 fvTotalAmbient = ambient * fvBaseColor;
- float4 fvTotalDiffuse = diffuse * diff * fvBaseColor;
- return fvTotalAmbient + shadow*(fvTotalDiffuse + specular);
- }
4. 所得结果:
phong镜面光照背后