使用HLSL和D3D11的Blinn-Phong着色:点光源

时间:2015-12-30 10:18:57

标签: shader hlsl lighting direct3d11 phong

我目前正在尝试使用HLSL中的Blinn-Phong着色在我的游戏中实现点光源。我所遵循的教程与here相关联 我现在的代码是:

struct VS_INPUT
{
    float4 Position : POSITION;
    float3 Normal : NORMAL;
    float2 TexCoord : TEXCOORD;
};

struct VS_OUTPUT
{
    float4 Position : SV_POSITION;
    float4 WorldPosition : POSITION;
    float3 Normal : NORMAL;
    float2 TexCoord : TEXCOORD;
};

struct NF3D_LIGHT_OMNIDIRECTIONAL
{
    float4 Diffuse;
    float4 Ambient;
    float3 Position;
    float3 Attitude;
    float Range;
    int BindSlot;
};


//--------------------------------------------------------------------------------------
// Constant Buffer Variables
//--------------------------------------------------------------------------------------
cbuffer CB_PROJECTION : register(b0)
{
    matrix Projection;
}

cbuffer CB_VIEW : register(b1)
{
    matrix View;
    float4 CameraPosition;
}

cbuffer CB_WORLD : register(b2)
{
    matrix World;
}

cbuffer Light : register(b5)
{
    NF3D_LIGHT_OMNIDIRECTIONAL light;
};


//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
VS_OUTPUT VS(VS_INPUT Input)
{
    VS_OUTPUT Output = (VS_OUTPUT)0;

    // Change the position vector to be 4 units for proper matrix calculations.
    Input.Position.w = 1.0f;

    // Aply the perspective to every vertex.
    Output.Position = mul(Input.Position, World);
    Output.WorldPosition = Output.Position;
    Output.Position = mul(Output.Position, View);
    Output.Position = mul(Output.Position, Projection);

    // Normalise the normal vector.
    Output.Normal = mul(Input.Normal, (float3x3)World);
    Output.Normal = normalize(Output.Normal);

    // Transfer the texture coordinates.
    Output.TexCoord = Input.TexCoord;

    return Output;
}


//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS(VS_OUTPUT Input) : SV_Target
{
    // https://brooknovak.wordpress.com/2008/11/13/hlsl-per-pixel-point-light-using-phong-blinn-lighting-model/
    // Phong relfection is ambient + light-diffuse + spec highlights.
    // I = Ia*ka*Oda + fatt*Ip[kd*Od(N.L) + ks(R.V)^n]
    // Ref: http://www.whisqu.se/per/docs/graphics8.htm
    // and http://en.wikipedia.org/wiki/Phong_shading

    // Get light direction for this fragment
    float3 lightDir = normalize(light.Position - Input.WorldPosition);

    // Note: Non-uniform scaling not supported
    float diffuseLighting = saturate(dot(Input.Normal, -lightDir)); // per pixel diffuse lighting

    // Introduce fall-off of light intensity
    diffuseLighting *= ((length(lightDir) * length(lightDir)) / dot(light.Position - Input.WorldPosition, light.Position - Input.WorldPosition));

    // Using Blinn half angle modification for perofrmance over correctness
    float3 h = normalize(normalize(CameraPosition.xyz - Input.WorldPosition) - lightDir);
    float specLighting = pow(saturate(dot(h, Input.Normal)), 2.0f);

    return saturate(light.Ambient + (light.Diffuse * diffuseLighting * 0.6f) + (specLighting * 0.5f));
}

在这张照片中可以看到问题: enter image description here 请注意,这些框架是故意排除的,我试图捕获它们,以便可以非常清楚地理解这个错误。

小行星是一个简单的模型,围绕点光的原点进行轨道运动。镜面光在所有面和像素上都应该是均匀的,但正如您所看到的,如果网格位于相机前面,则反射光会在其上形成清晰的形状。
我也意识到我的代码是错误的,如果我从等式中移除镜面光,问题就会消失。任何人都可以解释一下这里发生了什么,我怎么能解决这个问题?提前谢谢。

1 个答案:

答案 0 :(得分:1)

我解决了!问题是:

CameraPosition.xyz - Input.WorldPosition

它们都是float4类型,删除.xyz删除了这个烦人的错误。