Advertisement
Guest User

Renderer.cpp

a guest
Nov 22nd, 2013
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.56 KB | None | 0 0
  1. #include "pch.h"
  2. #include "Renderer.h"
  3.  
  4. using namespace DirectX;
  5. using namespace Microsoft::WRL;
  6. using namespace Windows::Foundation;
  7. using namespace Windows::UI::Core;
  8.  
  9. Renderer::Renderer() :
  10.     m_playerLoadComplete( false ),
  11.     m_asteroidsLoadComplete( false )
  12. {
  13. }
  14.  
  15. void Renderer::CreateDeviceResources()
  16. {
  17.     Direct3DBase::CreateDeviceResources();
  18.  
  19.     auto loadVSTask = DX::ReadDataAsync("SimpleVertexShader.cso");
  20.     auto loadPSTask = DX::ReadDataAsync("SimplePixelShader.cso");
  21.  
  22.     auto createVSTask = loadVSTask.then([this](Platform::Array<byte>^ fileData) {
  23.         DX::ThrowIfFailed(
  24.             m_d3dDevice->CreateVertexShader(
  25.                 fileData->Data,
  26.                 fileData->Length,
  27.                 nullptr,
  28.                 &m_vertexShader
  29.                 )
  30.             );
  31.  
  32.         const D3D11_INPUT_ELEMENT_DESC vertexDesc[] =
  33.         {
  34.             { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
  35.             { "COLOR",    0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  36.         };
  37.  
  38.         DX::ThrowIfFailed(
  39.             m_d3dDevice->CreateInputLayout(
  40.                 vertexDesc,
  41.                 ARRAYSIZE(vertexDesc),
  42.                 fileData->Data,
  43.                 fileData->Length,
  44.                 &m_inputLayout
  45.                 )
  46.             );
  47.     });
  48.  
  49.     auto createPSTask = loadPSTask.then([this](Platform::Array<byte>^ fileData) {
  50.         DX::ThrowIfFailed(
  51.             m_d3dDevice->CreatePixelShader(
  52.                 fileData->Data,
  53.                 fileData->Length,
  54.                 nullptr,
  55.                 &m_pixelShader
  56.                 )
  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.  
  69.     //PrepareObjects(createPSTask, createVSTask);
  70.  
  71.    
  72.     auto createPlayerTask = (createPSTask && createVSTask).then([this] () {
  73.         VertexPositionColor playerVertices[] =
  74.         {
  75.             {XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, 0.0f)},
  76.             {XMFLOAT3(-0.5f, -0.5f,  0.5f), XMFLOAT3(0.0f, 0.0f, 1.0f)},
  77.             {XMFLOAT3(-0.5f,  0.5f, -0.5f), XMFLOAT3(0.0f, 1.0f, 0.0f)},
  78.             {XMFLOAT3(-0.5f,  0.5f,  0.5f), XMFLOAT3(0.0f, 1.0f, 1.0f)},
  79.             {XMFLOAT3( 0.5f, -0.5f, -0.5f), XMFLOAT3(1.0f, 0.0f, 0.0f)},
  80.             {XMFLOAT3( 0.5f, -0.5f,  0.5f), XMFLOAT3(1.0f, 0.0f, 1.0f)},
  81.             {XMFLOAT3( 0.5f,  0.5f, -0.5f), XMFLOAT3(1.0f, 1.0f, 0.0f)},
  82.             {XMFLOAT3( 0.5f,  0.5f,  0.5f), XMFLOAT3(1.0f, 1.0f, 1.0f)},
  83.         };
  84.  
  85.         D3D11_SUBRESOURCE_DATA playerVertexBufferData = {0};
  86.         playerVertexBufferData.pSysMem = playerVertices;
  87.         playerVertexBufferData.SysMemPitch = 0;
  88.         playerVertexBufferData.SysMemSlicePitch = 0;
  89.         CD3D11_BUFFER_DESC playerVertexBufferDesc(sizeof(playerVertices), D3D11_BIND_VERTEX_BUFFER);
  90.         DX::ThrowIfFailed(
  91.             m_d3dDevice->CreateBuffer(
  92.                 &playerVertexBufferDesc,
  93.                 &playerVertexBufferData,
  94.                 &playerVertexBuffer
  95.                 )
  96.             );
  97.  
  98.         unsigned short playerIndices[] =
  99.         {
  100.             0,2,1, // -x
  101.             1,2,3,
  102.  
  103.             4,5,6, // +x
  104.             5,7,6,
  105.  
  106.             0,1,5, // -y
  107.             0,5,4,
  108.  
  109.             2,6,7, // +y
  110.             2,7,3,
  111.  
  112.             0,4,6, // -z
  113.             0,6,2,
  114.  
  115.             1,3,7, // +z
  116.             1,7,5,
  117.         };
  118.  
  119.         playerIndexCount = ARRAYSIZE(playerIndices);
  120.  
  121.         D3D11_SUBRESOURCE_DATA playerIndexBufferData = {0};
  122.         playerIndexBufferData.pSysMem = playerIndices;
  123.         playerIndexBufferData.SysMemPitch = 0;
  124.         playerIndexBufferData.SysMemSlicePitch = 0;
  125.         CD3D11_BUFFER_DESC playerIndexBufferDesc(sizeof(playerIndices), D3D11_BIND_INDEX_BUFFER);
  126.         DX::ThrowIfFailed(
  127.             m_d3dDevice->CreateBuffer(
  128.                 &playerIndexBufferDesc,
  129.                 &playerIndexBufferData,
  130.                 &playerIndexBuffer
  131.                 )
  132.             );
  133.     });
  134.  
  135.     createPlayerTask.then([this] () {
  136.         m_playerLoadComplete = true;
  137.     });
  138.  
  139.     // Asteroid
  140.  
  141.     auto createAsteroidTask = (createPSTask && createVSTask).then([this] () {
  142.        
  143.         VertexPositionColor asteroidVertices[] =
  144.         {
  145.             {XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, 0.0f)},
  146.             {XMFLOAT3(-0.5f, -0.5f,  0.5f), XMFLOAT3(0.0f, 0.0f, 1.0f)},
  147.             {XMFLOAT3(-0.5f,  0.5f, -0.5f), XMFLOAT3(0.0f, 1.0f, 0.0f)},
  148.             {XMFLOAT3(-0.5f,  0.5f,  0.5f), XMFLOAT3(0.0f, 1.0f, 1.0f)},
  149.             {XMFLOAT3( 0.5f, -0.5f, -0.5f), XMFLOAT3(1.0f, 0.0f, 0.0f)},
  150.             {XMFLOAT3( 0.5f, -0.5f,  0.5f), XMFLOAT3(1.0f, 0.0f, 1.0f)},
  151.             {XMFLOAT3( 0.5f,  0.5f, -0.5f), XMFLOAT3(1.0f, 1.0f, 0.0f)},
  152.             {XMFLOAT3( 0.5f,  0.5f,  0.5f), XMFLOAT3(1.0f, 1.0f, 1.0f)},
  153.         };
  154.  
  155.         unsigned short asteroidIndices[] =
  156.         {
  157.             0,2,1, // -x
  158.             1,2,3,
  159.  
  160.             4,5,6, // +x
  161.             5,7,6,
  162.  
  163.             0,1,5, // -y
  164.             0,5,4,
  165.  
  166.             2,6,7, // +y
  167.             2,7,3,
  168.  
  169.             0,4,6, // -z
  170.             0,6,2,
  171.  
  172.             1,3,7, // +z
  173.             1,7,5,
  174.         };
  175.        
  176.  
  177.         int asteroidCount = 3;
  178.  
  179.         asteroidVertexBuffers.reserve( asteroidCount );
  180.         asteroidIndexBuffers.reserve( asteroidCount );
  181.         asteroidIndexCounts.reserve( asteroidCount );
  182.  
  183.  
  184.         /*for(vector<Microsoft::WRL::ComPtr<ID3D11Buffer>>::iterator asteroid = asteroidVertexBuffers.begin(); asteroid != asteroidVertexBuffers.end(); ++asteroid)
  185.         {
  186.         }
  187.  
  188.         for(vector<Microsoft::WRL::ComPtr<ID3D11Buffer>>::iterator asteroid = asteroidIndexBuffers.begin(); asteroid != asteroidIndexBuffers.end(); ++asteroid)
  189.         {
  190.         }
  191.  
  192.         for(vector<uint32>::iterator asteroid = asteroidIndexCounts.begin(); asteroid != asteroidIndexCounts.end(); ++asteroid)
  193.         {
  194.         }*/
  195.        
  196.         for( int a = 0; a < asteroidCount; a++ )
  197.         {
  198.             D3D11_SUBRESOURCE_DATA asteroidVertexBufferData = {0};
  199.             asteroidVertexBufferData.pSysMem = asteroidVertices;
  200.             asteroidVertexBufferData.SysMemPitch = 0;
  201.             asteroidVertexBufferData.SysMemSlicePitch = 0;
  202.             CD3D11_BUFFER_DESC asteroidVertexBufferDesc(sizeof(asteroidVertices), D3D11_BIND_VERTEX_BUFFER);
  203.             DX::ThrowIfFailed(
  204.                 m_d3dDevice->CreateBuffer(
  205.                     &asteroidVertexBufferDesc,
  206.                     &asteroidVertexBufferData,
  207.                     &asteroidVertexBuffers.at( a )
  208.                     )
  209.                 );
  210.  
  211.             asteroidIndexCounts.at( a ) = ARRAYSIZE(asteroidIndices);
  212.  
  213.             D3D11_SUBRESOURCE_DATA asteroidIndexBufferData = {0};
  214.             asteroidIndexBufferData.pSysMem = asteroidIndices;
  215.             asteroidIndexBufferData.SysMemPitch = 0;
  216.             asteroidIndexBufferData.SysMemSlicePitch = 0;
  217.             CD3D11_BUFFER_DESC asteroidIndexBufferDesc(sizeof(asteroidIndices), D3D11_BIND_INDEX_BUFFER);
  218.             DX::ThrowIfFailed(
  219.                 m_d3dDevice->CreateBuffer(
  220.                     &asteroidIndexBufferDesc,
  221.                     &asteroidIndexBufferData,
  222.                     &asteroidIndexBuffers.at( a )
  223.                     )
  224.                 );
  225.         }
  226.     });
  227.  
  228.     createAsteroidTask.then([this] () {
  229.         m_asteroidsLoadComplete = true;
  230.     });
  231. }
  232.  
  233. void Renderer::PrepareObjects( Concurrency::task<void> createPSTask, Concurrency::task<void> createVSTask )
  234. {
  235. }
  236.  
  237. void Renderer::CreateWindowSizeDependentResources()
  238. {
  239.     Direct3DBase::CreateWindowSizeDependentResources();
  240.  
  241.     float aspectRatio = m_windowBounds.Width / m_windowBounds.Height;
  242.     float fovAngleY = 70.0f * XM_PI / 180.0f;
  243.     if (aspectRatio < 1.0f)
  244.     {
  245.         fovAngleY /= aspectRatio;
  246.     }
  247.  
  248.     XMStoreFloat4x4(
  249.         &m_constantBufferData.projection,
  250.         XMMatrixTranspose(
  251.             XMMatrixPerspectiveFovRH(
  252.                 fovAngleY,
  253.                 aspectRatio,
  254.                 0.01f,
  255.                 100.0f
  256.                 )
  257.             )
  258.         );
  259. }
  260.  
  261. void Renderer::SetUpViewingAngle()
  262. {
  263.     XMVECTOR eye = XMVectorSet(0.0f, 0.7f, 1.5f, 0.0f);
  264.     XMVECTOR at = XMVectorSet(0.0f, -0.1f, 0.0f, 0.0f);
  265.     XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
  266.  
  267.     XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixTranspose(XMMatrixLookAtRH(eye, at, up)));
  268. }
  269.  
  270. //float posX, float posY, float posZ, float rotX, float rotY, float rotZ
  271. void Renderer::Update( Placement^ placement )
  272. {
  273.     // Rotation
  274.     XMMATRIX m = XMMatrixRotationX( placement->GetRotX() );
  275.     m = XMMatrixMultiply(m, XMMatrixRotationY( placement->GetRotY() ) );
  276.     m = XMMatrixMultiply(m, XMMatrixRotationZ( placement->GetRotZ() ) );
  277.  
  278.     // Translation
  279.     m = XMMatrixMultiply(m, XMMatrixTranslation( placement->GetPosX(), placement->GetPosY(), placement->GetPosZ() ) );
  280.  
  281.     // Store
  282.     XMStoreFloat4x4( &m_constantBufferData.model, XMMatrixTranspose(m) );
  283. }
  284.  
  285. void Renderer::UpdateAsteroid( Placement^ placement )
  286. {
  287.     // Translation
  288.     XMMATRIX m = XMMatrixTranslation( placement->GetPosX(), placement->GetPosY(), placement->GetPosZ() );
  289.  
  290.     // Rotation
  291.     m = XMMatrixMultiply(m, XMMatrixRotationX( placement->GetRotX() ) );
  292.     m = XMMatrixMultiply(m, XMMatrixRotationY( placement->GetRotY() ) );
  293.     m = XMMatrixMultiply(m, XMMatrixRotationZ( placement->GetRotZ() ) );
  294.  
  295.     // Store
  296.     XMStoreFloat4x4( &m_constantBufferData.model, XMMatrixTranspose(m) );
  297. }
  298.  
  299. void Renderer::RenderBackground()
  300. {
  301.     const float midnightBlue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
  302.     m_d3dContext->ClearRenderTargetView(
  303.         m_renderTargetView.Get(),
  304.         midnightBlue
  305.         );
  306.  
  307.     m_d3dContext->ClearDepthStencilView(
  308.         m_depthStencilView.Get(),
  309.         D3D11_CLEAR_DEPTH,
  310.         1.0f,
  311.         0
  312.         );
  313. }
  314.  
  315. void Renderer::PreparePlayer()
  316. {
  317.     // Only draw the objects once they are loaded (loading is asynchronous).
  318.     if ( !m_playerLoadComplete )
  319.     {
  320.         return;
  321.     }
  322.     GetBuffer();
  323.    
  324.     UINT stride = sizeof(VertexPositionColor);
  325.     UINT offset = 0;
  326.     m_d3dContext->IASetVertexBuffers(
  327.         0,
  328.         1,
  329.         playerVertexBuffer.GetAddressOf(),
  330.         &stride,
  331.         &offset
  332.         );
  333.  
  334.     m_d3dContext->IASetIndexBuffer(
  335.         playerIndexBuffer.Get(),
  336.         DXGI_FORMAT_R16_UINT,
  337.         0
  338.         );
  339.  
  340.     m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  341.  
  342.     m_d3dContext->IASetInputLayout(m_inputLayout.Get());
  343.  
  344.     m_d3dContext->VSSetShader(
  345.         m_vertexShader.Get(),
  346.         nullptr,
  347.         0
  348.         );
  349.  
  350.     m_d3dContext->VSSetConstantBuffers(
  351.         0,
  352.         1,
  353.         m_constantBuffer.GetAddressOf()
  354.         );
  355.  
  356.     m_d3dContext->PSSetShader(
  357.         m_pixelShader.Get(),
  358.         nullptr,
  359.         0
  360.         );
  361.  
  362.     m_d3dContext->DrawIndexed(
  363.         playerIndexCount,
  364.         0,
  365.         0
  366.         );
  367. }
  368.  
  369. void Renderer::PrepareAsteroids( int index )
  370. {
  371.     // Only draw the objects once they are loaded (loading is asynchronous).
  372.     if ( !m_asteroidsLoadComplete )
  373.     {
  374.         return;
  375.     }
  376.     GetBuffer();
  377.    
  378.     UINT stride = sizeof(VertexPositionColor);
  379.     UINT offset = 0;
  380.     m_d3dContext->IASetVertexBuffers(
  381.         0,
  382.         1,
  383.         asteroidVertexBuffers.at( index ).GetAddressOf(),
  384.         &stride,
  385.         &offset
  386.         );
  387.  
  388.     m_d3dContext->IASetIndexBuffer(
  389.         asteroidIndexBuffers.at( index ).Get(),
  390.         DXGI_FORMAT_R16_UINT,
  391.         0
  392.         );
  393.  
  394.     m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  395.  
  396.     m_d3dContext->IASetInputLayout(m_inputLayout.Get());
  397.  
  398.     m_d3dContext->VSSetShader(
  399.         m_vertexShader.Get(),
  400.         nullptr,
  401.         0
  402.         );
  403.  
  404.     m_d3dContext->VSSetConstantBuffers(
  405.         0,
  406.         1,
  407.         m_constantBuffer.GetAddressOf()
  408.         );
  409.  
  410.     m_d3dContext->PSSetShader(
  411.         m_pixelShader.Get(),
  412.         nullptr,
  413.         0
  414.         );
  415.  
  416.     m_d3dContext->DrawIndexed(
  417.         asteroidIndexCounts.at( index ),
  418.         0,
  419.         0
  420.         );
  421. }
  422.  
  423. void Renderer::GetBuffer()
  424. {
  425.     m_d3dContext->OMSetRenderTargets(
  426.         1,
  427.         m_renderTargetView.GetAddressOf(),
  428.         m_depthStencilView.Get()
  429.         );
  430.  
  431.     m_d3dContext->UpdateSubresource(
  432.         m_constantBuffer.Get(),
  433.         0,
  434.         NULL,
  435.         &m_constantBufferData,
  436.         0,
  437.         0
  438.         );
  439. }
  440.  
  441. void Renderer::Render()
  442. {
  443. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement