2016-02-29 136 views
2

的DirectX照明我有,我不能設法弄清楚一個問題。我只是給我的項目添加了一個點光源,它使紋理完全變黑。我不知道爲什麼。C++在像素着色器的問題

我想,這可能是要麼無法正確更新,也可能是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); 

和渲染()我運行

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

莫不是你的點光源緩衝區對齊/填充問題? 我不確定C++和着色語言中的規則是否相同。 – jcoder

回答

0

如果紋理將爲黑色,s.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和/或採樣的紋理是零。然後用紋理樣本的非零常量嘗試。如果他們仍然使紋理看起來黑色,那麼你的光矢量計算就是罪魁禍首。