Commit 2beeca89 authored by chili's avatar chili
Browse files

depth buffering ftw

parent a820f8b0
...@@ -23,6 +23,11 @@ void App::DoFrame() ...@@ -23,6 +23,11 @@ void App::DoFrame()
{ {
const float c = sin( timer.Peek() ) / 2.0f + 0.5f; const float c = sin( timer.Peek() ) / 2.0f + 0.5f;
wnd.Gfx().ClearBuffer( c,c,1.0f ); wnd.Gfx().ClearBuffer( c,c,1.0f );
wnd.Gfx().DrawTestTriangle(
-timer.Peek(),
0.0f,
0.0f
);
wnd.Gfx().DrawTestTriangle( wnd.Gfx().DrawTestTriangle(
timer.Peek(), timer.Peek(),
wnd.mouse.GetPosX() / 400.0f - 1.0f, wnd.mouse.GetPosX() / 400.0f - 1.0f,
......
...@@ -74,6 +74,43 @@ Graphics::Graphics( HWND hWnd ) ...@@ -74,6 +74,43 @@ Graphics::Graphics( HWND hWnd )
wrl::ComPtr<ID3D11Resource> pBackBuffer; wrl::ComPtr<ID3D11Resource> pBackBuffer;
GFX_THROW_INFO( pSwap->GetBuffer( 0,__uuidof(ID3D11Resource),&pBackBuffer ) ); GFX_THROW_INFO( pSwap->GetBuffer( 0,__uuidof(ID3D11Resource),&pBackBuffer ) );
GFX_THROW_INFO( pDevice->CreateRenderTargetView( pBackBuffer.Get(),nullptr,&pTarget ) ); GFX_THROW_INFO( pDevice->CreateRenderTargetView( pBackBuffer.Get(),nullptr,&pTarget ) );
// create depth stensil state
D3D11_DEPTH_STENCIL_DESC dsDesc = {};
dsDesc.DepthEnable = TRUE;
dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
dsDesc.DepthFunc = D3D11_COMPARISON_LESS;
wrl::ComPtr<ID3D11DepthStencilState> pDSState;
GFX_THROW_INFO( pDevice->CreateDepthStencilState( &dsDesc,&pDSState ) );
// bind depth state
pContext->OMSetDepthStencilState( pDSState.Get(),1u );
// create depth stensil texture
wrl::ComPtr<ID3D11Texture2D> pDepthStencil;
D3D11_TEXTURE2D_DESC descDepth = {};
descDepth.Width = 800u;
descDepth.Height = 600u;
descDepth.MipLevels = 1u;
descDepth.ArraySize = 1u;
descDepth.Format = DXGI_FORMAT_D32_FLOAT;
descDepth.SampleDesc.Count = 1u;
descDepth.SampleDesc.Quality = 0u;
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
GFX_THROW_INFO( pDevice->CreateTexture2D( &descDepth,nullptr,&pDepthStencil ) );
// create view of depth stensil texture
D3D11_DEPTH_STENCIL_VIEW_DESC descDSV = {};
descDSV.Format = DXGI_FORMAT_D32_FLOAT;
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0u;
GFX_THROW_INFO( pDevice->CreateDepthStencilView(
pDepthStencil.Get(),&descDSV,&pDSV
) );
// bind depth stensil view to OM
pContext->OMSetRenderTargets( 1u,pTarget.GetAddressOf(),pDSV.Get() );
} }
void Graphics::EndFrame() void Graphics::EndFrame()
...@@ -99,9 +136,10 @@ void Graphics::ClearBuffer( float red,float green,float blue ) noexcept ...@@ -99,9 +136,10 @@ void Graphics::ClearBuffer( float red,float green,float blue ) noexcept
{ {
const float color[] = { red,green,blue,1.0f }; const float color[] = { red,green,blue,1.0f };
pContext->ClearRenderTargetView( pTarget.Get(),color ); pContext->ClearRenderTargetView( pTarget.Get(),color );
pContext->ClearDepthStencilView( pDSV.Get(),D3D11_CLEAR_DEPTH,1.0f,0u );
} }
void Graphics::DrawTestTriangle( float angle,float x,float y ) void Graphics::DrawTestTriangle( float angle,float x,float z )
{ {
HRESULT hr; HRESULT hr;
...@@ -182,7 +220,7 @@ void Graphics::DrawTestTriangle( float angle,float x,float y ) ...@@ -182,7 +220,7 @@ void Graphics::DrawTestTriangle( float angle,float x,float y )
dx::XMMatrixTranspose( dx::XMMatrixTranspose(
dx::XMMatrixRotationZ( angle ) * dx::XMMatrixRotationZ( angle ) *
dx::XMMatrixRotationX( angle ) * dx::XMMatrixRotationX( angle ) *
dx::XMMatrixTranslation( x,y,4.0f ) * dx::XMMatrixTranslation( x,0.0f,z + 4.0f ) *
dx::XMMatrixPerspectiveLH( 1.0f,3.0f / 4.0f,0.5f,10.0f ) dx::XMMatrixPerspectiveLH( 1.0f,3.0f / 4.0f,0.5f,10.0f )
) )
} }
...@@ -278,10 +316,6 @@ void Graphics::DrawTestTriangle( float angle,float x,float y ) ...@@ -278,10 +316,6 @@ void Graphics::DrawTestTriangle( float angle,float x,float y )
pContext->IASetInputLayout( pInputLayout.Get() ); pContext->IASetInputLayout( pInputLayout.Get() );
// bind render target
pContext->OMSetRenderTargets( 1u,pTarget.GetAddressOf(),nullptr );
// Set primitive topology to triangle list (groups of 3 vertices) // Set primitive topology to triangle list (groups of 3 vertices)
pContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); pContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
......
...@@ -61,4 +61,5 @@ private: ...@@ -61,4 +61,5 @@ private:
Microsoft::WRL::ComPtr<IDXGISwapChain> pSwap; Microsoft::WRL::ComPtr<IDXGISwapChain> pSwap;
Microsoft::WRL::ComPtr<ID3D11DeviceContext> pContext; Microsoft::WRL::ComPtr<ID3D11DeviceContext> pContext;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> pTarget; Microsoft::WRL::ComPtr<ID3D11RenderTargetView> pTarget;
Microsoft::WRL::ComPtr<ID3D11DepthStencilView> pDSV;
}; };
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment