Important: Please read the Qt Code of Conduct -

Using textures with 16 bits unsigned integers images with QOpenGLTexture

  • Hi,

    For a while I've been using RGB images in 32 bits floating point precision in textures with QOpenGLTexture. I had no trouble with it.
    Originally those images have an unsigned short data type, and I'd liketo keep this data type for sending the data to openGL (BTW, does it actually save some memory at all to do that?). After many attempts, I can't get QOpenGLTexture to display the image. All I end up with is a black image.
    Below is how I setup QOpenGLTexture. The parts that used floating points, and that worked so far, is commented out. The part that assumes images in 16 bits unsigned integers, is right below the latter, uncommented. I'm using OpenGL 3.3, GLSL 330, core profile, on a macbook pro retina with Iris graphics.

    QOpenGLTexture *oglt = new QOpenGLTexture(QOpenGLTexture::Target2D);
            //oglt->setFormat(QOpenGLTexture::RGB32F); // works
            oglt->setSize(naxis1, naxis2);
            //oglt->allocateStorage(QOpenGLTexture::RGB, QOpenGLTexture::Float32); // works
            //oglt->setData(QOpenGLTexture::RGB, QOpenGLTexture::Float32,; // works
            oglt->allocateStorage(QOpenGLTexture::RGB, QOpenGLTexture::UInt16);
            oglt->setData(QOpenGLTexture::RGB, QOpenGLTexture::UInt16,;

    So, in just these lines above, is there something wrong?
    My data in are between [0-65535] when I used UInt16. When I use QOpenGLTexture::Float32, the data are normalized so they would be within [0-1].

    Then, here is my fragment shader:

    #version 330 core
    in mediump vec2 TexCoord;
    out vec4 color;
    //uniform mediump sampler2D ourTexture; // Worked
    uniform usampler2D ourTexture; 
    void main()
        //mediump vec3 textureColor = texture(ourTexture, TexCoord).rgb; // Worked
        uvec3 textureColor = texture(ourTexture, TexCoord).rgb;
        color = vec4(textureColor, 1.0);

    In the fragment shader, all I changed is the type from the 32-bits float to the 16 bit unsigned integers case is sampler2D to usampler2D and vec3 to uvec3, but maybe I'm not supposed to do that, am I?

    Thanks for the help.

  • Maybe I'm meant to use QOPenGLTexture::RGB_Integer instead of QOpenGLTexture::RGB, but even then, I still have a black image.

  • After some experimenting, the culprit was the MagnificationFilter. Although there were no problem setting the Magnification with QOpenGLTexture::NearestMipMapNearest with float images, with integer images, I had to set to QOpenGLTexture::Nearest.