spark.developpez.com |
When I was preparing classes about particles I've decided not to write my own code for particle system but rather use some library.
I have my 'own' particle system and maybe in near future I will share some code of that. For my workshop about particles I wanted to show students how they can use some third party code and quickly develop some interesting effects.
Topics covered:
- basic description
- example code
- performance
- conclusion
Let's go
Description
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)
Structure:
- 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
Examples
The simplest way to create an effect:Model:
void init()
{
createModel();
createEmitter();
createGroup();
createRenderer();
createSystem();
}
void update()
{
particleSystem->update((float)deltaTimeInSeconds);
}
void render()
{
particleSystem->render();
}
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.
pm = SPK::Model::create(SPK::FLAG_RED | // state of
SPK::FLAG_GREEN | // particle
SPK::FLAG_BLUE |
SPK::FLAG_ALPHA |
SPK::FLAG_SIZE,
SPK::FLAG_ALPHA, // what mutable
SPK::FLAG_GREEN | // what random
SPK::FLAG_SIZE);
// 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);
pm->setLifeTime(7.0f,8.0f);
Emitter:
SPK::SphericEmitter* pe = SPK::SphericEmitter::create(...);Creation of an emitter is quite simple. There are several types of this class: like Straight, Random, Spherical.
pe->setZone(SPK::Point::create(SPK::Vector3D(0.0f,0.015f,0.0f)));
pe->setFlow(350);
pe->setForce(1.5f,1.5f);
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.
Performance
(i5 2400, Radeon 5570)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.
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
picture 1 |
picture 2 |
picture 3 |
Conclusion
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)