Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include "Renderer.h"
- using namespace DirectX;
- using namespace Microsoft::WRL;
- using namespace Windows::Foundation;
- using namespace Windows::UI::Core;
- Renderer::Renderer() :
- m_playerLoadComplete( false ),
- m_asteroidsLoadComplete( false )
- {
- }
- void Renderer::CreateDeviceResources()
- {
- Direct3DBase::CreateDeviceResources();
- auto loadVSTask = DX::ReadDataAsync("SimpleVertexShader.cso");
- auto loadPSTask = DX::ReadDataAsync("SimplePixelShader.cso");
- auto createVSTask = loadVSTask.then([this](Platform::Array<byte>^ fileData) {
- DX::ThrowIfFailed(
- m_d3dDevice->CreateVertexShader(
- fileData->Data,
- fileData->Length,
- nullptr,
- &m_vertexShader
- )
- );
- const D3D11_INPUT_ELEMENT_DESC vertexDesc[] =
- {
- { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
- { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
- };
- DX::ThrowIfFailed(
- m_d3dDevice->CreateInputLayout(
- vertexDesc,
- ARRAYSIZE(vertexDesc),
- fileData->Data,
- fileData->Length,
- &m_inputLayout
- )
- );
- });
- auto createPSTask = loadPSTask.then([this](Platform::Array<byte>^ fileData) {
- DX::ThrowIfFailed(
- m_d3dDevice->CreatePixelShader(
- fileData->Data,
- fileData->Length,
- nullptr,
- &m_pixelShader
- )
- );
- CD3D11_BUFFER_DESC constantBufferDesc(sizeof(ModelViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
- DX::ThrowIfFailed(
- m_d3dDevice->CreateBuffer(
- &constantBufferDesc,
- nullptr,
- &m_constantBuffer
- )
- );
- });
- //PrepareObjects(createPSTask, createVSTask);
- auto createPlayerTask = (createPSTask && createVSTask).then([this] () {
- VertexPositionColor playerVertices[] =
- {
- {XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, 0.0f)},
- {XMFLOAT3(-0.5f, -0.5f, 0.5f), XMFLOAT3(0.0f, 0.0f, 1.0f)},
- {XMFLOAT3(-0.5f, 0.5f, -0.5f), XMFLOAT3(0.0f, 1.0f, 0.0f)},
- {XMFLOAT3(-0.5f, 0.5f, 0.5f), XMFLOAT3(0.0f, 1.0f, 1.0f)},
- {XMFLOAT3( 0.5f, -0.5f, -0.5f), XMFLOAT3(1.0f, 0.0f, 0.0f)},
- {XMFLOAT3( 0.5f, -0.5f, 0.5f), XMFLOAT3(1.0f, 0.0f, 1.0f)},
- {XMFLOAT3( 0.5f, 0.5f, -0.5f), XMFLOAT3(1.0f, 1.0f, 0.0f)},
- {XMFLOAT3( 0.5f, 0.5f, 0.5f), XMFLOAT3(1.0f, 1.0f, 1.0f)},
- };
- D3D11_SUBRESOURCE_DATA playerVertexBufferData = {0};
- playerVertexBufferData.pSysMem = playerVertices;
- playerVertexBufferData.SysMemPitch = 0;
- playerVertexBufferData.SysMemSlicePitch = 0;
- CD3D11_BUFFER_DESC playerVertexBufferDesc(sizeof(playerVertices), D3D11_BIND_VERTEX_BUFFER);
- DX::ThrowIfFailed(
- m_d3dDevice->CreateBuffer(
- &playerVertexBufferDesc,
- &playerVertexBufferData,
- &playerVertexBuffer
- )
- );
- unsigned short playerIndices[] =
- {
- 0,2,1, // -x
- 1,2,3,
- 4,5,6, // +x
- 5,7,6,
- 0,1,5, // -y
- 0,5,4,
- 2,6,7, // +y
- 2,7,3,
- 0,4,6, // -z
- 0,6,2,
- 1,3,7, // +z
- 1,7,5,
- };
- playerIndexCount = ARRAYSIZE(playerIndices);
- D3D11_SUBRESOURCE_DATA playerIndexBufferData = {0};
- playerIndexBufferData.pSysMem = playerIndices;
- playerIndexBufferData.SysMemPitch = 0;
- playerIndexBufferData.SysMemSlicePitch = 0;
- CD3D11_BUFFER_DESC playerIndexBufferDesc(sizeof(playerIndices), D3D11_BIND_INDEX_BUFFER);
- DX::ThrowIfFailed(
- m_d3dDevice->CreateBuffer(
- &playerIndexBufferDesc,
- &playerIndexBufferData,
- &playerIndexBuffer
- )
- );
- });
- createPlayerTask.then([this] () {
- m_playerLoadComplete = true;
- });
- // Asteroid
- auto createAsteroidTask = (createPSTask && createVSTask).then([this] () {
- VertexPositionColor asteroidVertices[] =
- {
- {XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, 0.0f)},
- {XMFLOAT3(-0.5f, -0.5f, 0.5f), XMFLOAT3(0.0f, 0.0f, 1.0f)},
- {XMFLOAT3(-0.5f, 0.5f, -0.5f), XMFLOAT3(0.0f, 1.0f, 0.0f)},
- {XMFLOAT3(-0.5f, 0.5f, 0.5f), XMFLOAT3(0.0f, 1.0f, 1.0f)},
- {XMFLOAT3( 0.5f, -0.5f, -0.5f), XMFLOAT3(1.0f, 0.0f, 0.0f)},
- {XMFLOAT3( 0.5f, -0.5f, 0.5f), XMFLOAT3(1.0f, 0.0f, 1.0f)},
- {XMFLOAT3( 0.5f, 0.5f, -0.5f), XMFLOAT3(1.0f, 1.0f, 0.0f)},
- {XMFLOAT3( 0.5f, 0.5f, 0.5f), XMFLOAT3(1.0f, 1.0f, 1.0f)},
- };
- unsigned short asteroidIndices[] =
- {
- 0,2,1, // -x
- 1,2,3,
- 4,5,6, // +x
- 5,7,6,
- 0,1,5, // -y
- 0,5,4,
- 2,6,7, // +y
- 2,7,3,
- 0,4,6, // -z
- 0,6,2,
- 1,3,7, // +z
- 1,7,5,
- };
- int asteroidCount = 3;
- asteroidVertexBuffers.reserve( asteroidCount );
- asteroidIndexBuffers.reserve( asteroidCount );
- asteroidIndexCounts.reserve( asteroidCount );
- /*for(vector<Microsoft::WRL::ComPtr<ID3D11Buffer>>::iterator asteroid = asteroidVertexBuffers.begin(); asteroid != asteroidVertexBuffers.end(); ++asteroid)
- {
- }
- for(vector<Microsoft::WRL::ComPtr<ID3D11Buffer>>::iterator asteroid = asteroidIndexBuffers.begin(); asteroid != asteroidIndexBuffers.end(); ++asteroid)
- {
- }
- for(vector<uint32>::iterator asteroid = asteroidIndexCounts.begin(); asteroid != asteroidIndexCounts.end(); ++asteroid)
- {
- }*/
- for( int a = 0; a < asteroidCount; a++ )
- {
- D3D11_SUBRESOURCE_DATA asteroidVertexBufferData = {0};
- asteroidVertexBufferData.pSysMem = asteroidVertices;
- asteroidVertexBufferData.SysMemPitch = 0;
- asteroidVertexBufferData.SysMemSlicePitch = 0;
- CD3D11_BUFFER_DESC asteroidVertexBufferDesc(sizeof(asteroidVertices), D3D11_BIND_VERTEX_BUFFER);
- DX::ThrowIfFailed(
- m_d3dDevice->CreateBuffer(
- &asteroidVertexBufferDesc,
- &asteroidVertexBufferData,
- &asteroidVertexBuffers.at( a )
- )
- );
- asteroidIndexCounts.at( a ) = ARRAYSIZE(asteroidIndices);
- D3D11_SUBRESOURCE_DATA asteroidIndexBufferData = {0};
- asteroidIndexBufferData.pSysMem = asteroidIndices;
- asteroidIndexBufferData.SysMemPitch = 0;
- asteroidIndexBufferData.SysMemSlicePitch = 0;
- CD3D11_BUFFER_DESC asteroidIndexBufferDesc(sizeof(asteroidIndices), D3D11_BIND_INDEX_BUFFER);
- DX::ThrowIfFailed(
- m_d3dDevice->CreateBuffer(
- &asteroidIndexBufferDesc,
- &asteroidIndexBufferData,
- &asteroidIndexBuffers.at( a )
- )
- );
- }
- });
- createAsteroidTask.then([this] () {
- m_asteroidsLoadComplete = true;
- });
- }
- void Renderer::PrepareObjects( Concurrency::task<void> createPSTask, Concurrency::task<void> createVSTask )
- {
- }
- void Renderer::CreateWindowSizeDependentResources()
- {
- Direct3DBase::CreateWindowSizeDependentResources();
- float aspectRatio = m_windowBounds.Width / m_windowBounds.Height;
- float fovAngleY = 70.0f * XM_PI / 180.0f;
- if (aspectRatio < 1.0f)
- {
- fovAngleY /= aspectRatio;
- }
- XMStoreFloat4x4(
- &m_constantBufferData.projection,
- XMMatrixTranspose(
- XMMatrixPerspectiveFovRH(
- fovAngleY,
- aspectRatio,
- 0.01f,
- 100.0f
- )
- )
- );
- }
- void Renderer::SetUpViewingAngle()
- {
- XMVECTOR eye = XMVectorSet(0.0f, 0.7f, 1.5f, 0.0f);
- XMVECTOR at = XMVectorSet(0.0f, -0.1f, 0.0f, 0.0f);
- XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
- XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixTranspose(XMMatrixLookAtRH(eye, at, up)));
- }
- //float posX, float posY, float posZ, float rotX, float rotY, float rotZ
- void Renderer::Update( Placement^ placement )
- {
- // Rotation
- XMMATRIX m = XMMatrixRotationX( placement->GetRotX() );
- m = XMMatrixMultiply(m, XMMatrixRotationY( placement->GetRotY() ) );
- m = XMMatrixMultiply(m, XMMatrixRotationZ( placement->GetRotZ() ) );
- // Translation
- m = XMMatrixMultiply(m, XMMatrixTranslation( placement->GetPosX(), placement->GetPosY(), placement->GetPosZ() ) );
- // Store
- XMStoreFloat4x4( &m_constantBufferData.model, XMMatrixTranspose(m) );
- }
- void Renderer::UpdateAsteroid( Placement^ placement )
- {
- // Translation
- XMMATRIX m = XMMatrixTranslation( placement->GetPosX(), placement->GetPosY(), placement->GetPosZ() );
- // Rotation
- m = XMMatrixMultiply(m, XMMatrixRotationX( placement->GetRotX() ) );
- m = XMMatrixMultiply(m, XMMatrixRotationY( placement->GetRotY() ) );
- m = XMMatrixMultiply(m, XMMatrixRotationZ( placement->GetRotZ() ) );
- // Store
- XMStoreFloat4x4( &m_constantBufferData.model, XMMatrixTranspose(m) );
- }
- void Renderer::RenderBackground()
- {
- const float midnightBlue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
- m_d3dContext->ClearRenderTargetView(
- m_renderTargetView.Get(),
- midnightBlue
- );
- m_d3dContext->ClearDepthStencilView(
- m_depthStencilView.Get(),
- D3D11_CLEAR_DEPTH,
- 1.0f,
- 0
- );
- }
- void Renderer::PreparePlayer()
- {
- // Only draw the objects once they are loaded (loading is asynchronous).
- if ( !m_playerLoadComplete )
- {
- return;
- }
- GetBuffer();
- UINT stride = sizeof(VertexPositionColor);
- UINT offset = 0;
- m_d3dContext->IASetVertexBuffers(
- 0,
- 1,
- playerVertexBuffer.GetAddressOf(),
- &stride,
- &offset
- );
- m_d3dContext->IASetIndexBuffer(
- playerIndexBuffer.Get(),
- DXGI_FORMAT_R16_UINT,
- 0
- );
- m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
- m_d3dContext->IASetInputLayout(m_inputLayout.Get());
- m_d3dContext->VSSetShader(
- m_vertexShader.Get(),
- nullptr,
- 0
- );
- m_d3dContext->VSSetConstantBuffers(
- 0,
- 1,
- m_constantBuffer.GetAddressOf()
- );
- m_d3dContext->PSSetShader(
- m_pixelShader.Get(),
- nullptr,
- 0
- );
- m_d3dContext->DrawIndexed(
- playerIndexCount,
- 0,
- 0
- );
- }
- void Renderer::PrepareAsteroids( int index )
- {
- // Only draw the objects once they are loaded (loading is asynchronous).
- if ( !m_asteroidsLoadComplete )
- {
- return;
- }
- GetBuffer();
- UINT stride = sizeof(VertexPositionColor);
- UINT offset = 0;
- m_d3dContext->IASetVertexBuffers(
- 0,
- 1,
- asteroidVertexBuffers.at( index ).GetAddressOf(),
- &stride,
- &offset
- );
- m_d3dContext->IASetIndexBuffer(
- asteroidIndexBuffers.at( index ).Get(),
- DXGI_FORMAT_R16_UINT,
- 0
- );
- m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
- m_d3dContext->IASetInputLayout(m_inputLayout.Get());
- m_d3dContext->VSSetShader(
- m_vertexShader.Get(),
- nullptr,
- 0
- );
- m_d3dContext->VSSetConstantBuffers(
- 0,
- 1,
- m_constantBuffer.GetAddressOf()
- );
- m_d3dContext->PSSetShader(
- m_pixelShader.Get(),
- nullptr,
- 0
- );
- m_d3dContext->DrawIndexed(
- asteroidIndexCounts.at( index ),
- 0,
- 0
- );
- }
- void Renderer::GetBuffer()
- {
- m_d3dContext->OMSetRenderTargets(
- 1,
- m_renderTargetView.GetAddressOf(),
- m_depthStencilView.Get()
- );
- m_d3dContext->UpdateSubresource(
- m_constantBuffer.Get(),
- 0,
- NULL,
- &m_constantBufferData,
- 0,
- 0
- );
- }
- void Renderer::Render()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement