接下来再看粒子的创建
GLvoid CParticle:: Create(CParticleSystem* parent, float TimeCounter)
{
VECTOR3D TempVelocity;
float RandomYaw; //随机的增长
float RandomPitch;
float NewSpeed; //新的速度
mAge=0.0; //当前生命
//设置粒子的寿命
mDyingAge= (parent->mLife)+(RANDOM_FLOAT*(parent->mLifeCounter));
//粒子的当前颜色继承了整个粒子系统的颜色
mColor[0]= (parent->mStartColor.vertex[0])
+RANDOM_FLOAT*(parent->mColorCounter.vertex[0]);
mColor[1]= (parent->mStartColor.vertex[1])
+RANDOM_FLOAT*(parent->mColorCounter.vertex[1]);
mColor[2]= (parent->mStartColor.vertex[2])
+RANDOM_FLOAT*(parent->mColorCounter.vertex[2]);
mColor[3]= 1.0f;
// 计算粒子颜色计数器
mColorCounter[0]= ((parent->mEndColor.vertex[0])-mColor[0])/mDyingAge;
mColorCounter[1]= ((parent->mEndColor.vertex[1])-mColor[1])/mDyingAge;
mColorCounter[2]= ((parent->mEndColor.vertex[2])-mColor[2])/mDyingAge;
//计算粒子的透明度
mAlpha= (parent->mStartAlpha)+(RANDOM_FLOAT*(parent->mAlphaCounter));
// 计算Alpha计数器
mAlphaCounter=((parent->mEndAlpha)-mAlpha)/mDyingAge;
// 计算粒子的大小
mSize= (parent->mStartSize)+(RANDOM_FLOAT*(parent->mSizeCounter));
mSizeCounter= ((parent->mEndSize)-mSize)/mDyingAge;
// 计算粒子XYZ方向上的速度
TempVelocity.vertex[0]= ((parent->mCurrentLocation.vertex[0])
-(parent->mPreviousLocation.vertex[0]))/ TimeCounter;
TempVelocity.vertex[1]= ((parent->mCurrentLocation.vertex[1])
-(parent->mPreviousLocation.vertex[1]))/ TimeCounter;
TempVelocity.vertex[2]= ((parent->mCurrentLocation.vertex[2])
-(parent->mPreviousLocation.vertex[2]))/ TimeCounter;
// 将粒子从上一次的位置发射到当前位置
mCurrentLocation.vertex[0]= (parent->mPreviousLocation.vertex[0])
+ TempVelocity.vertex[0]* RANDOM_FLOAT*TimeCounter;
mCurrentLocation.vertex[1]= (parent->mPreviousLocation.vertex[1])
+ TempVelocity.vertex[1]* RANDOM_FLOAT*TimeCounter;
mCurrentLocation.vertex[2]= (parent->mPreviousLocation.vertex[2])
+ TempVelocity.vertex[2]* RANDOM_FLOAT*TimeCounter;
mCurrentLocation.vertex[0]+=(Random((parent->mSpreadMin),
(parent->mSpreadMax)))/(parent-> mSpreadFactor);
mCurrentLocation.vertex[1]+=(Random((parent->mSpreadMin),
(parent->mSpreadMax)))/(parent-> mSpreadFactor);
mCurrentLocation.vertex[2]+=(Random((parent->mSpreadMin),
(parent->mSpreadMax)))/(parent-> mSpreadFactor);
// 更新粒子上一次的位置
(parent->mPreviousLocation.vertex[0])=(parent->mCurrentLocation.vertex[0]);
(parent->mPreviousLocation.vertex[1])=(parent->mCurrentLocation.vertex[1]);
(parent->mPreviousLocation.vertex[2])=(parent->mCurrentLocation.vertex[2]);
//对粒子随机取一个发射方向
RandomYaw = (float)(RANDOM_FLOAT*PI*2.0f);
RandomPitch= (float)(DEG_TO_RAD(RANDOM_FLOAT*((parent->angle))));
// 使用球坐标对粒子的速度进行随机化
mVelocity.vertex[0]=(cosf(RandomPitch))*(parent->mVelocity.vertex[0]);
mVelocity.vertex[1]=(sinf(RandomPitch)*cosf(RandomYaw))
*(parent->mVelocity.vertex[1]);
mVelocity.vertex[2]=(sinf(RandomPitch)*sinf(RandomYaw))
*(parent->mVelocity.vertex[2]);
NewSpeed= ((parent->mSpeed)+(RANDOM_FLOAT*(parent->mSpeedCounter)));
mVelocity.vertex[0]*= NewSpeed;
mVelocity.vertex[1]*= NewSpeed;
mVelocity.vertex[2]*= NewSpeed;
//最后将粒子所在的粒子系统设置成parent
SetParentSystem(parent);
}