Commit 732beb28 authored by Administrator's avatar Administrator
Browse files

add depth buffer. fix issue of viewport.

viewport.MinDepth and viewport.MaxDepth were not initialized, so they were both 0, which prevents the depth buffer from being calculated correctly.
fix this issue by explicitly specify viewport.MinDepth as 0 and viewport.MaxDepth as 1.
parent 9dac251e
...@@ -140,6 +140,16 @@ const ColorConstantBuffer ccb = ...@@ -140,6 +140,16 @@ const ColorConstantBuffer ccb =
} }
}; };
// Define depth stencil buffer
Microsoft::WRL::ComPtr<ID3D11DepthStencilState> pDepthStencilState;
// Define depth stencil texture
Microsoft::WRL::ComPtr<ID3D11Texture2D> depthStencilTexture;
// Define depth stencil view
Microsoft::WRL::ComPtr<ID3D11DepthStencilView> pDSView;
// Define input layout // Define input layout
ID3D11InputLayout* pInputLayout; ID3D11InputLayout* pInputLayout;
...@@ -196,7 +206,7 @@ void DefineIndexBuffer() ...@@ -196,7 +206,7 @@ void DefineIndexBuffer()
devcon->IASetIndexBuffer(pIndexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0u); devcon->IASetIndexBuffer(pIndexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0u);
} }
void DefineConstantBuffer(float angle) void DefineConstantBuffer(float angle, float z)
{ {
cb = cb =
{ {
...@@ -209,7 +219,7 @@ void DefineConstantBuffer(float angle) ...@@ -209,7 +219,7 @@ void DefineConstantBuffer(float angle)
// scaling // scaling
//* dx::XMMatrixScaling(heightWidthRatio, 1.0f, 1.0f) //* dx::XMMatrixScaling(heightWidthRatio, 1.0f, 1.0f)
// translation // translation
* dx::XMMatrixTranslation(0.0f, 0.0f, 4.0f) * dx::XMMatrixTranslation(0.0f, 0.0f, z)
// projection // projection
* dx::XMMatrixPerspectiveLH(1.0f, heightWidthRatio, 0.5f, 10.0f) * dx::XMMatrixPerspectiveLH(1.0f, heightWidthRatio, 0.5f, 10.0f)
) )
...@@ -250,6 +260,47 @@ void DefineColorConstantBuffer() ...@@ -250,6 +260,47 @@ void DefineColorConstantBuffer()
devcon->PSSetConstantBuffers(0u, 1u, pColorConstantBuffer.GetAddressOf()); devcon->PSSetConstantBuffers(0u, 1u, pColorConstantBuffer.GetAddressOf());
} }
void DefineDepthStencilBuffer()
{
// Create depth stencil state
D3D11_DEPTH_STENCIL_DESC depthStencilDesc = {};
depthStencilDesc.DepthEnable = TRUE;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
// Create depth stencil state
dev->CreateDepthStencilState(&depthStencilDesc, &pDepthStencilState);
// bind depth state
devcon->OMSetDepthStencilState(pDepthStencilState.Get(), 1u);
// Create the depth stencil texture
D3D11_TEXTURE2D_DESC depthStencilTextureDesc = {};
depthStencilTextureDesc.Width = 800;
depthStencilTextureDesc.Height = 600;
depthStencilTextureDesc.MipLevels = 1u;
depthStencilTextureDesc.ArraySize = 1u;
depthStencilTextureDesc.Format = DXGI_FORMAT_D32_FLOAT;
depthStencilTextureDesc.SampleDesc.Count = 1u;
depthStencilTextureDesc.SampleDesc.Quality = 0u;
depthStencilTextureDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilTextureDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
// Create texture
dev->CreateTexture2D(&depthStencilTextureDesc, nullptr, &depthStencilTexture);
// Create the depth stencil view
D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc = {};
depthStencilViewDesc.Format = DXGI_FORMAT_D32_FLOAT;
depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDesc.Texture2D.MipSlice = 0u;
dev->CreateDepthStencilView(depthStencilTexture.Get(), &depthStencilViewDesc, &pDSView);
// Bind depth stencil view to out put merger
//devcon->OMSetRenderTargets(1u, targetView.GetAddressOf(), pDSView.Get());
}
void DefineInputLayout() void DefineInputLayout()
{ {
// Create input elements description, choosing whatever need to send to GPU // Create input elements description, choosing whatever need to send to GPU
...@@ -275,7 +326,7 @@ void DefineInputLayout() ...@@ -275,7 +326,7 @@ void DefineInputLayout()
devcon->IASetInputLayout(pInputLayout); devcon->IASetInputLayout(pInputLayout);
// bind render target // bind render target
devcon->OMSetRenderTargets(1u, targetView.GetAddressOf(), nullptr); //devcon->OMSetRenderTargets(1u, targetView.GetAddressOf(), nullptr);
} }
void DrawPrimitive() void DrawPrimitive()
...@@ -295,17 +346,23 @@ void DrawPrimitive() ...@@ -295,17 +346,23 @@ void DrawPrimitive()
devcon->DrawIndexed((UINT)std::size(indices), 0u, 0u); devcon->DrawIndexed((UINT)std::size(indices), 0u, 0u);
} }
void drawTriangle() void drawSingleTriangle(float angle, float z)
{ {
DefineShader(); DefineShader();
DefineVertexBuffer(); DefineVertexBuffer();
DefineIndexBuffer(); DefineIndexBuffer();
DefineConstantBuffer(timer.Peek()); DefineConstantBuffer(angle, z);
DefineColorConstantBuffer(); DefineColorConstantBuffer();
DefineInputLayout(); DefineInputLayout();
DrawPrimitive(); DrawPrimitive();
} }
void drawTriangle()
{
drawSingleTriangle(timer.Peek(), 4.0f);
drawSingleTriangle(-timer.Peek(), 4.5f);
}
// this function initializes and prepares Direct3D for use // this function initializes and prepares Direct3D for use
void InitD3D(HWND hWnd) void InitD3D(HWND hWnd)
{ {
...@@ -317,10 +374,10 @@ void InitD3D(HWND hWnd) ...@@ -317,10 +374,10 @@ void InitD3D(HWND hWnd)
// fill the swap chain description struct // fill the swap chain description struct
scd.BufferCount = 1; // one back buffer scd.BufferCount = 1; // one back buffer
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color scd.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // use 32-bit color
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used
scd.OutputWindow = hWnd; // the window to be used scd.OutputWindow = hWnd; // the window to be used
scd.SampleDesc.Count = 4; // how many multisamples scd.SampleDesc.Count = 1; // how many multisamples
scd.Windowed = TRUE; // windowed/full-screen mode scd.Windowed = TRUE; // windowed/full-screen mode
// create a device, device context and swap chain using the information in the scd struct // create a device, device context and swap chain using the information in the scd struct
...@@ -344,8 +401,11 @@ void InitD3D(HWND hWnd) ...@@ -344,8 +401,11 @@ void InitD3D(HWND hWnd)
// 根据back buffer,创建新的View Target // 根据back buffer,创建新的View Target
dev->CreateRenderTargetView(pBackBuffer.Get(), NULL, &targetView); dev->CreateRenderTargetView(pBackBuffer.Get(), NULL, &targetView);
DefineDepthStencilBuffer();
// 将渲染目标设置成我们新创建的View Target // 将渲染目标设置成我们新创建的View Target
devcon->OMSetRenderTargets(1, targetView.GetAddressOf(), NULL); //devcon->OMSetRenderTargets(1u, targetView.GetAddressOf(), NULL);
devcon->OMSetRenderTargets(1u, targetView.GetAddressOf(), pDSView.Get());
// 指定View Port // 指定View Port
D3D11_VIEWPORT viewport; D3D11_VIEWPORT viewport;
...@@ -355,6 +415,8 @@ void InitD3D(HWND hWnd) ...@@ -355,6 +415,8 @@ void InitD3D(HWND hWnd)
viewport.TopLeftY = 0; viewport.TopLeftY = 0;
viewport.Width = 800; viewport.Width = 800;
viewport.Height = 600; viewport.Height = 600;
viewport.MinDepth = 0;
viewport.MaxDepth = 1;
devcon->RSSetViewports(1, &viewport); devcon->RSSetViewports(1, &viewport);
} }
...@@ -366,6 +428,9 @@ void RenderFrame(void) ...@@ -366,6 +428,9 @@ void RenderFrame(void)
const float color[] = { 0.0f, 0.2f, 0.4f, 1.0f }; const float color[] = { 0.0f, 0.2f, 0.4f, 1.0f };
devcon->ClearRenderTargetView(targetView.Get(), color); devcon->ClearRenderTargetView(targetView.Get(), color);
// clear the depth stencil view
devcon->ClearDepthStencilView(pDSView.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0u);
// do 3D rendering on the back buffer here // do 3D rendering on the back buffer here
drawTriangle(); drawTriangle();
......
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