问题描述
我正在开发包含Qt3D视图的软件.此3D视图使我们能够可视化元素.对象的所有渲染部分都是使用自定义材质/着色器在QML中完成的.
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.
我能够创建将纹理传递到着色器进行纹理化的材质.包含纹理的QML对象是Texture2D(其c ++实现是 QTexture2D )
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 )
我的问题是我找不到动态改变纹理内容的方法.在该软件中,用户可以从光盘加载任何图像.我可以从该图像正确创建一个QImage实例.
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.
所以问题是:我在C ++中有一个QImage实例,我想将其转换为QTexture2D实例,以便可以将其传递给QML端.
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.
我该怎么做?
我已经研究过QAbstractTexture和QAbstractTextureImage类(及其子类),但是找不到从QImage创建任何此类的方法
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
推荐答案
好久了,这是我使用的解决方案:
Well, after a long time, here is the solution I used:
仅在c ++中存储包含纹理路径的QString,并在QML中创建所有Texture对象. QML看起来像这样:
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是对我创建的cpp对象的引用.该对象仅需要QString类型的属性,该属性具有读取",写入"和通知"选项
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
这篇关于Qt3D动态纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!