Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用QOpenGLTexture构造立方体贴图纹理?_Opengl_Glsl_Qt5_Skybox_Qtopengl - Fatal编程技术网

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

如何使用QOpenGLTexture构造立方体贴图纹理?,opengl,glsl,qt5,skybox,qtopengl,Opengl,Glsl,Qt5,Skybox,Qtopengl,我想使用QOpenGLTexture构建一个立方体贴图纹理,使用6幅图像。我想我把它们放对了 我正在使用下面的代码来构造cubemap const QImage posx = QImage(":/images/posx.jpg").mirrored().convertToFormat(QImage::Format_RGBA8888); const QImage posy = QImage(":/images/posy.jpg").mirrored().convertToFormat(QImage

我想使用QOpenGLTexture构建一个立方体贴图纹理,使用6幅图像。我想我把它们放对了

我正在使用下面的代码来构造cubemap

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);
}
顶点着色器片段如下所示

.....
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);
}
片段着色器片段如下所示

.....
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);
}
我还有另一部分代码,它在skybox上呈现立方体贴图。虽然我能够在skybox上投影6个图像并正确渲染,但我无法在场景中渲染圆环体对象上的反射


有人能帮忙吗?

编辑:这个问题现在已经解决了


在将其应用于圆环体之前,我必须在环境纹理上调用generateMipMap()。

指出您自己找到答案的正确方法是将答案作为答案发布。也就是说,使用底部的Post应答框。回答你自己的问题没关系。@Nicolas:好的。谢谢你指出。这是我的第一个问题,我会从中变得更聪明:-)