QDiffuseSpecularMaterial diffuse texture
Solved
General and Desktop
-
Hi,
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...
Jan
-
@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.
Jan
-
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.