CubeRenderer.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "pch.h"
  11. #include "CubeRenderer.h"
  12. using namespace DirectX;
  13. using namespace Microsoft::WRL;
  14. using namespace Windows::Foundation;
  15. using namespace Windows::UI::Core;
  16. CubeRenderer::CubeRenderer() :
  17. m_loadingComplete(false),
  18. m_indexCount(0)
  19. {
  20. }
  21. void CubeRenderer::CreateDeviceResources()
  22. {
  23. Direct3DBase::CreateDeviceResources();
  24. auto loadVSTask = DX::ReadDataAsync("SimpleVertexShader.cso");
  25. auto loadPSTask = DX::ReadDataAsync("SimplePixelShader.cso");
  26. auto createVSTask = loadVSTask.then([this](Platform::Array<byte>^ fileData) {
  27. DX::ThrowIfFailed(
  28. m_d3dDevice->CreateVertexShader(
  29. fileData->Data,
  30. fileData->Length,
  31. nullptr,
  32. &m_vertexShader
  33. )
  34. );
  35. const D3D11_INPUT_ELEMENT_DESC vertexDesc[] =
  36. {
  37. { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  38. { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  39. };
  40. DX::ThrowIfFailed(
  41. m_d3dDevice->CreateInputLayout(
  42. vertexDesc,
  43. ARRAYSIZE(vertexDesc),
  44. fileData->Data,
  45. fileData->Length,
  46. &m_inputLayout
  47. )
  48. );
  49. });
  50. auto createPSTask = loadPSTask.then([this](Platform::Array<byte>^ fileData) {
  51. DX::ThrowIfFailed(
  52. m_d3dDevice->CreatePixelShader(
  53. fileData->Data,
  54. fileData->Length,
  55. nullptr,
  56. &m_pixelShader
  57. )
  58. );
  59. CD3D11_BUFFER_DESC constantBufferDesc(sizeof(ModelViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
  60. DX::ThrowIfFailed(
  61. m_d3dDevice->CreateBuffer(
  62. &constantBufferDesc,
  63. nullptr,
  64. &m_constantBuffer
  65. )
  66. );
  67. });
  68. auto createCubeTask = (createPSTask && createVSTask).then([this] () {
  69. VertexPositionColor cubeVertices[] =
  70. {
  71. {XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, 0.0f)},
  72. {XMFLOAT3(-0.5f, -0.5f, 0.5f), XMFLOAT3(0.0f, 0.0f, 1.0f)},
  73. {XMFLOAT3(-0.5f, 0.5f, -0.5f), XMFLOAT3(0.0f, 1.0f, 0.0f)},
  74. {XMFLOAT3(-0.5f, 0.5f, 0.5f), XMFLOAT3(0.0f, 1.0f, 1.0f)},
  75. {XMFLOAT3( 0.5f, -0.5f, -0.5f), XMFLOAT3(1.0f, 0.0f, 0.0f)},
  76. {XMFLOAT3( 0.5f, -0.5f, 0.5f), XMFLOAT3(1.0f, 0.0f, 1.0f)},
  77. {XMFLOAT3( 0.5f, 0.5f, -0.5f), XMFLOAT3(1.0f, 1.0f, 0.0f)},
  78. {XMFLOAT3( 0.5f, 0.5f, 0.5f), XMFLOAT3(1.0f, 1.0f, 1.0f)},
  79. };
  80. D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
  81. vertexBufferData.pSysMem = cubeVertices;
  82. vertexBufferData.SysMemPitch = 0;
  83. vertexBufferData.SysMemSlicePitch = 0;
  84. CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(cubeVertices), D3D11_BIND_VERTEX_BUFFER);
  85. DX::ThrowIfFailed(
  86. m_d3dDevice->CreateBuffer(
  87. &vertexBufferDesc,
  88. &vertexBufferData,
  89. &m_vertexBuffer
  90. )
  91. );
  92. unsigned short cubeIndices[] =
  93. {
  94. 0,2,1, // -x
  95. 1,2,3,
  96. 4,5,6, // +x
  97. 5,7,6,
  98. 0,1,5, // -y
  99. 0,5,4,
  100. 2,6,7, // +y
  101. 2,7,3,
  102. 0,4,6, // -z
  103. 0,6,2,
  104. 1,3,7, // +z
  105. 1,7,5,
  106. };
  107. m_indexCount = ARRAYSIZE(cubeIndices);
  108. D3D11_SUBRESOURCE_DATA indexBufferData = {0};
  109. indexBufferData.pSysMem = cubeIndices;
  110. indexBufferData.SysMemPitch = 0;
  111. indexBufferData.SysMemSlicePitch = 0;
  112. CD3D11_BUFFER_DESC indexBufferDesc(sizeof(cubeIndices), D3D11_BIND_INDEX_BUFFER);
  113. DX::ThrowIfFailed(
  114. m_d3dDevice->CreateBuffer(
  115. &indexBufferDesc,
  116. &indexBufferData,
  117. &m_indexBuffer
  118. )
  119. );
  120. });
  121. createCubeTask.then([this] () {
  122. m_loadingComplete = true;
  123. });
  124. }
  125. void CubeRenderer::CreateWindowSizeDependentResources()
  126. {
  127. Direct3DBase::CreateWindowSizeDependentResources();
  128. float aspectRatio = m_windowBounds.Width / m_windowBounds.Height;
  129. float fovAngleY = 70.0f * XM_PI / 180.0f;
  130. if (aspectRatio < 1.0f)
  131. {
  132. fovAngleY /= aspectRatio;
  133. }
  134. XMStoreFloat4x4(
  135. &m_constantBufferData.projection,
  136. XMMatrixTranspose(
  137. XMMatrixPerspectiveFovRH(
  138. fovAngleY,
  139. aspectRatio,
  140. 0.01f,
  141. 100.0f
  142. )
  143. )
  144. );
  145. }
  146. void CubeRenderer::Update(float timeTotal, float timeDelta)
  147. {
  148. (void) timeDelta; // Unused parameter.
  149. XMVECTOR eye = XMVectorSet(0.0f, 0.7f, 1.5f, 0.0f);
  150. XMVECTOR at = XMVectorSet(0.0f, -0.1f, 0.0f, 0.0f);
  151. XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
  152. XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixTranspose(XMMatrixLookAtRH(eye, at, up)));
  153. XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixTranspose(XMMatrixRotationY(timeTotal * XM_PIDIV4)));
  154. }
  155. void CubeRenderer::Render()
  156. {
  157. const float midnightBlue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
  158. m_d3dContext->ClearRenderTargetView(
  159. m_renderTargetView.Get(),
  160. midnightBlue
  161. );
  162. m_d3dContext->ClearDepthStencilView(
  163. m_depthStencilView.Get(),
  164. D3D11_CLEAR_DEPTH,
  165. 1.0f,
  166. 0
  167. );
  168. // Only draw the cube once it is loaded (loading is asynchronous).
  169. if (!m_loadingComplete)
  170. {
  171. return;
  172. }
  173. m_d3dContext->OMSetRenderTargets(
  174. 1,
  175. m_renderTargetView.GetAddressOf(),
  176. m_depthStencilView.Get()
  177. );
  178. m_d3dContext->UpdateSubresource(
  179. m_constantBuffer.Get(),
  180. 0,
  181. NULL,
  182. &m_constantBufferData,
  183. 0,
  184. 0
  185. );
  186. UINT stride = sizeof(VertexPositionColor);
  187. UINT offset = 0;
  188. m_d3dContext->IASetVertexBuffers(
  189. 0,
  190. 1,
  191. m_vertexBuffer.GetAddressOf(),
  192. &stride,
  193. &offset
  194. );
  195. m_d3dContext->IASetIndexBuffer(
  196. m_indexBuffer.Get(),
  197. DXGI_FORMAT_R16_UINT,
  198. 0
  199. );
  200. m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  201. m_d3dContext->IASetInputLayout(m_inputLayout.Get());
  202. m_d3dContext->VSSetShader(
  203. m_vertexShader.Get(),
  204. nullptr,
  205. 0
  206. );
  207. m_d3dContext->VSSetConstantBuffers(
  208. 0,
  209. 1,
  210. m_constantBuffer.GetAddressOf()
  211. );
  212. m_d3dContext->PSSetShader(
  213. m_pixelShader.Get(),
  214. nullptr,
  215. 0
  216. );
  217. m_d3dContext->DrawIndexed(
  218. m_indexCount,
  219. 0,
  220. 0
  221. );
  222. }
粤ICP备19079148号