2012-01-27 22 views
0

我在Render類中有一個名爲showMainMenu()的方法。 在渲染我定義我的紋理位圖OpenGL只顯示在方法中加載的紋理?

Bitmap* bBall; 
Bitmap* bWall; 
Bitmap* bStart; 
Bitmap* bEnd; 
Bitmap* bHighscores; 
Bitmap* bHelp; 
Bitmap* bStar; 

在我的渲染的構造函數我做的:

this->bBall = new Bitmap("ball.bmp"); 
this->bEnd = new Bitmap("beenden.bmp"); 
this->bStart = new Bitmap("starten.bmp"); 
this->bStar = new Bitmap("star.bmp"); 
this->bHelp = new Bitmap("hilfe.bmp"); 
this->bHighscores = new Bitmap("highscores.bmp"); 
this->bWall = new Bitmap("wall.bmp"); 

在showMainMenu()我綁定以下列方式質地:

glEnable(GL_TEXTURE_2D); //Texturen aktivieren 

//draw Start button 
glBindTexture(GL_TEXTURE_2D, this->bStar->texture); 

但我的顯示器保持白色:( 當我加載我的方法內的紋理

Bitmap m = Bitmap("star.bmp"); 
glBindTexture(GL_TEXTURE_2D, m.texture); 

我可以看到紋理。 爲什麼不是第一次工作?

+2

這裏的信息太少了。你在哪裏生成紋理對象,你在哪裏加載紋理數據,你使用着色器,如果是的話,你在哪裏發送採樣器,...?嘗試提供一個簡明的,最小的工作示例,顯示您的問題。 – KillianDS 2012-01-27 13:06:22

回答

2

我最好的猜測是,你在創建OpenGL上下文之前創建了Bitmap實例。然後,位圖文件將被加載,但不會生成紋理對象。最簡單的方法來解決這個問題:在實例化Bitmap(即在構造函數中)時,只需加載文件數據並將紋理ID變量設置爲0.添加方法bindTexture,它爲您調用glBindTexture(無論如何,您應該這樣做,這就是OOP應該工作)。但是如果紋理ID爲零,並且在綁定之前生成ID和紋理,則還要添加一個測試。

例子:

void Bitmap::bindTexture() 
{ 
    if(!textureID) { 
     glGenTextured(1, &textureID); 
     glBindTexture(GL_TEXTURE_..., textureID); 
     upload_texture_data(); 
    } else { 
     glBindTexture(GL_TEXTURE_..., textureID); 
    } 
} 

BTW:通過this->訪問類成員被認爲是不好的風格,絕對沒有理由做這種方式。甚至將this指針指向基類將不會給你它的虛擬方法,因爲隱式的上傳是虛擬的全部點。

+0

不要忘記在上傳紋理數據之前綁定它。 – 2012-01-27 14:49:02