title: DirectX 11 Diffuse Lighting Implementation
body: <p>Following: <a href="https://www.gamasutra.com/view/feature/131275/implementing_lighting_models_with_.php?page=2" rel="nofollow noreferrer">https://www.gamasutra.com/view/feature/131275/implementing_lighting_models_with_.php?page=2</a></p>
  
  <p>I am trying to implement Diffuse Lighting, but I think I am not understanding something..., and I dont know if I am calculating it correctly.</p>
  
  <p>The vertex information for the cube is:</p>
  
  <pre><code>SimpleVertex vertices[] =
          {
              { DirectX::XMFLOAT3(-1.0f, 1.0f, -1.0f), DirectX::XMFLOAT3(0.0f, 1.0f, 0.0f), DirectX::XMFLOAT2(1.0f, 0.0f) },
              { DirectX::XMFLOAT3(1.0f, 1.0f, -1.0f), DirectX::XMFLOAT3(0.0f, 1.0f, 0.0f), DirectX::XMFLOAT2(0.0f, 0.0f) },
              { DirectX::XMFLOAT3(1.0f, 1.0f, 1.0f), DirectX::XMFLOAT3(0.0f, 1.0f, 0.0f), DirectX::XMFLOAT2(0.0f, 1.0f) },
              { DirectX::XMFLOAT3(-1.0f, 1.0f, 1.0f), DirectX::XMFLOAT3(0.0f, 1.0f, 0.0f), DirectX::XMFLOAT2(1.0f, 1.0f) },
  
              { DirectX::XMFLOAT3(-1.0f, -1.0f, -1.0f), DirectX::XMFLOAT3(0.0f, -1.0f, 0.0f), DirectX::XMFLOAT2(0.0f, 0.0f) },
              { DirectX::XMFLOAT3(1.0f, -1.0f, -1.0f), DirectX::XMFLOAT3(0.0f, -1.0f, 0.0f), DirectX::XMFLOAT2(1.0f, 0.0f) },
              { DirectX::XMFLOAT3(1.0f, -1.0f, 1.0f), DirectX::XMFLOAT3(0.0f, -1.0f, 0.0f), DirectX::XMFLOAT2(1.0f, 1.0f) },
              { DirectX::XMFLOAT3(-1.0f, -1.0f, 1.0f), DirectX::XMFLOAT3(0.0f, -1.0f, 0.0f), DirectX::XMFLOAT2(0.0f, 1.0f) },
  
              { DirectX::XMFLOAT3(-1.0f, -1.0f, 1.0f), DirectX::XMFLOAT3(-1.0f, 0.0f, 0.0f), DirectX::XMFLOAT2(0.0f, 1.0f) },
              { DirectX::XMFLOAT3(-1.0f, -1.0f, -1.0f), DirectX::XMFLOAT3(-1.0f, 0.0f, 0.0f), DirectX::XMFLOAT2(1.0f, 1.0f) },
              { DirectX::XMFLOAT3(-1.0f, 1.0f, -1.0f), DirectX::XMFLOAT3(-1.0f, 0.0f, 0.0f), DirectX::XMFLOAT2(1.0f, 0.0f) },
              { DirectX::XMFLOAT3(-1.0f, 1.0f, 1.0f), DirectX::XMFLOAT3(-1.0f, 0.0f, 0.0f), DirectX::XMFLOAT2(0.0f, 0.0f) },
  
              { DirectX::XMFLOAT3(1.0f, -1.0f, 1.0f), DirectX::XMFLOAT3(1.0f, 0.0f, 0.0f), DirectX::XMFLOAT2(1.0f, 1.0f) },
              { DirectX::XMFLOAT3(1.0f, -1.0f, -1.0f), DirectX::XMFLOAT3(1.0f, 0.0f, 0.0f), DirectX::XMFLOAT2(0.0f, 1.0f) },
              { DirectX::XMFLOAT3(1.0f, 1.0f, -1.0f), DirectX::XMFLOAT3(1.0f, 0.0f, 0.0f), DirectX::XMFLOAT2(0.0f, 0.0f) },
              { DirectX::XMFLOAT3(1.0f, 1.0f, 1.0f), DirectX::XMFLOAT3(1.0f, 0.0f, 0.0f), DirectX::XMFLOAT2(1.0f, 0.0f) },
  
              { DirectX::XMFLOAT3(-1.0f, -1.0f, -1.0f), DirectX::XMFLOAT3(0.0f, 0.0f, -1.0f), DirectX::XMFLOAT2(0.0f, 1.0f) },
              { DirectX::XMFLOAT3(1.0f, -1.0f, -1.0f), DirectX::XMFLOAT3(0.0f, 0.0f, -1.0f), DirectX::XMFLOAT2(1.0f, 1.0f) },
              { DirectX::XMFLOAT3(1.0f, 1.0f, -1.0f), DirectX::XMFLOAT3(0.0f, 0.0f, -1.0f), DirectX::XMFLOAT2(1.0f, 0.0f) },
              { DirectX::XMFLOAT3(-1.0f, 1.0f, -1.0f), DirectX::XMFLOAT3(0.0f, 0.0f, -1.0f), DirectX::XMFLOAT2(0.0f, 0.0f) },
  
              { DirectX::XMFLOAT3(-1.0f, -1.0f, 1.0f), DirectX::XMFLOAT3(0.0f, 0.0f, 1.0f), DirectX::XMFLOAT2(1.0f, 1.0f) },
              { DirectX::XMFLOAT3(1.0f, -1.0f, 1.0f), DirectX::XMFLOAT3(0.0f, 0.0f, 1.0f), DirectX::XMFLOAT2(0.0f, 1.0f) },
              { DirectX::XMFLOAT3(1.0f, 1.0f, 1.0f), DirectX::XMFLOAT3(0.0f, 0.0f, 1.0f), DirectX::XMFLOAT2(0.0f, 0.0f) },
              { DirectX::XMFLOAT3(-1.0f, 1.0f, 1.0f), DirectX::XMFLOAT3(0.0f, 0.0f, 1.0f), DirectX::XMFLOAT2(1.0f, 0.0f) },
          };
  </code></pre>
  
  <p>Vertex Shader Is:</p>
  
  <pre><code>cbuffer ConstantBuffer : register( b0 )
  {
      matrix World;
      matrix View;
      matrix Projection;
      float4 vMeshColor;
  };
  
  struct VS_INPUT
  {
      float4 Position : POSITION;
      float3 Normal : NORMAL;
      float2 Texture : TEXCOORD0;
  };
  
  struct PS_INPUT
  {
      float4 Position : SV_POSITION;
      float3 Normal : TEXCOORD0;
      float2 Texture : TEXCOORD1;
  };
  
  PS_INPUT VS( VS_INPUT input )
  {
      PS_INPUT output = (PS_INPUT)0;
  
      output.Position = mul( input.Position, World );
      output.Position = mul( output.Position, View );
      output.Position = mul( output.Position, Projection );
  
      output.Normal = mul( input.Normal, World );
      output.Normal = mul( output.Normal, View );
      output.Normal = mul( output.Normal, Projection );
      output.Normal = normalize( output.Normal );
  
      output.Texture = input.Texture;
  
      return output;
  }
  </code></pre>
  
  <p>Pixel Is:</p>
  
  <pre><code>PS_OUTPUT PS( PS_INPUT input )
  {
      PS_OUTPUT output;
      float4 ambient = {0.1, 0.0, 0.0, 1.0};
      float4 lightColor = { 1.0f, 1.0f, 1.0f, 1.0f};
  
      float3 lightPosition = ( 1.0f, 1.0f, 0.0f );
      float3 lightDirection = normalize(lightPosition - input.Position);
  
      float1 diffuse = saturate( dot( lightDirection, input.Normal )) * lightColor;
  
      output.color = diffuse;
  
      //float4 solidColor = float4( 1.0f, 1.0f, 0.0f, 1.0f );
      //output.color = solidColor;
      return output;
  }
  </code></pre>
  
  <p>Result (when cube is rotated 50):
  <a href="https://i.stack.imgur.com/dLbd7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dLbd7.png" alt="enter image description here"></a></p>
  
  <p>CPP: <a href="https://github.com/walbourn/directx-sdk-samples/blob/master/Direct3D11Tutorials/Tutorial06/Tutorial06.cpp" rel="nofollow noreferrer">https://github.com/walbourn/directx-sdk-samples/blob/master/Direct3D11Tutorials/Tutorial06/Tutorial06.cpp</a></p>
  
  <p>HLSL: <a href="https://github.com/walbourn/directx-sdk-samples/blob/master/Direct3D11Tutorials/Tutorial06/Tutorial06.fx" rel="nofollow noreferrer">https://github.com/walbourn/directx-sdk-samples/blob/master/Direct3D11Tutorials/Tutorial06/Tutorial06.fx</a></p>