问题
I add a plane to the 3D world and it should be in torus's center.
But if I add the plane first and the tour second, the plane will be tour covered (plane's coordinate is in tour center):
I think it is about paint order. The plane should be drawn in last order.
Changing plane and torus adding order will be correct:
Can QDepthTest
help me? Or any other way?
Can show me some examples? Thanks!
code (without the sphere):
class FlippedTextureImage : public Qt3DRender::QPaintedTextureImage
{
public:
FlippedTextureImage(Qt3DCore::QNode *parent = Q_NULLPTR):Qt3DRender::QPaintedTextureImage(parent) {}
void paint(QPainter *painter) override {
// qDebug() << backgroundImage.isNull();
qDebug() << qApp->applicationDirPath() + "/gift.svg";
QSvgRenderer renderer(QString(qApp->applicationDirPath() + "/gift.svg"));
QImage image(3000, 3000, QImage::Format_RGBA8888); // 512x512 RGBA
image.fill(0x00ffffff); // white background
QPainter painter2(&image);
renderer.render(&painter2);
painter->setCompositionMode(QPainter::CompositionMode::CompositionMode_Source);
painter->drawImage(0, 0, image);
}
};
Qt3DCore::QEntity *createScene()
{
// Root entity
Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;
// Material
Qt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity);
// Plane
Qt3DCore::QEntity *planeEntity = new Qt3DCore::QEntity(rootEntity);
Qt3DExtras::QPlaneMesh *planeMesh = new Qt3DExtras::QPlaneMesh(planeEntity);
planeMesh->setHeight(2);
planeMesh->setWidth(2);
planeMaterial = new Qt3DExtras::QTextureMaterial(planeEntity);
Qt3DRender::QTexture2D *planeTexture = new Qt3DRender::QTexture2D(planeMaterial);
FlippedTextureImage *planeTextureImage = new FlippedTextureImage(planeTexture);
planeTextureImage->setSize(QSize(3000, 3000));
planeTexture->addTextureImage(planeTextureImage);
planeMaterial->setTexture(planeTexture);
planeMaterial->setAlphaBlendingEnabled(true);
planeTransformClass *planeTransform = new planeTransformClass(planeEntity);
//planeTransform->setRotationX(90);
planeTransform->setTranslation(QVector3D(2, 0, 0));
planeEntity->addComponent(planeMesh);
planeEntity->addComponent(planeMaterial);
planeEntity->addComponent(planeTransform);
// Torus
Qt3DCore::QEntity *torusEntity = new Qt3DCore::QEntity(rootEntity);
Qt3DExtras::QTorusMesh *torusMesh = new Qt3DExtras::QTorusMesh;
torusMesh->setRadius(5);
torusMesh->setMinorRadius(1);
torusMesh->setRings(100);
torusMesh->setSlices(20);
Qt3DCore::QTransform *torusTransform = new Qt3DCore::QTransform;
torusTransform->setScale3D(QVector3D(1.5, 1, 0.5));
torusTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1, 0, 0), 90.0f));
torusEntity->addComponent(torusMesh);
torusEntity->addComponent(torusTransform);
torusEntity->addComponent(material);
return rootEntity;
}
int main(int argc, char* argv[])
{
QGuiApplication app(argc, argv);
Qt3DExtras::Qt3DWindow view;
Qt3DCore::QEntity *scene = createScene();
// Camera
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));
// For camera controls
Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(scene);
camController->setLinearSpeed( 50.0f );
camController->setLookSpeed( 180.0f );
camController->setCamera(camera);
view.setRootEntity(scene);
view.show();
return app.exec();
}
This is the SVG I use:
来源:https://stackoverflow.com/questions/62957001/qt3d-transparency-texture-will-be-covered-how-to-slove-that