Qt 中自定义一个1D纹理QOpenGLTexture

我想在Qt的QOpenGLWidget上显示自定义的纹理,其中自定义了一个1D纹理,一个2D纹理,一个3D纹理,然后传给shader使用,下面是自定义1D纹理的代码:

void QImage3DView::initTexture1D() {
    const int MAX_CNT = 10000;
    GLubyte *tff = (GLubyte *)calloc(MAX_CNT, sizeof(GLubyte));

    for (int i = 0; i < 180; i++) {
        tff[i * 4 + 0] = GLubyte(100);
        tff[i * 4 + 1] = GLubyte(100);
        tff[i * 4 + 2] = GLubyte(100);
        tff[i * 4 + 3] = GLubyte(1);
    }

    for (int i = 180; i < 208; i++) {
        int a = (i - 179) * 2;
        if (a > 10) a = 6;

        tff[i * 4 + 0] = GLubyte(255);
        tff[i * 4 + 1] = GLubyte(255);
        tff[i * 4 + 2] = GLubyte(255);
        tff[i * 4 + 3] = GLubyte(4);
    }

    for (int i = 208; i < 255; i++) {
        tff[i * 4 + 0] = GLubyte(0);
        tff[i * 4 + 1] = GLubyte(0);
        tff[i * 4 + 2] = GLubyte(0);
        tff[i * 4 + 3] = GLubyte(0);
    }

    *(tff + 1020) = '\0';

    m_texture1D = new QOpenGLTexture(QOpenGLTexture::Target1D);
    m_texture1D->setSize(255);
    m_texture1D->allocateStorage(QOpenGLTexture::PixelFormat::RGBA, QOpenGLTexture::PixelType::UInt8);
    m_texture1D->setData(0, QOpenGLTexture::PixelFormat::RGBA, QOpenGLTexture::PixelType::UInt8, tff);//(VOID*)
    m_texture1D->setMinificationFilter(QOpenGLTexture::Nearest);
    m_texture1D->setMagnificationFilter(QOpenGLTexture::Linear);
    m_texture1D->setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat);

    free(tff);
}

但是在shader中用不了这个纹理,渲染出来的都是黑色的,2D纹理和3D纹理也同样用不了。但是如果2D纹理使用QImage读图片的方式是可以显示的,所以我怀疑自定义纹理的方式可能有问题,请帮忙看一下代码,搞了几天了。。。

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^