title: DirectX 11.2 triangle shape is distorted by normal vectors
body: <p>I am new to DirectX. I now have a to some extent silly question.</p>
  
  <p>I am using Windows 8.1 DirectX 11.2 and I am using right-hand coordinate system.</p>
  
  <p>I tried to apply the texture to an equilateral triangle in x-y plane and centred at (0,0,0).
  But the output shape is distorted(It doesn't look like equilateral triangle at all!!). And In theory if looking from x-axis, I can see nothing because the equilateral triangle locates in the x-y plane. But it turns out that I can see the triangle. In addition, if I change the value of normal vectors, the output shape changes too! I do not understand why and please help!</p>
  
  <p>Here is the view matrix configuration:</p>
  
  <pre><code>    static const XMVECTORF32 eye = { 0.0f, 0.0f, 1.5f, 0.0f };
      static const XMVECTORF32 at = { 0.0f, 0.0f, 0.0f, 0.0f };
      static const XMVECTORF32 up = { 0.0f, 1.0f, 0.0f, 0.0f };
  </code></pre>
  
  <p>Here is the vertexshader:</p>
  
  <pre><code>    cbuffer ModelViewProjectionConstantBuffer : register(b0)
      {
          matrix model;
          matrix view;
          matrix projection;
      };
  
      struct VertexShaderInput
      {
          float3 pos : POSITION;
          float3 norm : NORMAL;
          float2 tex : TEXCOORD0;
      };
  
      struct PixelShaderInput
      {
          float4 pos : SV_POSITION;
          float3 norm : NORMAL;
          float2 tex : TEXCOORD0;
      };
  
      PixelShaderInput main(VertexShaderInput input)
      {
          PixelShaderInput vertexShaderOutput;
          float4 pos = float4(input.pos, 1.0f);
          pos = mul(pos, model);
          pos = mul(pos, view);
          pos = mul(pos, projection);
          vertexShaderOutput.pos = pos;
          vertexShaderOutput.tex = input.tex;
          vertexShaderOutput.norm = mul(float4(normalize(input.norm), 0.0f), model).xyz;
          return vertexShaderOutput;
      }
  </code></pre>
  
  <p>Here is the pixelshader:</p>
  
  <pre><code>    Texture2D Texture : register(t0);
      SamplerState Sampler : register(s0);
  
      struct PixelShaderInput
      {
              float4 pos : SV_POSITION;
              float3 norm : NORMAL;
              float2 tex : TEXCOORD0;
      };
  
      float4 main(PixelShaderInput input) : SV_TARGET
      {
          float3 lightDirection = normalize(float3(0, 0, -1));
          return Texture.Sample(Sampler, input.tex); //* (0.8f *                    saturate(dot(normalize(input.norm), -lightDirection)) + 0.2f);
      }
  </code></pre>
  
  <p>Here is the coordinates:</p>
  
  <pre><code>    VertexPositionTexture vertexPositionTexture[] =
  {
      { XMFLOAT3(-1.5, -0.5*sqrtf(3), 0.0f), XMFLOAT3(0.0f, 0.0f, 0.4f),   XMFLOAT2(0.0, 1.0) },
      { XMFLOAT3(0.0f, sqrtf(3), 0.0f), XMFLOAT3(0.0f, 0.0f, 0.4f), XMFLOAT2(0.5, 0.0) },
      { XMFLOAT3(1.5, -0.5*sqrtf(3), 0.0f), XMFLOAT3(0.0f, 0.0f, 0.4f), XMFLOAT2(1.0, 1.0) },
      }
  </code></pre>
  
  <p>The index array would simply be {0,1,2} in clockwise order.</p>
  
  <p>So if I change the normal vector value in vertexPositionTexture XMFLOAT3(0.0, 0.0, 0.4) to XMFLOAT3(0.0, 0.0, -1), the shape will definitely change. I don't know why?</p>
  
  <p>Here is how I create the DeviceDependentResources:</p>
  
  <pre><code>    void TextureSceneRenderer::CreateDeviceDependentResources()
     {
          // Load shaders asynchronously.
          auto loadVSTask = DX::ReadDataAsync(L"TextureVertexShader.cso");
          auto loadPSTask = DX::ReadDataAsync(L"TexturePixelShader.cso");
          BasicLoader^ loader = ref new BasicLoader(m_deviceResources-&gt;GetD3DDevice());
  
          loader-&gt;LoadTexture(
              L"cat.dds",
              &amp;m_texture,
              &amp;m_textureSRV
      );
  
          // create the sampler
          D3D11_SAMPLER_DESC samplerDesc;
          ZeroMemory(&amp;samplerDesc, sizeof(D3D11_SAMPLER_DESC));
          samplerDesc.Filter = D3D11_FILTER_ANISOTROPIC;
          samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
          samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
          samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
          samplerDesc.MipLODBias = 0.0f;
          // use 4x on feature level 9.2 and above, otherwise use only 2x
          samplerDesc.MaxAnisotropy = 8;
          samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
          samplerDesc.BorderColor[0] = 0.0f;
          samplerDesc.BorderColor[1] = 0.0f;
          samplerDesc.BorderColor[2] = 0.0f;
          samplerDesc.BorderColor[3] = 0.0f;
          // allow use of all mip levels
          samplerDesc.MinLOD = 0;
          samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
  
          DX::ThrowIfFailed(
             m_deviceResources-&gt;GetD3DDevice()-&gt;CreateSamplerState(
             &amp;samplerDesc,
             &amp;m_sampler)
      );
  
          // After the vertex shader file is loaded, create the shader and input layout.
          auto createVSTask = loadVSTask.then([this](const std::vector&lt;byte&gt;&amp; fileData)      {
      DX::ThrowIfFailed(
          m_deviceResources-&gt;GetD3DDevice()-&gt;CreateVertexShader(
          &amp;fileData[0],
          fileData.size(),
          nullptr,
          &amp;m_vertexShader
          )
          );
  
      static const D3D11_INPUT_ELEMENT_DESC vertexDesc[] =
      {
          { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
          { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
          { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
      };
  
      DX::ThrowIfFailed(
          m_deviceResources-&gt;GetD3DDevice()-&gt;CreateInputLayout(
          vertexDesc,
          ARRAYSIZE(vertexDesc),
          &amp;fileData[0],
          fileData.size(),
          &amp;m_inputLayout
          )
          );
  });
  
  // After the pixel shader file is loaded, create the shader and constant buffer.
          auto createPSTask = loadPSTask.then([this](const std::vector&lt;byte&gt;&amp; fileData) {
              DX::ThrowIfFailed(
                  m_deviceResources-&gt;GetD3DDevice()-&gt;CreatePixelShader(
                  &amp;fileData[0],
                  fileData.size(),
                  nullptr,
                  &amp;m_pixelShader
          )
          );
  
              CD3D11_BUFFER_DESC    constantBufferDesc(sizeof(ModelViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
              DX::ThrowIfFailed(
                  m_deviceResources-&gt;GetD3DDevice()-&gt;CreateBuffer(
                  &amp;constantBufferDesc,
                  nullptr,
                  &amp;m_constantBuffer
          )
          );
          });
  
  
  
          // Once both shaders are loaded, create the mesh.
          auto createCubeTask = (createPSTask &amp;&amp; createVSTask).then([this]() {
  
              D3D11_SUBRESOURCE_DATA vertexBufferData = { 0 };
              vertexBufferData.pSysMem = this-&gt;model-&gt;getVertexPositionTexture();
              vertexBufferData.SysMemPitch = 0;
              vertexBufferData.SysMemSlicePitch = 0;
              CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(this-&gt;model-  &gt;getVertexPositionTexture()[0])*this-&gt;model-&gt;n_texture_vertex, D3D11_BIND_VERTEX_BUFFER);
              DX::ThrowIfFailed(
                  m_deviceResources-&gt;GetD3DDevice()-&gt;CreateBuffer(
                  &amp;vertexBufferDesc,
                  &amp;vertexBufferData,
                  &amp;m_vertexBuffer
          )
          );
  
  
              m_indexCount = this-&gt;model-&gt;n_mesh;//ARRAYSIZE(cubeIndices);
  
              D3D11_SUBRESOURCE_DATA indexBufferData = { 0 };
              indexBufferData.pSysMem = this-&gt;model-&gt;getMeshTextureIndex();
              indexBufferData.SysMemPitch = 0;
              indexBufferData.SysMemSlicePitch = 0;
              CD3D11_BUFFER_DESC indexBufferDesc(sizeof(this-&gt;model-&gt;getMeshTextureIndex()[0])*m_indexCount, D3D11_BIND_INDEX_BUFFER);
              DX::ThrowIfFailed(
                  m_deviceResources-&gt;GetD3DDevice()-&gt;CreateBuffer(
                  &amp;indexBufferDesc,
                  &amp;indexBufferData,
                  &amp;m_indexBuffer
          )
          );
          });
  
          // Once the cube is loaded, the object is ready to be rendered.
          createCubeTask.then([this]() {
              m_loadingComplete = true;
          });
      }
  </code></pre>
  
  <p>Here is the render function:</p>
  
  <pre><code>    void TextureSceneRenderer::Render()
      {
          // Loading is asynchronous. Only draw geometry after it's loaded.
          if (!m_loadingComplete)
          {
             return;
          }
  
          auto context = m_deviceResources-&gt;GetD3DDeviceContext();
  
          // Set render targets to the screen.
          ID3D11RenderTargetView *const targets[1] = { m_deviceResources-&gt;GetBackBufferRenderTargetView() };
          context-&gt;OMSetRenderTargets(1, targets, m_deviceResources-&gt;GetDepthStencilView());
  
          // Prepare the constant buffer to send it to the graphics device.
          context-&gt;UpdateSubresource(
          m_constantBuffer.Get(),
          0,
          NULL,
          &amp;m_constantBufferData,
          0,
          0
          );
  
          // Each vertex is one instance of the VertexPositionColor struct.
          UINT stride = sizeof(VertexPositionColor);
          UINT offset = 0;
          context-&gt;IASetVertexBuffers(
          0,
          1,
          m_vertexBuffer.GetAddressOf(),
          &amp;stride,
          &amp;offset
      );
  
          context-&gt;IASetIndexBuffer(
          m_indexBuffer.Get(),
          DXGI_FORMAT_R16_UINT, // Each index is one 16-bit unsigned integer (short).
          0
      );
  
          context-&gt;IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  
          context-&gt;IASetInputLayout(m_inputLayout.Get());
  
          // Attach our vertex shader.
          context-&gt;VSSetShader(
          m_vertexShader.Get(),
          nullptr,
          0
      );
  
          // Send the constant buffer to the graphics device.
          context-&gt;VSSetConstantBuffers(
          0,
          1,
          m_constantBuffer.GetAddressOf()
      );
  
          // Attach our pixel shader.
          context-&gt;PSSetShader(
          m_pixelShader.Get(),
          nullptr,
          0
      );
  
          context-&gt;PSSetShaderResources(
              0,
              1,
              m_textureSRV.GetAddressOf()
      );
  
          context-&gt;PSSetSamplers(
              0,                          // starting at the first sampler slot
              1,                          // set one sampler binding
              m_sampler.GetAddressOf()
      );
  
          // Draw the objects.
          context-&gt;DrawIndexed(
              m_indexCount,
              0,
              0
      );
      }
  </code></pre>