CubeRenderer.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // clang-format off
  2. #include "pch.h"
  3. #include "CubeRenderer.h"
  4. // clang-format on
  5. using namespace DirectX;
  6. using namespace Microsoft::WRL;
  7. using namespace Windows::Foundation;
  8. using namespace Windows::UI::Core;
  9. using namespace JusticeLeagueWinRT;
  10. CubeRenderer::CubeRenderer()
  11. : m_loadingComplete(false)
  12. , m_indexCount(0)
  13. {
  14. // Create a new WinRT object to validate that we can link properly
  15. Batman ^ hero = ref new Batman();
  16. hero->savePeople();
  17. }
  18. void CubeRenderer::CreateDeviceResources()
  19. {
  20. Direct3DBase::CreateDeviceResources();
  21. auto loadVSTask = DX::ReadDataAsync("SimpleVertexShader.cso");
  22. auto loadPSTask = DX::ReadDataAsync("SimplePixelShader.cso");
  23. auto createVSTask =
  24. loadVSTask.then([this](Platform::Array<byte> ^ fileData) {
  25. DX::ThrowIfFailed(m_d3dDevice->CreateVertexShader(
  26. fileData->Data, fileData->Length, nullptr, &m_vertexShader));
  27. const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = {
  28. { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,
  29. D3D11_INPUT_PER_VERTEX_DATA, 0 },
  30. { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12,
  31. D3D11_INPUT_PER_VERTEX_DATA, 0 },
  32. };
  33. DX::ThrowIfFailed(m_d3dDevice->CreateInputLayout(
  34. vertexDesc, ARRAYSIZE(vertexDesc), fileData->Data, fileData->Length,
  35. &m_inputLayout));
  36. });
  37. auto createPSTask =
  38. loadPSTask.then([this](Platform::Array<byte> ^ fileData) {
  39. DX::ThrowIfFailed(m_d3dDevice->CreatePixelShader(
  40. fileData->Data, fileData->Length, nullptr, &m_pixelShader));
  41. CD3D11_BUFFER_DESC constantBufferDesc(
  42. sizeof(ModelViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
  43. DX::ThrowIfFailed(m_d3dDevice->CreateBuffer(&constantBufferDesc, nullptr,
  44. &m_constantBuffer));
  45. });
  46. auto createCubeTask = (createPSTask && createVSTask).then([this]() {
  47. VertexPositionColor cubeVertices[] = {
  48. { XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, 0.0f) },
  49. { XMFLOAT3(-0.5f, -0.5f, 0.5f), XMFLOAT3(0.0f, 0.0f, 1.0f) },
  50. { XMFLOAT3(-0.5f, 0.5f, -0.5f), XMFLOAT3(0.0f, 1.0f, 0.0f) },
  51. { XMFLOAT3(-0.5f, 0.5f, 0.5f), XMFLOAT3(0.0f, 1.0f, 1.0f) },
  52. { XMFLOAT3(0.5f, -0.5f, -0.5f), XMFLOAT3(1.0f, 0.0f, 0.0f) },
  53. { XMFLOAT3(0.5f, -0.5f, 0.5f), XMFLOAT3(1.0f, 0.0f, 1.0f) },
  54. { XMFLOAT3(0.5f, 0.5f, -0.5f), XMFLOAT3(1.0f, 1.0f, 0.0f) },
  55. { XMFLOAT3(0.5f, 0.5f, 0.5f), XMFLOAT3(1.0f, 1.0f, 1.0f) },
  56. };
  57. D3D11_SUBRESOURCE_DATA vertexBufferData = { 0 };
  58. vertexBufferData.pSysMem = cubeVertices;
  59. vertexBufferData.SysMemPitch = 0;
  60. vertexBufferData.SysMemSlicePitch = 0;
  61. CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(cubeVertices),
  62. D3D11_BIND_VERTEX_BUFFER);
  63. DX::ThrowIfFailed(m_d3dDevice->CreateBuffer(
  64. &vertexBufferDesc, &vertexBufferData, &m_vertexBuffer));
  65. unsigned short cubeIndices[] = {
  66. 0, 2, 1, // -x
  67. 1, 2, 3,
  68. 4, 5, 6, // +x
  69. 5, 7, 6,
  70. 0, 1, 5, // -y
  71. 0, 5, 4,
  72. 2, 6, 7, // +y
  73. 2, 7, 3,
  74. 0, 4, 6, // -z
  75. 0, 6, 2,
  76. 1, 3, 7, // +z
  77. 1, 7, 5,
  78. };
  79. m_indexCount = ARRAYSIZE(cubeIndices);
  80. D3D11_SUBRESOURCE_DATA indexBufferData = { 0 };
  81. indexBufferData.pSysMem = cubeIndices;
  82. indexBufferData.SysMemPitch = 0;
  83. indexBufferData.SysMemSlicePitch = 0;
  84. CD3D11_BUFFER_DESC indexBufferDesc(sizeof(cubeIndices),
  85. D3D11_BIND_INDEX_BUFFER);
  86. DX::ThrowIfFailed(m_d3dDevice->CreateBuffer(
  87. &indexBufferDesc, &indexBufferData, &m_indexBuffer));
  88. });
  89. createCubeTask.then([this]() { m_loadingComplete = true; });
  90. }
  91. void CubeRenderer::CreateWindowSizeDependentResources()
  92. {
  93. Direct3DBase::CreateWindowSizeDependentResources();
  94. float aspectRatio = m_windowBounds.Width / m_windowBounds.Height;
  95. float fovAngleY = 70.0f * XM_PI / 180.0f;
  96. if (aspectRatio < 1.0f) {
  97. fovAngleY /= aspectRatio;
  98. }
  99. // Note that the m_orientationTransform3D matrix is post-multiplied here
  100. // in order to correctly orient the scene to match the display orientation.
  101. // This post-multiplication step is required for any draw calls that are
  102. // made to the swap chain render target. For draw calls to other targets,
  103. // this transform should not be applied.
  104. XMStoreFloat4x4(
  105. &m_constantBufferData.projection,
  106. XMMatrixTranspose(XMMatrixMultiply(
  107. XMMatrixPerspectiveFovRH(fovAngleY, aspectRatio, 0.01f, 100.0f),
  108. XMLoadFloat4x4(&m_orientationTransform3D))));
  109. }
  110. void CubeRenderer::Update(float timeTotal, float timeDelta)
  111. {
  112. (void)timeDelta; // Unused parameter.
  113. XMVECTOR eye = XMVectorSet(0.0f, 0.7f, 1.5f, 0.0f);
  114. XMVECTOR at = XMVectorSet(0.0f, -0.1f, 0.0f, 0.0f);
  115. XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
  116. XMStoreFloat4x4(&m_constantBufferData.view,
  117. XMMatrixTranspose(XMMatrixLookAtRH(eye, at, up)));
  118. XMStoreFloat4x4(&m_constantBufferData.model,
  119. XMMatrixTranspose(XMMatrixRotationY(timeTotal * XM_PIDIV4)));
  120. }
  121. void CubeRenderer::Render()
  122. {
  123. const float midnightBlue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
  124. m_d3dContext->ClearRenderTargetView(m_renderTargetView.Get(), midnightBlue);
  125. m_d3dContext->ClearDepthStencilView(m_depthStencilView.Get(),
  126. D3D11_CLEAR_DEPTH, 1.0f, 0);
  127. // Only draw the cube once it is loaded (loading is asynchronous).
  128. if (!m_loadingComplete) {
  129. return;
  130. }
  131. m_d3dContext->OMSetRenderTargets(1, m_renderTargetView.GetAddressOf(),
  132. m_depthStencilView.Get());
  133. m_d3dContext->UpdateSubresource(m_constantBuffer.Get(), 0, NULL,
  134. &m_constantBufferData, 0, 0);
  135. UINT stride = sizeof(VertexPositionColor);
  136. UINT offset = 0;
  137. m_d3dContext->IASetVertexBuffers(0, 1, m_vertexBuffer.GetAddressOf(),
  138. &stride, &offset);
  139. m_d3dContext->IASetIndexBuffer(m_indexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0);
  140. m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  141. m_d3dContext->IASetInputLayout(m_inputLayout.Get());
  142. m_d3dContext->VSSetShader(m_vertexShader.Get(), nullptr, 0);
  143. m_d3dContext->VSSetConstantBuffers(0, 1, m_constantBuffer.GetAddressOf());
  144. m_d3dContext->PSSetShader(m_pixelShader.Get(), nullptr, 0);
  145. m_d3dContext->DrawIndexed(m_indexCount, 0, 0);
  146. }