我想使用 6 个图像使用 QOpenGLTexture 构造一个立方体贴图纹理。我想我把它们放对了。
我正在使用以下代码来构造立方体贴图
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 个图像投影到天空盒上并正确渲染,但我无法在场景中的圆环物体上渲染反射。
有人可以帮忙吗?