NeHe OpenGL Lesson19 – Particle Engine Using Triangle Strips(粒子引擎)

screen_shot1-300x238 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.

posted @   opencoder  阅读(540)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示