Alpha 通道在 directx11 和 c++ 中起作用并输出绿色

Alpha channel is acting and outputting green in directx11 and c++

我是 Directx11 的新手。我想为我的 program.So 创建点光源行为我定义了一个这样的结构

struct PointLights
{

    XMFLOAT3 Pos; //Light Position in (X,Y,Z)
    float Range; //the range after which objects will not be affected by this light
    XMFLOAT4 Color;  //Light Color
    XMFLOAT3 att; // Attenuation Parameters
    float Intensity;

};

并将其添加到常量缓冲区中,如

struct ConstantBuffer
{

 XMMATRIX World;
 XMMATRIX WorldInv;
 XMMATRIX View;
 XMMATRIX Project;

 XMFLOAT3 LighDirs[2];
 XMFLOAT4 LightColor[2];
 float SpecualrIntensity;
 XMFLOAT3 CameraPos;
 PointLights pointLight;

};

此函数用于创建缓冲区

void InitConstantBuffer(void)
{
    D3D11_BUFFER_DESC desc;
    SecureZeroMemory(&desc,sizeof(desc));

    desc.BindFlags= D3D11_BIND_CONSTANT_BUFFER;
    desc.ByteWidth = sizeof(ConstantBuffer);
    desc.CPUAccessFlags = 0;
    desc.Usage = D3D11_USAGE_DEFAULT;
    SecureZeroMemory(&g_ConstantBuffer,sizeof(ID3D11Buffer));

HRESULT HR= dev->CreateBuffer(&desc,0,&g_ConstantBuffer);
if(FAILED(HR))
    MessageBox(_hwnd,L"Couldnt Create constant buffer",L"ERROR",0);

}

现在当我将点光源颜色更新为黑色但将其 alpha 设置为 1 (0,0,0,1) 如果 alpha 为 0 (0,0,0,0)

,则输出为绿色,输出为黑色
PointLights pointLight;

    SecureZeroMemory(&pointLight,sizeof(PointLights));
    pointLight.att = XMFLOAT3(0,0,1);

    pointLight.Intensity = 5;
    pointLight.Pos = XMFLOAT3(Lppos[0],Lppos[1],Lppos[2]);
    pointLight.Range = 200;
    pointLight.Color = XMFLOAT4(0,0,0,1); // black

    ConstantBuffer cb;
    cb.World = XMMatrixTranspose(g_World);
    cb.View = XMMatrixTranspose(g_View);
    cb.Project = XMMatrixTranspose(g_Projection);
    cb.WorldInv  = InverseTranspose(g_World);
    cb.LighDirs[1] = lights[1].Dir;
    cb.LightColor[1] = lights[1].Color;

    cb.LightColor[0] = lights[0].Color;cb.LighDirs[0] = lights[0].Dir;
    cb.LightColor[0] = lights[0].Color;
    cb.SpecualrIntensity = 10;
    cb.CameraPos = XMFLOAT3(0,0,-10);
    cb.pointLight = pointLight;


    devcon->UpdateSubresource(g_ConstantBuffer,0,0,&cb,0,0);

![输出为绿色][1]

无法上传图片,因为 Whosebug 不允许我...我讨厌它

这是像素着色器

struct PointLights{
    float3 Position;
    float Range;
    float4 Color;
    float3 Atten;
    float Intensity;
};

cbuffer ConstantBuffer:register(b0)
{
 matrix world;
 matrix worldInv;
 matrix view;
 matrix project;

 float3 LightDir[2];
 float4 LightColor[2];
 float SpecualrIntensity;
 float3 CameraPos;

 PointLights pointLight;

};

float4 Ps(Vout vo):SV_TARGET
{
 float4 col = (float4)0;
 col = pointLight.Color;
return col;}

请帮帮我..我尝试重新排列结构的成员但仍然是徒劳

谢谢....

很可能 C++ 常量缓冲区结构和 HLSL cbuffer 声明之间的数据对齐不完全匹配。尝试使用 XMFLOAT4 而不是 XMFLOAT3 以确保它们在两者中以相同的方式排列。

您正在 struct ConstantBuffer 中使用 XMMATRIX可能有效,可能无效,具体取决于您如何使用它。它使得 ConstantBuffer 需要在内存中进行 16 字节对齐。如果您正在做此假设,请使用 XMVECTOR 而不是 XMFLOAT3XMFLOAT4。否则,将 XMMATRIX 更改为 XMMFLOAT4X4

如果您发现 DirectXMath 对齐令人困惑,请考虑使用 SimpleMath wrapper from DirectX Tool Kit