Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Render transparent 2D image on mesh Qt3D

Render transparent 2D image on mesh Qt3D

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 339 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PVRT
    wrote on last edited by
    #1

    I am new to Qt3D and I am trying to figure out a way to render a 2D image on top of a mesh and also control the transparency of the 2D image.

    I googled for it but could not find any solution. Looking at some of the examples provided with Qt i was able to display a 2D image on top as mesh by adding a plane (with image texture) above it. Is this the best way to do it? Are there any alternate ways?

    #include <QtGui/QGuiApplication>
    
    #include <Qt3DCore/QEntity>
    #include <Qt3DCore/QTransform>
    
    #include <Qt3DExtras/Qt3DWindow>
    #include <Qt3DExtras/QPhongMaterial>
    #include <Qt3DExtras/QSphereMesh>
    #include <Qt3DExtras/QTorusMesh>
    #include <Qt3DExtras/QOrbitCameraController>
    #include <Qt3DExtras/QDiffuseSpecularMaterial>
    #include <Qt3DExtras/QPlaneMesh>
    #include <Qt3DExtras/QFirstPersonCameraController>
    
    #include <Qt3DRender/QCamera>
    #include <Qt3DRender/QTexture>
    #include <Qt3DRender/QTextureImage>
    
    Qt3DCore::QEntity* createScene()
    {
        auto rootEntity = new Qt3DCore::QEntity;
    
        auto material = new Qt3DExtras::QPhongMaterial(rootEntity);
        material->setDiffuse(QColor(QRgb(0x928327)));
        material->setAmbient(QColor(QRgb(0x928327)));
    
        Qt3DCore::QEntity *sphereEntity = new Qt3DCore::QEntity(rootEntity);
        Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh;
        sphereMesh->setRadius(3);
        sphereMesh->setGenerateTangents(true);
    
        Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform;
        sphereEntity->addComponent(sphereMesh);
        sphereEntity->addComponent(sphereTransform);
        sphereEntity->addComponent(material);
    
        // Plane shape data
        Qt3DExtras::QPlaneMesh *planeMesh = new Qt3DExtras::QPlaneMesh();
        planeMesh->setWidth(2);
        planeMesh->setHeight(2);
    
        Qt3DCore::QTransform *planeTransform = new Qt3DCore::QTransform();
        planeTransform->setScale(0.5f);
        planeTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1.0f, 0.0f, 0.0f), 90));
        planeTransform->setTranslation(QVector3D(0.0f, 0.0f, 10.0f));
    
        auto textureImage = new Qt3DRender::QTextureImage;
        textureImage->setSource(QUrl(QStringLiteral("qrc:/new/prefix1/img")));
        auto planeImage = new Qt3DRender::QTexture2D();
        planeImage->addTextureImage(textureImage);
        auto planeMaterial = new Qt3DExtras::QDiffuseSpecularMaterial();
        planeMaterial->setDiffuse(QVariant::fromValue(planeImage));
        planeMaterial->setNormal(QVariant::fromValue(planeImage));
        planeMaterial->setSpecular(QVariant::fromValue(planeImage));
    
        planeMaterial->setShininess(1.0f);
        // Plane
        {
            auto m_planeEntity = new Qt3DCore::QEntity(rootEntity);
            m_planeEntity->addComponent(planeMesh);
            m_planeEntity->addComponent(planeMaterial);
            m_planeEntity->addComponent(planeTransform);
        }
    
        return rootEntity;
    }
    
    int main(int argc, char** argv)
    {
        QGuiApplication a(argc, argv);
    
        Qt3DExtras::Qt3DWindow view;
    
        // Camera
        Qt3DRender::QCamera *camera = view.camera();
        camera->lens()->setPerspectiveProjection(45.0f, 16.0f / 9.0f, 0.1f, 1000.0f);
        camera->setPosition(QVector3D(0, 0, 40.0f));
        camera->setViewCenter(QVector3D(0, 0, 0));
    
        auto scene = createScene();
        // For camera controls
        auto camController = new Qt3DExtras::QFirstPersonCameraController(scene);
        camController->setCamera(camera);
    
    
        view.setRootEntity(scene);
        view.show();
    
        return a.exec();
    }
    

    Is there a way to control the transparency of mesh with image texture?

    Thank you in advance.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved