如何在DirectX11中正确创建聚光灯?

时间:2015-11-16 13:15:11

标签: c++ directx directx-11 lighting

我正在DirectX11中开展一个3D项目,目前我正在使用Frank Luna 3D Game Programming with DirectX11 book with our existing code来实现不同的灯光。

目前,我正在开发一种聚光灯,它应该沿着相机的位置并朝同一方向看,然而,正在点亮的位置正在奇怪地移动。当位置改变时,光的方向矢量似乎在(+ x,+ y,0)方向上跟踪。最好用图片解释。

Boxes lit "properly"

它看起来像是正常点亮,如果相机停留在原处,聚光灯可以按照您的预期移动,并跟踪相机方向。

Boxes misbehaving

在这张图片中,我沿着z轴移动相机靠近方框,光点应该在最近的方框上变小,但它会向上跟踪。

这是聚光灯结构被设置为传递到常量缓冲区的代码,这是结构中的所有值,除了在末尾用作填充的浮点数:

cb.spotLight = SpotLight();
cb.spotLight.ambient = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f);
cb.spotLight.specular = XMFLOAT4(0.5, 0.5, 0.5, 10.0);
cb.spotLight.diffuse = XMFLOAT4(0.5, 0.5, 0.5, 1.0);
cb.spotLight.attenuation = XMFLOAT3(1, 1, 1);
cb.spotLight.range = 15;

XMVECTOR cameraP = XMLoadFloat3(&cameraPos);
XMVECTOR s = XMVectorReplicate(cb.spotLight.range);
XMVECTOR l = XMLoadFloat3(&camera.getForwards());
XMVECTOR lookat = XMVectorMultiplyAdd(s, l, cameraP);

XMStoreFloat3(&cb.spotLight.direction, XMVector3Normalize(lookat - XMVectorSet(cameraPos.x, cameraPos.y, cameraPos.z, 1.0f)));
cb.spotLight.position = cameraPos;

cb.spotLight.spot = 96;

以下是用于计算着色器中聚光灯的环境光,漫反射和镜面反射值的函数:

void calculateSpotLight(Material mat, SpotLight light, float3 position, float3 normal, float3 toEye,
out float4 ambient, out float4 diffuse, out float4 specular)
{
ambient = float4(0, 0, 0, 0);
specular = float4(0, 0, 0, 0);
diffuse = float4(0, 0, 0, 0);

float3 lightV = light.position - position;

float distance = length(lightV);

if (distance > light.range)
{
    return;
}

lightV /= distance;

ambient = mat.ambient * light.ambient;

float diffuseFact = dot(lightV, normal);

[flatten]
if (diffuseFact > 0.0f)
{
    float3 vect = reflect(-lightV, normal);

    float specularFact = pow(max(dot(vect, toEye), 0.0f), mat.specular.w);

    diffuse = diffuseFact * mat.diffuse * light.diffuse;
    specular = specularFact * mat.specular * light.specular;
}

float spot = pow(max(dot(-lightV, float3(-light.direction.x, -light.direction.y, light.direction.z)), 0.0f), light.spot);

float attenuation = spot / dot(light.attenuation, float3(1.0f, distance, distance*distance));

ambient *= spot;
diffuse *= attenuation;
specular *= attenuation;
}

为了完整起见,顶点和像素着色器的相关部分。

VS_OUTPUT VS( float4 Pos : POSITION, float3 NormalL : NORMAL, float2 TexC : TEXCOORD )
{
    VS_OUTPUT output = (VS_OUTPUT)0;
    output.Pos = mul( Pos, World );

    //Get normalised vector to camera position in world coordinates
    output.PosW = normalize(eyePos - output.Pos.xyz);

    output.Pos = mul( output.Pos, View );
    output.Pos = mul( output.Pos, Projection );

    //Getting normalised surface normal
    float3 normalW = mul(float4(NormalL, 0.0f), World).xyz;
    normalW = normalize(normalW);
    output.Norm = normalW;

    output.TexC = TexC;

    return output;
}

float4 PS( VS_OUTPUT input ) : SV_Target
{
input.Norm = normalize(input.Norm);
Material newMat;
newMat.ambient = material.ambient;
newMat.diffuse = texCol;
newMat.specular = specCol;

float4 ambient = (0.0f, 0.0f, 0.0f, 0.0f);
float4 specular = (0.0f, 0.0f, 0.0f, 0.0f);
float4 diffuse = (0.0f, 0.0f, 0.0f, 0.0f);

float4 amb, spec, diff;

calculateSpotLight(newMat, spotLight, input.PosW, input.Norm, input.PosW, amb, diff, spec);

ambient += amb;
specular += spec;
diffuse += diff;
//Other light types
float4 colour;
    colour = ambient + specular + diffuse;
    colour.a = material.diffuse.a;
    return colour;
}

我哪里出错了?

1 个答案:

答案 0 :(得分:3)

第三个参数input.PosW在这里不正确。你必须在世界空间中使用位置。 input.PosW规范化向量。从光位置减去归一化矢量没有任何意义。

你有

calculateSpotLight(newMat, spotLight, input.PosW, input.Norm, input.PosW, amb, diff, spec);

你需要(在WS中输入.Pos,而不是投影空间)

calculateSpotLight(newMat, spotLight, input.Pos, input.Norm, input.PosW, amb, diff, spec);
相关问题