0
votes

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

2
Do you have a glColor() call somewhere that you forgot about?genpfault
Internally, OpenGL takes the internalFormat parameter and applies whatever required calculations to your data to get it in the requested format. The result of this (if I understand the docs correctly) is always an RGBA image as far as GL is concerned. What happens if, for testing purposes, you explicitly set the internalFormat to GL_RGBA instead of GL_RGB in the function call?badgerr
nothing--no difference at alljhowland

2 Answers

7
votes

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);
...
2
votes

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.