简体   繁体   English

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

[英]OpenGL application looks weird after porting to Qt5

I recently ported my engine from QGLWidget to QWindow . 我最近将我的引擎从QGLWidget移植到QWindow Everything works nice, except texturing. 一切都很好,除了纹理。 They appear with a lot of holes, dark and really weird. 它们出现了很多洞,黑暗而且非常奇怪。

Example to show what I mean: (Broken build (QWindow)) 显示我的意思的例子:(破碎的构建(QWindow)) 破碎的构造(QWindow)

Second example: (Working build QGLWidget): 第二个例子:(工作构建QGLWidget): 正在构建QGLWidget

To set up the texture I used QOpenGLTexture . 要设置纹理,我使用了QOpenGLTexture It shows warning that glTextureView can't be resolved, but still works. 它显示警告glTextureView无法解析,但仍然有效。 In QGLWidget version I used QGLWidget::convertToGLFormat , and some raw OpenGL calls. 在QGLWidget版本中,我使用了QGLWidget::convertToGLFormat ,以及一些原始的OpenGL调用。

Code for QGLWidget version: 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;

Code for QWindow version: 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;

Any ideas what could have caused this? 可能导致这种情况的任何想法?

I figured this out!!! 我想出来了!!! Using QGLWidget::convertToGLFormat flipped the texure over Y axis for me. 使用QGLWidget::convertToGLFormat为我翻转了Y轴上的纹理。 QOpenGLTexture doesn't do that. QOpenGLTexture不会这样做。 So a simple line in the shader fixed this. 因此着色器中的一条简单线条修复了这个问题。

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);
}

Or I can just use img = img.mirrored(); 或者我可以使用img = img.mirrored(); to do that in C++. 用C ++做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM