移植到Qt5后,OpenGL应用程序看起来很奇怪

Naz*_*554 3 c++ opengl qt qglwidget qt5

我最近将我的引擎移植QGLWidget到了QWindow.一切都很好,除了纹理.它们出现了很多洞,黑暗而且非常奇怪.

显示我的意思的例子:(破碎的构建(QWindow)) 破碎的构造(QWindow)

第二个例子:(工作构建QGLWidget): 正在构建QGLWidget

设置我使用的纹理QOpenGLTexture.它显示glTextureView无法解决的警告,但仍然有效.在我使用的QGLWidget版本QGLWidget::convertToGLFormat和一些原始的OpenGL调用中.

QGLWidget版本代码:

if(!mProgram.isLinked())
{
    qCritical() << tr("Error: the shader program is not linked, object name: %1").arg(mName);
    return false;
}

if(mTextureID != 0)
{
    qCritical() << tr("Error: texture already set, object name: %1").arg(mName);
    return false;
}

QImage tex;
bool loadok = tex.load(path);
if(!loadok)
{
    qCritical() << tr("Error: failed to load the image, object name: %1").arg(mName);
    return false;
}

tex = QGLWidget::convertToGLFormat(tex);



glGenTextures(1, &mTextureID);
if(mTextureID == 0)
{
    qCritical() << tr("Error: failed to generate the texture, object name: %1").arg(mName);
    return false;
}
mVAO.bind();

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTextureID);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width(), tex.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.constBits());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

mProgram.setUniformValue("objectTexture", 0);

mVAO.release();

return true;
Run Code Online (Sandbox Code Playgroud)

QWindow版本代码:

if(!mProgram.isLinked())
{
    qCritical() << tr("Error: the shader program is not linked, object name: %1").arg(mName);
    return false;
}

QImage img;
bool ok = img.load(path);
if(!ok)
{
    qCritical() << tr("Error: failed to load the image, object name: %1").arg(mName);
    return ok;
}

ok = mTexture.create();

qDebug() << img.size();

if(!ok)
{
    qCritical() << tr("Error: create the texture, object name: %1").arg(mName);
    return ok;
}


mTexture.setFormat(QOpenGLTexture::RGBA8_UNorm);
mTexture.setData(img);

QOpenGLVertexArrayObject::Binder vaoBinder(&mVAO);
glActiveTexture(GL_TEXTURE0);
mTexture.bind();

mProgram.setUniformValue("objectTexture", 0);


return ok;
Run Code Online (Sandbox Code Playgroud)

可能导致这种情况的任何想法?

Naz*_*554 5

我想出来了!!! 使用QGLWidget::convertToGLFormat在Y轴上翻转纹理对我来说.QOpenGLTexture不这样做.因此着色器中的一条简单线条修复了这个问题.

varying highp vec2 outUV;
uniform sampler2D objectTexture;
void main()
{
    vec2 flipped_texcoord = vec2(outUV.x, 1.0 - outUV.y);
    gl_FragColor = texture2D(objectTexture, flipped_texcoord);
}
Run Code Online (Sandbox Code Playgroud)

或者我可以img = img.mirrored();在C++中使用它.