gpt4 book ai didi

c++ - Qt 5 与 QOpenGLTexture 和 16 位整数图像

转载 作者:太空宇宙 更新时间:2023-11-04 13:19:52 24 4
gpt4 key购买 nike

一段时间以来,我一直在 QOpenGLTexture 的纹理中使用 32 位浮点精度的 RGB 图像。我没有遇到任何问题。最初,这些图像具有无符号短数据类型,我想保留此数据类型以将数据发送到 openGL(顺便说一句,这样做实际上是否节省了一些内存?)。经过多次尝试,我无法让 QOpenGLTexture 显示图像。我最终得到的只是一张黑色图像。下面是我如何设置 QOpenGLTexture。使用 float 且到目前为止有效的部分已被注释掉。假定图像为 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);

那么,就以上这些行,有什么问题吗?当我使用 UInt16 时,我在 tempImageRGB.data 中的数据在 [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);
}

我错过了什么?

最佳答案

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

因此,代码只需将着色器中的“sampler2D”更改为“usampler2D”,将“setMagnificationFilter(QOpenGLTexture::NearestMipMapNearest)”更改为“setMagnificationFilter(QOpenGLTexture::Nearest)”即可工作。缩小过滤器不需要更改。此外,尽管它可以使用和不使用,但我不需要显式设置 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);
}

关于c++ - Qt 5 与 QOpenGLTexture 和 16 位整数图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35705682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com