我最近将引擎从QGLWidget
移植到QWindow
。一切都很好,除了纹理。它们出现了很多洞,黑暗而且非常奇怪。
显示我的意思的示例: (破碎版(QWindow))
第二个例子:(工作构建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;
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;
任何可能导致这种情况的想法?
答案 0 :(得分: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);
}
或者我可以使用img = img.mirrored();
在C ++中执行此操作。