Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SER431 Lecture 12

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

SER431 Lecture 12

Advanced Graphics
Particle Systems
(201809)

Javier Gonzalez-Sanchez PRO

September 25, 2018

Transcript

21 slides
  1. jgs SER 431 Advanced Graphics Lecture 12: Introduction to Particle

    Systems Javier Gonzalez-Sanchez [email protected] PERALTA 230U Office Hours: By appointment
  2. 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
  3. 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.
  4. 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
  5. 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 } };
  6. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 6 jgs

    Particle System class ParticleSystem { public: Particle * particle_head; void add(); void update(float time); void remove(); };
  7. 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; }
  8. 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; } }
  9. 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; } } }
  10. 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... }
  11. 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... }
  12. 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; }
  13. 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; }
  14. 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; }
  15. 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.
  16. 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.

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming