AstaTus
-- 夏天不热。。
posts - 22,comments - 12,trackbacks - 0
   前些日子 乘着有闲功夫,慢慢的hlsl看了起来,发现以前学的数学知识全用上了,只可惜忘得都差不多了,又要恶补数学了。
   做了个比较简单的 phong 光照模型。
   
float4x4 Scal;
float4x4 World;
float4x4 View;
float4x4 projection;
float4x4 WorldViewProjection;
float3 EyePosition;
float3 LightDir;
float4 LightColor;

struct VertexInput
{
    float4  Position : POSITION;
    float2  Tex : TEXCOORD0;
    float3  Normal : NORMAL;
}
;


struct VertexOutput
{
    float4  Position : POSITION;
    float2    Tex    : TEXCOORD0;
    float3    Normal : TEXCOORD1;
    float3  View     : TEXCOORD2;        
}
;


VertexOutput VertexMain(VertexInput input)
{
    VertexOutput output 
= (VertexOutput)0;
    
     WorldViewProjection 
= mul(mul(View, World), projection);
     
     output.Position 
= mul(mul(input.Position, Scal), WorldViewProjection);
     output.Tex 
= input.Tex;
     output.Normal 
= mul(input.Normal, World);
     output.View  
= EyePosition - mul(input.Position,  World);
     
     
return output;
}


float4 PixelMain(VertexOutput input) : COLOR0
{
    
float diffsum;
    
float specularsum;
    float4 color;
    
float sunshinepower;
    float4 amibent 
= float4(0.1f0.1f0.1f1.0f);
    sunshinepower 
= 16.0f;
    
    diffsum 
= specularsum = 0;
    
    
//漫反射
    LightDir = normalize(LightDir);
    diffsum 
= saturate(dot(LightDir, input.Normal));
    
    
//镜面反射
    float3 L = -LightDir;
    float3 R 
= normalize(reflect(L, input.Normal));
    float3 V 
= normalize(input.View);
    
    specularsum 
= pow(saturate(dot(R, V)), sunshinepower);
    
    color 
= specularsum + diffsum * LightColor + amibent;

    
    
return color;
}


technique techR 
{
    pass p0
    
{
        VertexShader 
= compile vs_2_0 VertexMain();
        PixelShader 
= compile ps_2_0 PixelMain();
    }

}

posted on 2009-02-16 10:10 AstaTus 阅读(2661) 评论(2)  编辑 收藏 引用 所属分类: HLSL

FeedBack:
# re: phong 光照模型。。
2012-06-10 15:52 | 小小子
能把上图的源码和exe程序给我拿来写作业吗?772862188@qq.com  回复  更多评论
  
# re: phong 光照模型。。
2012-06-10 15:53 | 小小子
忘了给你说感谢! 希望你能帮助我,谢谢。  回复  更多评论
  

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