为什么qopenglshaderprogram在qt中显示镜像

时间:2016-03-14 14:26:10

标签: c++ qt opengl opengl-es-2.0 qtquick2

我使用QQuickFramebufferObject在qml中使用opengl渲染视频,但输出我所看到的是原始镜像。

可能是错的。

代码:

QOpenGLShaderProgram        *m_Program;
glGenTextures(3, m_Texture);

    m_Program = new QOpenGLShaderProgram();
    m_Program->addShaderFromSourceCode(QOpenGLShader::Vertex,
                                       "attribute vec4 a_position;"
                                       "attribute vec2 a_texcoord;"
                                       "varying vec2 v_texcoord;"
                                       "void main()"
                                       "{"
                                       "    gl_Position = a_position;"
                                       "    v_texcoord = a_texcoord;"
                                       "}"
                                       );
    m_Program->addShaderFromSourceCode(QOpenGLShader::Fragment,
                                       "varying vec2 v_texcoord;"
                                       "uniform sampler2D u_Texture0;"
                                       "uniform sampler2D u_Texture1;"
                                       "uniform sampler2D u_Texture2;"
                                       "uniform mat4 u_colorMatrix;"
                                       "void main()"
                                       "{"
                                       "    gl_FragColor = u_colorMatrix * vec4("
                                       "                                texture2D(u_Texture0, v_texcoord).s,"
                                       "                                texture2D(u_Texture1, v_texcoord).t,"
                                       "                                texture2D(u_Texture2, v_texcoord).q,"
                                       "                                1.0);"
                                       "}"
                                       );
    m_Program->bindAttributeLocation("a_position", 0);
    m_Program->bindAttributeLocation("a_texcoord", 1);

    m_Program->link();

static double prevPos = 0.0;
    glDisable(GL_DEPTH_TEST);

    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    if(m_PlaneWidth == -1)
    {
        _clearView();
        return;
    }

    m_Program->bind();

    m_Program->enableAttributeArray(0);
    m_Program->enableAttributeArray(1);

    setPlayerPosition();

    m_pw=m_ph=1.0;
    float position[] =
    {
        m_pw, m_ph,
        m_pw,  -m_ph,
        -m_pw,  -m_ph,
        -m_pw, m_ph
    };

    float w =1.0-(m_PlaneWidth-float(m_ptrMediator->getWidth()))/m_PlaneWidth;
    float coord[] =
    {
        0.0, 1.0,
        0.0, 0.0,
        w, 0.0,
        w, 1.0
    };

    m_Program->setAttributeArray(0, GL_FLOAT, position, 2);
    m_Program->setAttributeArray(1, GL_FLOAT, coord, 2);

    const qreal zNear = 1.0, zFar = 4.0, fov = 45.0;
    const qreal aspect = m_ptrMediator->getWidth()/m_ptrMediator->getHeight();
    m_Projection.setToIdentity();
    m_Projection.perspective(fov, aspect, zNear, zFar);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE);

    refresh(&pFrame);

    m_Program->disableAttributeArray(0);
    m_Program->release();
glActiveTextureARB(GL_TEXTURE2_ARB);
glBindTexture(GL_TEXTURE_2D, m_Texture[2]);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, pFrame->width,
                pFrame->height, GL_LUMINANCE,
                GL_UNSIGNED_BYTE, pFrame->data[0]);

glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D, m_Texture[1]);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, pFrame->width/2,
                pFrame->height/2, GL_LUMINANCE,
                GL_UNSIGNED_BYTE, pFrame->data[1]);

glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D, m_Texture[0]);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, pFrame->width/2,
                pFrame->height/2, GL_ALPHA,
                GL_UNSIGNED_BYTE, pFrame->data[2]);


m_Program->setUniformValue("u_Texture0", 2/*m_Texture[2]-1*/);
m_Program->setUniformValue("u_Texture1", 1/*m_Texture[1]-1*/);
m_Program->setUniformValue("u_Texture2", 0/*m_Texture[0]-1*/);
m_Program->setUniformValue("u_colorMatrix", m_Conv);

glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

如果我改变位置,我可以看到一些变化,但我仍然没有得到实际数据。

有人可以帮助我摆脱这个

1 个答案:

答案 0 :(得分:0)

我刚刚修改了它开始工作的位置和纹理坐标。

m_pw=m_ph=1.0;
float position[] =
{
    -m_pw, -m_ph,
    m_pw,  -m_ph,
    m_pw,  m_ph,
    -m_pw, m_ph
};

float w =1.0-(m_PlaneWidth-float(m_ptrMediator->getWidth()))/m_PlaneWidth;
float coord[] =
{
    0.0, 0.0,
    w, 0.0,
    w, 1.0,
    0.0, 1.0
};