QOpenglWidget can not show texture when use glm::ortho,but it work well when I use glut



  • glm::ortho can not work well in QOpenGlWidget, it only display the glclearcolor.
    0_1501658036713_unnormal.jpg
    but it works well in glut created windows.
    ![0_1501658059389_normal.jpg](Uploading 100%)
    I don't know why,I have spent 3days on this problem.
    below is the core source.

    const GLfloat vertexVertices[] =
    {
    -1.0f, -1.0f,
    1.0f, -1.0f,
    -1.0f, 1.0f,
    1.0f, 1.0f
    };

    const GLfloat textureVertices[] =
    {
    0.0f, 1.0f,
    1.0f, 1.0f,
    0.0f, 0.0f,
    1.0f, 0.0f,
    };

    HozoHorizontalDrawer::HozoHorizontalDrawer() :m_fOrthLeftRatio(0), m_fOrthRightRatio(1.0), m_fOrthBottomRatio(0), m_fOrthTopRatio(1.0)
    {
    }

    HozoHorizontalDrawer::~HozoHorizontalDrawer()
    {
    glDisable(GL_TEXTURE_2D);
    glDeleteBuffers(2, m_nBufferArray);
    }

    void HozoHorizontalDrawer::InitOrthRatio()
    {
    m_fOrthLeftRatio = 0;
    m_fOrthRightRatio = 1.0;
    m_fOrthBottomRatio = 0;
    m_fOrthTopRatio = 1.0;
    }

    void HozoHorizontalDrawer::InitDrawer()
    {
    InitShader();
    GenTextureArray();
    glUniform1i(m_iYTextureUniform, 0);
    SetTextureEnvInitFlag(true);

    glUseProgram(m_nShaderProgram);
    glGenBuffers(2, m_nBufferArray);
    glBindBuffer(GL_ARRAY_BUFFER, m_nBufferArray[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexVertices), vertexVertices, GL_STATIC_DRAW);
    glVertexAttribPointer(eVertex, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
    glEnableVertexAttribArray(eVertex);
    
    glBindBuffer(GL_ARRAY_BUFFER, m_nBufferArray[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(textureVertices), textureVertices, GL_STATIC_DRAW);
    glVertexAttribPointer(eTexture, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
    glEnableVertexAttribArray(eTexture);
    

    }

    void HozoHorizontalDrawer::Draw(float PlayerControlWidth, float PlayerControlHeight, tagTextureDescription panoramaFrame)
    {
    glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    glClearDepth(1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glShadeModel(GL_SMOOTH);

    glUseProgram(m_nShaderProgram);
    
    //正交投影
    m_mat4View = glm::lookAt(glm::vec3(0, 0, -1), glm::vec3(0, 0, 0), glm::vec3(0, -1, 0));
    m_mat4Projection = glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f);
    m_mat4Model = glm::mat4(1.0f);
    m_mat4Model = glm::translate(m_mat4Model, glm::vec3(m_fXTranslate, m_fYTranslate, m_fZTranslate));
    m_mat4Model = glm::rotate(m_mat4Model, glm::radians(m_fXRotateDegree), glm::vec3(1.0f, 0, 0));
    m_mat4Model = glm::rotate(m_mat4Model, glm::radians(m_fYRotateDegree), glm::vec3(0, 1.0f, 0));
    m_mat4Model = glm::rotate(m_mat4Model, glm::radians(m_fZRotateDegree), glm::vec3(0, 0, 1.0f));
    m_mat4Model = glm::scale(m_mat4Model, glm::vec3(m_fXScale, m_fYScale, m_fZScale));
    m_mat4ProjectionModelView = m_mat4Projection * m_mat4View * m_mat4Model;
    glUniformMatrix4fv(m_iProjectionModelViewIDUniform, 1, GL_FALSE, &m_mat4ProjectionModelView[0][0]);
    glEnable(GL_TEXTURE_2D);
    SetTexturesImages(panoramaFrame);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);	
    glFlush();
    
    glUseProgram(0);
    glBindTexture(GL_TEXTURE_2D, 0);
    glDisable(GL_TEXTURE_2D);
    

    }

    QOpenglwidget source code is below.
    void QMyOpenGLWidget::initializeGL()
    {
    QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
    f->initializeOpenGLFunctions();
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    }

    void QMyOpenGLWidget::resizeGL(int w, int h)
    {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    }

    void QMyOpenGLWidget::paintGL()
    {
    makeCurrent();
    if (nullptr != m_Picture.pTextureData[0] && nullptr != m_pDrawer)
    {
    this->m_pDrawer->Draw(this->width(), this->height(), m_Picture);
    }
    }

    void QMyOpenGLWidget::drawPicturepaintGL()
    {
    this->update();
    return;
    }

    below is shader source.
    #version 330 core
    uniform mat4 ProjectionModelView;
    attribute vec4 vertexCoord;
    attribute vec2 textureCoord;
    varying vec2 outTextureCoord;

    void main(void){
    gl_Position = ProjectionModelView * vertexCoord;
    outTextureCoord = textureCoord;
    }

    varying vec2 outTextureCoord;
    uniform sampler2D textureY;

    void main(void)
    {
    vec3 bgr;
    bgr = texture2D(textureY, outTextureCoord).rgb;
    gl_FragColor = vec4(bgr, 1.0);
    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.