openGL纹理只显示纹理的红色成分

时间:2011-01-11 15:57:38

标签: qt opengl texture-mapping

我正在尝试将图像贴图到单个多边形。我的图像正在被正确读取,但只有图像的红色平面被纹理化。

我在QGLWidget中执行此操作

我已经检查了图像,并且正确读取了它的组件 - 也就是说,我获得了绿色和蓝色平面的有效值。

这是代码

QImageReader    *theReader = new QImageReader();

theReader->setFileName(imageFileName);
QImage  theImageRead = theReader->read();
if(theImageRead.isNull())
    {
        validTile = NOT_VALID_IMAGE_FILE;
        return;
    }
else
    {
        int newW = 1;
        int newH = 1;
        while(newW < theImageRead.width())
            {
                newW *= 2;
            }
        while(newH < theImageRead.height())
            {
                newH *= 2;
            }
        theImageRead = theImageRead.scaled(newW, newH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
// values checked in theImageRead are OK here
        glGenTextures(1,&textureObject);
        theTextureImage = QGLWidget::convertToGLFormat(theImageRead);
// values checked in theTextureImage are OK here
        glBindTexture(GL_TEXTURE_2D, textureObject);
        glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,newW, newH, 0, GL_RGBA, GL_UNSIGNED_BYTE,theTextureImage.bits() );
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glFlush();
        validTile = VALID_TEXTURE;
        return;
    }

then I draw like this:

{

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,textureTiles[tN]->getTextureObject() );
glBegin(GL_QUADS);

                    glTexCoord2f(0.0,0.0);
                    glVertex2f(textureTiles[tN]->lowerLeft.x(), textureTiles[tN]->lowerLeft.y());

                    glTexCoord2f(1.0,0.0);
                    glVertex2f(textureTiles[tN]->lowerRight.x(), textureTiles[tN]->lowerRight.y());

                    glTexCoord2f(1.0,1.0);
                    glVertex2f(textureTiles[tN]->upperRight.x(), textureTiles[tN]->upperRight.y());

                    glTexCoord2f(0.0,1.0);
                    glVertex2f(textureTiles[tN]->upperLeft.x(), textureTiles[tN]->upperLeft.y());

glEnd();

glDisable(GL_TEXTURE_2D);

}

QImageReader *theReader = new QImageReader(); theReader->setFileName(imageFileName); QImage theImageRead = theReader->read(); if(theImageRead.isNull()) { validTile = NOT_VALID_IMAGE_FILE; return; } else { int newW = 1; int newH = 1; while(newW < theImageRead.width()) { newW *= 2; } while(newH < theImageRead.height()) { newH *= 2; } theImageRead = theImageRead.scaled(newW, newH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); // values checked in theImageRead are OK here glGenTextures(1,&textureObject); theTextureImage = QGLWidget::convertToGLFormat(theImageRead); // values checked in theTextureImage are OK here glBindTexture(GL_TEXTURE_2D, textureObject); glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,newW, newH, 0, GL_RGBA, GL_UNSIGNED_BYTE,theTextureImage.bits() ); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR); glFlush(); validTile = VALID_TEXTURE; return; }

是否有人看到任何会导致我的纹理被解释的东西,好像它是(r,0,0,1)的值? (R,G,B,A)?

QT 4.7.1,Ubuntu 10.04,openGl 2.something或其他

提前感谢任何帮助

2 个答案:

答案 0 :(得分:7)

我遇到了类似的问题。我发现在绘制纹理四边形之前我必须将gl颜色“重置”为白色和不透明,否则颜色会变得混乱。像这样:

...
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,textureTiles[tN]->getTextureObject() );

glColor4f(1.0, 1.0, 1.0, 1.0); // reset gl color

glBegin(GL_QUADS);
...

答案 1 :(得分:2)

这是一个非常常见的问题。首先,将MIN / MAG过滤器设置为某些内容,因为默认值使用mipmap,并且由于您没有提供mipmap,因此纹理不完整。

采样不完整的纹理通常会产生白色。使用默认纹理环境的glColor调用将顶点颜色与纹理颜色相乘。你可能有类似glColor(红色)和红色*白色=红色的东西,这就是你看到红色的原因。

要解决此问题,请将MIN / MAG过滤器设置为GL_LINEAR或GL_NEAREST。