Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Clark Lin
hw3d
Commits
2beeca89
Commit
2beeca89
authored
Feb 16, 2019
by
chili
Browse files
depth buffering ftw
parent
a820f8b0
Changes
3
Show whitespace changes
Inline
Side-by-side
hw3d/App.cpp
View file @
2beeca89
...
...
@@ -23,6 +23,11 @@ void App::DoFrame()
{
const
float
c
=
sin
(
timer
.
Peek
()
)
/
2.0
f
+
0.5
f
;
wnd
.
Gfx
().
ClearBuffer
(
c
,
c
,
1.0
f
);
wnd
.
Gfx
().
DrawTestTriangle
(
-
timer
.
Peek
(),
0.0
f
,
0.0
f
);
wnd
.
Gfx
().
DrawTestTriangle
(
timer
.
Peek
(),
wnd
.
mouse
.
GetPosX
()
/
400.0
f
-
1.0
f
,
...
...
hw3d/Graphics.cpp
View file @
2beeca89
...
...
@@ -74,6 +74,43 @@ Graphics::Graphics( HWND hWnd )
wrl
::
ComPtr
<
ID3D11Resource
>
pBackBuffer
;
GFX_THROW_INFO
(
pSwap
->
GetBuffer
(
0
,
__uuidof
(
ID3D11Resource
),
&
pBackBuffer
)
);
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
()
...
...
@@ -99,9 +136,10 @@ void Graphics::ClearBuffer( float red,float green,float blue ) noexcept
{
const
float
color
[]
=
{
red
,
green
,
blue
,
1.0
f
};
pContext
->
ClearRenderTargetView
(
pTarget
.
Get
(),
color
);
pContext
->
ClearDepthStencilView
(
pDSV
.
Get
(),
D3D11_CLEAR_DEPTH
,
1.0
f
,
0u
);
}
void
Graphics
::
DrawTestTriangle
(
float
angle
,
float
x
,
float
y
)
void
Graphics
::
DrawTestTriangle
(
float
angle
,
float
x
,
float
z
)
{
HRESULT
hr
;
...
...
@@ -182,7 +220,7 @@ void Graphics::DrawTestTriangle( float angle,float x,float y )
dx
::
XMMatrixTranspose
(
dx
::
XMMatrixRotationZ
(
angle
)
*
dx
::
XMMatrixRotationX
(
angle
)
*
dx
::
XMMatrixTranslation
(
x
,
y
,
4.0
f
)
*
dx
::
XMMatrixTranslation
(
x
,
0.0
f
,
z
+
4.0
f
)
*
dx
::
XMMatrixPerspectiveLH
(
1.0
f
,
3.0
f
/
4.0
f
,
0.5
f
,
10.0
f
)
)
}
...
...
@@ -278,10 +316,6 @@ void Graphics::DrawTestTriangle( float angle,float x,float y )
pContext
->
IASetInputLayout
(
pInputLayout
.
Get
()
);
// bind render target
pContext
->
OMSetRenderTargets
(
1u
,
pTarget
.
GetAddressOf
(),
nullptr
);
// Set primitive topology to triangle list (groups of 3 vertices)
pContext
->
IASetPrimitiveTopology
(
D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST
);
...
...
hw3d/Graphics.h
View file @
2beeca89
...
...
@@ -61,4 +61,5 @@ private:
Microsoft
::
WRL
::
ComPtr
<
IDXGISwapChain
>
pSwap
;
Microsoft
::
WRL
::
ComPtr
<
ID3D11DeviceContext
>
pContext
;
Microsoft
::
WRL
::
ComPtr
<
ID3D11RenderTargetView
>
pTarget
;
Microsoft
::
WRL
::
ComPtr
<
ID3D11DepthStencilView
>
pDSV
;
};
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment