教程地址:
这次主要是实现立方体贴图,相较之前的不同之处在于,纹理的设置方式的区别,这里贴一下在QT中如何设置一个用于立方体贴图的纹理:
核心代码如下:
void SkyCube::CreateCubeTexture()
{
const QImage posright = QImage(":/img/skybox/right.jpg").convertToFormat(QImage::Format_RGBA8888);
const QImage posleft = QImage(":/img/skybox/left.jpg").convertToFormat(QImage::Format_RGBA8888);
const QImage postop = QImage(":/img/skybox/top.jpg").convertToFormat(QImage::Format_RGBA8888);
const QImage posbottom = QImage(":/img/skybox/bottom.jpg").convertToFormat(QImage::Format_RGBA8888);
const QImage posfront = QImage(":/img/skybox/front.jpg").convertToFormat(QImage::Format_RGBA8888);
const QImage posback = QImage(":/img/skybox/back.jpg").convertToFormat(QImage::Format_RGBA8888);
ourtexture=new QOpenGLTexture(QOpenGLTexture::TargetCubeMap);
ourtexture->create();
ourtexture->setSize(posright.width(),posright.height(),posright.depth());
ourtexture->setFormat(QOpenGLTexture::RGBA8_UNorm);
ourtexture->allocateStorage();
if(!ourtexture->isCreated())
{
qDebug()<<"failed to load texture";
}
else {
ourtexture->setData(0, 0, QOpenGLTexture::CubeMapPositiveX,
QOpenGLTexture::RGBA, QOpenGLTexture::UInt8,
(const void*)posright.constBits(), 0);
ourtexture->setData(0, 0, QOpenGLTexture::CubeMapNegativeX,
QOpenGLTexture::RGBA, QOpenGLTexture::UInt8,
(const void*)posleft.constBits(), 0);
ourtexture->setData(0, 0, QOpenGLTexture::CubeMapPositiveY,
QOpenGLTexture::RGBA, QOpenGLTexture::UInt8,
(const void*)postop.constBits(), 0);
ourtexture->setData(0, 0, QOpenGLTexture::CubeMapNegativeY,
QOpenGLTexture::RGBA, QOpenGLTexture::UInt8,
(const void*)posbottom.constBits(), 0);
ourtexture->setData(0, 0, QOpenGLTexture::CubeMapPositiveZ,
QOpenGLTexture::RGBA, QOpenGLTexture::UInt8,
(const void*)posfront.constBits(), 0);
ourtexture->setData(0, 0, QOpenGLTexture::CubeMapNegativeZ,
QOpenGLTexture::RGBA, QOpenGLTexture::UInt8,
(const void*)posback.constBits(), 0);
ourtexture->setWrapMode(QOpenGLTexture::ClampToEdge);
ourtexture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
ourtexture->setMagnificationFilter(QOpenGLTexture::LinearMipMapLinear);
}
}
按照指定的顺序去添加对应的纹理即可;
在使用时和之前的普通纹理一样,绑定就可以了;
第一部分的源码见:
这次我简单对源码进行了整理,删掉了部分重复代码;比之前干净了一些,还发现了之前存在的内存泄漏问题,(*/ω\*);
教程的后半部分主要是讲在立方体贴图(例子中是一个天空盒),讲物体如何从天空盒纹理中实现折射和反射效果;具体教程已经讲的很详细,代码也主要是变动了着色器部分,就不展开说了;
源码:
来源:oschina
链接:https://my.oschina.net/u/2009228/blog/4922402