openGL 纹理仅显示纹理的红色分量

发布于 2024-10-11 03:59:38 字数 2318 浏览 6 评论 0原文

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

我在 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 或其他

提前感谢您的帮助

I am trying to texture map an image to a single polygon. My image is being read correctly, but only the red plane of the image is being textured.

I am doing this within a QGLWidget

I have checked the image after it is read, and it's components are being read correctly--ie, I get valid values for the green and blue planes.

Here is the code

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

}

Does anybody see anything that would cause my texture to be interpreded as if it is values of (r,0,0,1)? (r,g,b,a)?

QT 4.7.1, Ubuntu 10.04, openGl 2.something or other

thanks in advance for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

病毒体 2024-10-18 03:59:38

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

I've had a similar problem. I found that I had to "reset" the gl color to white and opaque before drawing a texturized quad, or the colors would get messed up. Like this:

...
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);
...
画▽骨i 2024-10-18 03:59:38

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

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

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

This is a very common problem. First, set your MIN/MAG filters to something, since the defaults use mipmaps, and since you didn't provide mipmaps, the texture is incomplete.

Sampling a incomplete texture usually gives white. A glColor call with the default texture environment will multiply the vertex color with the texture color. You probably have something like glColor(red), and red * white = red, so that's why you're seeing red.

To fix it, set the MIN/MAG filters to GL_LINEAR or GL_NEAREST.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文