Search code examples
qtqt3d

How can I load a QPaintedTextureImage into a QTextureMaterial?


I am using Qt3D to create a 360 deg panorama viewer where the image in equirectangular format is loaded over the mesh of a sphere with negative radius. The problem is that I need to load the texture from memory, instead of a file.

In order to achieve that, I developed a custom QPaintedTextureImage with paint() overloaded to draw from a QImage. It works, but only when plugged into a QDiffuseMapMaterial. Since I don't want any light effect (just the original color of the pixels) it seems that QTextureMaterial would be the right choice, but I don't know how to do that.

Any idea?


Solution

  • Got it!

    class MyQPaintedTextureImage : public Qt3DRender::QPaintedTextureImage
    {
    private:
        QImage image;
    public:
        void setImage(QImage &i){
            image = i;
            setSize(i.size());
        }
        virtual void paint(QPainter *painter) override{
            painter->drawImage(0, 0, image);
        }
    };
    

    And then:

    auto *image = new MyQPaintedTextureImage;
    image->setImage(i);
    auto *planeMaterial = new Qt3DExtras::QTextureMaterial;
    planeMaterial->texture()->addTextureImage(image);
    m_sphereEntity->addComponent(planeMaterial);