/
particleGenerator.cpp
273 lines (221 loc) · 6.43 KB
/
particleGenerator.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
#include "particleGenerator.h"
ParticleGenerator::ParticleGenerator(ID3D11Device* device, ID3D11DeviceContext* context)
{
m_pD3DDevice = device;
m_pImmediateContext = context;
init();
for (UINT i = 0; i < 1000; i++)
{
p_Particle* p = new p_Particle();
m_free.push_back(p);
}
}
//13
ParticleGenerator::~ParticleGenerator()
{
while (!m_active.empty())
{
delete m_active.back();
m_active.pop_back();
}
while (!m_free.empty())
{
delete m_free.back();
m_free.pop_back();
}
if (m_pVertexBuffer) m_pVertexBuffer->Release();
if (m_pConstantBuffer) m_pConstantBuffer->Release();
}
HRESULT ParticleGenerator::init()
{
HRESULT hr = S_OK;
const unsigned int VertCount = 6;
XMFLOAT3 vertices[VertCount] =
{
XMFLOAT3(-1.0f, -1.0f, 0.0f),
XMFLOAT3(1.0f, 1.0f, 0.0f),
XMFLOAT3(-1.0f, 1.0f, 0.0f),
XMFLOAT3(-1.0f, -1.0f, 0.0f),
XMFLOAT3(1.0f, -1.0f, 0.0f),
XMFLOAT3(1.0f, 1.0f, 0.0f),
};
// setup the constant buffer
D3D11_BUFFER_DESC constant_buffer_desc;
ZeroMemory(&constant_buffer_desc, sizeof(constant_buffer_desc));
constant_buffer_desc.Usage = D3D11_USAGE_DEFAULT; //Can use UpdateSubresourse() to update
constant_buffer_desc.ByteWidth = sizeof(PARTICLE_CONSTANT_BUFFER); //MUST be a multiple of 16, calculated from the constant buffer struct
constant_buffer_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; //Use as a constant buffer
//create the constant buffer
hr = m_pD3DDevice->CreateBuffer(&constant_buffer_desc, NULL, &m_pConstantBuffer);
if (FAILED(hr))
return hr;
// setup the constant buffer
D3D11_BUFFER_DESC vertex_buffer_desc;
ZeroMemory(&vertex_buffer_desc, sizeof(vertex_buffer_desc));
vertex_buffer_desc.Usage = D3D11_USAGE_DYNAMIC; //Can use UpdateSubresourse() to update
vertex_buffer_desc.ByteWidth = sizeof(XMFLOAT3)*VertCount; //MUST be a multiple of 16, calculated from the constant buffer struct
vertex_buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; //Use as a constant buffer
vertex_buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
//create the constant buffer
hr = m_pD3DDevice->CreateBuffer(&vertex_buffer_desc, NULL, &m_pVertexBuffer);
if (FAILED(hr))
return hr;
D3D11_MAPPED_SUBRESOURCE ms;
m_pImmediateContext->Map(m_pVertexBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms);
memcpy(ms.pData, vertices, sizeof(vertices));
m_pImmediateContext->Unmap(m_pVertexBuffer, NULL);
return S_OK;
}
void ParticleGenerator::drawOne(RENDER_DESC2& desc, const p_Particle& p)
{
XMMATRIX world = XMMatrixIdentity();
XMVECTOR d;
d.x = p.Position.x - desc.camera->x;
d.y = p.Position.y - desc.camera->y;
d.z = p.Position.z - desc.camera->z;
float m_yAngle = atan2(d.x, d.z) + XM_PI;
float dyz = d.z / cos(m_yAngle);
float m_xAnlge = atan2(-d.y, dyz) + XM_PI;
world = XMMatrixScaling(p.scale, p.scale, p.scale);
world *= XMMatrixRotationX(m_xAnlge);
world *= XMMatrixRotationY(m_yAngle);
world *= XMMatrixTranslation(p.Position.x, p.Position.y, p.Position.z);
world *= (*desc.world);
//create constatnt buffer
PARTICLE_CONSTANT_BUFFER particle_cb_values;
ZeroMemory(&particle_cb_values, sizeof(PARTICLE_CONSTANT_BUFFER));
//add world view projection
particle_cb_values.WorldViewProjection = (world)*(*desc.view)*(*desc.projection);
particle_cb_values.colour = p.c.getXMFloat4();
//update
m_pImmediateContext->UpdateSubresource(m_pConstantBuffer, 0, 0, &particle_cb_values, 0, 0);
//Vertex shader c buffer
m_pImmediateContext->VSSetConstantBuffers(0, 1, &m_pConstantBuffer);
UINT stride = sizeof(XMFLOAT3);
UINT offset = 0;
m_pImmediateContext->IASetVertexBuffers(0, 1, &m_pVertexBuffer, &stride, &offset);
m_pImmediateContext->Draw(6, 0);
}
float ParticleGenerator::randomZeroToOne()
{
float output = float(rand() % 101);
return output / 100.0f;
}
float ParticleGenerator::randomNegOneToPosOne()
{
float output = float(rand() % 201 - 100);
return output / 100.0f;
}
void ParticleGenerator::create()
{
p_Particle* p = m_free.front();
p->Position = { 0.0f, 3.0f, 10.0f };
p->Velocity = { 0.5f, 0.0f, 0.0f };
p->scale = 0.1f;
p->Mass = 0.1f;
p->InvMass = 1 / p->Mass;
p->time = 0.0f;
p->checkColl = false;
p->c = Vector4(1.0f, 0.0f, 0.0f, 1.0f);
m_active.push_back(p);
m_free.pop_front();
p = m_free.front();
p->Position = { 3.0f, 3.0f, 10.0f };
p->Velocity = ZeroVector3;
p->scale = 0.1f;
p->Mass = 0.1f;
p->InvMass = 1 / p->Mass;
p->time = 0.0f;
p->checkColl = false;
p->c = Vector4(0.0f, 1.0f, 0.0f, 1.0f);
m_active.push_back(p);
m_free.pop_front();
p = m_free.front();
p->Position = { 1.5f, 0.0f, 10.0f };
p->Velocity = {0.0f, 1.0f, 0.0f};
p->scale = 0.1f;
p->Mass = 0.1f;
p->InvMass = 1 / p->Mass;
p->time = 0.0f;
p->checkColl = false;
p->c = Vector4(0.0f, 0.0f, 1.0f, 1.0f);
m_active.push_back(p);
m_free.pop_front();
}
void ParticleGenerator::drawP(RENDER_DESC2& desc)
{
p_Particle Test;
Test.Position = XMFLOAT3(0.0f, 0.0f, 0.0f);
Test.scale = 0.1f;
Test.c = Vector4(0.0f, 0.0f, 0.0f, 1.0f);
drawOne(desc, Test);
//update(dt);
for (p_Particle* p : m_active)
{
drawOne(desc, *p);
}
}
void ParticleGenerator::update(float dt)
{
for (p_Particle* p : m_active)
{
p->applyForce(gravity, dt);
}
std::list<p_Particle*>::iterator j, i = m_active.begin();
p_Particle *p1, *p2;
while (i != m_active.end())
{
j = i;
j++;
p1 = (*i);
p1->betterCheckBoundries(dt);
while (j != m_active.end())
{
p2 = (*j);
//check collision
if (p1->betterCollisionCheck(*p2, dt))
{
//resolve collision
p1->collisionResponse(*p2);
}
j++;
}
i++;
}
for (p_Particle* p : m_active)
{
p->stepPosition(dt);
}
}
void ParticleGenerator::spawnParticle()
{
if (!m_free.empty())
{
p_Particle* p = m_free.front();
//set p values
p->Velocity = { randomNegOneToPosOne(), randomZeroToOne(), randomNegOneToPosOne() };
p->Position = { randomNegOneToPosOne() * 3.0f, randomZeroToOne() * 3.0f, randomNegOneToPosOne() * 3.0f };
p->scale = 0.3f;
p->Mass = 1.0f;
p->InvMass = 1 / p->Mass;
p->time = 0.0f;
p->checkColl = false;
p->c = Vector4(randomZeroToOne(), randomZeroToOne(), randomZeroToOne(), 1.0f);
bool result = false;
for(p_Particle* Pi : m_active)
{
if (p->betterCollisionCheck(*Pi, 1.0f / 60.0f)) result = true;
}
while (result)
{
result = false;
p->Position = { randomNegOneToPosOne() * 3.0f, randomZeroToOne() * 3.0f, randomNegOneToPosOne() * 3.0f };
for (p_Particle* Pi : m_active)
{
if (p->betterCollisionCheck(*Pi, 1.0f / 60.0f)) result = true;
}
}
m_active.push_back(p);
m_free.pop_front();
}
}