-
Notifications
You must be signed in to change notification settings - Fork 1
/
particle_system.cpp
307 lines (255 loc) · 10.5 KB
/
particle_system.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
#include "particle_system.h"
#include <stdio.h>
#include <algorithm>
#include "ppm.h"
#include "common/shader.h"
#include "common/texture.hpp"
ParticleSystem::ParticleSystem()
{
initialized = false;
m_max_particles = 10000;
last_used_particle = 0;
lifetime = 5.0;
spread = 1.0f;
radius = 1.5f;
position = Point(0.0, 0.0, 0.0);
srand (time(NULL));
}
ParticleSystem::~ParticleSystem()
{
cleanup();
particles.clear();
if (initialized && system_type != POINTS) {
delete [] position_size_data;
delete [] color_data;
delete [] age_data;
}
}
void ParticleSystem::initialize()
{
initialized = true;
Shader manager;
/* sync data */
gravity_y = gravity[Y];
max_particles = m_max_particles;
particle_direction[X] = main_direction[X];
particle_direction[Y] = main_direction[Y];
particle_direction[Z] = main_direction[Z];
particles.resize(max_particles);
getCameraMatrices();
if (system_type != POINTS) {
position_size_data = new GLfloat[max_particles * 4];
color_data = new GLubyte[max_particles * 4];
age_data = new GLfloat[max_particles];
// Create and compile our GLSL program from the shaders
programID = manager.loadShader(vertex_shader.c_str(), fragment_shader.c_str());
// Vertex shader
CameraRight_worldspace_ID = glGetUniformLocation(programID, "CameraRight_worldspace");
CameraUp_worldspace_ID = glGetUniformLocation(programID, "CameraUp_worldspace");
ViewProjMatrixID = glGetUniformLocation(programID, "VP");
// fragment shader
texture_ID = glGetUniformLocation(programID, "myTextureSampler");
/* get handle for buffers */
squareVerticesID = glGetAttribLocation(programID, "squareVertices");
xyzsID = glGetAttribLocation(programID, "xyzs");
colorID = glGetAttribLocation(programID, "color");
ageID = glGetAttribLocation(programID, "age");
/* load dds files */
if (system_type == DDS){
texture = loadDDS(texture_file.c_str());
}
/* load ppm files */
else if (system_type == IMAGE) {
ppm image(texture_file);
texture = image.createAsTexture();
}
/* VBO containing the particle billboard used to instance particles */
glGenBuffers(1, &billboard_vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, billboard_vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data),
g_vertex_buffer_data, GL_STATIC_DRAW);
/* VBO containing the positions and sizes of the particles */
glGenBuffers(1, &particles_position_buffer);
glBindBuffer(GL_ARRAY_BUFFER, particles_position_buffer);
glBufferData(GL_ARRAY_BUFFER, max_particles * 4 * sizeof(GLfloat), NULL,
GL_STREAM_DRAW);
/* The VBO containing particle colors */
glGenBuffers(1, &particles_color_buffer);
glBindBuffer(GL_ARRAY_BUFFER, particles_color_buffer);
glBufferData(GL_ARRAY_BUFFER, max_particles * 4 * sizeof(GLubyte), NULL,
GL_STREAM_DRAW);
/* The VBO containing particle age */
glGenBuffers(1, &particles_age_buffer);
glBindBuffer(GL_ARRAY_BUFFER, particles_age_buffer);
glBufferData(GL_ARRAY_BUFFER, max_particles * sizeof(GLfloat), NULL,
GL_STREAM_DRAW);
}
last_time = glutGet(GLUT_ELAPSED_TIME);
}
void ParticleSystem::setGPUBuffers(Particle *particle, int particle_index)
{
int i = 4 * particle_index;
position_size_data[i + X] = particle->pos[X];
position_size_data[i + Y] = particle->pos[Y];
position_size_data[i + Z] = particle->pos[Z];
position_size_data[i + SIZE] = particle->size;
color_data[i + R] = particle->color.r * 255;
color_data[i + G] = particle->color.g * 255;
color_data[i + B] = particle->color.b * 255;
color_data[i + A] = particle->color.a * 255;
if (system_type == IMAGE) {
GLfloat age = 1.0 - (particle->lifetime / lifetime);
if (age > 1.0) age = 1.0;
age_data[particle_index] = age;
}
}
void ParticleSystem::drawParticles()
{
gravity = Vector(0.0, gravity_y, 0.0);
main_direction = Vector(particle_direction[X],
particle_direction[Y],
particle_direction[Z]);
int current_time = glutGet(GLUT_ELAPSED_TIME);
elapsed = (current_time - last_time) * 0.001f;
last_time = current_time;
getCameraMatrices();
if (max_particles != m_max_particles) {
max_particles = m_max_particles;
particles.resize(max_particles);
if (system_type != POINTS) {
delete [] position_size_data;
delete [] color_data;
delete [] age_data;
position_size_data = new GLfloat[max_particles * 4];
color_data = new GLubyte[max_particles * 4];
age_data = new GLfloat[max_particles];
}
}
computeParticles();
if (system_type != POINTS) {
sortParticles();
bindShaders();
}
}
/*
* findUnusedParticle - returns the index of the first dead particle
* (i.e. life < 0) in the particles vector.
*/
int ParticleSystem::findDeadParticle()
{
for(int i = last_used_particle; i < max_particles; i++) {
if (particles[i].lifetime < 0) {
last_used_particle = i;
return i;
}
}
for(int i = 0; i < last_used_particle; i++) {
if (particles[i].lifetime <= 0) {
last_used_particle = i;
return i;
}
}
return 0; /* all particles are taken, override the first one */
}
/*
* bindShaders - bind buffer data and pass shader variables.
* code based on example in http://www.opengl-tutorial.org/intermediate-tutorials/billboards-particles/particles-instancing/
*/
void ParticleSystem::bindShaders()
{
/* Update the buffers that OpenGL uses for rendering */
glBindBuffer(GL_ARRAY_BUFFER, particles_position_buffer);
glBufferData(GL_ARRAY_BUFFER, max_particles * 4 * sizeof(GLfloat), NULL, GL_STREAM_DRAW); // Buffer orphaning, a common way to improve streaming perf. See above link for details.
glBufferSubData(GL_ARRAY_BUFFER, 0, active_particles * sizeof(GLfloat) * 4, position_size_data);
glBindBuffer(GL_ARRAY_BUFFER, particles_color_buffer);
glBufferData(GL_ARRAY_BUFFER, max_particles * 4 * sizeof(GLubyte), NULL, GL_STREAM_DRAW); // Buffer orphaning, a common way to improve streaming perf. See above link for details.
glBufferSubData(GL_ARRAY_BUFFER, 0, active_particles * sizeof(GLubyte) * 4, color_data);
glBindBuffer(GL_ARRAY_BUFFER, particles_age_buffer);
glBufferData(GL_ARRAY_BUFFER, max_particles * sizeof(GLfloat), NULL, GL_STREAM_DRAW); // Buffer orphaning, a common way to improve streaming perf. See above link for details.
glBufferSubData(GL_ARRAY_BUFFER, 0, active_particles * sizeof(GLfloat), age_data);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Use our shader
glUseProgram(programID);
// Bind our texture in Texture Unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(texture_ID, 0);
glUniform3f(CameraRight_worldspace_ID, model_view[0][0], model_view[1][0], model_view[2][0]);
glUniform3f(CameraUp_worldspace_ID, model_view[0][1], model_view[1][1], model_view[2][1]);
glUniformMatrix4fv(ViewProjMatrixID, 1, GL_FALSE, &model_projection[0][0]);
/* billboard vertices */
glEnableVertexAttribArray(squareVerticesID);
glBindBuffer(GL_ARRAY_BUFFER, billboard_vertex_buffer);
glVertexAttribPointer(squareVerticesID, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);
/* bind particle attributes */
/* positions of particles' centers */
glEnableVertexAttribArray(xyzsID);
glBindBuffer(GL_ARRAY_BUFFER, particles_position_buffer);
glVertexAttribPointer(xyzsID, 4, GL_FLOAT, GL_FALSE, 0, (void *)0);
/* particles' colors */
glEnableVertexAttribArray(colorID);
glBindBuffer(GL_ARRAY_BUFFER, particles_color_buffer);
glVertexAttribPointer(colorID, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, (void *)0);
/* particles' age */
glEnableVertexAttribArray(ageID);
glBindBuffer(GL_ARRAY_BUFFER, particles_age_buffer);
glVertexAttribPointer(ageID, 1, GL_FLOAT, GL_TRUE, 0, (void *)0);
glVertexAttribDivisorARB(squareVerticesID, 0); // particles vertices : always reuse the same 4 vertices -> 0
glVertexAttribDivisorARB(xyzsID, 1); // positions: one per quad (its center) -> 1
glVertexAttribDivisorARB(colorID, 1); // color: one per quad -> 1
glVertexAttribDivisorARB(ageID, 1); /* age: one per quad */
/* draw each instance */
glDrawArraysInstancedARB(GL_TRIANGLE_STRIP, 0, 4, active_particles);
glDisableVertexAttribArray(squareVerticesID);
glDisableVertexAttribArray(xyzsID);
glDisableVertexAttribArray(colorID);
glDisableVertexAttribArray(ageID);
}
/* getRandVector - returns a vector in a random direction */
Vector ParticleSystem::getRandVector()
{
float phi = (rand() / (float)RAND_MAX) * 2 * M_PI;
float costheta = (2 * (rand() / (float)RAND_MAX)) - 1;
float u = (rand() / (float)RAND_MAX);
float theta = acos(costheta);
float r = radius * pow(u, (1/3));
float x = r * sin(theta) * cos(phi);
float y = r * sin(theta) * sin(phi);
float z = r * cos(theta);
return Vector(x, y, z);
}
/*
* getCameraMatrices - sets the model_view and model_projection matrices and
* camera_position Point.
*/
void ParticleSystem::getCameraMatrices()
{
GLfloat mv[16];
GLfloat p[16];
glm::mat4 projection;
glm::mat4 inverse_model_view;
glGetFloatv(GL_MODELVIEW_MATRIX, mv);
glGetFloatv(GL_PROJECTION_MATRIX, p);
model_view = glm::make_mat4(mv);
projection = glm::make_mat4(p);
model_projection = projection * model_view;
inverse_model_view = glm::inverse(model_view);
camera_position = Point(inverse_model_view[3][0],
inverse_model_view[3][1],
inverse_model_view[3][2]);
}
void ParticleSystem::cleanup()
{
glDeleteBuffers(1, &particles_position_buffer);
glDeleteBuffers(1, &billboard_vertex_buffer);
glDeleteBuffers(1, &particles_color_buffer);
glDeleteBuffers(1, &particles_age_buffer);
glDeleteTextures(1, &texture_ID);
glDeleteProgram(programID);
glUseProgram(0);
}
void ParticleSystem::sortParticles()
{
std::sort(&particles[0], &particles[max_particles]);
}