title: Drawing spheres via DirectX12 Tool Kit
body: <p>I have three problems when trying to draw spheres:</p>
  
  <ol>
  <li>Sphere is glitching in some angles:
  <a href="https://i.stack.imgur.com/7fT1Y.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7fT1Y.jpg" alt="enter image description here"></a></li>
  <li>Don’t know how to apply a color to sphere</li>
  <li>Can’t draw two spheres at one - only one is shown. </li>
  </ol>
  
  <p>Code. Render:</p>
  
  <pre><code>void Game::Render()
  {
  if (m_timer.GetFrameCount() == 0)
  {
  return;
  }
  
  m_deviceResources-&gt;Prepare();
  Clear();
  
  auto commandList = m_deviceResources-&gt;GetCommandList();
  PIXBeginEvent(commandList, PIX_COLOR_DEFAULT, L"Render");
  
  auto commandList2 = m_deviceResources-&gt;GetCommandList();
  PIXBeginEvent(commandList2, PIX_COLOR_DEFAULT, L"Draw Sphere");
  
  m_shapeEffect-&gt;Apply(m_deviceResources-&gt;GetCommandList());
  
  for (int i(0); i &lt; vsphere; i++)
  {
  m_shapeEffect-&gt;SetWorld(d3dsphere[i]-&gt;world);
  d3dsphere[i]-&gt;Draw(commandList2);
  }
  
  PIXEndEvent(commandList2);
  PIXEndEvent(commandList);
  PIXBeginEvent(m_deviceResources-&gt;GetCommandQueue(), PIX_COLOR_DEFAULT, L"Present");
  m_deviceResources-&gt;Present();
  m_graphicsMemory-&gt;Commit(m_deviceResources-&gt;GetCommandQueue());
  PIXEndEvent(m_deviceResources-&gt;GetCommandQueue());
  }
  </code></pre>
  
  <p>How I create shape effect</p>
  
  <pre><code>EffectPipelineStateDescription pd(
  &amp;VertexPositionColor::InputLayout,
  CommonStates::Opaque,
  CommonStates::DepthNone,
  CommonStates::CullNone,
  rtState,
  D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE);
  m_shapeEffect = std::make_unique&lt;BasicEffect&gt;(device, EffectFlags::VertexColor, pd);
  </code></pre>
  
  <p><code>d3dsphere</code> is a <code>D3DSphere</code> class object; the constructor and <code>Draw</code> method</p>
  
  <pre><code>D3DSphere(float x, float y, float z, float radius, float r, float g, float b, float a)  
  {
  this-&gt;x = x;
  this-&gt;y = y;
  this-&gt;z = z;
  
  this-&gt;radius = radius;
  
  this-&gt;shape = GeometricPrimitive::CreateSphere(radius*2.0f);
  
  this-&gt;world = Matrix::Identity;
  this-&gt;world = XMMatrixMultiply(this-&gt;world, Matrix::CreateTranslation(x, y, z));
  this-&gt;index = vsphere;
  
  d3dsphere[vsphere] = this;
  vsphere++;
  }
  
  void D3DSphere::Draw(ID3d12GraphicsCommandList* commandList)
  {
     this-&gt;shape-&gt;Draw(commandList);
  }
  </code></pre>
  
  <p>Perhaps it will be useful if I figure, that the sphere radius(and everything in my scene) is very tiny(~10^-6)</p>
  
  <p>By unknown for me reasons, the code of examples in <a href="https://github.com/Microsoft/DirectXTK12/wiki" rel="nofollow noreferrer">Microsoft DirectX12 Tool Kit wiki page</a> in most is not compatible with the headers I have installed via NuGet packages - I have different constructors, different arguments in methods. I think the problem is that I am using Visual Studio 15, but Microsoft recommends to use at least 17(there are two different DirectX12TK NuGet packages - one for VS15 and one for VS17 and above). It is weird.</p>
  
  <hr>
  
  <p><strong>I solved</strong> the third problem by changing code in rendering:</p>
  
  <pre><code>for(int i(0);i&lt;vsphere;i++)
  {
  m_shapeEffect-&gt;SetMatrices(d3dsphere-&gt;world, m_view, m_projection);
  m_shapeEffect-&gt;Apply(m_deviceResources-&gt;GetCommandList());
  d3dsphere[i]-&gt;Draw();
  }
  </code></pre>
  
  <p>DirectX12TK NuGet package version I use is <code>2019.12.17.1</code></p>