-
Notifications
You must be signed in to change notification settings - Fork 0
/
skybox.cpp
228 lines (185 loc) · 7.09 KB
/
skybox.cpp
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "skybox.h"
#include <winrt/base.h>
#define NOMINMAX
#include <d3d11.h>
#include <SimpleMath.h>
#include "config.h"
#include "graphics.h"
#include "camera.h"
using namespace DirectX;
using namespace DirectX::SimpleMath;
namespace
{
const D3D11_INPUT_ELEMENT_DESC g_inputElementDesc[]
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
const UINT g_numInputElements = static_cast<unsigned>(std::size(g_inputElementDesc));
struct Vertex
{
[[maybe_unused]] XMFLOAT3 position{};
[[maybe_unused]] XMFLOAT2 uv{};
};
const Vertex g_vertices[]
{
{ { -0.5f, 0.5f, -0.5f }, { 0, 0 } },
{ { 0.5f, 0.5f, -0.5f }, { 1, 0 } },
{ { -0.5f, 0.5f, 0.5f }, { 0, 1 } },
{ { 0.5f, 0.5f, 0.5f }, { 1, 1 } },
{ { -0.5f, 0.5f, 0.5f }, { 0, 0 } },
{ { 0.5f, 0.5f, 0.5f }, { 1, 0 } },
{ { -0.5f, -0.5f, 0.5f }, { 0, 1 } },
{ { 0.5f, -0.5f, 0.5f }, { 1, 1 } },
{ { 0.5f, 0.5f, 0.5f }, { 0, 0 } },
{ { 0.5f, 0.5f, -0.5f }, { 1, 0 } },
{ { 0.5f, -0.5f, 0.5f }, { 0, 1 } },
{ { 0.5f, -0.5f, -0.5f }, { 1, 1 } },
{ { 0.5f, 0.5f, -0.5f }, { 0, 0 } },
{ { -0.5f, 0.5f, -0.5f }, { 1, 0 } },
{ { 0.5f, -0.5f, -0.5f }, { 0, 1 } },
{ { -0.5f, -0.5f, -0.5f }, { 1, 1 } },
{ { -0.5f, 0.5f, -0.5f }, { 0, 0 } },
{ { -0.5f, 0.5f, 0.5f }, { 1, 0 } },
{ { -0.5f, -0.5f, -0.5f }, { 0, 1 } },
{ { -0.5f, -0.5f, 0.5f }, { 1, 1 } },
};
const UINT g_numVertices = static_cast<unsigned>(std::size(g_vertices));
struct ConstantBuffer
{
[[maybe_unused]] XMMATRIX world{};
[[maybe_unused]] XMMATRIX view{};
[[maybe_unused]] XMMATRIX projection{};
};
}
class SkyboxImpl
{
public:
explicit SkyboxImpl(std::shared_ptr<Graphics> pGraphics) : m_pGraphics{ pGraphics } {}
SkyboxImpl(const SkyboxImpl&) = delete;
SkyboxImpl(SkyboxImpl&&) = delete;
SkyboxImpl& operator=(const SkyboxImpl&) = delete;
SkyboxImpl& operator=(SkyboxImpl&&) = delete;
~SkyboxImpl() = default;
[[nodiscard]] bool load();
void update(float tick);
void draw(const Camera& camera) const;
private:
std::shared_ptr<Graphics> m_pGraphics{ nullptr };
winrt::com_ptr<ID3D11RasterizerState> m_pRasterizerState{ nullptr };
winrt::com_ptr<ID3D11Buffer> m_pVertexBuffer{ nullptr };
winrt::com_ptr<ID3D11Buffer> m_pConstantBuffer{ nullptr };
winrt::com_ptr<ID3D11VertexShader> m_pVertexShader{ nullptr };
winrt::com_ptr<ID3D11InputLayout> m_pInputLayout{ nullptr };
winrt::com_ptr<ID3D11PixelShader> m_pPixelShader{ nullptr };
winrt::com_ptr<ID3D11SamplerState> m_pSamplerState{ nullptr };
winrt::com_ptr<ID3D11ShaderResourceView> m_pTextureView[5]{ nullptr, nullptr, nullptr, nullptr, nullptr };
};
bool SkyboxImpl::load()
{
const auto pDevice = m_pGraphics->getDevice();
const D3D11_RASTERIZER_DESC rasterizerDesc
{
.FillMode = D3D11_FILL_SOLID,
.CullMode = D3D11_CULL_NONE,
.FrontCounterClockwise = TRUE,
.DepthBias = 0,
.DepthBiasClamp = 0.0f,
.SlopeScaledDepthBias = 0.0f,
.DepthClipEnable = FALSE,
.ScissorEnable = FALSE,
.MultisampleEnable = FALSE,
.AntialiasedLineEnable = FALSE,
};
HRESULT hr = pDevice->CreateRasterizerState(&rasterizerDesc, m_pRasterizerState.put());
if (FAILED(hr))
return false;
if (!m_pGraphics->loadVertexBuffer(g_vertices, g_numVertices, sizeof(Vertex), m_pVertexBuffer.put()))
return false;
if (!m_pGraphics->createConstantBuffer(sizeof(ConstantBuffer), m_pConstantBuffer.put()))
return false;
if (!m_pGraphics->loadVertexShader(L"res\\skybox_vs.cso", m_pVertexShader.put(), g_inputElementDesc, g_numInputElements, m_pInputLayout.put()))
return false;
if (!m_pGraphics->loadPixelShader(L"res\\skybox_ps.cso", m_pPixelShader.put()))
return false;
const D3D11_SAMPLER_DESC samplerDesc
{
.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT,
.AddressU = D3D11_TEXTURE_ADDRESS_BORDER,
.AddressV = D3D11_TEXTURE_ADDRESS_BORDER,
.AddressW = D3D11_TEXTURE_ADDRESS_BORDER,
.MipLODBias = 0,
.MaxAnisotropy = 0,
.ComparisonFunc = D3D11_COMPARISON_NEVER,
.BorderColor = { 0, 0, 0, 0 },
.MinLOD = 0,
.MaxLOD = 0,
};
hr = pDevice->CreateSamplerState(&samplerDesc, m_pSamplerState.put());
if (FAILED(hr))
return false;
if (!m_pGraphics->loadDDSTexture(L"res\\envmap_miramar\\results\\miramar_up_tga_dxt1_1.dds", m_pTextureView[0].put())
|| !m_pGraphics->loadDDSTexture(L"res\\envmap_miramar\\results\\miramar_rt_tga_dxt1_1.dds", m_pTextureView[1].put())
|| !m_pGraphics->loadDDSTexture(L"res\\envmap_miramar\\results\\miramar_ft_tga_dxt1_1.dds", m_pTextureView[2].put())
|| !m_pGraphics->loadDDSTexture(L"res\\envmap_miramar\\results\\miramar_lf_tga_dxt1_1.dds", m_pTextureView[3].put())
|| !m_pGraphics->loadDDSTexture(L"res\\envmap_miramar\\results\\miramar_bk_tga_dxt1_1.dds", m_pTextureView[4].put()))
return false;
return true;
}
void SkyboxImpl::update(const float /*tick*/)
{
}
void SkyboxImpl::draw(const Camera& camera) const
{
const auto pDeviceContext = m_pGraphics->getDeviceContext();
pDeviceContext->RSSetState(m_pRasterizerState.get());
const Matrix world = Matrix::CreateScale(Config::farPlane, Config::farPlane, Config::farPlane) * Matrix::CreateTranslation(camera.getPosition());
const ConstantBuffer constantBuffer
{
.world = world.Transpose(),
.view = camera.getView().Transpose(),
.projection = camera.getProjection().Transpose(),
};
pDeviceContext->UpdateSubresource(m_pConstantBuffer.get(), 0, nullptr, &constantBuffer, 0, 0);
const UINT numConstantBuffers = 1;
ID3D11Buffer* const constantBuffers[numConstantBuffers]{ m_pConstantBuffer.get() };
pDeviceContext->VSSetConstantBuffers(0, numConstantBuffers, constantBuffers);
pDeviceContext->VSSetShader(m_pVertexShader.get(), nullptr, 0);
pDeviceContext->PSSetConstantBuffers(0, numConstantBuffers, constantBuffers);
pDeviceContext->PSSetShader(m_pPixelShader.get(), nullptr, 0);
const UINT numSamplerStates = 1;
ID3D11SamplerState* const samplerStates[numSamplerStates]{ m_pSamplerState.get() };
pDeviceContext->PSSetSamplers(0, 1, samplerStates);
const UINT numVertexBuffers = 1;
const UINT vertexOffsets[numVertexBuffers]{ 0 };
const UINT vertexStrides[numVertexBuffers]{ sizeof(Vertex) };
ID3D11Buffer* const vertexBuffers[numVertexBuffers]{ m_pVertexBuffer.get() };
pDeviceContext->IASetVertexBuffers(0, numVertexBuffers, vertexBuffers, vertexStrides, vertexOffsets);
pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
pDeviceContext->IASetInputLayout(m_pInputLayout.get());
for (int s = 0; s < 5; s++)
{
const UINT numTextureViews = 1;
ID3D11ShaderResourceView* const textureViews[numTextureViews]{ m_pTextureView[s].get() };
pDeviceContext->PSSetShaderResources(0, numTextureViews, textureViews);
pDeviceContext->Draw(4, s * 4);
}
m_pGraphics->resetRasterizer();
}
Skybox::Skybox(std::shared_ptr<Graphics> pGraphics)
: m_pImpl{ std::make_unique<SkyboxImpl>(pGraphics) }
{
}
Skybox::~Skybox() = default;
bool Skybox::load() const
{
return m_pImpl->load();
}
void Skybox::update(const float tick) const
{
m_pImpl->update(tick);
}
void Skybox::draw(const Camera& camera) const
{
m_pImpl->draw(camera);
}