2011-11-16 14 views
1

このDigibenサンプルを修正して、スポット(衝突点)から生成し、火花のような上向きの浮遊粒子を生成します。サンプルは円で回転している粒子を持っています...私はコサイン/サイン関数を削除し、通常のglTranslateをY値の増加に置き換えようとしましたが、実際の結果は得られません...誰でも指摘してくださいおおよそどこにこのコードを翻訳してその結果を得るべきですか?今はglPushMatrix()を追加していについて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; 
    } 
} 

、glTranslate敵の位置としてX、Y、Zと右ループの前に上記ラン()関数内の(x、y、z)は、敵の上にそれらを置くために....それはそのための最高の場所ですか?

ありがとうございました!

+1

sinとcosを使用しないで使用しようとしていた修正済みコードも表示できますか? – NickLH

+0

私は、各パスパーティクル[i] .angleが増加したので、 'glTranslatef(xPos、yPos、PDEPTH);を' glTranslatef(0、particle [i] .angle、PDEPTH)すべての方向で空間全体に散らばっている粒子が得られます..... – Alex

+0

glTranslatefを使用する場合は、散乱を引き起こすので、glRotatefを使用しないでください。翻訳作業が完了したら、回転を戻し始めます。あなたが行う作業の順序に注意してください。回転の後の翻訳は、回転の後の翻訳と同じではありません。 – NickLH

答えて

1

glTranslateとglRotateを使用すると、実際にプログラムのパフォーマンスが低下します。 OpenGLはシーングラフではないので、行列操作関数は描画プロセスに直接影響を与えます。つまり、オブジェクト状態を設定しません。実行している問題は、4×4の行列と行列の乗算に64の乗算と16の加算が含まれていることです。したがって、頂点の位置を直接更新するよりも、パーティクルを移動するための計算能力の96倍を費やしています。

問題が発生しました:すでに述べたように、glTranslateは4つの選択可能な行列のうちの1つの(グローバルな)行列状態で動作します。そして、効果が蓄積される、すなわち、各glTranslateは、前のglTranslateが残ったマトリクスから開始する。 OpenGLは行列スタックを提供します。ここでは、現在の行列のコピーをプッシュしてから、以前の状態に戻すためにポップすることができます。

しかし:マトリックス操作は、OpenGL-3コアと完全後から除去されています。 OpenGLの行列操作は決して加速されませんでした(1996年頃にSGIによって作成された特定のグラフィックワークステーションを除いて)。 3Dジオメトリを扱う優れたプログラムはいずれも、独自の実装またはサードパーティのライブラリによって、より洗練された行列操作を使用していたため、今日は時代錯誤です。 OpenGLの行列スタックは冗長であった。だから私は、OpenGLの行列操作機能を忘れて、あなた自身でロールすることを強くお勧めします。

+0

2つの行列操作関数は、glPushMatrix(http://www.opengl.org/sdk/docs/man/xhtml/glPushMatrix.xml)およびglPopMatrix(http://www.opengl.org/sdk/docs/man/)です。 xhtml/glPushMatrix.xml) – Lalaland

+0

その特定のシナリオでは使用しないでください。パフォーマンスは深刻です。 – datenwolf

+0

アイデアは何かを働かせてから効率を心配することです。さもなければ、あなたは永遠に "完璧な"解決策を見出そうとするかもしれません。 – Lalaland