我有一個包含以下內容的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。
這可能是什麼原因造成的?
非常感謝!我一直在尋找各種教程一段時間,我沒有注意到已繪製的代碼,認爲它是我的紋理加載器中的東西... – Danicco
樂於幫助:D – Foggzie
不要忘記,沒有支撐glEnd完成繪圖操作。 – datenwolf