Velutis
Velutis

Reputation: 95

Why does a texture render correctly in Qt5 but not in Qt6 using QOpenGLWidget?

why Qt6 is not rendering texture which is ok on Qt5. Code compiles and runs without any warnings or errors, just instead texture white rectangle is rendered. I run this on Windows 9934e671-5fff-4d98-810d-1d64e060c872-image.png here is minimal example https://wetransfer.com/downloads/9ba8d348518621098f202e80e82d013320250213080224/f0c923?t_exp=1739692944&t_lsid=499ef440-c742-4b62-8bdd-6f9cca09f054&t_network=link&t_rid=Z29vZ2xlLW9hdXRoMnwxMTI0NDU3OTU3NDYxMTAwMzgwNTQ=&t_s=download_link&t_ts=1739433755 code is based on this example https://learnopengl.com/Getting-started/Textures Thanks! PS if you try project you should change line stbi_load("c:/temp/wall.jpg", ... to your path to image.

Here is main code that covers problem:

    void GLWidget::initializeGL()
    {
        initializeOpenGLFunctions();
        m_shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, R"(
    in vec3 aPos;
    in vec3 aColor;
    in vec2 aTexCoord;
    
    varying vec3 ourColor;
    varying vec2 TexCoord;
    
    void main()
    {
        gl_Position = vec4(aPos, 1.0);
        ourColor = aColor;
        TexCoord = aTexCoord;//vec2(aTexCoord.x, aTexCoord.y);
    }
                 )");
        m_shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment,R"(
     in vec3 ourColor;
     in vec2 TexCoord;
    
     // texture sampler
     uniform sampler2D texture1;
    
     void main()
     {
         vec4 colorfromtexture = texture2D(texture1, TexCoord);
         gl_FragColor  = colorfromtexture;
     }
    )");
        m_shaderProgram.link();
        m_shaderProgram.bind();
    
    
        unsigned int iVBO, iEBO;
        float vertices[] = {
            // positions          // colors           // texture coords
            0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f, // top right
            0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f, // bottom right
            -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f, // bottom left
            -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f  // top left
        };
        unsigned int indices[] = {
            0, 1, 3, // first triangle
            1, 2, 3  // second triangle
        };
    
        glGenBuffers(1, &iVBO);
        glGenBuffers(1, &iEBO);
    
        glBindBuffer(GL_ARRAY_BUFFER, iVBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iEBO);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    
        // position attribute
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
        glEnableVertexAttribArray(0);
        // color attribute
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
        glEnableVertexAttribArray(1);
        // texture coord attribute
        glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
        glEnableVertexAttribArray(2);
    
    
        // load and create a texture
        // -------------------------
        glGenTextures(1, &texture);
        glBindTexture(GL_TEXTURE_2D, texture); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
        // set the texture wrapping parameters
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);   // set texture wrapping to GL_REPEAT (default wrapping method)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        // set texture filtering parameters
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        // load image, create texture and generate mipmaps
        int width, height, nrChannels;
        // The FileSystem::getPath(...) is part of the GitHub repository so we can find files on any IDE/platform; replace it with your own image path.
        unsigned char *data = stbi_load("c:/temp/wall.jpg", &width, &height, &nrChannels, 0);
        if (data)
        {
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
            glGenerateMipmap(GL_TEXTURE_2D);
        }
        else
        {
            qDebug() << "Failed to load texture";
        }
        stbi_image_free(data);
    }

void GLWidget::paintGL()
{
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // bind Texture
    glBindTexture(GL_TEXTURE_2D, texture);

    //glBindVertexArray(VAO);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}

void GLWidget::resizeGL(int width, int height)
{
    glViewport(0, 0, width, height);
    repaint();
}

Upvotes: -2

Views: 84

Answers (1)

Velutis
Velutis

Reputation: 95

fix is following - add m_shaderProgram.bind() in paintGL:

    void GLWidget::paintGL()
{
    m_shaderProgram.bind();
...

Upvotes: -2

Related Questions