前些日子 乘着有闲功夫,慢慢的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.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;
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 阅读(2688)
评论(2) 编辑 收藏 引用 所属分类:
HLSL