2012-06-26 32 views
1

我畫我的背景每幀:如何繪製我的opengl背景保持良好性能?

void Window::paintGL() 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity(); 
    glEnable(GL_BLEND); 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

    backgrd->Draw(); 

    texture = text->loadTexture(text->amis_path.toStdString(),text->w,text->h); 
    amis->Draw(); 

    texture = text->loadTexture(text->enemis_path.toStdString(),text->w,text->h); 
    for (int i = 0; i<liste_enemis.length(); i++){ 
     liste_enemis[i]->Draw(); 
    } 

    for (int i = 0; i<liste_missiles.length(); i++){ 
     liste_missiles[i]->Draw(); 
    } 
    swapBuffers(); 
} 

但是當我運行遊戲,幀率一個非常糟糕的(1fps)。

編輯:

嗯,我試圖加載我的背景紋理一個時間,但它不工作:

Background::Background(int w, int h) 
{ 
    width = w; 
    height = h; 
    glGenTextures(1, &texture); 
    glBindTexture(GL_TEXTURE_2D, texture); 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glTexImage2D(GL_TEXTURE_2D,0, GL_RGBA, back.width(), back.height(), 0, 
    GL_RGBA, GL_UNSIGNED_BYTE, back.bits()); 
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 
} 

void Background::Draw(){ 
    glEnable(GL_TEXTURE_2D); 
    glBindTexture(GL_TEXTURE_2D,texture); 
    glPushMatrix(); 
    glTranslatef(width/2, height/2, 0.0f); 
    glBegin(GL_QUADS); 
    glTexCoord2d(0,0); glVertex3f(-width/2, -height/2, 0.0f); 
    glTexCoord2d(0,1); glVertex3f(-width/2, height/2, 0.0f); 
    glTexCoord2d(1,1); glVertex3f(width/2, height/2, 0.0f); 
    glTexCoord2d(1,0); glVertex3d(width/2, -height/2, 0.0f); 
    glEnd(); 
    glPopMatrix(); 
} 

構造函數加載背景glTexImage2D(...)並將其鏈接到「紋理」變量。 然後在我的paintGL()函數中,我調用Background :: Draw()函數。

但是當我運行遊戲時,沒有背景。

如果我將glTexParameteri(...)和glTexImage2D(...)函數從構造函數移動到Draw函數,它將起作用。

回答

5

你正在重新加載紋理的每一幀,這就是什麼導致你的表現(它也將消耗你的記憶在任何時間)。

不是每幀重新加載紋理,只加載一次紋理,然後在紋理之間切換使用glBindTexture