CubeRenderer.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 = loadVSTask.then([this](Platform::Array<byte>^ fileData) {
  18. DX::ThrowIfFailed(
  19. m_d3dDevice->CreateVertexShader(
  20. fileData->Data,
  21. fileData->Length,
  22. nullptr,
  23. &m_vertexShader
  24. )
  25. );
  26. const D3D11_INPUT_ELEMENT_DESC vertexDesc[] =
  27. {
  28. { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  29. { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  30. };
  31. DX::ThrowIfFailed(
  32. m_d3dDevice->CreateInputLayout(
  33. vertexDesc,
  34. ARRAYSIZE(vertexDesc),
  35. fileData->Data,
  36. fileData->Length,
  37. &m_inputLayout
  38. )
  39. );
  40. });
  41. auto createPSTask = loadPSTask.then([this](Platform::Array<byte>^ fileData) {
  42. DX::ThrowIfFailed(
  43. m_d3dDevice->CreatePixelShader(
  44. fileData->Data,
  45. fileData->Length,
  46. nullptr,
  47. &m_pixelShader
  48. )
  49. );
  50. CD3D11_BUFFER_DESC constantBufferDesc(sizeof(ModelViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
  51. DX::ThrowIfFailed(
  52. m_d3dDevice->CreateBuffer(
  53. &constantBufferDesc,
  54. nullptr,
  55. &m_constantBuffer
  56. )
  57. );
  58. });
  59. auto createCubeTask = (createPSTask && createVSTask).then([this] () {
  60. VertexPositionColor cubeVertices[] =
  61. {
  62. {XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, 0.0f)},
  63. {XMFLOAT3(-0.5f, -0.5f, 0.5f), XMFLOAT3(0.0f, 0.0f, 1.0f)},
  64. {XMFLOAT3(-0.5f, 0.5f, -0.5f), XMFLOAT3(0.0f, 1.0f, 0.0f)},
  65. {XMFLOAT3(-0.5f, 0.5f, 0.5f), XMFLOAT3(0.0f, 1.0f, 1.0f)},
  66. {XMFLOAT3( 0.5f, -0.5f, -0.5f), XMFLOAT3(1.0f, 0.0f, 0.0f)},
  67. {XMFLOAT3( 0.5f, -0.5f, 0.5f), XMFLOAT3(1.0f, 0.0f, 1.0f)},
  68. {XMFLOAT3( 0.5f, 0.5f, -0.5f), XMFLOAT3(1.0f, 1.0f, 0.0f)},
  69. {XMFLOAT3( 0.5f, 0.5f, 0.5f), XMFLOAT3(1.0f, 1.0f, 1.0f)},
  70. };
  71. D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
  72. vertexBufferData.pSysMem = cubeVertices;
  73. vertexBufferData.SysMemPitch = 0;
  74. vertexBufferData.SysMemSlicePitch = 0;
  75. CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(cubeVertices), D3D11_BIND_VERTEX_BUFFER);
  76. DX::ThrowIfFailed(
  77. m_d3dDevice->CreateBuffer(
  78. &vertexBufferDesc,
  79. &vertexBufferData,
  80. &m_vertexBuffer
  81. )
  82. );
  83. unsigned short cubeIndices[] =
  84. {
  85. 0,2,1, // -x
  86. 1,2,3,
  87. 4,5,6, // +x
  88. 5,7,6,
  89. 0,1,5, // -y
  90. 0,5,4,
  91. 2,6,7, // +y
  92. 2,7,3,
  93. 0,4,6, // -z
  94. 0,6,2,
  95. 1,3,7, // +z
  96. 1,7,5,
  97. };
  98. m_indexCount = ARRAYSIZE(cubeIndices);
  99. D3D11_SUBRESOURCE_DATA indexBufferData = {0};
  100. indexBufferData.pSysMem = cubeIndices;
  101. indexBufferData.SysMemPitch = 0;
  102. indexBufferData.SysMemSlicePitch = 0;
  103. CD3D11_BUFFER_DESC indexBufferDesc(sizeof(cubeIndices), D3D11_BIND_INDEX_BUFFER);
  104. DX::ThrowIfFailed(
  105. m_d3dDevice->CreateBuffer(
  106. &indexBufferDesc,
  107. &indexBufferData,
  108. &m_indexBuffer
  109. )
  110. );
  111. });
  112. createCubeTask.then([this] () {
  113. m_loadingComplete = true;
  114. });
  115. }
  116. void CubeRenderer::CreateWindowSizeDependentResources()
  117. {
  118. Direct3DBase::CreateWindowSizeDependentResources();
  119. float aspectRatio = m_windowBounds.Width / m_windowBounds.Height;
  120. float fovAngleY = 70.0f * XM_PI / 180.0f;
  121. if (aspectRatio < 1.0f)
  122. {
  123. fovAngleY /= aspectRatio;
  124. }
  125. // Note that the m_orientationTransform3D matrix is post-multiplied here
  126. // in order to correctly orient the scene to match the display orientation.
  127. // This post-multiplication step is required for any draw calls that are
  128. // made to the swap chain render target. For draw calls to other targets,
  129. // this transform should not be applied.
  130. XMStoreFloat4x4(
  131. &m_constantBufferData.projection,
  132. XMMatrixTranspose(
  133. XMMatrixMultiply(
  134. XMMatrixPerspectiveFovRH(
  135. fovAngleY,
  136. aspectRatio,
  137. 0.01f,
  138. 100.0f
  139. ),
  140. XMLoadFloat4x4(&m_orientationTransform3D)
  141. )
  142. )
  143. );
  144. }
  145. void CubeRenderer::Update(float timeTotal, float timeDelta)
  146. {
  147. (void) timeDelta; // Unused parameter.
  148. XMVECTOR eye = XMVectorSet(0.0f, 0.7f, 1.5f, 0.0f);
  149. XMVECTOR at = XMVectorSet(0.0f, -0.1f, 0.0f, 0.0f);
  150. XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
  151. XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixTranspose(XMMatrixLookAtRH(eye, at, up)));
  152. XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixTranspose(XMMatrixRotationY(timeTotal * XM_PIDIV4)));
  153. }
  154. void CubeRenderer::Render()
  155. {
  156. const float midnightBlue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
  157. m_d3dContext->ClearRenderTargetView(
  158. m_renderTargetView.Get(),
  159. midnightBlue
  160. );
  161. m_d3dContext->ClearDepthStencilView(
  162. m_depthStencilView.Get(),
  163. D3D11_CLEAR_DEPTH,
  164. 1.0f,
  165. 0
  166. );
  167. // Only draw the cube once it is loaded (loading is asynchronous).
  168. if (!m_loadingComplete)
  169. {
  170. return;
  171. }
  172. m_d3dContext->OMSetRenderTargets(
  173. 1,
  174. m_renderTargetView.GetAddressOf(),
  175. m_depthStencilView.Get()
  176. );
  177. m_d3dContext->UpdateSubresource(
  178. m_constantBuffer.Get(),
  179. 0,
  180. NULL,
  181. &m_constantBufferData,
  182. 0,
  183. 0
  184. );
  185. UINT stride = sizeof(VertexPositionColor);
  186. UINT offset = 0;
  187. m_d3dContext->IASetVertexBuffers(
  188. 0,
  189. 1,
  190. m_vertexBuffer.GetAddressOf(),
  191. &stride,
  192. &offset
  193. );
  194. m_d3dContext->IASetIndexBuffer(
  195. m_indexBuffer.Get(),
  196. DXGI_FORMAT_R16_UINT,
  197. 0
  198. );
  199. m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  200. m_d3dContext->IASetInputLayout(m_inputLayout.Get());
  201. m_d3dContext->VSSetShader(
  202. m_vertexShader.Get(),
  203. nullptr,
  204. 0
  205. );
  206. m_d3dContext->VSSetConstantBuffers(
  207. 0,
  208. 1,
  209. m_constantBuffer.GetAddressOf()
  210. );
  211. m_d3dContext->PSSetShader(
  212. m_pixelShader.Get(),
  213. nullptr,
  214. 0
  215. );
  216. m_d3dContext->DrawIndexed(
  217. m_indexCount,
  218. 0,
  219. 0
  220. );
  221. }