我正在DirectX11的3D项目中工作,目前正在使用现有的代码使用带有DirectX11的Frank Luna 3D游戏编程实现不同的灯光。

目前,我正在开发一个聚光灯,该聚光灯应跟随相机的位置并朝同一方向看,但是,被点亮的位置在奇怪地移动。当位置改变时,光的方向 vector 似乎在(+ x,+ y,0)方向上跟踪。最好用图片解释。

c++ - 如何在DirectX11中正确创建聚光灯?-LMLPHP

此处看起来好像它们已正确点亮,并且如果照相机停留在原处,则聚光灯可以按您期望的那样移动,并跟踪照相机的方向。

c++ - 如何在DirectX11中正确创建聚光灯?-LMLPHP

在此图像中,我已将相机沿z轴移到了靠近框的位置,并且在最近的框上光点应该变小了,但它是向上跟踪的。

这是将Spotlight结构设置为传递到常量缓冲区的代码,即结构中的所有值,除了浮点数在最后用作填充:

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;
}

我哪里做错了?

最佳答案

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

你有

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

您需要(WS中的input.Pos,而不是投影空间)
calculateSpotLight(newMat, spotLight, input.Pos, input.Norm, input.PosW, amb, diff, spec);

关于c++ - 如何在DirectX11中正确创建聚光灯?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33736320/

10-13 09:30