-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExplosionShaderClass.cpp
181 lines (157 loc) · 6.88 KB
/
ExplosionShaderClass.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
#include "ExplosionShaderClass.h"
ExplosionShaderClass::ExplosionShaderClass(ID3D11Device* device, Camera* cam)
{
m_pVertexShader = 0;
m_pGeometryShader = 0;
m_pPixelShader = 0;
m_pVertexLayout = 0;
m_pConstantBuffer = 0;
m_pSampler = 0;
m_pCamera = cam;
initialize(device);
}
ExplosionShaderClass::~ExplosionShaderClass(void)
{
if (m_pVertexShader) m_pVertexShader->Release();
if (m_pGeometryShader) m_pGeometryShader->Release();
if (m_pPixelShader) m_pPixelShader->Release();
if (m_pVertexLayout) m_pVertexLayout->Release();
if (m_pConstantBuffer) m_pConstantBuffer->Release();
if (m_pSampler) m_pSampler->Release();
m_pVertexShader = 0;
m_pPixelShader = 0;
m_pVertexLayout = 0;
m_pConstantBuffer = 0;
m_pSampler = 0;
m_pCamera = 0;
}
void ExplosionShaderClass::initialize(ID3D11Device* device)
{
// Compile the vertex shader
HRESULT hr;
ID3DBlob* pVSBlob = nullptr;
hr = CompileShaderFromFile(L"ExplosionShaders_VS.hlsl", "main", "vs_4_0", &pVSBlob);
if(FAILED(hr))
MessageBox(nullptr, L"The FX file cannot be compiled. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK);
// Create the vertex shader
hr = device->CreateVertexShader(pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), nullptr, &m_pVertexShader);
if(FAILED(hr))
pVSBlob->Release();
// Define the input layout
D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 32, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 48, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};
UINT numElements = ARRAYSIZE(layout);
// Create the input layout
hr = device->CreateInputLayout(layout, numElements, pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), &m_pVertexLayout);
pVSBlob->Release();
// Compile the pixel shader
ID3DBlob* pPSBlob = nullptr;
hr = CompileShaderFromFile(L"ExplosionShaders_PS.hlsl", "main", "ps_4_0", &pPSBlob);
if(FAILED(hr))
MessageBox(nullptr, L"The FX file cannot be compiled. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK);
// Create the pixel shader
hr = device->CreatePixelShader(pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), nullptr, &m_pPixelShader);
pPSBlob->Release();
// Compile the geometry shader
ID3DBlob* pGSBlob = nullptr;
hr = CompileShaderFromFile(L"GeoShaders_GS.hlsl", "main", "gs_4_0", &pGSBlob);
if(FAILED(hr))
MessageBox(nullptr, L"The FX file cannot be compiled. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK);
// Create the geometry shader
hr = device->CreateGeometryShader(pGSBlob->GetBufferPointer(), pGSBlob->GetBufferSize(), nullptr, &m_pGeometryShader);
pGSBlob->Release();
// Init Constant Buffer
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof(ExplosionTextureConstBuffer);
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bd.CPUAccessFlags = 0;
hr = device->CreateBuffer(&bd, nullptr, &m_pConstantBuffer);
// Init Texture Sampler State
D3D11_SAMPLER_DESC sampDesc;
ZeroMemory(&sampDesc, sizeof(sampDesc));
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
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 = device->CreateSamplerState(&sampDesc, &m_pSampler);
}
void ExplosionShaderClass::setParameters(ID3D11DeviceContext* deviceContext, GameObject* p_object, XMFLOAT2 t, XMFLOAT3 origin)
{
//get updated object world matrix
XMFLOAT4X4 g_World = *(p_object->getWorldMatrix());
XMFLOAT4X4 g_View = m_pCamera->getViewMatrix();
XMFLOAT4X4 g_Projection = m_pCamera->getProjectionMatrix();
// Update constant buffer
ExplosionTextureConstBuffer cb;
cb.mWorld = XMMatrixTranspose(XMLoadFloat4x4(&g_World));
cb.mView = XMMatrixTranspose(XMLoadFloat4x4(&g_View));
cb.mProjection = XMMatrixTranspose(XMLoadFloat4x4(&g_Projection));
cb.mTimer = XMFLOAT4(t.x, t.y, 0.0f, 0.0f);
XMFLOAT3 up = m_pCamera->getUp();
XMFLOAT3 dir = m_pCamera->getFwd();
XMVECTOR side = XMVector3Cross(XMLoadFloat3(&up), XMLoadFloat3(&dir));
XMVector3Normalize(side);
cb.mUp = XMFLOAT4(up.x, up.y, up.z, 0);
XMFLOAT3 side3;
XMStoreFloat3(&side3, side);
cb.mSide = XMFLOAT4(side3.x, side3.y, side3.z, 0);
cb.mOrigin = XMFLOAT4(origin.x, origin.y, origin.z, 1.0f);
// Set contant buffer
deviceContext->UpdateSubresource(m_pConstantBuffer, 0, nullptr, &cb, 0, 0);
// Send the textures to the Pixel Shader
ID3D11ShaderResourceView* texView = p_object->getModel()->getTexShadResView();
deviceContext->PSSetShaderResources(0, 1, &texView);
}
void ExplosionShaderClass::render(ID3D11DeviceContext* deviceContext, int vertCount)
{
// Set the input layout
deviceContext->IASetInputLayout(m_pVertexLayout);
// Set Shader properties
deviceContext->VSSetShader(m_pVertexShader, nullptr, 0 );
deviceContext->VSSetConstantBuffers(0, 1, &m_pConstantBuffer);
deviceContext->PSSetShader(m_pPixelShader, nullptr, 0 );
deviceContext->PSSetConstantBuffers(0, 1, &m_pConstantBuffer);
deviceContext->PSSetSamplers(0, 1, &m_pSampler);
deviceContext->GSSetShader(m_pGeometryShader, nullptr, 0 );
deviceContext->GSSetConstantBuffers(0, 1, &m_pConstantBuffer);
// Render triangles
deviceContext->Draw(vertCount, 0);
}
HRESULT ExplosionShaderClass::CompileShaderFromFile(WCHAR* szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel, ID3DBlob** ppBlobOut)
{
HRESULT hr = S_OK;
DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#ifdef _DEBUG
// Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders.
// Setting this flag improves the shader debugging experience, but still allows
// the shaders to be optimized and to run exactly the way they will run in
// the release configuration of this program.
dwShaderFlags |= D3DCOMPILE_DEBUG;
// Disable optimizations to further improve shader debugging
dwShaderFlags |= D3DCOMPILE_SKIP_OPTIMIZATION;
#endif
ID3DBlob* pErrorBlob = nullptr;
hr = D3DCompileFromFile( szFileName, nullptr, nullptr, szEntryPoint, szShaderModel,
dwShaderFlags, 0, ppBlobOut, &pErrorBlob );
if( FAILED(hr) )
{
if( pErrorBlob )
{
OutputDebugStringA( reinterpret_cast<const char*>( pErrorBlob->GetBufferPointer() ) );
pErrorBlob->Release();
}
return hr;
}
if( pErrorBlob ) pErrorBlob->Release();
return S_OK;
}