QSphereMesh in Qt 3D: how can I correctly add a te

2019-08-02 21:07发布

I want to draw a semi-transparent textured sphere in Qt 3D (to plot some kind of colormap on it).

I'm trying to draw a sphere and add a texture. The texture fits good on some regions of the sphere, but on the poligons near its poles it becomes sizzled.

Can you tell me, what's my mistake?

Here's my code that tries to draw a sphere:

#include <QApplication>

#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DCore/QEntity>
#include <Qt3DExtras/QSphereMesh>
#include <Qt3DExtras/QTextureMaterial>
#include <Qt3DExtras/QOrbitCameraController>
#include <Qt3DRender/QTexture>
#include <QPhongAlphaMaterial>
#include <qt3dwindow.h>
#include <QtWidgets/QWidget>

#include <qcamera.h>
#include <qforwardrenderer.h>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Qt3DExtras::Qt3DWindow* m_view = new Qt3DExtras::Qt3DWindow();
    QWidget* container = QWidget::createWindowContainer(m_view);
    container->show();

    Qt3DCore::QEntity* m_rootEntity = new Qt3DCore::QEntity();
    m_view->setRootEntity(m_rootEntity);
    m_view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x4d4d4f)));

    // let's make a sphere
    Qt3DCore::QEntity* m_sphereEntity = new Qt3DCore::QEntity(m_rootEntity);
    Qt3DExtras::QSphereMesh* m_sphereMesh = new Qt3DExtras::QSphereMesh();
    m_sphereMesh->setRadius(12.);
    m_sphereEntity->addComponent(m_sphereMesh);

    // and glue some texture onto our sphere
    Qt3DRender::QTextureLoader *loader = new Qt3DRender::QTextureLoader(m_sphereEntity);
    Qt3DExtras::QTextureMaterial *material = new Qt3DExtras::QTextureMaterial(m_sphereEntity);
    loader->setSource(QUrl::fromLocalFile(a.applicationDirPath() + QStringLiteral("/temp1.png")));
    material->setTexture(loader);
    m_sphereEntity->addComponent(material);


    // initialise camera

    Qt3DRender::QCamera *camera = m_view->camera();
    camera->lens()->setPerspectiveProjection(45.0, 16.0 / 9.0, 0.1, 1000.0);
    camera->setPosition(QVector3D(0.0, 0.0, 40.0));
    camera->setViewCenter(QVector3D(0.0, 0.0, 0.0));

    // For camera controls.
    Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(m_rootEntity);
    camController->setLinearSpeed(50.0);
    camController->setLookSpeed(180.0);
    camController->setCamera(camera);


    return a.exec();
}

Here's the temp1.png (the texture of colormap):

enter image description here

And here's the screenshot of my badly textured sphere: enter image description here

P.S. Also, can you tell me, is there a way to add an alpha channel to my textured material of Qt 3D? It can be extremely useful for me.

标签: qt qt3d
0条回答
登录 后发表回答