QT 3D无人机模型可旋转的QT类
时间: 2023-05-26 12:07:37 浏览: 561
以下是一个可以旋转的QT类,用于显示无人机模型:
```c++
#include <Qt3DCore/QEntity>
#include <Qt3DCore/QTransform>
#include <Qt3DExtras/QCylinderMesh>
#include <Qt3DExtras/QSphereMesh>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DRender/QMaterial>
#include <Qt3DRender/QPointLight>
class DroneEntity : public Qt3DCore::QEntity
{
public:
DroneEntity(Qt3DCore::QEntity* parent = nullptr)
: Qt3DCore::QEntity(parent)
{
// add drone body
Qt3DCore::QEntity *bodyEntity = new Qt3DCore::QEntity(this);
Qt3DExtras::QCylinderMesh* bodyCylinder = new Qt3DExtras::QCylinderMesh;
bodyCylinder->setRadius(0.2f);
bodyCylinder->setLength(0.5f);
bodyEntity->addComponent(bodyCylinder);
// add drone arms
Qt3DCore::QEntity *frontLeftArmEntity = new Qt3DCore::QEntity(this);
Qt3DCore::QEntity *frontRightArmEntity = new Qt3DCore::QEntity(this);
Qt3DCore::QEntity *rearLeftArmEntity = new Qt3DCore::QEntity(this);
Qt3DCore::QEntity *rearRightArmEntity = new Qt3DCore::QEntity(this);
Qt3DExtras::QCylinderMesh* armCylinder = new Qt3DExtras::QCylinderMesh;
armCylinder->setRadius(0.05f);
armCylinder->setLength(0.2f);
frontLeftArmEntity->addComponent(armCylinder);
frontRightArmEntity->addComponent(armCylinder);
rearLeftArmEntity->addComponent(armCylinder);
rearRightArmEntity->addComponent(armCylinder);
// add propellers
Qt3DCore::QEntity *frontLeftPropellerEntity = new Qt3DCore::QEntity(this);
Qt3DCore::QEntity *frontRightPropellerEntity = new Qt3DCore::QEntity(this);
Qt3DCore::QEntity *rearLeftPropellerEntity = new Qt3DCore::QEntity(this);
Qt3DCore::QEntity *rearRightPropellerEntity = new Qt3DCore::QEntity(this);
Qt3DExtras::QSphereMesh* propellerSphere = new Qt3DExtras::QSphereMesh;
propellerSphere->setRadius(0.08f);
frontLeftPropellerEntity->addComponent(propellerSphere);
frontRightPropellerEntity->addComponent(propellerSphere);
rearLeftPropellerEntity->addComponent(propellerSphere);
rearRightPropellerEntity->addComponent(propellerSphere);
// set positions of arms and propellers
frontLeftArmEntity->addComponent(new Qt3DCore::QTransform);
frontLeftArmEntity->component<Qt3DCore::QTransform>()->setTranslation(QVector3D(0.2f, 0.1f, 0.2f));
frontLeftPropellerEntity->addComponent(new Qt3DCore::QTransform);
frontLeftPropellerEntity->component<Qt3DCore::QTransform>()->setTranslation(QVector3D(0.2f, 0.1f, 0.35f));
frontRightArmEntity->addComponent(new Qt3DCore::QTransform);
frontRightArmEntity->component<Qt3DCore::QTransform>()->setTranslation(QVector3D(-0.2f, 0.1f, 0.2f));
frontRightPropellerEntity->addComponent(new Qt3DCore::QTransform);
frontRightPropellerEntity->component<Qt3DCore::QTransform>()->setTranslation(QVector3D(-0.2, 0.1f, 0.35f));
rearLeftArmEntity->addComponent(new Qt3DCore::QTransform);
rearLeftArmEntity->component<Qt3DCore::QTransform>()->setTranslation(QVector3D(-0.2f, 0.1f, -0.2f));
rearLeftPropellerEntity->addComponent(new Qt3DCore::QTransform);
rearLeftPropellerEntity->component<Qt3DCore::QTransform>()->setTranslation(QVector3D(-0.2f, 0.1f, -0.35f));
rearRightArmEntity->addComponent(new Qt3DCore::QTransform);
rearRightArmEntity->component<Qt3DCore::QTransform>()->setTranslation(QVector3D(0.2f, 0.1f, -0.2f));
rearRightPropellerEntity->addComponent(new Qt3DCore::QTransform);
rearRightPropellerEntity->component<Qt3DCore::QTransform>()->setTranslation(QVector3D(0.2f, 0.1f, -0.35f));
// set material to all entities
Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial;
bodyEntity->addComponent(material);
frontLeftArmEntity->addComponent(material);
frontRightArmEntity->addComponent(material);
rearLeftArmEntity->addComponent(material);
rearRightArmEntity->addComponent(material);
frontLeftPropellerEntity->addComponent(material);
frontRightPropellerEntity->addComponent(material);
rearLeftPropellerEntity->addComponent(material);
rearRightPropellerEntity->addComponent(material);
// add point light to drone
Qt3DCore::QEntity* lightEntity = new Qt3DCore::QEntity(this);
Qt3DRender::QPointLight* pointLight = new Qt3DRender::QPointLight(lightEntity);
pointLight->setColor(Qt::white);
pointLight->setIntensity(2);
lightEntity->addComponent(pointLight);
// add transform component to drone and set initial rotation
addComponent(new Qt3DCore::QTransform);
component<Qt3DCore::QTransform>()->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0, 1, 0), 180.0f));
}
void setRotation(float angle)
{
// rotate the drone clockwise around Y axis
component<Qt3DCore::QTransform>()->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0, 1, 0), angle));
}
};
```
说明:
- DroneEntity 类继承自 Qt3DCore::QEntity,作为一个 Entity 对象,它可以包含多个组件(Qt3DCore::QComponent);
- 该类中包含一个构造函数,它会创建出无人机的各个部分(机身、四个臂、四个螺旋桨)并添加到该 Entity 对象中;
- 每个部件使用不同的颜色和材质,以区分开来;
- 最后,该类还提供了一个公共的接口 `setRotation()`,用于设置该 Entity 对象的旋转角度,以实现无人机模型的旋转效果。
使用该类可以创建一个无人机模型,然后通过在每帧更新时调用 setRotation() 方法来改变无人机的角度实现旋转效果:
```c++
DroneEntity* droneEntity = new DroneEntity;
Qt3DCore::QEntity* rootEntity = new Qt3DCore::QEntity;
rootEntity->addChild(droneEntity);
// set initial position and add to view
Qt3DCore::QTransform* transform = new Qt3DCore::QTransform;
transform->setTranslation(QVector3D(0, 0.5, -2));
rootEntity->addComponent(transform);
Qt3DExtras::Qt3DWindow* view = new Qt3DExtras::Qt3DWindow;
view->setRootEntity(rootEntity);
// set up timer to update rotation
QTimer* timer = new QTimer;
QObject::connect(timer, &QTimer::timeout, [&]()
{
static float angle = 0;
droneEntity->setRotation(angle);
angle += 1;
});
timer->start(10);
view->show();
```
阅读全文
相关推荐














