Qt3D dynamic texture

2019-05-26 10:32发布

问题:

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

回答1:

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



标签: c++ qt qml qt3d