NeHe OpenGL Lesson19 – Particle Engine Using Triangle Strips(粒子引擎)
Here is a simple particle engine. The particle effect is a very important element in game. It could be used simulate an explosion, water fountain, flaming star and so on.
Generally speaking, a particle is a group of triangle billboards. Each triangle has position, moving direction & speed, moving accelerate direction & speed, gravity, life time, whether active or not, color attributes and so on. During their life time, their color attribute will fade in and fade out. And those triangles will always face to the view camera no matter how the camera set up.
Something Further
This program covers almost all key points of a particle engine. Some thing like initialize the particle triangle attributes, update them during their life time (include fade in & fade out the color), then de-active or re-active them if they run out of their life time. Well, but still some thing more we could improve:
1) The particle engine spawn triangle strip billboards in the fixed z-axes. We could make the particle billboards be spawned at any location in the 3D scene. And at the same time, we will make those triangles face toward the view camera.
2) We could make particle billboards be spawned based on some rules. Like, spawn them in a box, among a sphere, along a circle, cylinder or even a quadrics surface. This will make them more real, more cool.
3) Those attributes that change along the time-line could be linear as this program does. Or even more complicated, how about changing along a curve?
4) More attributes works with more complicated material could be changing along time-line. Like texture coordinates, vertex color, scale size and so on.
5) A better solution to design a particle engine is separate those attributes that need to change as time goes. That means you need to design two vertex array: dynamic one for those vertex attribute that need to change, and a static one for other stuff. This ways is called un-rolled data structure. It is deserved as you strong feel that the particle engine part become the bottle neck of the program. But as the same time, you need to think carefully before you apply this solution into your project. Because this will break object-oriented rule, and make the source code not so easy to understand. This may involved a big change in code, and break the stable version. Think twice! The other way could be reducing the number of the particles.
The following code(particle attributes updating) are the core of this particle engine. You could modify them based on your plan here.
particle[loop].x+=particle[loop].xi/(slowdown*1000);// Move On The X Axis By X Speed particle[loop].y+=particle[loop].yi/(slowdown*1000);// Move On The Y Axis By Y Speed particle[loop].z+=particle[loop].zi/(slowdown*1000);// Move On The Z Axis By Z Speed particle[loop].xi+=particle[loop].xg; // Take Pull On X Axis Into Account particle[loop].yi+=particle[loop].yg; // Take Pull On Y Axis Into Account particle[loop].zi+=particle[loop].zg; // Take Pull On Z Axis Into Account particle[loop].life-=particle[loop].fade; // Reduce Particles Life By 'Fade' if (particle[loop].life<0.0f) // If Particle Is Burned Out { particle[loop].life=1.0f; // Give It New Life particle[loop].fade=float(rand()%100)/1000.0f+0.003f; // Random Fade Value particle[loop].x=0.0f; // Center On X Axis particle[loop].y=0.0f; // Center On Y Axis particle[loop].z=0.0f; // Center On Z Axis particle[loop].xi=xspeed+float((rand()%60)-32.0f); // X Axis Speed And Direction particle[loop].yi=yspeed+float((rand()%60)-30.0f); // Y Axis Speed And Direction particle[loop].zi=float((rand()%60)-30.0f); // Z Axis Speed And Direction particle[loop].r=colors[col][0]; // Select Red From Color Table particle[loop].g=colors[col][1]; // Select Green From Color Table particle[loop].b=colors[col][2]; // Select Blue From Color Table }
The full source code could be downloaded from here.