用Shader实现标准光照模型

1649 篇文章 11 订阅
1623 篇文章 22 订阅

http://www.cppblog.com/sunicdavy/archive/2010/06/01/116895.html

 

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 );
   
}
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值