-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackground.cpp
More file actions
172 lines (132 loc) · 4.91 KB
/
Copy pathBackground.cpp
File metadata and controls
172 lines (132 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "Background.h"
using namespace std;
using namespace DirectX;
#define NUM_INDICES 6
Background::Background()
{
m_pVertexBuffer = nullptr;
m_pIndexBuffer = nullptr;
m_pTextureResourceView = nullptr;
m_pSamplerLinear = nullptr;
// Initialize the world matrix
XMStoreFloat4x4(&m_World, XMMatrixIdentity());
}
Background::~Background()
{
release();
}
void Background::release()
{
if (m_pVertexBuffer)
m_pVertexBuffer->Release();
m_pVertexBuffer = nullptr;
if (m_pIndexBuffer)
m_pIndexBuffer->Release();
m_pIndexBuffer = nullptr;
if (m_pTextureResourceView)
m_pTextureResourceView->Release();
m_pTextureResourceView = nullptr;
if (m_pSamplerLinear)
m_pSamplerLinear->Release();
m_pSamplerLinear = nullptr;
}
HRESULT Background::initMesh(ID3D11Device* pd3dDevice)
{
float minusOne = -1.0;
float One = 1;
SimpleVertex v[] =
{
// NOTE Normal NOT required BUT USING SAME shader as cube rendering
{ XMFLOAT3(minusOne, minusOne, 0.0f), XMFLOAT3(0.0f, 0.0f, 0.0f), XMFLOAT2(0.0f, 1.0f) },
{ XMFLOAT3(minusOne, One, 0.0f), XMFLOAT3(0.0f, 0.0f, 0.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(One, One, 0.0f), XMFLOAT3(0.0f, 0.0f, 0.0f), XMFLOAT2(1.0f, 0.0f) },
{ XMFLOAT3(One, minusOne, 0.0f), XMFLOAT3(0.0f, 0.0f, 0.0f), XMFLOAT2(1.0f, 1.0f) },
};
WORD indices[] = {
// Front Face
0, 1, 2,
0, 2, 3,
};
D3D11_BUFFER_DESC indexBufferDesc;
ZeroMemory(&indexBufferDesc, sizeof(indexBufferDesc));
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(WORD) * NUM_INDICES;
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA iinitData;
iinitData.pSysMem = indices;
HRESULT hr = pd3dDevice->CreateBuffer(&indexBufferDesc, &iinitData, &m_pIndexBuffer);
D3D11_BUFFER_DESC vertexBufferDesc;
ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc));
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(SimpleVertex) * 4;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA vertexBufferData;
ZeroMemory(&vertexBufferData, sizeof(vertexBufferData));
vertexBufferData.pSysMem = v;
hr = pd3dDevice->CreateBuffer(&vertexBufferDesc, &vertexBufferData, &m_pVertexBuffer);
// load and setup textures
hr = CreateDDSTextureFromFile(pd3dDevice, L"Resources\\map.dds", nullptr, &m_pTextureResourceView);
if (FAILED(hr))
return hr;
D3D11_SAMPLER_DESC sampDesc;
ZeroMemory(&sampDesc, sizeof(sampDesc));
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
sampDesc.MinLOD = 0;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
hr = pd3dDevice->CreateSamplerState(&sampDesc, &m_pSamplerLinear);
return hr;
}
void Background::depthTest(const bool isOn, ID3D11Device* pd3dDevice, ID3D11DeviceContext* pContext)
{
ID3D11DepthStencilState* depthStencilState;
D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
if (isOn) {
depthStencilDesc.DepthEnable = TRUE;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
}
else {
depthStencilDesc.DepthEnable = FALSE;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_ALWAYS;
}
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.StencilEnable = FALSE;
depthStencilDesc.StencilReadMask = 0xFF;
depthStencilDesc.StencilWriteMask = 0xFF;
// Stencil operations if pixel is front-facing
depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
// Stencil operations if pixel is back-facing
depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
pd3dDevice->CreateDepthStencilState(&depthStencilDesc, &depthStencilState);
pContext->OMSetDepthStencilState(depthStencilState, 0);
depthStencilState->Release();
}
void Background::draw(ID3D11Device* pd3dDevice, ID3D11DeviceContext* pContext)
{
// Turn depth write off
depthTest(false, pd3dDevice, pContext);
UINT stride = sizeof(SimpleVertex);
UINT offset = 0;
// Set vertex buffer
pContext->IASetVertexBuffers(0, 1, &m_pVertexBuffer, &stride, &offset);
// Set index buffer
pContext->IASetIndexBuffer(m_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0);
// Set primitive topology
pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
pContext->DrawIndexed(NUM_INDICES, 0, 0);
// Turn depth write back on
depthTest(true, pd3dDevice, pContext);
}