我試圖修改這個Digiben樣本,以獲得從點(衝擊點)產生的粒子效果,並向上漂浮,就像火焰一樣。樣品有顆粒在一個圓圈內旋轉......我嘗試去除餘弦/正弦函數,並用正常的glTranslate替換它們以增加Y值,但我無法得到任何實際結果......有誰能指出大致在哪裏我應該添加/修改此代碼中的翻譯以獲得該結果?OpenGL粒子,幫助控制方向
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;
}
}
現在我增加了glPushMatrix(),glTranslate(X,Y,Z)在上面的run()函數,在循環之前右,其中x,Y,Z爲敵人的位置把它們放在敵人的頂端....那是最好的地方嗎?
感謝您的任何意見!
你也可以顯示你正在嘗試使用的修改過的代碼,它不使用sin和cos嗎? – NickLH
據我所知,在每遍粒子[I] .angle增加,所以我取代了'的glTranslatef(XPOS,yPos,PDEPTH);''用的glTranslatef(0,粒子[I] .angle,PDEPTH);'到希望獲得Ÿaccelleration但我得到各個方向分散在整個空間粒子..... – Alex
如果你使用的glTranslatef,儘量不要使用glRotatef,因爲這會造成散射。一旦你的翻譯工作,然後開始把旋轉回來,並小心你做的事情的順序,翻譯後跟一個旋轉是不一樣的旋轉後跟一個翻譯。 – NickLH