Render QImage with OpenGL

前端 未结 2 1983
[愿得一人]
[愿得一人] 2021-02-06 07:54

Related to my other question, I think the more central question would be, how you render a QImage with OpenGL?

I think the code has to look something like t

2条回答
  •  走了就别回头了
    2021-02-06 08:23

    I managed to draw a QImage with OpenGL from a picture source, but not from the SoOffscreenRenderer as described here.

    void init(){
        loadTexture2("kitten.png", backgroundimage);
    }
    
    QImage loadTexture2(char *filename, GLuint &textureID){
        glEnable(GL_TEXTURE_2D); // Enable texturing
    
        glGenTextures(1, &textureID); // Obtain an id for the texture
        glBindTexture(GL_TEXTURE_2D, textureID); // Set as the current texture
    
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    
        QImage im(filename);
        QImage tex = QGLWidget::convertToGLFormat(im);
    
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width(), tex.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());
    
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    
        glDisable(GL_TEXTURE_2D);
    
        return tex;
    }
    
    void renderSceneGL2(){
        glClearColor(0.4f, 0.1f, 0.1f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, backgroundimage);
    
        glBegin(GL_QUADS);
            glTexCoord2f(0,0); glVertex3f(-1, -1, -1);
            glTexCoord2f(1,0); glVertex3f(1, -1, -1);
            glTexCoord2f(1,1); glVertex3f(1, 1, -1);
            glTexCoord2f(0,1); glVertex3f(-1, 1, -1);
    
        glEnd();
    
        glDisable(GL_TEXTURE_2D);
    }
    

    Also QGLWidget::convertToGLFormat throws random Access violation errors from time to time.

提交回复
热议问题