【问题标题】:openGL texturing shows only red component of textureopenGL 纹理仅显示纹理的红色分量
【发布时间】:2011-01-13 06:40:56
【问题描述】:

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

我在 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;
    }

然后我这样画:

{ 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); }

有没有人看到任何会导致我的纹理被解释为 (r,0,0,1) 值的东西? (r,g,b,a)?

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

提前感谢您的帮助

【问题讨论】:

  • 您是否在某处忘记了 glColor() 电话?
  • 在内部,OpenGL 采用 internalFormat 参数并将所需的任何计算应用于您的数据,以获取请求的格式。就 GL 而言,其结果(如果我正确理解文档)始终是 RGBA 图像。如果出于测试目的,您在函数调用中将 internalFormat 显式设置为 GL_RGBA 而不是 GL_RGB 会发生什么?
  • 没什么——完全没有区别

标签: qt opengl texture-mapping


【解决方案1】:

我也遇到过类似的问题。我发现在绘制纹理化四边形之前,我必须将 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);
...

【讨论】:

  • 你试过了吗?我知道这似乎无关紧要,但确实如此。 (无论如何对我来说;-)
  • 这对我也有用。我还没有找到任何文档或任何说你需要重置颜色的东西,所以看起来有点粗略。
【解决方案2】:

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

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

要修复它,请将 MIN/MAG 过滤器设置为 GL_LINEAR 或 GL_NEAREST。

【讨论】:

  • 我做到了——没有区别。我也做的是使用 glTexEnvi(GL_TEX_ENV,GL_TEX_ENV_MODE, GL_REPLACE) ,这一切都不同——似乎有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-10
  • 2012-11-22
  • 1970-01-01
相关资源
最近更新 更多