forked from vixorien/AdvancedDX11Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaterial.cpp
92 lines (76 loc) · 2.59 KB
/
Material.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
#include "Material.h"
Material::Material(
SimpleVertexShader* vs,
SimplePixelShader* ps,
DirectX::XMFLOAT4 color,
float shininess,
DirectX::XMFLOAT2 uvScale)
{
this->vs = vs;
this->ps = ps;
this->color = color;
this->shininess = shininess;
this->uvScale = uvScale;
}
Material::~Material()
{
}
void Material::PrepareMaterial(Transform* transform, Camera* cam)
{
// Turn shaders on
vs->SetShader();
ps->SetShader();
// Set vertex shader data
vs->SetMatrix4x4("world", transform->GetWorldMatrix());
vs->SetMatrix4x4("worldInverseTranspose", transform->GetWorldInverseTransposeMatrix());
vs->SetMatrix4x4("view", cam->GetView());
vs->SetMatrix4x4("projection", cam->GetProjection());
vs->SetFloat2("uvScale", uvScale);
vs->CopyAllBufferData();
// Set pixel shader data
ps->SetFloat4("Color", color);
ps->SetFloat("Shininess", shininess);
ps->CopyBufferData("perMaterial");
// Loop and set any other resources
for (auto t : psTextureSRVs) { ps->SetShaderResourceView(t.first.c_str(), t.second); }
for (auto t : vsTextureSRVs) { vs->SetShaderResourceView(t.first.c_str(), t.second); }
for (auto s : psSamplers) { ps->SetSamplerState(s.first.c_str(), s.second); }
for (auto s : vsSamplers) { vs->SetSamplerState(s.first.c_str(), s.second); }
}
void Material::SetPerMaterialDataAndResources(bool copyToGPUNow)
{
// Set vertex shader per-material vars
vs->SetFloat2("uvScale", uvScale);
if (copyToGPUNow)
{
vs->CopyBufferData("perMaterial");
}
// Set pixel shader per-material vars
ps->SetFloat4("Color", color);
ps->SetFloat("Shininess", shininess);
if (copyToGPUNow)
{
ps->CopyBufferData("perMaterial");
}
// Loop and set any other resources
for (auto t : psTextureSRVs) { ps->SetShaderResourceView(t.first.c_str(), t.second); }
for (auto t : vsTextureSRVs) { vs->SetShaderResourceView(t.first.c_str(), t.second); }
for (auto s : psSamplers) { ps->SetSamplerState(s.first.c_str(), s.second); }
for (auto s : vsSamplers) { vs->SetSamplerState(s.first.c_str(), s.second); }
}
void Material::AddPSTextureSRV(std::string shaderName, Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv)
{
psTextureSRVs.insert({ shaderName, srv });
}
void Material::AddVSTextureSRV(std::string shaderName, Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv)
{
vsTextureSRVs.insert({ shaderName, srv });
}
void Material::AddPSSampler(std::string shaderName, Microsoft::WRL::ComPtr<ID3D11SamplerState> sampler)
{
psSamplers.insert({ shaderName, sampler });
}
void Material::AddVSSampler(std::string shaderName, Microsoft::WRL::ComPtr<ID3D11SamplerState> sampler)
{
vsSamplers.insert({ shaderName, sampler });
}