Solved QDiffuseSpecularMaterial diffuse texture
I would like to use a texture for the diffuse component of a QDiffuseSpecularMaterial. In the documentation I find for the diffuse property:
"Holds the diffuse color of the material that is emitted for rough surface reflections with the lights. This can be either a plain color value or a texture."
but how do I give it a texture (loaded from a png on disk)? It expects a QVariant...
Just found this issue here. I am also having the same problem. QVariant is not happy with QTexture2D.
I am Using 5.12.3 at the moment, but maybe I'd better check 5.13.
@kippa Same on 5.13.0. I must be missing something.
@kippa After a lot of trying, I succeeded by doing the following
Qt3DExtras::QDiffuseSpecularMaterial* material = new Qt3DExtras::QDiffuseSpecularMaterial(_renderData); Qt3DRender::QTexture2D* tex = new Qt3DRender::QTexture2D(_renderData); SVGTexture* texIm = new SVGTexture(texture, static_cast<int>(width), static_cast<int>(height), tex); //Subclass of QPaintedTextureImage with SvgRenderer to use an svg as texture tex->addTextureImage(texIm); texIm->update(); material->setDiffuse(QVariant::fromValue(qobject_cast<QObject*>(tex)));
Hope this helps.
JanWillemsen Thanks. Of course it's the cast. Works like a charm. Thanks for getting me out of my mental quicksand. Perhaps QVariant will become qabstracttexture aware someday.
I eventually found this to work for me
texture = new Qt3DRender::QTexture2D(); material = new Qt3DExtras::QDiffuseSpecularMaterial(); material->setAlphaBlendingEnabled(true); material->setAmbient(QColor(0xff,0xff,0xff,0x00)); // SymbolImage is my subclass of qabstracttexture img = new SymbolImage(name, material, blah, blah, blah, blah, blah); texture->addTextureImage(img); // the two textures are identical for my work material->setDiffuse(QVariant::fromValue(texture)); material->setSpecular(QVariant::fromValue(texture));
So just QVariant::fromValue().
Then later upon a callback:
/// assume diffuse is same as specular, or use specular QTexture2D *t = material->diffuse().value<QTexture2D*>(); SymbolImage *s = static_cast<SymbolImage*>(t->textureImages().first());
That's what I ended up using.