【发布时间】:2011-11-16 23:39:33
【问题描述】:
我正在尝试修改这个 Digiben 样本,以获得从一个点(撞击点)产生的粒子效果,并像火的火花一样向上漂浮。样本中的粒子旋转了一圈...我尝试删除余弦/正弦函数并用正常的 glTranslate 替换它们并增加 Y 值,但我无法得到任何实际结果...谁能指出大致上我应该在哪里添加/修改此代码中的翻译以获得该结果?
void ParticleMgr::init(){
tex.Load("part.bmp");
GLfloat angle = 0; // A particle's angle
GLfloat speed = 0; // A particle's speed
// Create all the particles
for(int i = 0; i < P_MAX; i++)
{
speed = float(rand()%50 + 450); // Make a random speed
// Init the particle with a random speed
InitParticle(particle[i],speed,angle);
angle += 360 / (float)P_MAX; // Increment the angle so when all the particles are
// initialized they will be equally positioned in a
// circular fashion
}
}
void ParticleMgr::InitParticle(PARTICLE &particle, GLfloat sss, GLfloat aaa)
{
particle.speed = sss; // Set the particle's speed
particle.angle = aaa; // Set the particle's current angle of rotation
// Randomly set the particles color
particle.red = rand()%255;
particle.green = rand()%255;
particle.blue = rand()%255;
}
void ParticleMgr::DrawParticle(const PARTICLE &particle)
{
tex.Use();
// Calculate the current x any y positions of the particle based on the particle's
// current angle -- This will make the particles move in a "circular pattern"
GLfloat xPos = sinf(particle.angle);
GLfloat yPos = cosf(particle.angle);
// Translate to the x and y position and the #defined PDEPTH (particle depth)
glTranslatef(xPos,yPos,PDEPTH);
// Draw the first quad
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex3f(-5, 5, 0);
glTexCoord2f(1,0);
glVertex3f(5, 5, 0);
glTexCoord2f(1,1);
glVertex3f(5, -5, 0);
glTexCoord2f(0,1);
glVertex3f(-5, -5, 0);
glEnd(); // Done drawing quad
// Draw the SECOND part of our particle
tex.Use();
glRotatef(particle.angle,0,0,1); // Rotate around the z-axis (depth axis)
//glTranslatef(0, particle.angle, 0);
// Draw the second quad
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex3f(-4, 4, 0);
glTexCoord2f(1,0);
glVertex3f(4, 4, 0);
glTexCoord2f(1,1);
glVertex3f(4, -4, 0);
glTexCoord2f(0,1);
glVertex3f(-4, -4, 0);
glEnd(); // Done drawing quad
// Translate back to where we began
glTranslatef(-xPos,-yPos,-PDEPTH);
}
void ParticleMgr::run(){
for(int i = 0; i < P_MAX; i++)
{
DrawParticle(particle[i]);
// Increment the particle's angle
particle[i].angle += ANGLE_INC;
}
}
现在我在上面的 run() 函数中添加一个 glPushMatrix(), glTranslate(x, y, z),就在循环之前,x,y,z 作为敌人放置它们的位置敌人的头顶……那是最好的地方吗?
感谢您的任何意见!
【问题讨论】:
-
您能否也显示您尝试使用的不使用 sin 和 cos 的修改后的代码?
-
我知道在每次通过时,particle[i].angle 都会增加,因此我将
glTranslatef(xPos,yPos,PDEPTH);替换为glTranslatef(0,particle[i].angle,PDEPTH);以希望获得 Y 加速度,但我得到粒子分散在各个方向的空间中..... -
如果你使用glTranslatef,尽量不要使用glRotatef,因为这会导致散射。一旦你让平移工作,然后开始放回旋转,并注意你做事情的顺序,平移后旋转与旋转后平移不同。