您需要知道相机的方向才能确定如何更改火焰粒子的方向。您需要基本上反转相机的旋转矩阵。如果我这样做,我会在本地保留一个转换的副本,以便我可以快速访问相机旋转。另一种方法是读取变换矩阵并从矩阵计算逆旋转。
static void inverseRotMatrix(const GLfloat in[4][4], GLfloat out[4][4])
{
out[0][0] = in[0][0];
out[0][1] = in[1][0];
out[0][2] = in[2][0];
out[0][3] = 0.0f;
out[1][0] = in[0][1];
out[1][1] = in[1][1];
out[1][2] = in[2][1];
out[1][3] = 0.0f;
out[2][0] = in[0][2];
out[2][1] = in[1][2];
out[2][2] = in[2][2];
out[2][3] = 0.0f;
out[3][0] = 0.0f;
out[3][1] = 0.0f;
out[3][2] = 0.0f;
out[3][3] = 1.0f;
}
void RenderFlame()
{
GLfloat matrix[4][4];
GLfloat invMatrix[4][4];
glGetFloatv(GL_MODELVIEW_MATRIX, matrix[0]);
inverseRotMatrix(matrix, invMatrix);
glPushMatrix();
// glMultMatrixf(invMatrix[0]); If you want to rotate the entire body of particles
for ... each particle
...
glTranslatef(particleX, particleY, particleZ);
glMultMatrixf(invMatrix[0]);
DrawParticle();
...
glPopMatrix();
}
这可能对你有用,也可能对你不起作用,这取决于你在做什么。如果颗粒向各个方向扩散应该没问题,但如果火焰基本上是平的,你就会遇到其他问题。所有这一切都是旋转每个粒子,使其面向屏幕。如果您只是旋转相机,它将正常工作。如果您移动相机,您必须围绕火焰的中心点旋转所有点。但这确实通过反转旋转矩阵为您提供了所需的旋转,这只是您应用转换多少次的问题。(我添加了一条评论,您可以在其中应用另一个旋转来旋转整个粒子)