Qt文档阅读笔记-Qt 3D: Simple C++ Example解析

x33g5p2x  于2022-01-04 转载在 C/C++  
字(3.0k)|赞(0)|评价(0)|浏览(558)

本节官方Demo,使用Qt3D渲染场景

在main.cpp文件中,设置scene。

为了能使用相关的类和函数,需要include、Qt3DCore、Qt3D、Qt3DExtras模块。

  1. #include <Qt3DCore/QEntity>
  2. #include <Qt3DRender/QCamera>
  3. #include <Qt3DRender/QCameraLens>
  4. #include <Qt3DCore/QTransform>
  5. #include <Qt3DCore/QAspectEngine>
  6. #include <Qt3DInput/QInputAspect>
  7. #include <Qt3DRender/QRenderAspect>
  8. #include <Qt3DExtras/QForwardRenderer>
  9. #include <Qt3DExtras/QPhongMaterial>
  10. #include <Qt3DExtras/QCylinderMesh>
  11. #include <Qt3DExtras/QSphereMesh>
  12. #include <Qt3DExtras/QTorusMesh>

首先需要设置场景,和指定的root实体

  1. Qt3DCore::QEntity *createScene()
  2. {
  3. Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;

再使用PhongRendering为root实体指定material

  1. Qt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity);

在root实体上创建圆环实体,旋转、以及material组建等:

  1. Qt3DCore::QEntity *torusEntity = new Qt3DCore::QEntity(rootEntity);
  2. Qt3DExtras::QTorusMesh *torusMesh = new Qt3DExtras::QTorusMesh;
  3. torusMesh->setRadius(5);
  4. torusMesh->setMinorRadius(1);
  5. torusMesh->setRings(100);
  6. torusMesh->setSlices(20);
  7. Qt3DCore::QTransform *torusTransform = new Qt3DCore::QTransform;
  8. torusTransform->setScale3D(QVector3D(1.5, 1, 0.5));
  9. torusTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1, 0, 0), 45.0f));
  10. torusEntity->addComponent(torusMesh);
  11. torusEntity->addComponent(torusTransform);
  12. torusEntity->addComponent(material);

指定各个sphere的实体及他们的部件:

  1. Qt3DCore::QEntity *sphereEntity = new Qt3DCore::QEntity(rootEntity);
  2. Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh;
  3. sphereMesh->setRadius(3);
  4. Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform;
  5. OrbitTransformController *controller = new OrbitTransformController(sphereTransform);
  6. controller->setTarget(sphereTransform);
  7. controller->setRadius(20.0f);
  8. QPropertyAnimation *sphereRotateTransformAnimation = new QPropertyAnimation(sphereTransform);
  9. sphereRotateTransformAnimation->setTargetObject(controller);
  10. sphereRotateTransformAnimation->setPropertyName("angle");
  11. sphereRotateTransformAnimation->setStartValue(QVariant::fromValue(0));
  12. sphereRotateTransformAnimation->setEndValue(QVariant::fromValue(360));
  13. sphereRotateTransformAnimation->setDuration(10000);
  14. sphereRotateTransformAnimation->setLoopCount(-1);
  15. sphereRotateTransformAnimation->start();
  16. sphereEntity->addComponent(sphereMesh);
  17. sphereEntity->addComponent(sphereTransform);
  18. sphereEntity->addComponent(material);

使用了一个属性来实现动画的效果。

最后,创建Qt3D窗口:

  1. int main(int argc, char* argv[])
  2. {
  3. QGuiApplication app(argc, argv);
  4. Qt3DExtras::Qt3DWindow view;
  5. Qt3DCore::QEntity *scene = createScene();
  6. // Camera
  7. Qt3DRender::QCamera *camera = view.camera();
  8. camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
  9. camera->setPosition(QVector3D(0, 0, 40.0f));
  10. camera->setViewCenter(QVector3D(0, 0, 0));
  11. // For camera controls
  12. Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(scene);
  13. camController->setLinearSpeed( 50.0f );
  14. camController->setLookSpeed( 180.0f );
  15. camController->setCamera(camera);
  16. view.setRootEntity(scene);
  17. view.show();
  18. return app.exec();
  19. }

相关文章