-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticlesClass.cpp
365 lines (278 loc) · 11.2 KB
/
ParticlesClass.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "ParticlesClass.hpp"
ParticleSystem::ParticleSystem()
{
srand(time(NULL));
this->Capacity = 1000;
this->MaxVertexCount = this->Capacity * 3; // Each particle is one triangle, hence 3 vertices
this->CurrentParticleCount = 0;
this->LastKeyTime = 0.0f;
this->CurrentTime = 0.0f;
this->PropertiesArray = new ParticleProperties[this->Capacity]; // Create an array for all particle properties
this->VertexArray = new ParticleVertexData[this->MaxVertexCount]; // Create vertex array for particles to be rendered
}
ParticleSystem::~ParticleSystem()
{
}
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// //
// PUBLIC FUNCTIONS //
// //
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
void ParticleSystem::ReleaseAll()
{
delete[] this->VertexArray;
}
void ParticleSystem::InitializeAll(
ID3D11Device* *Device
)
{
this->InitializeShaders(Device);
this->InitializeBuffers(Device);
}
void ParticleSystem::UpdateSystem(
ID3D11DeviceContext* *DeviceContext,
float dt,
Camera* theCamera
)
{
this->CurrentTime = dt;
float TempTimeDifference = this->CurrentTime - this->LastKeyTime;
if (TempTimeDifference > 0.1f)
{
this->CreateParticle(theCamera->GetCameraPositionFloat3());
this->LastKeyTime = this->CurrentTime;
}
this->UpdateParticlePositions
(
theCamera->GetCameraPositionFloat3()
);
this->UpdateVertexBuffer(
DeviceContext,
theCamera
);
}
void ParticleSystem::SetShadingContext(
ID3D11DeviceContext* *DeviceContext
)
{
// Set InputLayout & PrimitiveTopology
(*DeviceContext)->IASetInputLayout(this->VertexLayout);
(*DeviceContext)->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
(*DeviceContext)->VSSetShader(this->VertexShader, nullptr, 0);
(*DeviceContext)->HSSetShader(nullptr, nullptr, 0);
(*DeviceContext)->DSSetShader(nullptr, nullptr, 0);
(*DeviceContext)->GSSetShader(this->GeometryShader, nullptr, 0);
(*DeviceContext)->PSSetShader(this->PixelShader, nullptr, 0);
}
void ParticleSystem::RenderParticles(
ID3D11DeviceContext* *DeviceContext
)
{
// Set the object's VertexBuffer to the IA.
UINT32 VertexSize = sizeof(ParticleVertexData); // float3_pos + float2_uv + float3_color
UINT32 offset = 0;
(*DeviceContext)->IASetVertexBuffers(
0,
1,
&this->VertexBuffer,
&VertexSize,
&offset
);
// Render the geometry
(*DeviceContext)->Draw((UINT)(this->CurrentParticleCount * 3), 0);
}
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// //
// PRIVATE FUNCTIONS //
// //
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
void ParticleSystem::InitializeShaders(
ID3D11Device* *Device
)
{
HRESULT hr;
//C R E A T E V E R T E X S H A D E R
ID3DBlob* pVS = nullptr;
D3DCompileFromFile(
L"Particle_Vertex.hlsl",
nullptr,
nullptr,
"PARTICLES_VS_main",
"vs_5_0",
0,
0,
&pVS,
nullptr
);
(*Device)->CreateVertexShader(pVS->GetBufferPointer(), pVS->GetBufferSize(), nullptr, &this->VertexShader);
//create input layout (verified using vertex shader)
D3D11_INPUT_ELEMENT_DESC inputDesc[] = {
{ "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 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
hr = (*Device)->CreateInputLayout(inputDesc, ARRAYSIZE(inputDesc), pVS->GetBufferPointer(), pVS->GetBufferSize(), &this->VertexLayout);
if (FAILED(hr))
MessageBox(NULL, L"Failed to create 'Vertex Shader'.", L"ERROR: 'InitialiseShaders() - ParticlesClass.cpp'", MB_OK);
// we do not need anymore this COM object, so we release it.
pVS->Release();
// C R E A T E G E O M E T R Y S H A D E R
ID3DBlob* pGS = nullptr;
ID3DBlob* error_GS = nullptr;
D3DCompileFromFile(
L"Particle_Geometry.hlsl",
nullptr,
nullptr,
"PARTICLES_GS_main",
"gs_5_0",
D3D10_SHADER_DEBUG,
0,
&pGS,
&error_GS
);
if (error_GS)
{
OutputDebugStringA((char*)error_GS->GetBufferPointer());
error_GS->Release();
}
hr = (*Device)->CreateGeometryShader(pGS->GetBufferPointer(), pGS->GetBufferSize(),
nullptr, &this->GeometryShader);
if (FAILED(hr))
MessageBox(NULL, L"Failed to create 'Geometry Shader'.", L"ERROR: 'InitialiseShaders() - ParticlesClass.cpp'", MB_OK);
//We no longer need this COM object and therefore release it
pGS->Release();
//----------------------------------- Pixel Shader
ID3DBlob* pPS = nullptr;
D3DCompileFromFile(
L"Particle_Pixel.hlsl",
nullptr,
nullptr,
"PARTICLES_PS_main",
"ps_5_0",
D3D10_SHADER_DEBUG,
0,
&pPS,
nullptr
);
hr = (*Device)->CreatePixelShader(pPS->GetBufferPointer(), pPS->GetBufferSize(), nullptr, &this->PixelShader);
if (FAILED(hr))
MessageBox(NULL, L"Failed to create 'Pixel Shader'.", L"ERROR: 'InitialiseShaders() - ParticlesClass.cpp'", MB_OK);
pPS->Release();
}
void ParticleSystem::InitializeBuffers(
ID3D11Device* *Device
)
{
HRESULT hr;
memset(this->VertexArray, 0, (sizeof(ParticleVertexData) * this->MaxVertexCount));
D3D11_BUFFER_DESC VertexBufferDesc;
VertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
VertexBufferDesc.ByteWidth = sizeof(ParticleVertexData) * this->MaxVertexCount;
VertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
VertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
VertexBufferDesc.MiscFlags = 0;
VertexBufferDesc.StructureByteStride = 0;
D3D11_SUBRESOURCE_DATA VertexData;
VertexData.pSysMem = this->VertexArray;
VertexData.SysMemPitch = 0;
VertexData.SysMemSlicePitch = 0;
hr = (*Device)->CreateBuffer(&VertexBufferDesc, &VertexData, &this->VertexBuffer);
if (FAILED(hr))
MessageBox(NULL, L"Failed to Create a VertexBuffer.", L"ERROR: 'InitialiseBuffers() - ParticlesClass.cpp'", MB_OK);
}
void ParticleSystem::UpdateVertexBuffer(
ID3D11DeviceContext* *DeviceContext,
Camera* theCamera
)
{
HRESULT hr;
D3D11_MAPPED_SUBRESOURCE MappedResource;
ParticleVertexData* VerticesPtr;
int MaxVertexCount = this->Capacity * 3;
memset(this->VertexArray, 0, (sizeof(ParticleVertexData)) * MaxVertexCount);
for (int i = 0; i < this->CurrentParticleCount; i++)
{
DirectX::XMVECTOR ParticleToCamera = DirectX::XMVectorSubtract(
DirectX::XMVECTOR{
this->PropertiesArray[i].Position.x,
this->PropertiesArray[i].Position.y,
this->PropertiesArray[i].Position.z },
theCamera->CameraPosition);
ParticleToCamera = DirectX::XMVector3Normalize(ParticleToCamera);
DirectX::XMVECTOR TempCross1, TempCross2;
TempCross1 = DirectX::XMVector3Cross(ParticleToCamera, theCamera->CameraUpDirection);
TempCross1 = DirectX::XMVector3Normalize(TempCross1);
TempCross2 = DirectX::XMVector3Cross(TempCross1, ParticleToCamera);
TempCross2 = DirectX::XMVector3Normalize(TempCross2);
DirectX::XMFLOAT3 ParticleRightVector, ParticleUpVector;
DirectX::XMStoreFloat3(&ParticleRightVector, TempCross1);
DirectX::XMStoreFloat3(&ParticleUpVector, TempCross2);
// BOTTOM LEFT
this->VertexArray[(i * 3)].Position.x = this->PropertiesArray[i].Position.x - (ParticleRightVector.x * PARTICLE_SIZE);
this->VertexArray[(i * 3)].Position.y = this->PropertiesArray[i].Position.y - (ParticleRightVector.y * PARTICLE_SIZE);
this->VertexArray[(i * 3)].Position.z = this->PropertiesArray[i].Position.z - (ParticleRightVector.z * PARTICLE_SIZE);
this->VertexArray[(i * 3)].TexCoord = DirectX::XMFLOAT2(0.0f, 1.0f);
this->VertexArray[(i * 3)].Color = this->PropertiesArray[i].Color;
// TOP MIDDLE
this->VertexArray[((i * 3) + 1)].Position.x = this->PropertiesArray[i].Position.x + (ParticleUpVector.x * PARTICLE_SIZE);
this->VertexArray[((i * 3) + 1)].Position.y = this->PropertiesArray[i].Position.y + (ParticleUpVector.y * PARTICLE_SIZE);
this->VertexArray[((i * 3) + 1)].Position.z = this->PropertiesArray[i].Position.z + (ParticleUpVector.z * PARTICLE_SIZE);
this->VertexArray[((i * 3) + 1)].TexCoord = DirectX::XMFLOAT2(0.5f, 0.0f);
this->VertexArray[((i * 3) + 1)].Color = this->PropertiesArray[i].Color;
// BOTTOM RIGHT
this->VertexArray[((i * 3) + 2)].Position.x = this->PropertiesArray[i].Position.x + (ParticleRightVector.x * PARTICLE_SIZE);
this->VertexArray[((i * 3) + 2)].Position.y = this->PropertiesArray[i].Position.y + (ParticleRightVector.y * PARTICLE_SIZE);
this->VertexArray[((i * 3) + 2)].Position.z = this->PropertiesArray[i].Position.z + (ParticleRightVector.z * PARTICLE_SIZE);
this->VertexArray[((i * 3) + 2)].TexCoord = DirectX::XMFLOAT2(1.0f, 1.0f);
this->VertexArray[((i * 3) + 2)].Color = this->PropertiesArray[i].Color;
}
hr = (*DeviceContext)->Map(this->VertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource);
if (FAILED(hr))
MessageBox(NULL, L"Failed to Map a VertexBuffer.", L"ERROR: 'UpdateVertexBuffer() - ParticlesClass.cpp'", MB_OK);
VerticesPtr = (ParticleVertexData*)MappedResource.pData;
memcpy(VerticesPtr, (void*)this->VertexArray, (sizeof(ParticleVertexData) * this->MaxVertexCount));
(*DeviceContext)->Unmap(this->VertexBuffer, 0);
}
void ParticleSystem::UpdateParticlePositions
(
DirectX::XMFLOAT3 CameraPosition
)
{
for (int i = 0; i < this->CurrentParticleCount; i++)
{
this->PropertiesArray[i].Position.x = CameraPosition.x +
this->PropertiesArray[i].CameraRelativePosition.x;
this->PropertiesArray[i].Position.y = (this->PropertiesArray[i].CameraRelativePosition.y -= 0.1f);
this->PropertiesArray[i].Position.z = CameraPosition.z +
this->PropertiesArray[i].CameraRelativePosition.z;
}
}
void ParticleSystem::CreateParticle(DirectX::XMFLOAT3 CameraPosition)
{
if (this->CurrentParticleCount == this->Capacity)
this->CurrentParticleCount = 0;
//static int test = 0;
//if (test < this->Capacity)
// test++;
//else
// test = 0;
// [this->CurrentParticleCount % this->Capacity
this->PropertiesArray[this->CurrentParticleCount].Active = true;
this->PropertiesArray[this->CurrentParticleCount].CameraRelativePosition.x = ((float)(rand() % 100 + 1) * 0.2);
this->PropertiesArray[this->CurrentParticleCount].CameraRelativePosition.y = ((float)(rand() % 100 + 1) * 0.5);
this->PropertiesArray[this->CurrentParticleCount].CameraRelativePosition.z = ((float)(rand() % 100 + 1) * 0.2);
if (rand() % 2 + 1 == 2)
this->PropertiesArray[this->CurrentParticleCount].CameraRelativePosition.x *= -1.0f;
if (rand() % 2 + 1 == 2)
this->PropertiesArray[this->CurrentParticleCount].CameraRelativePosition.z *= -1.0f;
this->PropertiesArray[this->CurrentParticleCount].Position.x = CameraPosition.x +
this->PropertiesArray[this->CurrentParticleCount].CameraRelativePosition.x;
this->PropertiesArray[this->CurrentParticleCount].Position.y = CameraPosition.y +
this->PropertiesArray[this->CurrentParticleCount].CameraRelativePosition.y;
this->PropertiesArray[this->CurrentParticleCount].Position.z = CameraPosition.z +
this->PropertiesArray[this->CurrentParticleCount].CameraRelativePosition.z;
this->PropertiesArray[this->CurrentParticleCount].Color = DirectX::XMFLOAT3(1.0f, 1.0f, 1.0f);
this->PropertiesArray[this->CurrentParticleCount].Velocity = 1.0f;
if (this->CurrentParticleCount < this->Capacity)
this->CurrentParticleCount++;
}