Slide 1

Slide 1 text

jgs SER 431 Advanced Graphics Lecture 12: Introduction to Particle Systems Javier Gonzalez-Sanchez [email protected] PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 1 jgs Particle Systems § Uses a large number of very small graphic objects to simulate "fuzzy" phenomena § Examples: fire, explosions, smoke, moving water (such as a waterfall), sparks, falling leaves, rock falls, clouds, fog, snow, dust, meteor tails, stars and galaxies

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 2 jgs Implementation § Emitter: a source of particles § Particles behavioral parameters a) spawning rate: how many particles are generated per unit of time b) initial velocity vector: the direction they are emitted upon creation c) lifetime: the length of time each individual particle exists before disappearing a) color b) etc. § Most parameters are "fuzzy" — instead of a precise numeric value, it is specified a central value and the degree of randomness allowable on either side of the central value.

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 3 jgs Example Images from: http://www.gamefromscratch.com/post/2015/07/09/Godot-Engine-Tutorial-Part-12-Particles.aspx

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 4 jgs Source Code

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 5 jgs Particle class Particle { public: float position[3]; float direction[3]; float life; Particle * next; Particle() { position[0] = 0.5f; position[2] = 0.5f; position[1] = 0.0f; // emitter location direction[0] = (10000 - rand() % 20000) / 10000.0f; direction[1] = (10000 - rand() % 20000) / 10000.0f; direction[2] = (10000 - rand() % 20000) / 10000.0f; // -1 to 1 ? life = rand() % 10000 / 10000.0f; // 0 to 1 } };

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 6 jgs Particle System class ParticleSystem { public: Particle * particle_head; void add(); void update(float time); void remove(); };

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 7 jgs Particle System | add // add void add() { Particle* p = new Particle(); p->next = particle_head; particle_head = p; }

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 8 jgs Particle System | update // update void update(float time) { Particle* p = particle_head; while (p) { p->life -= time; // decrease lifespan p->direction[1] += 9.81f * time; // why? // modify position p->position[0] += time * p->direction[0]; p->position[1] += time * p->direction[1]; p->position[2] += time * p->direction[2]; p = p->next; } }

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 9 jgs Particle System | remove void remove() { Particle* curr = particle_head; Particle* prev = 0; while (curr) { if (curr->life < 0) { if (prev) prev->next = curr->next; else particle_head = curr->next; Particle* temp = curr; curr = curr->next; delete temp; } else { prev = curr; curr = curr->next; } } }

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 10 jgs Particle System | display 1 ParticleSystem ps; void display() { // known code here ... ps.add(); ps.update(0.001); ps.remove(); glPushMatrix(); drawParticles(); glPopMatrix(); // known code here... }

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 11 jgs Particle System | display 2 ParticleSystem ps; void init() { init_frame_timer(); // known code here ... } void display() { // old code here ... ps.add(); ps.update(calculate_frame_time()); ps.remove(); glPushMatrix(); drawParticles(); glPopMatrix(); // known code here... }

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 12 jgs Particle System | frame time void init_frame_timer() { LONGLONG rate; // Counts per second: 2,435,913 if (!QueryPerformanceFrequency((LARGE_INTEGER*)&rate)) return; if (!rate) return; // Improved value if nothing fail (returned) frequency = 1.0f / (float)rate; // Time stamp (performance-counter value): 27 929 379 692 if (!QueryPerformanceCounter((LARGE_INTEGER*)&start_clock)) return; }

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 13 jgs Particle System | frame time float get_current_time() { LONGLONG end_clock; QueryPerformanceCounter((LARGE_INTEGER*)&end_clock); return (end_clock - start_clock) * frequency; }

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 14 jgs Particle System | frame time float calculate_frame_time() { do { endOfFrame = get_current_time(); } while (endOfFrame == startOfFrame); float f = endOfFrame - startOfFrame; ++dx;dx = dx % 5; lastFiveTimes[dx] = f; startOfFrame = endOfFrame; float *p = lastFiveTimes; float *e = p + 5; averageFrameTime = 0.0f; for (; p != e; ++p) { averageFrameTime += *p;} averageFrameTime *= 0.2f; return averageFrameTime; }

Slide 16

Slide 16 text

jgs Test Yourselves

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 16 jgs Test

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 17 jgs Bonus

Slide 19

Slide 19 text

jgs Bonus Points Due date: Saturday, September 29, 11:59 PM

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 19 jgs Bonus Point to be added to your final grade: a) +1 for a simple implementation of fire changing particles color and/or size and emulating smoke and flames. Such as in the image here b) +3 for a realistic implementation of fire. Something similar the image here : colors, blending (transparency), improved physics, lighting. c) +5 for a WoW implementation. Additional programming is needed. Something equal or better than the image here * In all cases we are talking about a fire in 3D – it is not flat.

Slide 21

Slide 21 text

jgs SER431 Advanced Graphics Javier Gonzalez-Sanchez [email protected] Fall 2018 Disclaimer. These slides can only be used as study material for the class SER431 at ASU. They cannot be distributed or used for another purpose.