Qt 5使用QOpenGLTexture和16位整数图像

时间:2016-02-29 17:28:20

标签: c++ opengl textures qt5

有一段时间我一直在使用QOpenGLTexture在纹理中使用32位浮点精度的RGB图像。我没有遇到任何麻烦。 最初这些图像有一个无符号的短数据类型,我会保留这种数据类型,用于将数据发送到openGL(BTW,它实际上是否会节省一些内存来做到这一点?)。经过多次尝试,我无法获得QOpenGLTexture来显示图像。我最终得到的只是一张黑色图像。 以下是我设置QOpenGLTexture的方法。使用浮点的部分,到目前为止,已经注释掉了。以16位无符号整数假设图像的部分位于后者的正下方,未注释。我在带有Iris图形的macbook pro视网膜上使用OpenGL 3.3,GLSL 330,核心配置文件。

QOpenGLTexture *oglt = new QOpenGLTexture(QOpenGLTexture::Target2D);
    oglt->setMinificationFilter(QOpenGLTexture::NearestMipMapNearest);
    oglt->setMagnificationFilter(QOpenGLTexture::NearestMipMapNearest);
    //oglt->setFormat(QOpenGLTexture::RGB32F); // works
    oglt->setFormat(QOpenGLTexture::RGB16U);  
    oglt->setSize(naxis1, naxis2);
    oglt->setMipLevels(10);

    //oglt->allocateStorage(QOpenGLTexture::RGB, QOpenGLTexture::Float32); // works
    //oglt->setData(QOpenGLTexture::RGB, QOpenGLTexture::Float32, tempImageRGB.data); // works
    oglt->allocateStorage(QOpenGLTexture::RGB_Integer, QOpenGLTexture::UInt16);
    oglt->setData(QOpenGLTexture::RGB_Integer, QOpenGLTexture::UInt16, tempImageRGB.data);

所以,在上面的这些行中,有什么不对吗? 当我使用tempImageRGB.data时,UInt16中的数据介于[0-65535]之间。当我使用QOpenGLTexture :: Float32时,tempImageRGB.data中的值已经标准化,因此它们将在[0-1]范围内。

然后,这是我的片段着色器:

#version 330 core

in mediump vec2 TexCoord;
out vec4 color;

uniform mediump sampler2D ourTexture; 


    void main()
    {
        mediump vec3 textureColor = texture(ourTexture, TexCoord).rgb; 
        color = vec4(textureColor, 1.0);
    }

我错过了什么?

1 个答案:

答案 0 :(得分:0)

似乎我通过简单地不使用NearestMipMapNearest进行放大滤镜来解决问题。如果我只将它用于缩小,事情就会奏效。虽然一般来说它是有道理的但我不明白为什么在浮点数情况下使用NearestMipMapNearest进行放大和缩小时没有问题。

因此,通过将'setMagnificationFilter(QOpenGLTexture :: NearestMipMapNearest)'更改为'setMagnificationFilter(QOpenGLTexture :: Nearest)',只需将着色器中的'sampler2D'更改为'usampler2D'即可。缩小过滤器不需要更改。此外,虽然它可以使用和不使用,但我不需要显式设置MipMapLevels,因此我可以删除oglt->setMipLevels(10)

要清楚,这是更正后的代码:

QOpenGLTexture *oglt = new QOpenGLTexture(QOpenGLTexture::Target2D);
oglt->setMinificationFilter(QOpenGLTexture::NearestMipMapNearest);
oglt->setMagnificationFilter(QOpenGLTexture::Nearest);
//oglt->setFormat(QOpenGLTexture::RGB32F); // works
oglt->setFormat(QOpenGLTexture::RGB16U); // now works with integer images (unsigned)
oglt->setSize(naxis1, naxis2);

//oglt->allocateStorage(QOpenGLTexture::RGB, QOpenGLTexture::Float32); // works
//oglt->setData(QOpenGLTexture::RGB, QOpenGLTexture::Float32, tempImageRGB.data); // works
oglt->allocateStorage(QOpenGLTexture::RGB_Integer, QOpenGLTexture::UInt16); // now works with integer images (unsigned)
oglt->setData(QOpenGLTexture::RGB_Integer, QOpenGLTexture::UInt16, tempImageRGB.data); // now works with integer images (unsigned)

片段着色器变得简单:

#version 330 core

in mediump vec2 TexCoord;
out vec4 color;
uniform mediump usampler2D ourTexture; 

    void main()
    {
        mediump vec3 textureColor = texture(ourTexture, TexCoord).rgb; 
        color = vec4(textureColor, 1.0);
    }