2012-01-27 67 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