c++ - OpenGL 多纹理加载错误

标签 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* 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 覆盖。 即使我不从 spritest2 调用 Draw(),也会显示图像而不是 spritest

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

最佳答案

glBegin 必须在 Draw 中的纹理绑定(bind)函数之后:

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

glBindTexture 的文档中它指出:

When a texture is bound to a target, the previous binding for that target is automatically broken.

您还会在 glBegin 的文档中找到它它指出:

Only a subset of GL commands can be used between glBegin and glEnd. The commands are glVertex, glColor, glSecondaryColor, glIndex, glNormal, glFogCoord, glTexCoord, glMultiTexCoord, glVertexAttrib, glEvalCoord, glEvalPoint, glArrayElement, glMaterial, and glEdgeFlag. Also, it is acceptable to use glCallList or glCallLists to execute display lists that include only the preceding commands. If any other GL command is executed between glBegin and glEnd, the error flag is set and the command is ignored.

因此,在您的 Draw 函数中,您需要放置 glBindTexture(GL_TEXTURE_2D, textureID);glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE , GL_DECAL);glBegin(GL_QUADS) 之前。否则,glBindTexture 什么都不做,因为它在 glBegin 中,因此 SecondBMP.bmp 仍然绑定(bind)到目标 GL_TEXTURE_2D

关于c++ - OpenGL 多纹理加载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14073775/

相关文章:

c++ - 如何在 Linux 上从源代码安装 TBB 并使其工作

opengl - 在OpenGL中将文本打印到屏幕上最简单的方法是什么?

c++ - 将 PNG 纹理导入内存而不是帧缓冲区 - 使用 C++ 和 OpenGL

java - 打开 : glGetTexImage for 2d textures and 1d texture array confusion

java - 来自 LibGDX 中 texturepacker 的纹理

c++ - EncryptFile 函数是如何工作的,为什么我仍然可以保存内容?

c++ - 使用 SSE/AVX 获取存储在 __m256d 中的值的总和

java - OpenGL 淡出/淡入不起作用

java - 如何在 LibGDX 中正确旋转和移动 3D 透视相机

C++如何使二维引擎平台独立