前些日子 乘着有闲功夫,慢慢的hlsl看了起来,发现以前学的数学知识全用上了,只可惜忘得都差不多了,又要恶补数学了。
做了个比较简单的 phong 光照模型。
float4x4 Scal;
float4x4 World;
float4x4 View;
float4x4 projection;
float4x4 WorldViewProjection;
float3 EyePosition;
float3 LightDir;
float4 LightColor;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
struct VertexInput
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
float4 Position : POSITION;
float2 Tex : TEXCOORD0;
float3 Normal : NORMAL;
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
struct VertexOutput
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
float4 Position : POSITION;
float2 Tex : TEXCOORD0;
float3 Normal : TEXCOORD1;
float3 View : TEXCOORD2;
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
VertexOutput VertexMain(VertexInput input)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
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;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
float4 PixelMain(VertexOutput input) : COLOR0
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
float diffsum;
float specularsum;
float4 color;
float sunshinepower;
float4 amibent = float4(0.1f, 0.1f, 0.1f, 1.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;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return color;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
technique techR
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
pass p0
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
VertexShader = compile vs_2_0 VertexMain();
PixelShader = compile ps_2_0 PixelMain();
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
posted on 2009-02-16 10:10
AstaTus 阅读(2677)
评论(2) 编辑 收藏 引用 所属分类:
HLSL