-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParticleEmitter.cpp
333 lines (214 loc) · 8.85 KB
/
ParticleEmitter.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
#include "SolarSystem.h"
#include "Shader.h"
#include "Texture.h"
#include "ParticleEmitter.h"
ParticleEmitter::ParticleEmitter(int amount, int newParticles) {
this->amount = amount;
this->newParticles = newParticles;
//TODO: getter/setter etc.
this->rotation = QUAT_IDENTITY;
this->translation = glm::vec3(5.0, 0.0, 0.0);
this->scale = glm::vec3(1.0);
this->init();
}
void ParticleEmitter::initShader() {
std::string vertexShaderSource = "../../res/shader/particle.vert";
std::string fragmentShaderSource = "../../res/shader/particle.frag";
// Load simple shaders.
GLuint vertexShader = Shader::LoadShader( GL_VERTEX_SHADER, vertexShaderSource );
GLuint fragmentShader = Shader::LoadShader( GL_FRAGMENT_SHADER, fragmentShaderSource );
// Vector for shaders
std::vector<GLuint> shaders;
shaders.push_back(vertexShader);
shaders.push_back(fragmentShader);
// Create the shader program.
shader = Shader::CreateShaderProgram(shaders);
assert( shader != 0 );
std::cout << "Particle-Shader initalized.\n";
}
void ParticleEmitter::initUniforms() {
uniform_mvp = glGetUniformLocation(this->shader, "u_mvp");
uniform_offset = glGetUniformLocation(this->shader, "u_offset");
uniform_texture = glGetUniformLocation(this->shader, "u_texture");
uniform_color = glGetUniformLocation(this->shader, "u_color");
uniform_modelMatrix = glGetUniformLocation(this->shader, "u_modelMatrix");
uniform_viewMatrix = glGetUniformLocation(this->shader, "u_viewMatrix");
uniform_projectionMatrix = glGetUniformLocation(this->shader, "u_projectionMatrix");
}
void ParticleEmitter::initTexture() {
std::string textureFile = "../../res/textures/fire-particles.png";
texture = Texture::LoadTexture(textureFile);
std::cout << "Particle-Texture initialized.\n";
}
void ParticleEmitter::initVao() {
struct VertexXYZ {
glm::vec3 m_Pos;
glm::vec2 m_UV;
};
VertexXYZ g_Vertices[4] = {
{glm::vec3( -1, -1, 0), glm::vec2(0,1)},
{glm::vec3( 1, -1, 0), glm::vec2(1,1)},
{glm::vec3( -1, 1, 0), glm::vec2(0,0)},
{glm::vec3( 1, 1, 0), glm::vec2(1,0)},
};
GLuint g_Indices[6] = {
0,1,2,
2,1,3
};
//------------------------------------------------------------------------------------------------------------------
// get shader inputs
GLuint positionAttribID = (GLuint) glGetAttribLocation(this->shader, "in_position");
GLuint textureAttribID = (GLuint) glGetAttribLocation(this->shader, "in_uv");
// Create a VAO for the cube
glGenVertexArrays(1, &this->vao);
glBindVertexArray(this->vao);
// Create buffer
GLuint vertexBuffer, indexBuffer;
glGenBuffers(1, &vertexBuffer);
glGenBuffers(1, &indexBuffer);
// Bind buffer for vertices
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_Vertices), g_Vertices, GL_STATIC_DRAW);
// Bind buffer for indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(g_Indices), g_Indices, GL_STATIC_DRAW);
// Vertices
glVertexAttribPointer(positionAttribID, 3, GL_FLOAT, GL_FALSE, sizeof(VertexXYZ), MEMBER_OFFSET(VertexXYZ,m_Pos));
glEnableVertexAttribArray(positionAttribID);
// UV
glVertexAttribPointer(textureAttribID, 2, GL_FLOAT, GL_FALSE, sizeof(VertexXYZ), MEMBER_OFFSET(VertexXYZ,m_UV));
glEnableVertexAttribArray(textureAttribID);
//------------------------------------------------------------------------------------------------------------------
// Clean up
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(positionAttribID);
glDisableVertexAttribArray(textureAttribID);
// cout
std::cout << "Particle-VAO initialized.\n";
}
void ParticleEmitter::init() {
this->initShader();
this->initTexture();
this->initVao();
this->initUniforms();
this->initParticles();
}
void ParticleEmitter::draw(float tpf) {
//TODO: remove this -> use billboarding
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
// Use additive blending to give it a 'glow' effect
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
// bind shaderprogram
glUseProgram( shader );
//----------------------------------------------------------------------------------------------------------------------
for (Particle particle : this->particles) {
if (particle.Life > 0.0f) {
// bind vao
glBindVertexArray( vao );
//----------------------------------------
// Map Texture-Units
glUniform1i(uniform_texture, 0);
// Activate and bind TU
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
//----------------------------------------
glUniformMatrix4fv( uniform_modelMatrix, 1, GL_FALSE, glm::value_ptr(this->model_matrix));
glUniformMatrix4fv( uniform_viewMatrix, 1, GL_FALSE, glm::value_ptr(this->view_matrix));
glUniformMatrix4fv( uniform_projectionMatrix, 1, GL_FALSE, glm::value_ptr(this->projection_matrix));
glUniformMatrix4fv( uniform_mvp, 1, GL_FALSE, glm::value_ptr(this->mvp));
glUniform3fv( uniform_offset, 1, glm::value_ptr(particle.Position));
glUniform4fv( uniform_color, 1, glm::value_ptr(particle.Color));
//----------------------------------------
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
//----------------------------------------
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
//----------------------------------------------------------------------------------------------------------------------
// clean up
glUseProgram(0);
// Don't forget to reset to default blending mode
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//TODO: remove this -> use billboarding
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
}
void ParticleEmitter::updateMatrices(glm::mat4 projectionMatrix, glm::mat4 viewMatrix) {
// std::cout << glm::to_string(viewMatrix) << std::endl;
glm::mat4 translation = glm::translate(this->translation);
glm::mat4 scale = glm::scale(this->scale);
glm::mat4 rotation = glm::toMat4(this->rotation);
glm::mat4 modelMatrix = translation * rotation * scale;
this->projection_matrix = projectionMatrix;
this->view_matrix = viewMatrix;
this->model_matrix = modelMatrix;
this->mvp = projectionMatrix * viewMatrix * modelMatrix;
}
void ParticleEmitter::initParticles() {
// Create this->amount default particle instances
for (int i = 0; i < this->amount; i++)
this->particles.push_back(Particle());
}
void ParticleEmitter::update(float tpf) {
// Add new particles
for (int i = 0; i < this->newParticles; i++) {
int unusedParticle = this->firstUnusedParticle();
if (unusedParticle >= 0)
this->respawnParticle(unusedParticle);
}
// Update all particles
float dt = tpf / 1000;
for (Particle &p : this->particles) {
// reduce life
p.Life -= dt;
// particle is alive, thus update
if (p.Life > 0.0f) {
p.Position -= p.Velocity * dt;
p.Color.a -= dt;
}
}
}
int lastUsedParticle = 0;
int ParticleEmitter::firstUnusedParticle() {
// First search from last used particle, this will usually return almost instantly
for (int i = lastUsedParticle; i < this->amount; i++){
if (this->particles[i].Life <= 0.0f){
lastUsedParticle = i;
return i;
}
}
// Otherwise, do a linear search
for (int i = 0; i < lastUsedParticle; i++){
if (this->particles[i].Life <= 0.0f){
lastUsedParticle = i;
return i;
}
}
lastUsedParticle = 0;
return -1;
}
void ParticleEmitter::respawnParticle(int index) {
// random position
// glm::vec3 pos = glm::ballRand(5.0);
glm::vec3 pos = glm::sphericalRand(1.0);
// random velocity
//glm::vec3 vel = glm::sphericalRand(1.0);
glm::vec3 vel = glm::ballRand(2.0);
//------------------------------------------------------------------------------
this->particles[index].Position = pos;
this->particles[index].Velocity = vel;
this->particles[index].Life = 1.0f;
//TODO: lil random color
this->particles[index].Color = glm::vec4(1.0);
}
//-----------------------------------------------------------------------------------------------------
void ParticleEmitter::setRotation(const glm::quat &rotation) {
ParticleEmitter::rotation = rotation;
}
const glm::quat &ParticleEmitter::getRotation() const {
return rotation;
}