C++ OpenGL多纹理加载错误
我有一个Model2D类,它包含以下内容:C++ OpenGL多纹理加载错误,c++,opengl,textures,C++,Opengl,Textures,我有一个Model2D类,它包含以下内容: 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*
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生成的TextureID也不同,它们也被正确调用,调试时X、Y和Z位置也正确,但原因不明,spritest图像数据正在被spritest2覆盖。
即使我没有从sprites2调用Draw(),也会显示图像,而不是spritest
这可能是什么原因造成的?
glBegin
必须位于Draw
中的纹理绑定函数之后:
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, textureID);
glBegin(GL_QUADS);
它的文件中指出:
将纹理绑定到目标时,该目标的先前绑定将自动断开
您还可以在it文档中找到:
只能在glBegin
和glEnd
之间使用GL命令的子集。这些命令是glVertex
,glColor
,glSecondaryColor
,glIndex
,glFogCoord
,glTexCoord
,glMultiTexCoord
,glVertexAttrib
,glEvalCoord
,glEvalPoint
,glarrangement
,glMaterial
和glEdgeFlag
。此外,可以使用glCallList
或glCallList
执行仅包含前面命令的显示列表。如果在glBegin
和glEnd
之间执行任何其他GL命令,则设置错误标志并忽略该命令
因此,在
Draw
函数中,需要放置glBindTexture(GL_TEXTURE_2D,textureID)代码>和glTexEnvf(GL_纹理_环境,GL_纹理_环境模式,GL_贴花)代码>在glBegin(GL\u QUADS)
之前。否则,glBindTexture
不会执行任何操作,因为它位于glBegin
中,因此SecondBMP.bmp
仍然绑定到目标GL\u TEXTURE\u 2D
glBegin
中的纹理绑定函数必须位于Draw
中的纹理绑定函数之后:
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, textureID);
glBegin(GL_QUADS);
它的文件中指出:
将纹理绑定到目标时,该目标的先前绑定将自动断开
您还可以在it文档中找到:
只能在glBegin
和glEnd
之间使用GL命令的子集。这些命令是glVertex
,glColor
,glSecondaryColor
,glIndex
,glFogCoord
,glTexCoord
,glMultiTexCoord
,glVertexAttrib
,glEvalCoord
,glEvalPoint
,glarrangement
,glMaterial
和glEdgeFlag
。此外,可以使用glCallList
或glCallList
执行仅包含前面命令的显示列表。如果在glBegin
和glEnd
之间执行任何其他GL命令,则设置错误标志并忽略该命令
因此,在Draw
函数中,需要放置glBindTexture(GL_TEXTURE_2D,textureID)代码>和glTexEnvf(GL_纹理_环境,GL_纹理_环境模式,GL_贴花)代码>在glBegin(GL\u QUADS)
之前。否则,glBindTexture
不会执行任何操作,因为它位于glBegin
中,因此SecondBMP.bmp
仍然绑定到目标GL\u TEXTURE\u 2D
非常感谢!我已经阅读了一段时间的各种教程,我没有注意到绘图代码,认为它是我的纹理加载器中的某个东西…不要忘记,没有任何工具可以完成绘图操作。非常感谢!我已经阅读了一段时间的各种教程,我没有注意到绘图代码,认为这是我的纹理加载器中的某个东西…不要忘记,没有任何工具可以完成绘图操作。