CubeRenderer.cpp 6.1 KB

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