opengl粒子系统的简单实现

Particle表示一个粒子

struct Particle
{
	Vector3f<float> position;/**< 粒子的位置 */
	Vector3f<float> color;   /**< 粒子的颜色 */
	Vector3f<float> v;   /**< 粒子的速度(x,y,z方向) */
	Vector3f<float> g;      /**< 粒子在x,y,z上的加速度 */
	
	Vector2f<float> lt;     /**< 粒子生命值 *//**< 粒子消失的速度 */
};
    //生成每一个粒子的属性
	for (int i = 0; i < num; i++)
	{
         //位置
		p[i].position._Vector3f.x = Rand(4);
		p[i].position._Vector3f.y = 4.f;
		p[i].position._Vector3f.z = Rand(0.5) + 0.5;
         //颜色
		p[i].color._Vector3f.x = 255;
		p[i].color._Vector3f.y = 0;
		p[i].color._Vector3f.z = 0;
         //初始速度
		p[i].v._Vector3f.x = Rand(0.1) + 1;
		p[i].v._Vector3f.y = 0;
		p[i].v._Vector3f.z = Rand(0.01) -0.01;
        //加速度
		p[i].g._Vector3f.x = Rand(0.01) - 0.01;
		p[i].g._Vector3f.y = Rand(0.05) - 0.05;
		p[i].g._Vector3f.z = Rand(0.01) - 0.01;
         //生命周期
		p[i].lt.data[0] = Rand(20)+20;
		p[i].lt.data[1] = 0.5;
	}

//Shader.vs代码
     //如果开启粒子系统
	if(Particle == 1)
	{
        //tt为实际粒子时间,tex.s为粒子的生命周期,time为从程序运行到现在经历的时间
	   float tt = time - (int(time / tex.s)) *tex.s;
	   position = Position + v*tt + 0.5 * g * tt*tt;
	}

  

 

posted @ 2017-05-10 17:23  风轻云淡走天涯  阅读(2829)  评论(0编辑  收藏  举报