2012-12-28 61 views
1

我有一个包含以下内容的Model2D类:的OpenGL多重纹理加载错误

class Model2D 
{ 
    public: //it's private, but I'm shortening it here 
    unsigned char* bitmapImage; 
    unsigned int textureID; 
    int imageWidth, imageHeight; 

    void Load(char* bitmapFilename); 
} 
void Model2D::Load(char* bitmapFilename) 
{ 
    ifstream readerBMP; 
    readerBMP.open(bitmapFilename, ios::binary); 
    //I get the BMP header and fill the width, height 

    bitmapImage = new GLubyte[imageWidth * imageHeight * 4]; 
    //Loop to read all the info in the BMP and fill the image array, close reader 

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 

    glGenTextures(1, &textureID); 
    glBindTexture(GL_TEXTURE_2D, textureID); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 
    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, imageWidth, imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, bitmapImage); 
} 
void Model2D::Draw() 
{ 
    glBegin(GL_QUADS);   
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 
    glBindTexture(GL_TEXTURE_2D, textureID); 

    float texScale = 500.0; //this is just so I don't have to resize images 

    glTexCoord2f(0.0, 0.0); glVertex3f(-(imageWidth/texScale), -(imageHeight/texScale), 0.0); 
    glTexCoord2f(0.0, 1.0); glVertex3f(-(imageWidth/texScale), (imageHeight/texScale), 0.0); 
    glTexCoord2f(1.0, 1.0); glVertex3f((imageWidth/texScale), (imageHeight/texScale), 0.0); 
    glTexCoord2f(1.0, 0.0); glVertex3f((imageWidth/texScale), -(imageHeight/texScale), 0.0);  
} 

而在我的主循环中,我有:

Model2D spritest; 
spritest.LoadFromScript("FirstBMP.bmp"); 
spritest.Z(-5); spritest.X(-2); 

Model2D spritest2; 
spritest2.LoadFromScript("SecondBMP.bmp"); 
spritest2.X(+2); spritest2.Z(-6); 

//... 
//main loop 
spritest.Draw(); 
spritest2.Draw(); 

在调试这似乎是工作的罚款,位图的地址是不同的,OpenGL生成的纹理ID也是不同的,并且它们也被正确调用,并且X,Y和Z位置在调试时也是正确的,但由于某些未知原因,spritest图像数据正在被覆盖十由spritest2。 即使我没有从spritest2调用Draw(),图像正在显示而不是spritest

这可能是什么原因造成的?

回答

1

glBegin必须拿出你的纹理绑定功能后Draw

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 
glBindTexture(GL_TEXTURE_2D, textureID); 
glBegin(GL_QUADS);   

在文档glBindTexture它指出:

当纹理绑定到一个目标,以前为绑定目标被自动破坏。

你还会发现,对于glBegin它规定的文件中:

只有GL命令的子集可以glBeginglEnd之间使用。命令是glVertexglColorglSecondaryColorglIndexglNormalglFogCoordglTexCoordglMultiTexCoordglVertexAttribglEvalCoordglEvalPointglArrayElementglMaterial,和glEdgeFlag。此外,使用glCallListglCallLists执行仅包含上述命令的显示列表是可以接受的。如果在glBeginglEnd之间执行任何其他GL命令,则会设置错误标志并忽略该命令。

正因为如此,在你Draw功能,你将需要放置glBindTexture(GL_TEXTURE_2D, textureID);glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);glBegin(GL_QUADS)之前。否则,glBindTexture什么都不做,因为它在glBegin之内,因此SecondBMP.bmp仍然绑定到目标GL_TEXTURE_2D

+0

非常感谢!我一直在寻找各种教程一段时间,我没有注意到已绘制的代码,认为它是我的纹理加载器中的东西... – Danicco

+0

乐于帮助:D – Foggzie

+0

不要忘记,没有支撑glEnd完成绘图操作。 – datenwolf