战魂小筑

讨论群:309800774 知乎关注:http://zhihu.com/people/sunicdavy 开源项目:https://github.com/davyxu

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  257 随笔 :: 0 文章 :: 506 评论 :: 0 Trackbacks

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 );
   
}
posted on 2010-06-01 14:51 战魂小筑 阅读(3398) 评论(1)  编辑 收藏 引用 所属分类: 渲染 Shader 引擎

评论

# re: 用Shader实现标准光照模型 2012-04-19 17:09 七星重剑
楼主用的Blinn-Phong shading model啊  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理