4

I'm working on software that contains a Qt3D view. This 3D view allow us to visualize elements. All the rendering part of the object is done in QML with custom materials/shaders.

I am able to create a material that passes a texture to a shader for texturing. The QML object holding the texture is Texture2D (its c++ implementation is QTexture2D )

My problem is that I don't find a way to dynamically change the texture content. In the software, the user can load any image from disc. I can properly create a QImage instance from this image.

So the question is: I have a QImage instance in c++ and I want to convert it to a QTexture2D instance so that I can pass it to the QML side.

How do I do that ?

I already looked into the QAbstractTexture and QAbstractTextureImage classes (and their children) but can't find a way to create any of these from a QImage

Basile Perrenoud
  • 4,039
  • 3
  • 29
  • 52

1 Answers1

3

Well, after a long time, here is the solution I used:

Only store a QString containing the path to the texture in c++ and create all the Texture object in QML. The QML looks like this:

MyDynamicTextureMaterial { // Custom material passing a Texture2D to the shader
    id: myMaterial

    texture: Texture2D {
        id: myTexture
        minificationFilter: Texture.Linear
        magnificationFilter: Texture.Linear
        wrapMode {
            x: WrapMode.Repeat
            y: WrapMode.Repeat
        }
        maximumAnisotropy: 16.0
        TextureImage {
            id: textureImage
            layer: 0
            mipLevel: 0
            source: cppObjectReference.texturePath ? cppObjectReference.texturePath : ""
        }
    }
}

cppObjectReference is a reference to a cpp object I created. This object simply needs a property of type QString with Read, Write and Notify options

Basile Perrenoud
  • 4,039
  • 3
  • 29
  • 52
  • What is the type of `cppObjectReference.texturePath`? A string to a file? Did you manage to use a `QImage` as source for the TextureImage? – marsl Apr 12 '19 at 15:12
  • That was more than a year ago, but I guess everything is in the answer here: texturePath is a QString containing the path to an image file. I didn't use a QImage – Basile Perrenoud Apr 13 '19 at 07:39