gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:47:32 25 4
gpt4 key购买 nike

我有一个我无法解决的问题。我刚刚在我的项目中添加了一个点光源,它使纹理完全变黑。我不知道为什么。

我认为这可能是法线没有正确更新,也可能是 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);

最佳答案

如果 s.x, s.y, s.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,这样您就可以确定您是否正确设置了contant buffer。如果在您更改它们之后,它仍然是黑色,则它必须是 nDotL 和/或采样纹理为零。然后尝试使用非零常数作为纹理样本。如果它们仍然导致纹理看起来变黑,那么您的光 vector 计算就是罪魁祸首。

关于像素着色器问题中的 c++ DirectX 照明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35705333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com