Qt3D类使用说明

Qt3D类说白了就是处理3D图形的类接口,对比2D图形graphicview 类,3D图形也有场景,有实体类,灯光实体,摄像机实体类,材质类等处理。

下面是常用的Qt3D类以及它们的使用说明:

1. **Qt3DCore:** 这个模块包含了一些核心类,用于管理3D场景中的实体、组件、变换等。其中最重要的类是`Qt3DCore::QEntity`,它代表了3D场景中的实体,可以包含各种组件。

2. **Qt3DRender:** 这个模块包含了一些用于渲染的类,比如`Qt3DRender::QCamera`代表了摄像机,`Qt3DRender::QMaterial`代表了材质,`Qt3DRender::QMesh`代表了网格等。

3. **Qt3DInput:** 这个模块包含了一些用于输入处理的类,比如`Qt3DInput::QInputAspect`用于处理用户输入。

4. **Qt3DLogic:** 这个模块包含了一些用于逻辑处理的类,比如`Qt3DLogic::QFrameAction`用于在每一帧更新时执行特定的操作。

5. **Qt3DExtras:** 这个模块包含了一些额外的类,用于简化一些常见的3D场景创建和渲染操作,比如`Qt3DExtras::QForwardRenderer`用于实现前向渲染。

使用Qt3D时,通常需要创建一个`Qt3DCore::QEntity`作为场景的根实体,并在其上添加各种组件,比如渲染组件、变换组件等。然后可以创建摄像机、灯光、模型等各种元素,将它们添加到场景中,最终通过渲染器进行渲染。

Qt3D的还涉及3D图形编程、着色器编写等,因此也需要进行学习的。

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个可以旋转的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(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1号程序媛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值