Qt 3D QTextureImage cannot apply texture from png image
-
Hi all,
I am trying to apply a texture to a Sphere Mesh, but I'm getting a dark scene and the folowing error message:
No QTextureImageData generated from functor yet, texture will be invalid for this frame
I could not find any demos or documentation related to this topic, so maybe I'm doing it completely wrong, but so far I'm stuck and have no ide how to fix this.
The code is this:
// Sphere shape data Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh(); sphereMesh->setRings(32); sphereMesh->setSlices(32); sphereMesh->setRadius(4); // Sphere mesh transform Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform(); sphereTransform->setScale(1.0f); sphereTransform->setTranslation(QVector3D(0.0f, 0.0f, 0.0f)); sphereTransform->setRotation(QQuaternion(1.0f, 0.0f, 0.0f, 0.0f)); Qt3DRender::QTextureImage *diffuseTextureImage = new Qt3DRender::QTextureImage(); diffuseTextureImage->setMirrored(false); diffuseTextureImage->setSource(QUrl(QStringLiteral("qrc:/textrue.png"))); Qt3DExtras::QDiffuseSpecularMapMaterial *diffuseSpecularMaterial = new Qt3DExtras::QDiffuseSpecularMapMaterial(); diffuseSpecularMaterial->diffuse()->addTextureImage(diffuseTextureImage); // Sphere auto m_sphereEntity = new Qt3DCore::QEntity(m_rootEntity); m_sphereEntity->addComponent(sphereMesh); m_sphereEntity->addComponent(diffuseSpecularMaterial); m_sphereEntity->addComponent(sphereTransform); return m_rootEntity;
-
@blehotai85 said in Qt 3D QTextureImage cannot apply texture from png image:
QStringLiteral("qrc:/textrue.png")));
Hi! Maybe it's just a typo, "textrue".
-
Thank you, that was indeed a typo, but there is still no texture. The problem is that I couldn't find any tutorial or description about the usage of this, so I'm in the dark now. If anyone links some examples (not those officials in QML), I would really appreciate it.
-
Here's some code from my own Qt3D experiments. Can't test it right now but I think the following worked:
texturedplaneentity.h
#ifndef TEXTUREDPLANEENTITY_H #define TEXTUREDPLANEENTITY_H #include <QObject> #include <QSize> #include <QUrl> #include <Qt3DCore/QEntity> class TexturedPlaneEntity : public Qt3DCore::QEntity { Q_OBJECT public: explicit TexturedPlaneEntity(QSize size, QUrl const &textureSource, bool mirrored, Qt3DCore::QNode *parent = nullptr); }; #endif // TEXTUREDPLANEENTITY_H
texturedplaneentity.cpp
#include "texturedplaneentity.h" #include <Qt3DExtras/QPlaneMesh> #include <Qt3DExtras/QNormalDiffuseSpecularMapMaterial> #include <Qt3DRender/QTextureImage> #include <Qt3DCore/QTransform> TexturedPlaneEntity::TexturedPlaneEntity(QSize size, const QUrl &textureSource, bool mirrored, Qt3DCore::QNode *parent) : QEntity(parent) { auto mesh = new Qt3DExtras::QPlaneMesh(); mesh->setWidth(size.width()); mesh->setHeight(size.height()); auto material = new Qt3DExtras::QNormalDiffuseSpecularMapMaterial(); auto textImg1 = new Qt3DRender::QTextureImage(); auto textImg2 = new Qt3DRender::QTextureImage(); auto textImg3 = new Qt3DRender::QTextureImage(); textImg1->setSource(textureSource); textImg2->setSource(textureSource); textImg3->setSource(textureSource); textImg1->setMirrored(mirrored); textImg2->setMirrored(mirrored); textImg3->setMirrored(mirrored); material->diffuse()->addTextureImage(textImg1); material->normal()->addTextureImage(textImg2); material->specular()->addTextureImage(textImg3); material->setShininess(1.0); material->setAmbient("white"); auto transform = new Qt3DCore::QTransform(); // ... addComponent(mesh); addComponent(material); addComponent(transform); }
-
Here's some code from my own Qt3D experiments. Can't test it right now but I think the following worked:
texturedplaneentity.h
#ifndef TEXTUREDPLANEENTITY_H #define TEXTUREDPLANEENTITY_H #include <QObject> #include <QSize> #include <QUrl> #include <Qt3DCore/QEntity> class TexturedPlaneEntity : public Qt3DCore::QEntity { Q_OBJECT public: explicit TexturedPlaneEntity(QSize size, QUrl const &textureSource, bool mirrored, Qt3DCore::QNode *parent = nullptr); }; #endif // TEXTUREDPLANEENTITY_H
texturedplaneentity.cpp
#include "texturedplaneentity.h" #include <Qt3DExtras/QPlaneMesh> #include <Qt3DExtras/QNormalDiffuseSpecularMapMaterial> #include <Qt3DRender/QTextureImage> #include <Qt3DCore/QTransform> TexturedPlaneEntity::TexturedPlaneEntity(QSize size, const QUrl &textureSource, bool mirrored, Qt3DCore::QNode *parent) : QEntity(parent) { auto mesh = new Qt3DExtras::QPlaneMesh(); mesh->setWidth(size.width()); mesh->setHeight(size.height()); auto material = new Qt3DExtras::QNormalDiffuseSpecularMapMaterial(); auto textImg1 = new Qt3DRender::QTextureImage(); auto textImg2 = new Qt3DRender::QTextureImage(); auto textImg3 = new Qt3DRender::QTextureImage(); textImg1->setSource(textureSource); textImg2->setSource(textureSource); textImg3->setSource(textureSource); textImg1->setMirrored(mirrored); textImg2->setMirrored(mirrored); textImg3->setMirrored(mirrored); material->diffuse()->addTextureImage(textImg1); material->normal()->addTextureImage(textImg2); material->specular()->addTextureImage(textImg3); material->setShininess(1.0); material->setAmbient("white"); auto transform = new Qt3DCore::QTransform(); // ... addComponent(mesh); addComponent(material); addComponent(transform); }
@Wieland Really appreciate it. I will try out later today. Thank you!
-
Just to close this topic, I found that the code posted here seemed to be okay. I tried it again just recently with Qt 5.9.4 and a notebook with a GPU capable of OpenGL 4.5 and it ran smoothly. I think my old PC with intel GMA / driver was the problem. Thanks everyone for the comments.