Learning OpenGL for N-body Sims #5 - Particle Fountain

Now that we have the building blocks of shaders, textures, and manipulating the camera view, lets build a particle fountain. We can draw particles by pushing out a textured quad where the texture is a gaussian blurred point whose alpha increases near the edges. This makes for nice soft particles whose size varies based on distance.
1M Particle Fountain
1 Million Particles in a continuous particle fountain
In this case a particle is an object with an age, position, velocity, and color.

Similarly, the particle generator is a system that keeps track of how many particles currently exist, kill old particles, and spawn new particles with an initial configuration of point of origin and velocity as needed. We'll have our fountain create new particles at a particular point with a random velocity aimed upwards, creating the fountain effect.

// This particle will live 5 seconds. ParticlesContainer[particleIndex].life = 5.0f + 2.5f*((rand() % 1000)/1000.0f-0.5f); // Initial position is same for all particles ParticlesContainer[particleIndex].pos = glm::vec3(0,0,-20.0f);
float spread = 1.5f;
glm::vec3 maindir = glm::vec3(0.0f, 10.0f, 0.0f);
// Very bad way to generate a random direction;
// See for instance http://stackoverflow.com/questions/5408276/python-uniform-spherical-distribution instead,
// combined with some user-controlled parameters (main direction, spread, etc)
glm::vec3 randomdir = glm::vec3(
(rand()%2000 - 1000.0f)/1000.0f,
(rand()%2000 - 1000.0f)/1000.0f,
(rand()%2000 - 1000.0f)/1000.0f
ParticlesContainer[particleIndex].speed = maindir + randomdir*spread;

We can implement gravity for our particle system so the particles fall.

// Simulate simple physics : gravity only, no collisions p.speed += glm::vec3(0.0f,-9.81f, 0.0f) * (float)delta * 0.5f;
p.pos += p.speed * (float)delta;

Source code on Github

Popular posts from this blog

Strawberry DNA extraction and viewing with a cheap USB Microscope

Visualizing TLE Orbital Elements with Python and matplotlib

Relearning web development by writing a clicking game using React, Npm and Browserify