1
\$\begingroup\$

I am following this tutorials series on rastertek.com and I got a bit stuck on the Diffuse Lighting Tutorial. Particulary, the part that does not work for me is alpha channel of the light color. Here are the [I think they are] relevant code parts:

Light Shader:

cbuffer MatrixBuffer
{
    matrix worldMatrix;
    matrix viewMatrix;
    matrix projectionMatrix;
};

Texture2D shaderTexture;
SamplerState SampleType;

cbuffer LightBuffer
{
    float4 ambientColor;
    float4 diffuseColor;
    float3 lightDirection;
    float padding;
};

struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
    float3 normal : NORMAL;
};

float4 LightPixelShader(PixelInputType input) : SV_TARGET
{
    float4 textureColor;
    float3 lightDir;
    float lightIntensity;
    float4 color;

    textureColor = shaderTexture.Sample(SampleType, input.tex);

    lightDir = -lightDirection;

    lightIntensity = saturate(dot(input.normal, lightDir));

    color = saturate(diffuseColor * lightIntensity);

    color = color * textureColor;

    return color;
}

PixelInputType LightVertexShader(VertexInputType input)
{
    PixelInputType output;

    input.position.w = 1.0f;

    output.position = mul(input.position, worldMatrix);
    output.position = mul(output.position, viewMatrix);
    output.position = mul(output.position, projectionMatrix);

    output.tex = input.tex;

    output.normal = mul(input.normal, (float3x3)worldMatrix);

    output.normal = normalize(output.normal);

    return output;
}

The problem here is the shader completely ignores the alpha channel of the light. If I set the diffuse color to red, then the texture will be reddish no matter what alpha is supplied.

The only way I could make the alpha work is changing the line

color = color * textureColor;

to

color = color * color.w * textureColor;

in the pixel shader. This however gives overall wrong result.

Might it be the DirectX setup issue, here are some parts of the device initialization:

// Set regular 32-bit surface for the back buffer.
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;

[...]

// Set up the description of the depth buffer.
depthBufferDesc.Width = screenWidth;
depthBufferDesc.Height = screenHeight;
depthBufferDesc.MipLevels = 1;
depthBufferDesc.ArraySize = 1;
depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthBufferDesc.SampleDesc.Count = 1;
depthBufferDesc.SampleDesc.Quality = swapChainDesc.SampleDesc.Quality;
depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthBufferDesc.CPUAccessFlags = 0;
depthBufferDesc.MiscFlags = 0;

[...]

// Setup the raster description which will determine how and what polygons will be drawn.
rasterDesc.AntialiasedLineEnable = false;
rasterDesc.CullMode = D3D11_CULL_BACK;
rasterDesc.DepthBias = 0;
rasterDesc.DepthBiasClamp = 0.0f;
rasterDesc.DepthClipEnable = true;
rasterDesc.FillMode = D3D11_FILL_SOLID;
rasterDesc.FrontCounterClockwise = false;
rasterDesc.MultisampleEnable = false;
rasterDesc.ScissorEnable = false;
rasterDesc.SlopeScaledDepthBias = 0.0f;

What's wrong with this shader?

I'm not sure what other code might be relevant, I will gladly add any at your request.

\$\endgroup\$
  • \$\begingroup\$ What effect would you expect / want an alpha value of a light to have? \$\endgroup\$ – DMGregory Feb 12 at 10:20
  • \$\begingroup\$ @DMGregory to affect the light intensity, i.e. if I set my diffuse light to (1.0, 1.0, 1.0, 0.2), the textured model will only have 20% lightness as if the light source was weak. But my understand of diffuse light might be wrong... \$\endgroup\$ – lolbas Feb 12 at 11:09
  • \$\begingroup\$ But you already have a dedicated light intensity variable, in addition to the brightness/darkness of the light's RGB color itself. Why add a third variable to control the same thing? \$\endgroup\$ – DMGregory Feb 12 at 11:16
  • \$\begingroup\$ @DMGregory but isn't lightIntensity is for intensity across the polygon, not for the intensity of the light source? Or do you mean I can multiply it bydiffuseColor.w to get the result I describe? I actually haven't tried this way and couldn't test it soon but it sounds.. reasonable? \$\endgroup\$ – lolbas Feb 12 at 11:28
  • \$\begingroup\$ Ah, sorry, that was a misreading on my part (on mobile so I only see part of the code at a time). I thought it was part of your light buffer rather than a name for your Lambertian / diffuse illumination / n dot l term. \$\endgroup\$ – DMGregory Feb 12 at 11:30

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.