C++17 In Detail

20 July 2012

The SPARK Particle Library

See my new website at cppstories.com

When I was preparing presentation and workshop about particles, I've decided not to write my own code for the system but use some external library.
I have my 'own' particle system and maybe in the near future I will share the code (here is a post from the future). For my classes I wanted to show students how to use third party code and quickly develop some interesting effects. While searching for the library, I found a very interesting piece of software: SPARK Particle Library.
  • Basic description
  • Example code
  • Performance
  • Conclusion
Let's go


Lib version: 1.5.5
  • Spark is a cross platform particle lib build written in C++ (OO with heavy use of STL) and designed to work well with OpenGL, SFML and Irrlicht.
  • It is quite low level so when one decides to use it some facade should be added.
  • Particles simulation is performed on the CPU side only (and then transferred to the GPU only for rendering)
  • Rendering is done using fixed function pipeline unfortunately (but will change probably in th second version of the library)
  • System is a collection of Groups - it is optional, because each group can be handled independently if you want.
  • Group is a core object in the simulation.
    • contains Model (description of particles)
    • contains particles array (in thing called Pool)
    • and there are some Emitters and Modifiers
  • Renderer is connected with a group


The simplest way to create an effect:
void init() 
void update()
void render()
pm = SPK::Model::create(SPK::FLAG_RED |    // state of 
                        SPK::FLAG_GREEN |  // particle
                        SPK::FLAG_BLUE | 
                        SPK::FLAG_ALPHA | 
                        SPK::FLAG_ALPHA,  // what mutable      
                        SPK::FLAG_GREEN | // what random

// alpha is mutable from 1.0 to 0.2 over the lifetime of particle
// is is interpolated linearly
pm->setParam(SPK::PARAM_ALPHA, 1.0f, 0.2f);

// green and size are random 
pm->setParam(SPK::PARAM_GREEN, 0.0f, 1.0f); 
pm->setParam(SPK::PARAM_SIZE,  0.2f, 4.0f); 

Model is quite flexible structure. You can choose what is enabled, what is randomized, what is linearly interpolated over the time. You can even provide custom interpolator for given parameter.

SPK::SphericEmitter* pe = SPK::SphericEmitter::create(...);
Creation of an emitter is quite simple. There are several types of this class: like Straight, Random, Spherical.

After creation of model and emitters you need to create group(s) and attach emmiters to group. Then attach group(s) to system.

For more source code look at attached examples.


(i5 2400, Radeon 5570)
Rendering: Quads, with texture on
200 000 particles and 50 000 flow: 43 FPS (picture 1)
1 000 000 particles and 100 000 flow: 12...14 FPS (picture 2)
2 000 000 particles and 200 000 flow: 5..6 FPS (picture 3) 
for the last test I got around 15 FPS when switched to basic PointRenderer
Of course there are many factors that influence the results, but all in all I think that the performance of this library is quite decent. For effects with millions of particles I suggest using something else (simulation should be than computed on the GPU rather than on the CPU), but for common effects is is quite enough.
picture 1
picture 2
picture 3


What I like:
  • ease of use (in several minutes you have something working) 
  • very nice documentation 
  • ability to extend the particle system by own classes and techniques 
  • performance is quite decent
What I dislike:
  • fixed pipeline rendering (but it should change in second version of the library)
  • Last version is from 2009... support and development could be a bit faster :)
Source code with my sample: spark_155_particles.cpp

One demo from the library (not mine, official)


If you want to get additional C++ resources, exlusive articles, early access content, private Discord server and weekly curated news, check out my Patreon website: (see all benefits):

© 2017, Bartlomiej Filipek, Blogger platform
Disclaimer: Any opinions expressed herein are in no way representative of those of my employers. All data and information provided on this site is for informational purposes only. I try to write complete and accurate articles, but the web-site will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use.
This site contains ads or referral links, which provide me with a commission. Thank you for your understanding.