如何使用QOpenGLTexture构建立方体贴图纹理?

时间:2016-02-01 17:53:57

标签: opengl glsl qt5 skybox qtopengl

我想使用QOpenGLTexture使用6个图像构建立方体贴图纹理。我想我已经把它们放好了。

我使用以下代码构建立方体贴图

const QImage posx = QImage(":/images/posx.jpg").mirrored().convertToFormat(QImage::Format_RGBA8888);
const QImage posy = QImage(":/images/posy.jpg").mirrored().convertToFormat(QImage::Format_RGBA8888);
const QImage posz = QImage(":/images/posz.jpg").mirrored().convertToFormat(QImage::Format_RGBA8888);
const QImage negx = QImage(":/images/negx.jpg").mirrored().convertToFormat(QImage::Format_RGBA8888);
const QImage negy = QImage(":/images/negy.jpg").mirrored().convertToFormat(QImage::Format_RGBA8888);
const QImage negz = QImage(":/images/negz.jpg").mirrored().convertToFormat(QImage::Format_RGBA8888);

d->environment = new QOpenGLTexture(QOpenGLTexture::TargetCubeMap);
d->environment->create();
d->environment->setSize(posx.width(), posx.height(), posx.depth());
d->environment->setFormat(QOpenGLTexture::RGBA8_UNorm);
d->environment->allocateStorage();
d->environment->setData(0, 0, QOpenGLTexture::CubeMapPositiveX,
                        QOpenGLTexture::RGBA, QOpenGLTexture::UInt8,
                        (const void*)posx.constBits(), 0);
d->environment->setData(0, 0, QOpenGLTexture::CubeMapPositiveY,
                        QOpenGLTexture::RGBA, QOpenGLTexture::UInt8,
                        (const void*)posy.constBits(), 0);
d->environment->setData(0, 0, QOpenGLTexture::CubeMapPositiveZ,
                        QOpenGLTexture::RGBA, QOpenGLTexture::UInt8,
                        (const void*)posz.constBits(), 0);
d->environment->setData(0, 0, QOpenGLTexture::CubeMapNegativeX,
                        QOpenGLTexture::RGBA, QOpenGLTexture::UInt8,
                        (const void*)negx.constBits(), 0);
d->environment->setData(0, 0, QOpenGLTexture::CubeMapNegativeY,
                        QOpenGLTexture::RGBA, QOpenGLTexture::UInt8,
                        (const void*)negy.constBits(), 0);
d->environment->setData(0, 0, QOpenGLTexture::CubeMapNegativeZ,
                        QOpenGLTexture::RGBA, QOpenGLTexture::UInt8,
                        (const void*)negz.constBits(), 0);
d->environment->setWrapMode(QOpenGLTexture::ClampToEdge);
d->environment->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
d->environment->setMagnificationFilter(QOpenGLTexture::LinearMipMapLinear);

然后我在paintGL()期间绑定环境纹理,如下所示

.....
d->environment->bind(0);
d->shader->setUniformValue("qt_Environment", 0);

const int nrIndicies = d->torusResolution * d->tubeResolution * 6;
glDrawElements(GL_TRIANGLES, nrIndicies, GL_UNSIGNED_INT, (void*)0);
.....

顶点着色器片段如下

....
varying vec3 v_TexCoord;
....

void main(void)
{
    ....
    v_TexCoord = normalize(v_Normal + v_Position);
    ....
}

片段着色器片段如下

.....
varying vec3 v_TexCoord;
uniform samplerCube qt_Environment;
.....

vec4 evaluateColor(in vec3 normal, in vec3 texCoord)
{
    vec3 finalColor ....
    .....
    .....

    finalColor += textureCube(qt_Environment, texCoord).rgb;    
    return vec4( finalColor, c_one );
}

void main(void)
{
    gl_FragColor = evaluateColor(v_Normal, v_TexCoord);
}

我还有另一部分代码在天空盒上渲染立方体贴图。虽然我能够在天空盒上投影6个图像并正确渲染,但我无法渲染场景中圆环对象的反射。

I am getting a well lit torus, with no reflection.

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

编辑:此问题现已解决。 http://lists.qt-project.org/pipermail/interest/2016-February/020788.html

我必须在环境纹理上调用generateMipMap(),然后才能在环面上应用它。