像素着色器问题中的c ++ DirectX照明

时间:2016-02-29 17:07:26

标签: c++ directx directx-11 pixel-shader

我有一个问题,我无法弄明白。我刚刚为我的项目添加了一个点光源,它使纹理变得完全黑了。我不明白为什么。

我认为它可能是正常情况下没有正确更新,也可能是s.x,s.y和s.z的计算。

如果有人有时间看一下并帮助我,我会很高兴。感谢。

因此。这是我的像素着色器:

Texture2D txDiffuse : register(t0);
SamplerState sampState;
cbuffer PointLight : register(b0)
{
    float3 Pos;
    float diff;
    float amb;
    float spec;
    float range;
    float intensity;
};
struct VS_IN
{
    float4 Pos : SV_POSITION;
    float2 Tex : TEXCOORD;
    float4 Norm : NORMAL;
    float4 Pos2 : POSITION;
};
float4 PS_main(VS_IN input) : SV_Target
{
    float3 s = txDiffuse.Sample(sampState, input.Tex).xyz;

    float3 lightPos = Pos;

    float3 lightVector = lightPos - input.Pos2;
    lightVector = normalize(lightVector);
    float nDotL = dot(lightVector, input.Norm);

    float diff1 = 0.8;
    float amb1 = 0.1;

    s.x = (s.x * diff * nDotL + s.x * amb);
    s.y = (s.y * diff * nDotL + s.y * amb);
    s.z = (s.z * diff * nDotL + s.z * amb);



    return float4(s, 0.0);

};

几何着色器:

cbuffer worldMatrix : register(b0)
{
    matrix world;
}
cbuffer viewMatrix : register(b1)
{
    matrix view;
}
cbuffer projectionMatrix : register(b2)
{
    matrix projection;
}
struct VS_IN
{
    float4 Pos : SV_POSITION;
    float2 Tex : TEXCOORD;
};
struct VS_OUT
{
    float4 Pos : SV_POSITION;
    float2 Tex : TEXCOORD;
    float4 Norm : NORMAL;
    float4 Pos2 : POSITION;
};

[maxvertexcount(6)]
void main(triangle VS_IN input[3] : SV_POSITION, inout TriangleStream< VS_OUT > output2)
{

    matrix wvp = mul(projection, mul(world, view));
    matrix worldView = mul(world, view);


    float4 normal = float4(cross(input[1].Pos - input[0].Pos, input[2].Pos - input[0].Pos), 0.0f);
    normal = normalize(normal);

    float4 rotNorm = mul(worldView, normal);
    rotNorm = normalize(rotNorm);

    VS_OUT output[3];
    for (uint i = 0; i < 3; i++)
    {
        output[i].Pos = input[i].Pos;
        output[i].Pos = mul(wvp, input[i].Pos);
        output[i].Tex = input[i].Tex;
        output[i].Norm = rotNorm;
        output[i].Pos2 = mul(worldView, output[i].Pos);
        output2.Append(output[i]);
    }
    output2.RestartStrip();

    VS_OUT outputcopy[3];
    for (uint i = 0; i < 3; i++)
    {
        outputcopy[i].Pos = input[i].Pos + (normal);
        outputcopy[i].Pos = mul(wvp, outputcopy[i].Pos);
        outputcopy[i].Tex = input[i].Tex;
        outputcopy[i].Norm = rotNorm;
        outputcopy[i].Pos2 = mul(worldView, outputcopy[i].Pos);
        output2.Append(outputcopy[i]);
    }

    output2.RestartStrip();
}

初始化点光源的代码:

struct PointLight
{
    Vector3 Pos;
    float diff;
    float   amb;
    float   spec;
    float   range;
    float   intensity;

};
PointLight* pointLight = nullptr;
PointLight PL =
    {
    Vector3(0.0f, 0.0f, -3.0f),
    0.8f,
    0.2f,
    0.0f,
    100.0f,
    1.0f
    };

    pointLight = &PL;

    D3D11_BUFFER_DESC lightBufferDesc;
    memset(&lightBufferDesc, 0, sizeof(lightBufferDesc));
    lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    lightBufferDesc.Usage = D3D11_USAGE_DEFAULT;
    lightBufferDesc.StructureByteStride = 0;
    lightBufferDesc.MiscFlags = 0;
    lightBufferDesc.ByteWidth = sizeof(PointLight);

    D3D11_SUBRESOURCE_DATA pointLightData;
    memset(&pointLightData, 0, sizeof(pointLightData));
    pointLightData.pSysMem = &PL;

    gDevice->CreateBuffer(&lightBufferDesc, &pointLightData, &lightBuffer);

并在render()中运行

gDeviceContext->PSSetConstantBuffers(0, 1, &lightBuffer);

1 个答案:

答案 0 :(得分:0)

如果s.xs.ys.z等于零,纹理将为黑色。

s.x = (s.x * diff * nDotL + s.x * amb);
s.y = (s.y * diff * nDotL + s.y * amb);
s.z = (s.z * diff * nDotL + s.z * amb);

尝试使用非零常量更改diffamb,以便您可以确定是否正确设置了容器缓冲区。如果在更改它们之后,它仍然是黑色,那么它必须是nDotL和/或采样纹理为零。然后尝试使用非零常量作为纹理样本。如果它们仍然导致纹理看起来变黑,那么光矢量计算就是罪魁祸首。